Main Menu

Texture problem

Started by Daslee, January 18, 2015, 12:12:27

Previous topic - Next topic

Daslee

I was trying new LWJGL 3 and got problem with texturing. I'm writing my program in modern opengl (vao, custom pipeline) and got this problem when added texture to the triangle (texture is supposed to be simple checkerboard):



And I don't know where is the problem, but I'm sure that it's not texture coordinates problem. I thought it's problem with my video card or drivers (Nvidia Geforce 610m), but after switching to integrated Intel video card the problem still remained.

This is my triangle initialization:
/*
 * vertices:
 * -0.5f, -0.5f, 0.0
 * 0.5f, -0.5f, 0.0
 * 0.5f, 0.5f, 0.0
 * 
 * texcoords:
 * 0, 0
 * 1, 0
 * 1, 1
 */

vaoHandle = glGenVertexArrays();
glBindVertexArray(vaoHandle);
		
/* Vertex Buffer Initialization */
vertexBufferHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferHandle);
glBufferData(GL_ARRAY_BUFFER, arrayListToFloatBuffer3(vertices), GL_STATIC_DRAW);
GL20.glEnableVertexAttribArray(0);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);

/* Texture Coordinates Buffer Handle Initialization */
texCoordBufferHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, texCoordBufferHandle);
glBufferData(GL_ARRAY_BUFFER, arrayListToFloatBuffer2(texCoords), GL_STATIC_DRAW);
GL20.glEnableVertexAttribArray(2);
GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, 0, 0);


Render:
glBindVertexArray(vaoHandle);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, verticesCount);


Texture loading (using TWL's PNGDecoder):
PNGDecoder decoder = new PNGDecoder(new FileInputStream(f));
ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth()*4, Format.RGBA);
buffer.flip();

int texId = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texId);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, decoder.getWidth(), decoder.getHeight());
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, decoder.getWidth(), decoder.getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, buffer);


Vertex shader:
#version 130

layout(location = 0) vec3 VertexPosition;
layout(location = 2) vec2 VertexTexCoord;

out vec2 TexCoord;

void main()
{
	TexCoord = VertexTexCoord;
	gl_Position = vec4(VertexPosition, 1.0);
}


Fragment shader:
#version 130

uniform sampler2D tex1;
in vec2 TexCoord;

layout(location = 0) out vec4 FragColor;

void main()
{
	FragColor = texture2D(tex1, TexCoord);
}


Where is the problem? If you need more code, tell me.

quew8

Well then it is probably a problem with the texture loading. Try using an OpenGL debugging tool which can show you the contents of loaded textures (list here: https://www.opengl.org/wiki/Debugging_Tools, I recommend APITrace). See if the loaded texture is what you expect.

Otherwise there could be a problem with this method "arrayListToFloatBuffer2(texCoords)" or the "texCoords" that you pass it, neither of which you've shown.

Daslee

Damn it.. I have two functions for loading textures, one for jpg and other for png format. I thought I was loading png texture, but actually I was loading jpg texture and there was problem in jpg image loading..