Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Applying texture to a triangle mesh

  1. #1

    Applying texture to a triangle mesh

    I am using "TRIANGLE_FAN" to make a cone.

    To apply image texture to the cone should i use cube_map texture?

    I was successful while i applied image texture to a single triangle, but while applying to the cone in simillar way the application crashed.

    So i had to use cube map texturing to apply the texture to the cone. Is this the right way? Does it mean for any 3D object i need to use cub maps to apply texture?

    Any reply to this would be helpful.

    Thank you.

  2. #2
    Junior Member
    Join Date
    Dec 2008
    Location
    Oslo, Norway
    Posts
    25

    Re: Applying texture to a triangle mesh

    No, you do not need a cube map to texture a normal object. Cube maps are intended for things like environment mapping (ie "reflections").

    The crash sounds like a bug in your application - care to post the source code?
    Erik "kusma" Faye-Lund

  3. #3

    Re: Applying texture to a triangle mesh

    OK,

    I could solve the problem, but now i dont know why it works.

    Please find below my code, the blue block followed by the red block. If executed in the same sequence it is successful. If i just exchange, i.e red block first and then blue block, it crashes at glDrawarray !

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 5 * (sizeof(GLbyte) * 3), vertices, GL_STATIC_DRAW);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3,GL_BYTE, sizeof(GLbyte) * 3, 0);

    glScalef( 0.01f, 0.01f, 0.01f );
    glRotatef((GLfloat)(subtestcase*10), 0.0f, 1.0f, 0.0f);


    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glScalef( 0.01f,0.01f,0.01f );
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_BYTE, sizeof(GLbyte) * 4, 0);


    glDrawArrays(GL_TRIANGLE_FAN, 0, 5);

  4. #4
    Junior Member
    Join Date
    Dec 2008
    Location
    Oslo, Norway
    Posts
    25

    Re: Applying texture to a triangle mesh

    No wonder it's crashing if you swap the two blocks around, you're feeding a NULL-pointer as texture coordinates. Because there's no buffer object bound yet, the pointer-parameter to glTexCoordPointer is treated as an absolute pointer, and you pass 0. If you order the blocks the other way around (ie. the way they are in your post), a buffer object with ALMOST sufficient data is bound before setting the pointer. The fact that the code as it is now runs without crashing, is pure luck

    If buffer object 0 is bound all vertex attribute pointers are absolute. If not, they are relative to the currently bound buffer object. Which leads to the next problem - you haven't made a vbo or filled in sufficient data into the vbo named "buffer". Since you pass a stride of sizeof(GLbyte) * 4 to glTexCoordPointer, you need sizeof(GLbyte) * 4 * vertex count of data, but you only fill in sizeof(GLbyte) * 3 * vertex count in the buffer object called "buffer". My guess is that you'd either want a separate buffer object for the texture coordinates (this is the easiest), or interleave the vertex positions and the texture coordinates inside the same buffer object (this is a bit harder, but also usually a bit faster).
    Erik "kusma" Faye-Lund

  5. #5

    Re: Applying texture to a triangle mesh

    Now that i know why it crashes, the second point is not very clear to me.

    My understanding was a bit different.

    The "size" argument of glvertexpointer was calculated as : vertex_count (5) * co-ordinate size (1byte) * co-ordinate count (3 : xyz). I think this is sufficient to store the vertices.

    The "stride" argument of glTexCoordPointer was calculated as : texture is in RGBA format with 1 byte each for color and alpha component. I though that "glTexImage2D" would upload the actual texture.

    Please correct my understanding.

  6. #6
    Senior Member
    Join Date
    May 2006
    Posts
    353

    Re: Applying texture to a triangle mesh

    Quote Originally Posted by Newbie
    The "stride" argument of glTexCoordPointer was calculated as : texture is in RGBA format with 1 byte each for color and alpha component. I though that "glTexImage2D" would upload the actual texture.
    There's a big difference between textures and texture coordinates. The former is typically an image, the latter are per-vertex attributes used to define which part of a texture should be mapped to a triangle. You need both for texturing.
    Georg Kolling, Imagination Technologies
    Please ask questions specific to PowerVR hardware or SDKs on the PowerVR Insider Forum
    DevTech@imgtec.com | http://www.powervrinsider.com

  7. #7

    Re: Applying texture to a triangle mesh

    I still dont get the textures on all the faces of a cube.

    Please find below my code, if i dont use texCoords then the texture is rendered only on the front face and gets distorted on other faces of the cube.

    GLubyte indices[36] = {0, 1, 2, 0, 2, 3,
    0, 3, 4, 0, 4, 5,
    0, 5, 6, 0, 6, 1,
    7, 6, 1, 7, 1, 2,
    7, 4, 5, 7, 5, 6,
    7, 2, 3, 7, 3, 4
    };

    GLfloat texCoords[] = {
    //FRONT
    0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
    // BACK
    1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
    // LEFT
    1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
    // RIGHT
    1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
    // TOP
    0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
    // BOTTOM
    1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
    };

    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 8 * (sizeof(GLfloat) * 4), cube, GL_STATIC_DRAW);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(GLfloat) * 3, 0);

    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);

    glDrawElements(GL_TRIANGLES, sizeof(indices),GL_UNSIGNED_BYTE, indices);

    Thank you.

  8. #8
    Senior Member
    Join Date
    May 2006
    Posts
    353

    Re: Applying texture to a triangle mesh

    Indices refer to all vertex attributes, so if you have an array of 24 texcoords you should also have an array of 24 positions as well, not just 8. And the indices should range from 0 to 23. You cannot share vertices between faces if the texture coordinates aren't also shared.
    Georg Kolling, Imagination Technologies
    Please ask questions specific to PowerVR hardware or SDKs on the PowerVR Insider Forum
    DevTech@imgtec.com | http://www.powervrinsider.com

  9. #9

    Re: Applying texture to a triangle mesh

    Thank you very much for the reply.

    I modified my code to have 8 texture coordinates, since the cube has 8 vertex coordinates. The vertices are shared. The indices range from 0 to 7. But still the program hangs after executing "glDrawElements".
    and i am using Opengl es 1.1 library.

    GLubyte indices[36] = {
    0, 1, 2, 0, 2, 3,
    0, 3, 4, 0, 4, 5,
    0, 5, 6, 0, 6, 1,
    7, 6, 1, 7, 1, 2,
    7, 4, 5, 7, 5, 6,
    7, 2, 3, 7, 3, 4
    };

    GLfloat texCoords[] = {
    1, 0, 0, 0, 0, 1, 1, 1,
    0, 0, 0, 1, 1, 1, 1, 0,
    };


    glBufferData(GL_ARRAY_BUFFER, 8 * (sizeof(GLfloat) * 4), cube, GL_STATIC_DRAW);
    glVertexPointer(3, GL_FLOAT, sizeof(GLfloat) * 3, 0);

    glTexCoordPointer(2, GL_FLOAT, sizeof(GLfloat) * 3, texCoords);

    glDrawElements(GL_TRIANGLES, sizeof(indices),GL_UNSIGNED_BYTE, indices);

    Please help me.

  10. #10
    Junior Member
    Join Date
    Dec 2008
    Location
    Oslo, Norway
    Posts
    25

    Re: Applying texture to a triangle mesh

    If you're not using a buffer object for the texcoords (which is indicated by the fact that you pass a pointer (texCoords) as the last parameter to glTexCoordPointer, you need to unbind the current buffer object before calling glTexCoordPointer. Alternatively, you could either make two buffer objects, or even better - interleave the positions and texture coordinates inside one buffer object.

    For performance-reasons, you should also consider putting the indices in a buffer object.

    Edit: unbinding the current buffer object would in this case be done by calling "glBindBuffer(GL_ARRAY_BUFFER, 0)"
    Erik "kusma" Faye-Lund

Page 1 of 2 12 LastLast

Similar Threads

  1. Triangular Mesh Texture Mapping
    By jiapei100 in forum OpenGL ES general technical discussions
    Replies: 0
    Last Post: 08-11-2009, 01:52 AM
  2. Replies: 1
    Last Post: 05-13-2008, 10:16 AM

Posting Permissions

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