Drawing using glDrawArrays

Apprentice
Posts: 18
Joined: 2010.07
Post: #1
Hey guys, I just started iPhone development a few weeks ago and am not entirely comfortable with using openGL just yet. Here is my problem. I have the following structs:

Code:
struct Vertex {
    vec3 Position;
    vec4 Color;
};

typedef struct object{
    vector<Vertex> model;
};

With which I've created a vector<object*>. Right now I have one model, a cone, which is built with the following:

Code:
object* RenderingEngine1::buildCone(GLfloat x, GLfloat y, GLfloat r, GLfloat h, GLfloat slice)
{
    object* returnObject = new object;
    
    returnObject->model.resize((slice + 1) * 2);
    vector<Vertex>::iterator vertex = returnObject->model.begin();
    const float dtheta = TwoPi / slice;
    for (float theta = 0; vertex != returnObject->model.end(); theta += dtheta)
    {
        // Grayscale gradient
        float brightness = abs(sin(theta));
        vec4 color(brightness, brightness, brightness, 1);
        
        // Apex vertex
        vertex->Position = vec3(x, y, h);
        vertex->Color = color;
        vertex++;
        
        // Rim Vertex
        vertex->Position.x = r * cos(theta) + x;
        vertex->Position.y = r * sin(theta) + y;
        vertex->Position.z = 0;
        vertex->Color = color;
        vertex++;    
    }
    
    return returnObject;
}

Lastly, I construct my vector<object*> as such:

Code:
    cone_Vector.resize(1);
    vector<object*>::iterator vert;
    for (vert = cone_Vector.begin(); vert != cone_Vector.end(); vert++) {
        (*vert) = buildCone(coneX, coneY, .5, 1, 10);
        coneX += 2;
        coneY += 2;
    }

during the initialization of the renderer. Here is the code that is giving me the problem:

Code:
    for (vector<object*>::iterator vert = cone_Vector.begin(); vert != cone_Vector.end(); vert++) {
        glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &((*vert)->model[0].Position.x));
        glColorPointer(4, GL_FLOAT, sizeof(Vertex), &((*vert)->model[0].Color.x));    
        glDrawArrays(GL_TRIANGLE_STRIP, 0, (*vert)->model.size());
    }

If I hard code in the creation and draw calls of multiple objects I have no problem, but with this method when I create more than 1 (that is, resize cone_vector > 1), I get EXC_BAD_ACCESS after only a few draw calls when running on the device, however the simulator has no problems. I have a feeling I may be incorrectly setting the parameters to the calls for the draw, but I'm not quite sure. If it is of any use I'm compiling with the 4.0 SDK and my device is a 3G running 3.1.3.

Thanks in advance for any help!
Quote this message in a reply
Member
Posts: 95
Joined: 2009.09
Post: #2
I don't really see through your code, but as I recently experienced myself, try making sure all your pointers are allocated and freed correctly.

Furthermore, please note that its not necessary to store color as 4 floats, you may store them as one bitwise int in the following way: (red,green,blue and alpha are ints with values between 0 and 255)
Code:
unsigned color = (alpha << 24) | (blue << 16) | (green << 8) | (red << 0);
and draw them with this command:
Code:
    glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), ...);
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2010.07
Post: #3
The only place I allocate any pointers is in the creation of the cone_Vector when buildCone is called. The objects in cone_Vector are than freed before the rendering engine goes out of context, so I can't imagine it would be a pointer issue, though I could be wrong. Since it works with 1 but not with anything greater I have a feeling my calls to glVertexPointer, glColorPointer and glDrawArrays have incorrect parameters, the stride perhaps?

Also, I checked out your blog and bought your game. Strategy games aren't exactly my forte, but I appreciate your battle with the common game type on the app store and wanted to see the engine in action after reading your more technical posts. Great stuff!
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #4
(Jul 14, 2010 09:39 AM)Bersaelor Wrote:  its not necessary to store color as 4 floats, you may store them as one bitwise int in the following way: (red,green,blue and alpha are ints with values between 0 and 255)
Code:
unsigned color = (alpha << 24) | (blue << 16) | (green << 8) | (red << 0);
and draw them with this command:
Code:
    glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), ...);

Warning, this is assuming little endian. An int is not an array of 4 GL_UNSIGNED_BYTES. This code will produce the wrong colors if you run it on PowerPC.
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2010.07
Post: #5
You were right Bersaelor. I wasn't allocating the pointer in the vector, I just wasn't thinking when I wrote it, assuming the pointer returned to was initialized and that was all that mattered. Thanks again though!

Edit: Well, I thought I had fixed it. Now if it is larger than 8 its gets a EXC_BAD_ACCESS on the size() call
Alright, I'm back to thinking this is an error in my openGL calls. It works with a coneVector size greater than 8, but only when I resize to a multiple of 8. I think my stride is wrong, and the multiples of 8 just gets me lucky.
Quote this message in a reply
Member
Posts: 95
Joined: 2009.09
Post: #6
(Jul 14, 2010 12:17 PM)arekkusu Wrote:  Warning, this is assuming little endian. An int is not an array of 4 GL_UNSIGNED_BYTES. This code will produce the wrong colors if you run it on PowerPC.

Well it works exactly how it should on iOS Devices, and thats what we are talking about, are we Wink

agreendev Wrote:Alright, I'm back to thinking this is an error in my openGL calls. It works with a coneVector size greater than 8, but only when I resize to a multiple of 8. I think my stride is wrong, and the multiples of 8 just gets me lucky.
Well your stride seems to be ok, although
Code:
&((*vert)->model[0].Color.x
seems a little complicated way to denote the interleaved array.
I just use a Array of the type "Vertex" and then call
Code:
&_interleavedVerts[0].v
Though what you did might make perfect sense for your app, it might just look complicated for me Smile

EDIT: Now that I look at it, shouldn't you use : "&((*vert)->model[0].Color" and "&((*vert)->model[0].Position" ?
I mean you want the entire color and the entire 3-dim position, not just the first component.
I call ".v" and not ".v.x".

EDIT2: Thank you for trying my little game. Be sure to upgrade it once V1.5 ships, it will be awesome!
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2010.07
Post: #7
The reason I have &((*vert)->model[0].Position.x) is because I assumed that I needed a reference to the very first position. I thought that's what I'm doing, since the entire value is encapsulated with the reference. I've only been doing it this way since the book I read to learn ES (Iphone 3D Programming) went about it this way. Am I miss understanding the last parameter to the pointer methods?

Also, I figured my bug was much different that I had originally assumed. This thread may need to be moved as I realize it's more of a memory error now. I had vector<object*> cone_Vector declared as a global, and it is originally populated by the initialize method of the instance of the rendering class. The error occurs in the render method when the cone_Vector is accessed again (I'm sure of this, as I changed the names of EVERYTHING just in case I was missing a case where they were modified). It usually wasn't the first time through after the instantiation, but the second time. But the weirdest part was that as long as the size of cone_Vector was a multiple of 8, it would work. If I change cone_Vector to a private variable of the render method, it blows up everytime.

I thought I had a pretty solid understanding of mixing vectors, pointers, and structs but I feel like I must be assuming something about the objects not being changed when in fact they are.

Also, I thought I read something about crash logs in Tactica, is that implemented right now? I played for a few hours last night, and it crash only once with a exception due to a touch being out of bounds. Just wanted to be able to give you some more detailed information.
Quote this message in a reply
Member
Posts: 95
Joined: 2009.09
Post: #8
(Jul 15, 2010 06:15 AM)agreendev Wrote:  Also, I thought I read something about crash logs in Tactica, is that implemented right now? I played for a few hours last night, and it crash only once with a exception due to a touch being out of bounds. Just wanted to be able to give you some more detailed information.
It's not implemented yet, at least not the part where one can send the log to me.
It's only showing an error-view with the error embedded as a text view.
Do you remember what it sayd exactly, i.e. what was out of bounds?
Could you play on after you tapped "ok"? Or did it abort?
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2010.07
Post: #9
(Jul 15, 2010 09:56 AM)Bersaelor Wrote:  It's not implemented yet, at least not the part where one can send the log to me.
It's only showing an error-view with the error embedded as a text view.
Do you remember what it sayd exactly, i.e. what was out of bounds?
Could you play on after you tapped "ok"? Or did it abort?

Yeah, it showed the error, but it wasn't completely in the view box, it was sort half out of the box (another bug perhaps?). Honestly I can't remember exactly what was out of bounds, I thought it was an NSTouch but I could be wrong. Also, it was completely unresponsive at that point. I just quit out.
Quote this message in a reply
Member
Posts: 95
Joined: 2009.09
Post: #10
(Jul 16, 2010 05:35 AM)agreendev Wrote:  Yeah, it showed the error, but it wasn't completely in the view box, it was sort half out of the box (another bug perhaps?). Honestly I can't remember exactly what was out of bounds, I thought it was an NSTouch but I could be wrong. Also, it was completely unresponsive at that point. I just quit out.

Yeah well, I believe the error-message should be scrollable.
More to the point I think I'm still trying to reproduce any error in touchview but everything seems to be fine. Sadly bugs that show up so rarely are hard to find.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL glDrawArrays not working dotbianry 12 12,037 Dec 21, 2012 09:21 AM
Last Post: Skorche
  glColor4f not working after glDrawArrays Technoman 2 6,587 Aug 15, 2009 08:09 AM
Last Post: Technoman
  glDrawArrays Muffinking 24 15,929 Oct 28, 2003 01:47 PM
Last Post: Mars_999
  glDrawElements vs. glDrawArrays - The numbers are in! inio 22 20,579 Jul 19, 2003 10:00 AM
Last Post: Josh