Add C Arrays together?

Discussion in 'iOS Programming' started by ArtOfWarfare, Feb 3, 2012.

  1. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    Here's the goal:
    Draw multiple objects in OpenGL from a single template.

    I followed a tutorial that ended with me drawing a single cube on screen. I played around with that to change the cube into a more star-like shape. Now I'd like to make several of these stars with me only providing the X, Y, Z coordinates of where each should appear.

    Looking back over the code, the way it worked was a C array of structs defined the vertices and the colors and textures attached to the vertices, and a second C array of ints defined which vertices should be attached to each other.

    So I'm thinking the way to do this is create a subclass of NSObject, I'll call it ModelGenerator and give it a method starAtX:Y:Z:. This method would return a C array full of vertices. I'd append this to the end of the current vertices array. I was thinking using it would look something like this:

    Code:
    Vertices[sizeof(Vertices)] = [ModelGenerator starAtX:2 Y:2 Z:2];
    Would this work at all? Would it cause problems (IE, would Vertices end up overflowing and writing itself ontop of other things in memory?) Or would it not work (maybe because Obj-C methods can't return C arrays? I feel like this may be the case but I can't remember.) Maybe I'm going about this all wrong... suggestions, anyone?
     
  2. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    Don't change your vertice coordinates, change your current drawing matrice on a per object basis.

    Look into glPushMatrix, glPopMatrix, glTranslate.

    Pseudo code :

    Code:
    for numstars
        glPushMatrix()
        glTranslate(newstarX, newstarY, newstarZ)
        loop
           glVertex()
           glOtherDrawingStuff <--- obviously not an OpenGL function
        end loop
        glPopMatrix()
    end for
    
    That's the proper way to render objects in OpenGL.
     
  3. thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    I just flipped through the table of contents and index of my book and it doesn't mention pushing or popping matrices...

    yet Xcode offered to autocomplete using those method names when I typed "glP"... so presumably they would work...

    is "OpenGL ES 2.0 Programming Guide" what I need to read to learn this stuff or am I looking in the wrong place?
     
  4. lloyddean, Feb 3, 2012
    Last edited by a moderator: Feb 4, 2012

    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    That could be because it isn't supported in OpenGL ES. 2.0

    Check out this handy Reference

    <http://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf">
     
  5. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #5
    I do admit most of my experience is with true OpenGL of older revisions. It seems the old Matrix manipulations still work under OpenGL ES 1.0 but were removed in OpenGL ES 2.0.

    That frankly sucks if you ask me.
     
  6. ArtOfWarfare, Feb 4, 2012
    Last edited: Feb 4, 2012

    thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    :-/

    I can't find any sample code where people use GLKit and Open GL ES 2.0 to set up multiple meshes.

    Edit: Oh, and I ended up writing this code:
    Code:
    void createStarAt(float x, float y, float z)
    {
    	for (int i = 0; i < 30; i++)
    	{
    		//NSLog(@"%i", sizeof(StarVertices));
    		Vertices[vertexCount+i] = StarVertices[i];
    		Vertex currentVertex = Vertices[vertexCount+i];
    		currentVertex.Position[0] += x;
    		currentVertex.Position[1] += y;
    		currentVertex.Position[2] += z;
    	}
    	
    	for (int j = 0; j < 168; j++)
    	{
    		Indices[indiceCount+j] = (StarIndices[j] + vertexCount);
    	}
    	
    	vertexCount += 30;
    	indiceCount += 156;
    	NSLog(@"Finished making star.");
    }
    Using the debugger, it seems that the vertices positions are all properly assigned, and yet my star is drawn at the center of the screen.

    I think this is because in another method, I have these lines:
    Code:
    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0, 0, -6.0f); 
    self.effect.transform.modelviewMatrix = modelViewMatrix;
    This seems to center both of my meshes at that point (0, 0, -6).

    Changing those numbers will cause both of my meshes to be drawn on top of each other at whatever other point I want. What I can't figure out is how to drawn just one mesh at a point and another mesh at another point...

    Edit:
    Commenting out those modelviewMatrix lines causes a star to appear on the first frame and then disappear and never reappear...
     
  7. lloyddean, Feb 4, 2012
    Last edited: Feb 4, 2012

    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    dejo,

    I on purpose post URL's the way that I do so they can be seen in their entirety (instead of condensed depending upon the users view settings) and, so, when they accidentally get clicked upon the user doesn't get taken away to another page.

    I both like and dislike the Magic Trackpad it's just as often irritating as useful.
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Alright. URL restored.
     

Share This Page