Log in or register to post comments

Problem with OpenGL on DrawElements

December 30, 2011 - 12:02pm #1

Hi,

I'm new in openGL, I'm trying to draw a rectangle over my Frame Marker. I'm using the Frame Marker sample and I'm able to draw my rectangle without showing the video but when I uncomment the line which render the video background, I have an EXC_BAD_ACCESS error on the glDrawElements line.

In the renderFrameQCAR method I have added the lines :

CC3GLMatrix *projection = [CC3GLMatrix matrix];
        float h = 4.0f * self.frame.size.height / self.frame.size.width;
        [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
        glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
        
        CC3GLMatrix *modelView = [CC3GLMatrix matrix];
        [modelView populateFromTranslation:CC3VectorMake(sin(CACurrentMediaTime()), 0, -7)];
        glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
        
        glViewport(0, 0, self.frame.size.width, self.frame.size.height);
        
        glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
        glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
        
        glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

With the following line commented it's working, but not when I uncomment it to render the video :
QCAR::State state = QCAR::Renderer::getInstance().begin();

Any idea ? If you need more of my code to help me, don't hesitate.

- DaProd

Re: Problem with OpenGL on DrawElements

January 2, 2012 - 9:35am #7

glVertexPointer isn't implemented in OpenGL ES 2.0. Instead, you should be using glVertexAttribPointer, as the samples show.

I suggest taking a look at the VirtualButtons sample. It traces the button rectangles using GL_LINES. You should be able to do something quite similar for frame markers.

- Kim

Re: Problem with OpenGL on DrawElements

January 2, 2012 - 6:27am #6

Ok, there is definitely something I don't get. I decided to restart from the sample code FrameMarkers.

I'm just adding this after the drawElements from renderFrameQCAR :

const GLfloat squareVertices[] = {
                -1.0, 1.0, 0.0,            
                -1.0, -1.0, -0.0,           
                1.0, -1.0, 0.0,            
                1.0, 1.0, 0.0              
            };
            //glMatrixMode(GL_MODELVIEW);
            
            //glLoadIdentity();
            //glTranslatef(1.5, 0.0, -5.0);
            glVertexPointer(3, GL_FLOAT, 0, squareVertices);
            //glEnableClientState(GL_VERTEX_ARRAY);
            //glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

And I get a glError in the console. Am I missing something obvious ? How can I add some basic OpenGL ?

- DaProd

PS: Sorry if I am bothering you a lot with that but I can't get through...

Re: Problem with OpenGL on DrawElements

December 31, 2011 - 6:23am #5

I'm trying to encapsulate and not to separate anything. I just want to draw lines around Marker borders, if you are aware of a sample code or a post talking about that don't hesitate to point me that out.

I guess the problem comes from VBOs. I'll keep trying and looking around.

- DaProd

Re: Problem with OpenGL on DrawElements

December 31, 2011 - 4:55am #4

Hi DaProd,

Given your code works without QCAR rendering, it seems there must be an incompatibility or conflict between the two OpenGL contexts (although it's difficult to see how that generates an EXC_BAD_ACCESS).

Try to make your draw code as encapsulated as possible (restore/unassign all the OpenGL state that you change such as textures, buffers and VBOs etc), so that when QCAR renders the background next frame it has a clean state. One way to verify this is the problem is to see if QCAR renders it's frame first time through, then you render your objects, then second time through it all falls apart.

Although you may have good reason, changing the fustrum and model view seems to me like the wrong thing to be doing as the alignment of target, view and augmentation is dependent upon this - take a look at the samples more closely.

Re: Problem with OpenGL on DrawElements

December 31, 2011 - 3:24am #3

Yes I'm using VOBs. I followed ray wenderlich tutorials and tried to adapt with Frame Marker sample.

My binding code is the following :

GLuint vertexBuffer;
        glGenBuffers(1, &vertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
        
        GLuint indexBuffer;
        glGenBuffers(1, &indexBuffer);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

Indices is a GLubyte array. I tried to put it in the renderFrameQCAR method, this time the app is not crashing but I got nothing on screen and I have a glError in the Logs.

Re: Problem with OpenGL on DrawElements

December 30, 2011 - 2:51pm #2

This seems a little suspect:

sizeof(Indices)/sizeof(Indices[0])

What type of array is Indices?

It looks like you're using VBOs. Are you binding your VBO each frame before using it?

- Kim

Log in or register to post comments