سؤال

I have a small problem, I made a very basic VBO cube. It works, but when I try to move it (glTranslatef) it get's messy.

Img: http://imgur.com/8mhQm40

Code: http://pastebin.com/hKp0u0QQ

Why does this happen? Also, if anyone sees a solution for the texture problem, that would be great :)

Thanks for reading :)

Included from comments for clarification

I'm using 3 for loops to generate a big cube (16*16*16) But their positions are messed up :/

for (int x = 0; x < 16; x++)
{ 
    for (int y = 0; y < 16; y++)
    { 
        for (int z = 0; z < 16; z++)
        { 
            VBOrender(x, y, z); 
        } 
    } 
 } 
هل كانت مفيدة؟

المحلول

The issue, I believe, is from your use of glTranslate.

As an example we have 3 cubes. We wish to draw them at (0,0,0), (1,0,0), (2,0,0). According to your use of glTranslate the following will happen:

Cube 1 will be drawn at (0,0,0) as glTranslate(0,0,0) is envoked.

Cube 2 will be drawn at (1,0,0) as glTranslate(1,0,0) is envoked.

Cube 3 will be drawn at (3,0,0) as glTranslate(2,0,0) is envoked.

Did you catch what went wrong?

glTranslate translates from the current matrix, not the origin. So you need to reset the matrix back to the origin (0,0,0) at the end of each call to VBOrender. So:

{
    glTranslate(x,y,z);

    // ...

    glTranslate(-x,-y,-z);
}

I suggest using some sort of matrix-stack or creating your own to avoid issues like this in the future.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top