glDrawArrays=EXC_BAD_ACCESS after drawing 3D model

#1

I’m using these classes made by Bill Dudney for loading and drawing OBJ models. It’s great – it has support for materials and tons of good stuff.

http://bill.dudney.net/roller/objc/entr-…-es-working

I can get the model loaded and drawing on the screen. Here is the code:

I make a WaveFrontOBJScene in setup:

  
  
NSString *path = [[NSBundle mainBundle] pathForResource:@"SnowGlobe_v5" ofType:@"obj"];  
scene = [[WaveFrontOBJScene alloc] initWithPath:path];	  
  

And then in draw:

  
  
        ofEnableAlphaBlending();	  
	ofPushMatrix();  
	ofTranslate(ofGetWidth()/2, ofGetHeight()-80, 0);  
	ofScale(theScale, -theScale, theScale);  
	ofRotate(ofGetElapsedTimeMillis()*.01, 0, 1, 0);  
	  
	glEnableClientState(GL_NORMAL_ARRAY);  
	glEnableClientState(GL_VERTEX_ARRAY);  
	for(WaveFrontOBJGroup *group in scene.groups) {  
  
		if(group.smoothing) {  
			glShadeModel(GL_SMOOTH);  
		} else {  
			glShadeModel(GL_FLAT);  
		}  
		  
		// load (if necessary) and bind the vertices  
		GLuint verticesName = [group verticesName:GL_STATIC_DRAW];  
		glBindBuffer(GL_ARRAY_BUFFER, verticesName);  
		glVertexPointer(3, GL_FLOAT, 0, 0);  
		  
		// load (if necessary) and bind the normals  
		GLuint normalsName = [group normalsName:GL_STATIC_DRAW];  
		glBindBuffer(GL_ARRAY_BUFFER, normalsName);  
		glNormalPointer(GL_FLOAT, 0, 0);  
  
		// Set all of the colors  
		ColorRGBA color = group.material.ambientColor;  
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (GLfloat *)&color);  
		color = group.material.diffuseColor;  
		glColor4f(color.red, color.green, color.blue, color.alpha);  
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (GLfloat *)&color);  
		color = group.material.specularColor;  
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (GLfloat *)&color);  
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, group.material.shine);  
		  
		// load (if necessary) and bind the texture  
		GLuint textureCoordsName;  
		if(group.textureCoordinatesIndexData.length > 0) {  
			//glEnableClientState(GL_TEXTURE_COORD_ARRAY);  
			textureCoordsName = [group textureCoordinatesName:GL_STATIC_DRAW];  
			glEnable(GL_TEXTURE_2D);  
			glBindBuffer(GL_ARRAY_BUFFER, textureCoordsName);  
			glTexCoordPointer([group texCoordSize], GL_FLOAT, 0, 0);  
			GLuint texId = [group.material.diffuseTexture textureName];  
			glBindTexture(GL_TEXTURE_2D, texId);  
		}  
		  
		GLuint indexesName = [group indexesName:GL_STATIC_DRAW];  
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexesName);  
		glDrawElements(GL_TRIANGLES, group.indexCount, GL_UNSIGNED_SHORT, NULL);  
  
		if(group.textureCoordinatesIndexData.length > 0) {  
			glDisableClientState(GL_TEXTURE_COORD_ARRAY);  
		}  
	}  
	glDisableClientState(GL_NORMAL_ARRAY);  
	glDisableClientState(GL_VERTEX_ARRAY);  
	ofPopMatrix();  
	ofDisableAlphaBlending();  
  

The problem occurs when I try to do any other oF drawing commands while drawing the model.

When I try to draw a texture, I get an EXC_BAD_ACCESS in ofTexture, line 604:

glDrawArrays( GL_TRIANGLE_FAN, 0, 4 );

When I try to draw a circle, the error is in ofCircle,

glDrawArrays( (drawMode == OF_FILLED) ? GL_TRIANGLE_FAN : GL_LINE_LOOP, 0, numCirclePts);

So I imagine it is some problem with the state of openGL, but I can’t figure out what. There are various things going on in WaveFrontOBJGroup when verticesName and normalsName are called, like this:

  
  
- (GLuint)verticesName:(GLenum)usage { // the VBO for the vertices  
  if(0 == verticesName) {  
    glGenBuffers(1, &verticesName);  
    glBindBuffer(GL_ARRAY_BUFFER, verticesName);  
    glBufferData(GL_ARRAY_BUFFER, vertexData.length,   
                 vertexData.bytes, usage);  
  }  
  return verticesName;  
}  

I got myself the OpenGL bible for xmas, but I can’t wait that long!

Thanks in advance for help.

#2

In case anyone else has this problem, the great @aarontweets showed me the light in about 5 seconds. Apparently you need to unbind some buffers.

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

1 Like
#3

thank you very much jefftimesten, you just saved my ass

1 Like