Indexed VBOs

tdc

Bekanntes Mitglied
Hi,
ich versuche zurzeit von VBOs zu indexed VBOs in LWJGL umzusteigen. Wenn ich glDrawElements verwende, wird allerdings nichts mehr gerendert.
Mein Code:
Java:
	public void setup() {
		if (vboIndexHandle != 0)
			glDeleteBuffers(vboIndexHandle);
		if (vboVertexHandle != 0)
			glDeleteBuffers(vboVertexHandle);
		if (vboColorHandle != 0)
			glDeleteBuffers(vboColorHandle);
		if (vboTextureCoordHandle != 0)
			glDeleteBuffers(vboTextureCoordHandle);
		if (vboNormalHandle != 0)
			glDeleteBuffers(vboNormalHandle);

		...

		FloatBuffer indexData = BufferUtils.createFloatBuffer(allVertices
				* vertexSize);
		FloatBuffer vertexData = BufferUtils.createFloatBuffer(allVertices * vertexSize);
		FloatBuffer colorData = BufferUtils.createFloatBuffer(allVertices
				* colorSize);
		FloatBuffer textureData = BufferUtils.createFloatBuffer(allVertices
				* texSize);
		FloatBuffer normalData = BufferUtils.createFloatBuffer(allVertices
				* vertexSize);

		...
		
		for (int p = 0; p < polys.size(); p++) {
			... //Befüllen der Buffer
		}
		indexData.flip();
		vertexData.flip();
		colorData.flip();
		textureData.flip();
		normalData.flip();

		vboIndexHandle = glGenBuffers();
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndexHandle);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData, GL_STATIC_DRAW);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
		
		vboVertexHandle = glGenBuffers();
		glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
		glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);

		vboColorHandle = glGenBuffers();
		glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
		glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		
		vboTextureCoordHandle = glGenBuffers();
		glBindBuffer(GL_ARRAY_BUFFER, vboTextureCoordHandle);
		glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);

		vboNormalHandle = glGenBuffers();
		glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
		glBufferData(GL_ARRAY_BUFFER, normalData, GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}

	@Override
	public void render() {
		glPushMatrix();

		...
		
		glEnableClientState(GL_VERTEX_ARRAY);
		glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
		glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
		
		if (renderColor) {
			glEnableClientState(GL_COLOR_ARRAY);
			glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
			glColorPointer(colorSize, GL_FLOAT, 0, 0L);
		}
		
		if (renderTexCoords) {
			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
			glBindBuffer(GL_ARRAY_BUFFER, vboTextureCoordHandle);
			glTexCoordPointer(texSize, GL_FLOAT, 0, 0L);
		}
		
		if (renderNormals) {
			glEnableClientState(GL_NORMAL_ARRAY);
			glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
			glNormalPointer(GL_FLOAT, 0, 0L);
		}

		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndexHandle);
        
		glDrawElements(GL_TRIANGLES, polys.size() * 3, GL_UNSIGNED_BYTE, 0);
		
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
		if (renderNormals)
			glDisableClientState(GL_NORMAL_ARRAY);
		if (renderTexCoords)
			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		if (renderColor)
			glDisableClientState(GL_COLOR_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);
		
		...
		
		glPopMatrix();
	}
Am Befüllen der Buffer sollte es jedenfalls nicht liegen, beim Befüllen lasse ich die Werte ausgeben: (V == Vertex, I == Index)
Code:
V: -0.5; -0.5; -0.5
V: -0.5; 0.5; -0.5
V: 0.5; 0.5; -0.5
I: 0; 1; 2
V: 0.5; -0.5; -0.5
I: 0; 2; 3
V: -0.5; -0.5; 0.5
V: 0.5; -0.5; 0.5
V: 0.5; 0.5; 0.5
I: 4; 5; 6
V: -0.5; 0.5; 0.5
I: 4; 6; 7
I: 0; 4; 7
I: 0; 7; 1
I: 3; 2; 6
I: 3; 6; 5
I: 1; 7; 6
I: 1; 6; 2
I: 0; 3; 5
I: 0; 5; 4
Was mache ich bisher falsch?
 

Marco13

Top Contributor
Beim ersten Überfliegen: Das "indexData" sollte kein FloatBuffer sein, sondern ein IntBuffer. Und nebenbei bei glDrawElements nicht GL_UNSIGNED_BYTE sondern GL_UNSIGNED_INT.
 
Ähnliche Java Themen

Ähnliche Java Themen


Oben