Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: texture fills only part of mesh

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2011
    Posts
    15

    texture fills only part of mesh

    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:
    Code :
    for (int i=0; i<64*64; i++) _texture[i] = 255;

    The texture is afterwards set this way:
    Code :
    _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:
    Code :
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, _textureID);
    glUniform1i(_locTexture, 0);

    The shaders look like this:

    Vertex shader:
    Code :
    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:
    Code :
    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?
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	texture.png 
Views:	14 
Size:	14.5 KB 
ID:	750  

  2. #2
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    381
    To ensure you correctly initialize the entire texture data, and not just a third of it, you should probably have something like:
    Code :
    [LEFT][COLOR=#333333]for (int i=0; i<64*64*3; i++) _texture[i] = 255; // note the *3[/COLOR][/LEFT]
    or
    Code :
    [COLOR=#333333][LEFT]for (int i=0; i<64*64; i++) {
    _texture[i][0] = red; [/LEFT]
    [/COLOR][COLOR=#333333][LEFT]_texture[i][1] = green; [/LEFT]
    [/COLOR][COLOR=#333333][LEFT]_texture[i][2] = blue;[/LEFT]
    [/COLOR][COLOR=#333333][LEFT]}[/LEFT]
    [/COLOR]

    Also, since you using pixel data that is 3 bytes in size, you might need to use:
    Code :
    [B]glPixelStorei([/B][COLOR=#000000][FONT=monospace]GL_UNPACK_ALIGNMENT, 1);[/FONT][/COLOR]

    instead of using the default value of 4.
    Last edited by Dan Bartlett; 06-02-2012 at 05:38 PM.

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2011
    Posts
    15
    Yes, that was it
    Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •