Sign in to follow this  
newObjekt

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this