0

I've recently learned how to texture in OpenGL. What I've been doing is something like this:

GLfloat vertexdata = {
    -1.0f, -1.0f, 0.0f,    0.0, 0.0,  //first three are xyz, next two are tex coords
    0.0f, 1.0f, 0.0f,      0.5f, 1.0f, 
    ...
};

Then I buffer this into the gpu, create/buffer indices, and create a VAO obj and draw with glDrawElements. Now this worked great for things like triangles or squares, the problem is now I'm trying to draw a cube. For some reason, this is no londer working correctly. Why is that?

vertex data:

static GLfloat data[] = {

    0.5f, 0.5f, 0.5f,      1.0f, 1.0f,
    0.5f, -0.5f, 0.5f,     1.0f, 0.0f,
    -0.5f, -0.5f, 0.5f,    0.0f, 0.0f,
    -0.5f, 0.5f, 0.5f,     0.0f, 1.0f,

    0.5f, 0.5f, -0.5f,     1.0f, 1.0f,
    0.5f, -0.5f, -0.5f,    1.0f, 0.0f,
    -0.5f, -0.5f, -0.5f,   0.0f, 0.0f,
    -0.5f, 0.5f, -0.5f,    0.0f, 1.0f,
};

static GLuint indices[] = {
    0, 1, 2, 3, 2, 0, //front face
    0, 4, 1, 4, 5, 1, //right face
    3, 2, 7, 2, 6, 7, //left face
    4, 5, 6, 7, 6, 4, //back face
    3, 7, 0, 0, 4, 7, //top face
    2, 1, 5, 2, 6, 5,  //bottom face
};

The following image shows the result (I intended for the batman logo to appear once once on each face sorta like this: http://learnopengl.com/img/getting-started/textures_combined2.png ) enter image description here

Ashwin Gupta
  • 2,100
  • 7
  • 23
  • 54
  • In what way is it not working correctly? – user253751 Jul 01 '16 at 04:25
  • @immibis in the picture, notice how the batman logo is displayed on the front, but sorta at an angle and cut off. I want it centered exactly and fully displayed for each face. Also, the sides/tops are completely messed up. The logo is streteched across or doesn't display at all. – Ashwin Gupta Jul 01 '16 at 04:31
  • If you look at the texture coordinates that you're using for each vertex of the cube, you might find out why that is. In particular, the texture coordinates for most of the faces *aren't* texture coordinates that will display the whole image. So the right face is using a slice of the image and stretching it across the whole face in one direction, for example. (I'm not sure why it's sheared though) – user253751 Jul 01 '16 at 10:11
  • You need more than 8 vertices defined, because your coincident vertices from different faces need different UVs to achieve what you want. – Tenfour04 Jul 01 '16 at 14:30
  • @Tenfour04 ok thats what I figured. So there is no other way? I couldn't say, load the tex coords into the indicies array maybe? – Ashwin Gupta Jul 01 '16 at 23:00
  • 2
    No other way. ---- – Tenfour04 Jul 01 '16 at 23:28
  • @Tenfour04 ok thanks, good to know. – Ashwin Gupta Jul 02 '16 at 00:29

0 Answers0