Need help with texturing

Started by bogieman9876, February 02, 2014, 16:47:09

Previous topic - Next topic

bogieman9876

Hi,

I'm having an issue with texturing, I'm not sure what the actual problem is, however, I suspect it may be how I'm binding the UV co-ordinates.
If it's not that, I think it may be some-thing to do with the shaders.
Outputting the UV co-ordinates show that they are correct, from what I can tell anyway.

I'm learning to use modern OpenGL, by reading OpenGL SuperBible Sixth Edition and googling any bits I'm stuck with, due to differences in C++ and Java and the methods used, due to C++ using GLUT etc and using LWJGL with Java.

What I see:

In Blender:


I tried to get texture binding as close as I can to http://lwjgl.org/wiki/index.php?title=The_Quad_textured.
setVboT(GL11.glGenTextures());
		GL13.glActiveTexture(getTextureType());
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, getVboT());
		
		GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, getWidth(), getHeight(), 
				0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, getTexBuffer());
		GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
		
		GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
		
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
		
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
		GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
		
		ErrorCheck.checkForGLError("Error loading texture");


Here is the code for binding the vertices and such for the model.

     
                FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(getVertices().length);
		vertBuffer.put(getVertices());
		vertBuffer.flip();
		
		FloatBuffer uvBuffer = BufferUtils.createFloatBuffer(getUvCoords().length);
		uvBuffer.put(getVboUv());
		uvBuffer.flip();
		
		IntBuffer indsBuffer = BufferUtils.createIntBuffer(getIndeces().length);
		indsBuffer.put(getIndeces());
		indsBuffer.flip();
		
		setVao(GL30.glGenVertexArrays());
		GL30.glBindVertexArray(getVao());
		{
			setVbo(GL15.glGenBuffers());
			
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, getVbo());
			GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
			GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
			
			setVboUv(GL15.glGenBuffers());
			
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, getVboUv());
			GL15.glBufferData(GL15.GL_ARRAY_BUFFER, uvBuffer, GL15.GL_STATIC_DRAW);
			GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 0, 0);
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
		}
		GL30.glBindVertexArray(0);
		
		setVboI(GL15.glGenBuffers());
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, getVboI());
		GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indsBuffer, GL15.GL_STATIC_DRAW);
		
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);


Shaders:

------ Vertex Shader ------

#version 430 core

layout(location = 0) in vec4 in_Position;
layout(location = 1) in vec2 in_TextureCoord;

out VS_OUT {
	vec2 pass_TextureCoord;
	vec4 pass_Color;
} vs_out;

uniform mat4 proj_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;

void main(void) {		
	gl_Position = in_Position;					
	gl_Position = proj_matrix * view_matrix * model_matrix * in_Position;

  	//vs_out.pass_Color = in_Position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);
	vs_out.pass_Color = new vec4(1.0, 1.0, 1.0, 1.0);
	vs_out.pass_TextureCoord = in_TextureCoord;
}



------- Fragment Shader -------

#version 430 core

layout(location = 2) uniform sampler2D diffuse_Tex;

out vec4 out_Color;

in VS_OUT {
	vec2 pass_TextureCoord;
	vec4 pass_Color;
} fs_in;

void main(void) {
	//out_Color = fs_in.pass_Color;
	out_Color = texture(diffuse_Tex, fs_in.pass_TextureCoord);
}


Rendering code:
GL30.glClearBuffer(GL11.GL_COLOR, 0, getClColBuffer());
		GL30.glClearBuffer(GL11.GL_DEPTH, 0, getClDepthBuffer());
		
		Matrix4f.translate(getCameraPos(), getViewMatrix(), getViewMatrix());
				
		GL20.glUseProgram(shader.getShaderProgram());
		
		getProjectionMatrix().store(getMatrixBuffer()); 
		getMatrixBuffer().flip();
		GL20.glUniformMatrix4(getShader().getProjectionMatLoc(), false, getMatrixBuffer());
		getViewMatrix().store(getMatrixBuffer()); 
		getMatrixBuffer().flip();
		GL20.glUniformMatrix4(getShader().getViewMatLoc(), false, getMatrixBuffer());
		gameObject.getModelMatrix().store(getMatrixBuffer()); 
		getMatrixBuffer().flip();
		GL20.glUniformMatrix4(getShader().getModelMatLoc(), false, getMatrixBuffer());
		
		GL13.glActiveTexture(GL13.GL_TEXTURE0);
                // getVboT() returns the locatoin where the texture is, I named it that as it's bound in a similar-ish fashion, personal preference is all.
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, gameObject.getMaterial().getTexture().getVboT());
		
		GL30.glBindVertexArray(gameObject.getModel().getVao());
		GL20.glEnableVertexAttribArray(0);
		GL20.glEnableVertexAttribArray(1);
		
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, gameObject.getModel().getVboI());
		
		GL11.glDrawElements(GL11.GL_TRIANGLES, gameObject.getModel().getNumOfIndeces(), GL11.GL_UNSIGNED_INT, 0);
		
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
		GL20.glDisableVertexAttribArray(0);
		GL20.glDisableVertexAttribArray(1);
		GL30.glBindVertexArray(0);	
		GL11.glDisable(GL11.GL_TEXTURE_2D);
		
		GL20.glUseProgram(0);
		
		if(getIfSync()) {
			Display.sync(getFps());
		}
		Display.update();

		ErrorCheck.checkForGLError("Error in render update");


Thanks in advance for any help

Edit: I'm just wondering that perhaps it may be the values passed for the UV co-ordinates that are causing the issue.

Here's the data in the obj file. It's a basic cube exported from Blender.
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.500000 0.500000
vt 0.250043 0.500000
vt 0.250043 0.250043
vt 0.749957 0.500000
vt 0.749957 0.250043
vt 0.999913 0.500000
vt 0.500000 0.749957
vt 0.000087 0.500000
vt 0.500000 0.000087
vt 0.749957 0.000087
vt 0.500000 0.250043
vt 0.749957 0.749957
vt 0.999913 0.250043
vt 0.000087 0.250043
usemtl Material
s off
f 1/1 2/2 3/3
f 5/4 8/5 6/6
f 1/1 5/4 2/7
f 2/2 6/8 3/3
f 3/9 7/10 8/5
f 5/4 1/1 8/5
f 4/11 1/1 3/3
f 5/4 6/12 2/7
f 1/1 4/11 8/5
f 8/5 7/13 6/6
f 4/11 3/9 8/5
f 6/8 7/14 3/3


Now understanding that "vt" is the texture co-ordinates, should I just pass all the "vt" values?
As they are, in their current order.
Which I then must ask, how do I tell OpenGL, what co-ordinate goes to which vertex and when, as one vertex can have multiple texture co-ordinates.

Or do I pass the appropriate values for each value provided in "f"?
Like so for the first tri-angle.
(1, 2, 3)
0.500000, 0.500000, 0.250043, 0.500000, 0.250043, 0.250043
And the second.
(4, 5, 6)
0.749957, 0.500000, 0.749957, 0.250043, 0.999913, 0.500000
And so on.
This would then be the vertex locations and UV co-ordinates for the first tri-angle. I did it this way.


bogieman9876


quew8

You're right the second time. People always seem to get hung up on this idea. You have to put the vertex data into the Buffer in the order they are defined in the face definitions. Really you should also check that none of the vertices are exactly the same (ie same pos, same normal, same texture coords) which will happen frequently with meshes and you can save a lot of VRAM by not duplicating them.

I could not see any problem with your code.

bogieman9876

Ok thank you, I'll adjust the loading code and I'll report the results, if I have any further issues or not.

bogieman9876

After many distractions and re-writes. I managed to get it to work.
Although it seems I also had to invert the values used on the y-axis I think it is.
Is that normal?

Thanks for the help :D

quew8

Yeah that's normal. OpenGL decided to go against the grain on that one. God knows why but we just have to live with it.