Problem with using a shader progam for texturing [SOLVED]

Started by Lissborn, February 09, 2013, 20:47:26

Previous topic - Next topic

Lissborn

I'm trying to texture a plane using a shader program.
I've been following this tutorial http://www.lwjgl.org/wiki/index.php?title=The_Quad_with_Projection,_View_and_Model_matrices on how to use a shader program for the MVP matrix and the other one for texturing.
Any help would be appreciated.

This is how i setup the shaders.
// Create a new shader program that links both shaders
               mShaderProgramID = GL20.glCreateProgram();
		GL20.glAttachShader(mShaderProgramID, vsId);
		GL20.glAttachShader(mShaderProgramID, fsId);
		GL20.glLinkProgram(mShaderProgramID);

		// Position information will be attribute 0
		GL20.glBindAttribLocation(mShaderProgramID, 0, "in_Position");
		// Color information will be attribute 1
		GL20.glBindAttribLocation(mShaderProgramID, 1, "in_Color");
		// Textute information will be attribute 2
		GL20.glBindAttribLocation(mShaderProgramID, 2, "in_TextureCoord");
		
		projectionMatrixLocation = GL20.glGetUniformLocation(mShaderProgramID, "ProjectionMatrix");
		viewMatrixLocation = GL20.glGetUniformLocation(mShaderProgramID, "ViewMatrix");
		modelMatrixLocation = GL20.glGetUniformLocation(mShaderProgramID, "ModelMatrix");
		
		GL20.glValidateProgram(mShaderProgramID);


Update the the matrix information.
GL20.glUseProgram(this.mShaderProgramID);
			
			MeshNode r = (MeshNode) this.mParent;
			matrix44Buffer.clear();
			r.mCamRef.GetProjectionMatrix().store(matrix44Buffer); matrix44Buffer.flip();
			GL20.glUniformMatrix4(projectionMatrixLocation, false, matrix44Buffer);
			r.mCamRef.GetViewMatrix().store(matrix44Buffer); matrix44Buffer.flip();
			GL20.glUniformMatrix4(viewMatrixLocation, false, matrix44Buffer);
			inSharedData.ModelMatrix.store(matrix44Buffer); matrix44Buffer.flip();
			GL20.glUniformMatrix4(modelMatrixLocation, false, matrix44Buffer);
			
			GL20.glUseProgram(0);


Render
GL20.glUseProgram(this.mShaderProgramID);

	// Bind the texture
	GL13.glActiveTexture(GL13.GL_TEXTURE0);
	GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);

	// Bind to the VAO that has all the information about the vertices
	GL30.glBindVertexArray(this.vertexArrayId);
	GL20.glEnableVertexAttribArray(0);
	GL20.glEnableVertexAttribArray(1);
	GL20.glEnableVertexAttribArray(2);

	// Bind to the index VBO that has all the information about the order of
	// the vertices
	GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.vertexBufferObjectIndicesId);

	// Draw the vertices
	GL11.glDrawElements(GL11.GL_TRIANGLES, this.mIndicesCount,
			GL11.GL_UNSIGNED_SHORT, 0);

	// Put everything back to default (deselect)
	GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
	GL20.glDisableVertexAttribArray(0);
	GL20.glDisableVertexAttribArray(1);
	GL20.glDisableVertexAttribArray(2);
	GL30.glBindVertexArray(0);



Vertex shader.
#version 430 core

in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;

uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ModelMatrix;

out vec4 pass_Color;
out vec2 pass_TextureCoord;

void main(void) {
	gl_Position = in_Position;
	gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix *  in_Position;
	pass_Color = in_Color;
	pass_TextureCoord = in_TextureCoord;
}


Fragment shader.
#version 430 core

uniform sampler2D texture_diffuse;

in vec4 pass_Color;
in vec2 pass_TextureCoord;

out vec4 out_Color;

void main(void) {
	out_Color = pass_Color;
	// Override out_Color with our texture pixel
	out_Color = texture2D(texture_diffuse, pass_TextureCoord );
}


This is the data for the plane and as you can see, generated by blender.
# Blender v2.64 (sub 0) OBJ File: ''
# www.blender.org
o Plane
v -0.50000 0.500000 0.000000
v -0.500000 -0.500000 0.000000
v 0.500000 -0.500000 0.000000
v 0.50000 0.500000 0.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vn 1.000000 -0.000001 -0.000001
s off
f 1/1/1 2/2/1 3/3/1
f 3/1/1 4/3/1 1/4/1


The attached image represents how the plane looks with the texture turned on and off. The droid is just there do show the scale.
If I need to supply more information to solve this problem, please let me know.
Any help would be appreciated.


*SOLVED*
I'm using 2 different classed for vertexdata. One for texture and one without. I had intermixed the both and didn't consider that the stride value was different between the two.

int stride = VertexData.stride;	
	if(inSharedData.Textured)
	{
	  stride = TexturedVertexData.stride;
	}

         ...
         GL20.glVertexAttribPointer(0, VertexData.positionElementCount,
	GL11.GL_FLOAT, false, stride,    <----
	VertexData.positionByteOffset);
        ...
[\code]