[GLSL] Changing Vertex Attributes After glBegin(GL_TRIANGLES) ...

Started by
1 comment, last by newObjekt 11 years, 1 month ago

Okay so I have a weird problem. I'm using Java, LWJGL, and GLSL.

Basically heres the problem.

This code gets me this result - http://inferno.codebrainshideout.net/Images/glslbug2.png


public class Triangle {

	//Triangle coordinates
	public Vertex a, b, c;
	//Triangle normals
	public Vertex i, j, k;
	//Triangle texture coordiantes
	public Vertex u, v, w;
	//Triangle Material
	public Material mat;
	//Model that this triangle belongs too.
	public Model model;

//// constructor and other methods here /////

public void draw() {
		mat.glMaterialSet();		

		glVertexAttrib3f(glGetAttribLocation(GraphicsCore.program, "indices"), (float)a.weights[0].frame.index, 0, 0);

		glBegin(GL_TRIANGLES);

		glTexCoord3d(u.x, u.y, u.z);
		glNormal3d(i.x, i.y, i.z);
		glVertex3d(a.x, a.y, a.z);
		
		glTexCoord3d(v.x, v.y, v.z);
		glNormal3d(j.x, j.y, j.z);
		glVertex3d(b.x, b.y, b.z);

		glTexCoord3d(w.x, w.y, w.z);
		glNormal3d(k.x, k.y, k.z);
		glVertex3d(c.x, c.y, c.z);
		
		glEnd();
}
}


But if I move glVertexAttrib3f() inside of the glBegin() I get this - http://inferno.codebrainshideout.net/Images/glslbug1.png


public class Triangle {

	//Triangle coordinates
	public Vertex a, b, c;
	//Triangle normals
	public Vertex i, j, k;
	//Triangle texture coordiantes
	public Vertex u, v, w;
	//Triangle Material
	public Material mat;
	//Model that this triangle belongs too.
	public Model model;

//// constructor and other methods here /////

public void draw() {
		mat.glMaterialSet();		

		glBegin(GL_TRIANGLES);

		glVertexAttrib3f(glGetAttribLocation(GraphicsCore.program, "indices"), (float)a.weights[0].frame.index, 0, 0);

		glTexCoord3d(u.x, u.y, u.z);
		glNormal3d(i.x, i.y, i.z);
		glVertex3d(a.x, a.y, a.z);
		
		glTexCoord3d(v.x, v.y, v.z);
		glNormal3d(j.x, j.y, j.z);
		glVertex3d(b.x, b.y, b.z);

		glTexCoord3d(w.x, w.y, w.z);
		glNormal3d(k.x, k.y, k.z);
		glVertex3d(c.x, c.y, c.z);
		
		glEnd();
}
}

What did I do wrong? I've seen people change attributes during there glBegin in examples. Why does it literally destroy my model when I do it?

This happens with any attribute I try to change, regardless of the data in it. Basically if I use glVertexAttrib() in any way inside of glBegin I get this problem.

I need to change the indices attribute for each vertex in the triangle, but I can't do that because any attributes I change inside of glBegin causes that to happen. What do?

Advertisement

Very probably because "glGetAttribLocation(GraphicsCore.program, "indices")" returned 0, which is specified to be equivalent to a glVertex call. You should explicitly set this (before program link time) using glBindAttribLocation so that you've a valid (non-zero) value in it, and so that you don't have to be calling glGetAttribLocation constantly at runtime (which may be slow).

Even if it didn't, you shouldn't mix glVertexAttrib calls with fixed attrib calls (glVertex/glTexcoord/etc) - use either one or the other.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Yeah I talked with my friend who had more experiance. I stopped using glBegin and started using interleaved VertexBuffers. All is well now.

This topic is closed to new replies.

Advertisement