Projection, View and Model matrices tutorial broken

Started by rjmatthews62, August 23, 2013, 06:48:52

Previous topic - Next topic

rjmatthews62

The last of the modern OpenGL tutorials is slightly broken.
http://www.lwjgl.org/wiki/index.php?title=The_Quad_with_Projection,_View_and_Model_matrices



Issue 1:
import premade.VertexData;  // doesn't exist. BUT also not used, so can just remove.

Issue 2:
Camera pos is initialized to the same location as the model, so you see nothing when you start.
Fix:
cameraPos = new Vector3f(0, 0, -1);


Issue 3:
setupShaders is basically broken. Through trial and error I got it working.

Through trial and error, I found that you needed to call glBindAttributeLocation BEFORE glLinkProgram, and glGetUniformLocation has be called after.
Also, there was code that carefully detached and deleted the shaders before doing anything else, which seemed odd.

Fixed code:
private void setupShaders() {		
		// Load the vertex shader
		vsId = this.loadShader("assets/shaders/moving/vertex.glsl", GL20.GL_VERTEX_SHADER);
		// Load the fragment shader
		fsId = this.loadShader("assets/shaders/moving/fragment.glsl", GL20.GL_FRAGMENT_SHADER);
		
		// Create a new shader program that links both shaders
		pId = GL20.glCreateProgram();
		GL20.glAttachShader(pId, vsId);
		GL20.glAttachShader(pId, fsId);

        // NB: glBindAttributeLocation seems to be needed BEFORE glLinkProgram
		// Position information will be attribute 0
		GL20.glBindAttribLocation(pId, 0, "in_Position");
		// Color information will be attribute 1
		GL20.glBindAttribLocation(pId, 1, "in_Color");
		// Textute information will be attribute 2
		GL20.glBindAttribLocation(pId, 2, "in_TextureCoord");

		GL20.glLinkProgram(pId);
		GL20.glValidateProgram(pId);

		// Get matrices uniform locations
		// NB: glGetUnifromLocation seems to require at least glLinkProgram to work.
		projectionMatrixLocation = GL20.glGetUniformLocation(pId,"projectionMatrix");
		viewMatrixLocation = GL20.glGetUniformLocation(pId, "viewMatrix");
		modelMatrixLocation = GL20.glGetUniformLocation(pId, "modelMatrix");

		this.exitOnGLError("setupShaders");
	}


Hope this helps someone.

quew8

Hmmm, lots of people seem to be finding problems with these tutorials. I'll go and make your changes, but for the record requiring glBindAttributeLocation before glLinkProgram (and a few other things as I recall) is a driver specific thing. Nvidia cards specifically I think.

quew8

Changes have been made according to your post. Thankyou.

rjmatthews62

Ta!
I did wonder if it was vendor specific.

For the record, I'm using an AMD M880G with ATI Mobility Radeon HD 4250

OpenGL Version: 3.2.10179 Core Profile Forward-Compatible Context
OpenGL Vendor: ATI Technologies Inc.