PDA

View Full Version : Add C Arrays together?




ArtOfWarfare
Feb 3, 2012, 10:20 AM
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:

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?



KnightWRX
Feb 3, 2012, 11:14 AM
Don't change your vertice coordinates, change your current drawing matrice on a per object basis.

Look into glPushMatrix, glPopMatrix, glTranslate.

Pseudo 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.

ArtOfWarfare
Feb 3, 2012, 10:42 PM
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?

lloyddean
Feb 4, 2012, 12:18 AM
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">

KnightWRX
Feb 4, 2012, 07:15 AM
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.

ArtOfWarfare
Feb 4, 2012, 09:06 AM
:-/

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:
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:
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...

lloyddean
Feb 4, 2012, 11:11 AM
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.

dejo
Feb 4, 2012, 01:20 PM
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.

Alright. URL restored.