PDA

View Full Version : texture fills only part of mesh



vreal
06-02-2012, 07:24 AM
Hello,
I am trying to read the position and texture data out of a collada file.
Reading the position data works fine, but the texture is not mapped correctly onto my plane.

I create the texture as follows:

for (int i=0; i<64*64; i++) _texture[i] = 255;

The texture is afterwards set this way:


_locTexture = glGetUniformLocation(shaderProgramId, "u_texture");
glGenTextures(1, &_textureID);
glBindTexture(GL_TEXTURE_2D, _textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, _texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


During the draw phase I added this code:


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _textureID);
glUniform1i(_locTexture, 0);


The shaders look like this:

Vertex shader:


attribute vec4 a_position; // Vertex position
attribute vec4 a_normal; // Vertex normal
attribute vec4 a_color; // Vertex color
attribute vec2 a_uv; // Vertex texture coordinate
uniform mat4 u_mvpMatrix; // model view matrix
varying vec4 v_color; // Resulting color per vertex
varying vec2 v_uv; // texture coordinate

void main(void) {

v_color = a_color;

gl_Position = u_mvpMatrix * a_position;
v_uv = a_uv;
}



Fragment shader:


uniform sampler2D u_texture;
varying vec2 v_uv;
varying vec4 v_color; // interpolated color calculated in the vertex shader


void main() {
gl_FragColor = texture2D(u_texture, v_uv);
}


The UV coordinates are read from the collada file which has been made in Blender 2.63a.


The result is shown in the attached screenshot.
Only the lower part is white, the rest seems to be random.

Does anybody have any idea what I'm doing wrong?

Dan Bartlett
06-02-2012, 05:32 PM
To ensure you correctly initialize the entire texture data, and not just a third of it, you should probably have something like:


for (int i=0; i<64*64*3; i++) _texture[i] = 255; // note the *3

or



for (int i=0; i<64*64; i++) {
_texture[i][0] = red;

_texture[i][1] = green;

_texture[i][2] = blue;

}



Also, since you using pixel data that is 3 bytes in size, you might need to use:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
instead of using the default value of 4.

vreal
06-03-2012, 01:10 AM
Yes, that was it :)
Thank you very much!