1
votes

I want to create a 3d cube with openGL. Also, I want to cover each side with an image that I transform in a texture.

I find cube coordinates in 2D, and I create a QUADS for each side.

My problem is that when I render textures corresponding cube sides, I see these textures overlap each other, as you can see in this image:

enter image description here

my code is:

Initialization:

glGenTextures(2, textures);
glClearColor (0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0f);                         // Depth Buffer Setup
glEnable(GL_DEPTH_TEST);                        // Enables Depth Testing
glDepthFunc(GL_ALWAYS);

Transform image in thexture:

up = imread("up.png");

glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glBindTexture(GL_TEXTURE_2D, textures[1]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, up.cols, up.rows, GL_RGB, GL_UNSIGNED_BYTE, up.data);

Display cube:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);

    // Set Projection Matrix
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, WIDTH, HEIGHT, 0); 

    // Switch to Model View Matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 

glBindTexture(GL_TEXTURE_2D, textures[1]);
        glBegin(GL_QUADS);
        //sopra
        glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((coord[6].x)),(GLfloat)(coord[6].y));
        glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((coord[5].x)),(GLfloat)(coord[5].y));
        glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((coord[4].x)),(GLfloat)(coord[4].y));
        glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((coord[7].x)),(GLfloat)(coord[7].y)); 
        glEnd();

I do the same for the other sides of the cube. The order in which I render textures is:

bottom (ground) side

up side

behind side

front side

left side

right side

what is wrong or what am I missing? Or, Maybe cannot create a 3d cube with 2d coordinates (glVertex2f (...))?

Thanks for your help!

1
Looks like a problem with z-buffering. - Connor Hollis
could you tell me something more? OpenGL is new for me... - Cristina1986
Why don't you use regular 3D object instead of faking it in 2D? It messes up your depth buffer and distorts your texture. - Matzi
@Matzi because I'm able to find 2d coordinates... I don't know how to find 3d coordinates.. - Cristina1986

1 Answers

2
votes

You can't create a cube with 2d coordinates. The sides are overlapping because they are all on the same plane in space. A cube is in 3d space so needs 3 coordinates, x, y, and z.

So try using:

glVertex3f(x, y, z);

and use some appropriate z values depending on where you want each face.

For the texture you can still use:

glTexCoord2f(x, y);

since the textures are in 2 dimensional space.

If you are still confused about what to use for your coordinates I suggest you read this to help you understand 3d space in openGL:

http://www.falloutsoftware.com/tutorials/gl/gl0.htm