[Solved]Different draw with every launch

Started by
3 comments, last by ScherzkeCks 10 years, 8 months ago

Hi,

can somebody explain to me why the same Program just launched over and over again results in different drawing output?

The Data is static so it doesn't change. I attached two sampleImages that show a partly rendered Sphere where there should be a full sphere

The code show the actual Code to draw the sphere, the if(vertexArrayObjectName == 0) only get called once and the data returned in currentObject->getData() is always the same.

Thanks so much in advance

Stefan


RenderObject *currentObject = objectsToRender.at(i);
		GLuint vertexArrayObjectName = currentObject->getVertexArrayName();
		if (vertexArrayObjectName == 0)
		{
			//Create vertexarrayobject and buffers etc.
			GLfloat* vertices = NULL;
			GLfloat* normals = NULL;
			GLuint* indices = NULL;
			GLfloat* uvCoordinates = NULL;
			int verticesCount;
			int indicesCount;
			short textureWidth, textureHeight;
			char* textureData = NULL;
			try {
				currentObject->getData(&vertices, &normals, &indices, &uvCoordinates, &verticesCount, &indicesCount, &textureWidth, &textureHeight, &textureData);
			}
			catch (ExceptionFile* e) {
				printf("%s\n", e->what());
				//TODO: remove from objectHandler and further error-Handling
				continue;
			}

			//creating vertexarray
			glGenVertexArrays(1,&vertexArrayObjectName);
			glBindVertexArray(vertexArrayObjectName);

			currentObject->setVertexArrayName(vertexArrayObjectName);

			//create the buffers belonging to this vertexarray
			GLuint vboId;
			glGenBuffers(1,&vboId);
			glBindBuffer(GL_ARRAY_BUFFER,vboId);
			glBufferData(GL_ARRAY_BUFFER,verticesCount * sizeof(GLfloat) * 4,vertices, GL_STATIC_DRAW);
			glVertexAttribPointer(RENDER_POSITION_LOCATION,4,GL_FLOAT,GL_FALSE,0,0);
			glEnableVertexAttribArray(RENDER_POSITION_LOCATION);

			//normalBuffer
			GLuint normalBuffer;
			glGenBuffers(1,&normalBuffer);
			glBindBuffer(GL_ARRAY_BUFFER,normalBuffer);
			glBufferData(GL_ARRAY_BUFFER, verticesCount * 4 * sizeof(GLfloat),normals, GL_STATIC_DRAW);
			glVertexAttribPointer(RENDER_NORMALS_LOCATION,4,GL_FLOAT,GL_FALSE,0,NULL);
			glEnableVertexAttribArray(RENDER_NORMALS_LOCATION);

			//uvCoordinatesBuffer
			GLuint uvCoordinatesID;
			glGenBuffers(1,&uvCoordinatesID);
			glBindBuffer(GL_ARRAY_BUFFER,uvCoordinatesID);
			glBufferData(GL_ARRAY_BUFFER,verticesCount * sizeof(GLfloat) * 2,uvCoordinates,GL_STATIC_DRAW);
			glVertexAttribPointer(RENDER_UV_LOCATION,2,GL_FLOAT,GL_FALSE,0,0);
			glEnableVertexAttribArray(RENDER_UV_LOCATION);

			GLuint indicesID;
			glGenBuffers(1,&indicesID);
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indicesID);
			glBufferData(GL_ELEMENT_ARRAY_BUFFER,indicesCount * sizeof(GLuint),indices,GL_STATIC_DRAW);

			//Texture
			GLuint textureID;
			glGenTextures(1, &textureID);
			glBindTexture(GL_TEXTURE_2D, textureID);
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, textureData);

			currentObject->setTextureID(textureID);

		}
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D,currentObject->getTextureID());
		glUniform1i(textureSampler,0);

		glm::mat4  modelMatrix = currentObject->getModelMatrix();

		glUniformMatrix4fv(modelMatrixLocation,1,GL_FALSE,&modelMatrix[0][0]);

		glBindVertexArray(vertexArrayObjectName);

		glDrawElements(GL_TRIANGLES,currentObject->getIndicesCount(),GL_UNSIGNED_INT,0);
Advertisement

Did you ensure that you cleared the depth buffer before drawing?

Yes I call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); every frame

Yes I call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); every frame

did you cal glEnable(GL_DEPTH_TEST); By default depht test is off.

Yes I do.

It was rendering just fine before when I had the geometry and such hard written into the code. Only thing I changed was loading from file (which works as confirmed by debugging) and I installed the OpenGL Profiler from Apple, which shouldn't interfere with my program, should it? Oh and I added the texture-loading. But I am not using the texture in the shader yet, I just give out plain red in the shader.

EDIT: I just found the bug. Shows again, that getting a night of sleep solves a lot of problems. I didn't initialize the variable indicesCount right that gets returned in currentObject->getIndicesCount()

Thank you all for your help!

This topic is closed to new replies.

Advertisement