在opengl中使用for循环创建多维数据集

时间:2015-10-29 01:18:52

标签: c++ opengl glut cubes

我正在尝试使用for循环在OpenGL / glut中创建多维数据集,但到目前为止我只会出现溢出。

这是使用纹理创建多维数据集的代码:

void Cubes1Class::render()
{
    for (int face = 0; face < 6; face++)
    {
        if (face < 1)
        {
            glBindTexture(GL_TEXTURE_2D, texHandle[1]);
            glBegin(GL_QUADS);
            for (int vert = 0; vert <= 4; vert++)
            {
                glVertex3fv(VertsZ[FacesZ[face][vert]]);
                glTexCoord2f(TexZ[vert][0], TexZ[vert][1]);
            }
        }
        else
        {
            glBindTexture(GL_TEXTURE_2D, texHandle[0]);
            glBegin(GL_QUADS);
            for (int vert = 0; vert <= 4; vert++)
            {
                glVertex3fv(VertsZ[FacesZ[face][vert]]);
                glTexCoord2f(TexZ[vert][0], TexZ[vert][1]);
            }
        }
        glEnd();
    }
}

这是我尝试用for循环渲染它的地方:

CubesClass::CubesClass(int num)   // constructor
{
    numOfCubes = 3;
    for (int i=0; i<numOfCubes; i++)
    {
        lst[i]=new Cubes1Class();
    }
}

void CubesClass::render()
{
    int x = 0;
    numOfCubes = 3;

    for (x = 0; x < numOfCubes; x++)
    {
        glPushMatrix();
            glTranslatef(0.0, x, 0.0);
            lst[x]->render();
        glPopMatrix();
    }
}

我无法弄清楚我哪里出错了。

有什么想法吗?

编辑:

如果我只是渲染然后翻译然后渲染然后翻译等,那么立方体渲染没有for循环。 这些是数组:

static GLfloat VertsZ[8][3] = {
  { -0.5,  0.5, -0.5 }, /* 0 left top rear */
  {  0.5,  0.5, -0.5 }, /* 1 right top rear */
  {  0.5, -0.5, -0.5 }, /* 2 right bottom rear */
  { -0.5, -0.5, -0.5 }, /* 3 left bottom rear */
  { -0.5,  0.5,  0.5 }, /* 4 left top front */
  {  0.5,  0.5,  0.5 }, /* 5 right top front */
  {  0.5, -0.5,  0.5 }, /* 6 right bottom front */
  { -0.5, -0.5,  0.5 }  /* 7 left bottom front */
};

static GLuint FacesZ[6][4] = {
  7, 6, 5, 4,   /* front */
  6, 2, 1, 5,   /* right */
  3, 7, 4, 0,   /* left */
  5, 1, 0, 4,   /* top */
  7, 6, 2, 3,   /* bottom */
  2, 3, 0, 1    /* rear */
};

static GLfloat TexZ[4][2] = {
    0.0, 0.0,
    0.0, 1.0,
    1.0, 1.0,
    1.0, 0.0
};

cubes1Class目前为空

1 个答案:

答案 0 :(得分:0)

现在不能确定为什么它没有 对不起,如果我浪费时间:/

这是结束代码:

#include "Cubes.h"
#include "bitmap.h"

static GLfloat VertsZ[8][3] = {
{ -0.5,  0.5, -0.5 }, /* 0 left top rear */
{  0.5,  0.5, -0.5 },   /* 1 right top rear */
{  0.5, -0.5, -0.5 },   /* 2 right bottom rear */
{ -0.5, -0.5, -0.5 },   /* 3 left bottom rear */
{ -0.5,  0.5,  0.5 },   /* 4 left top front */
{  0.5,  0.5,  0.5 },   /* 5 right top front */
{  0.5, -0.5,  0.5 },   /* 6 right bottom front */
{ -0.5, -0.5,  0.5 }    /* 7 left bottom front */
};

static GLuint FacesZ[6][4] = {
7, 6, 5, 4, /* front */
6, 2, 1, 5, /* right */
3, 7, 4, 0, /* left */
5, 1, 0, 4, /* top */
7, 6, 2, 3, /* bottom */
2, 3, 0, 1  /* rear */
};

static GLfloat NormalsZ[6][3] = {
0, 0, 1,    /* front */
1, 0, 0,    /* right */
-1, 0, 0,   /* left */
0, 1, 0,    /* top */
0, -1, 0, /* bottom */
0, 0, -1   /* rear */
};

static GLfloat TexZ[4][2] = {
{0.0, 0.0 },
{0.0, 1.0 },
{1.0, 1.0 },
{1.0, 0.0 }
};

static CBitmap *bmp0; // the texture 
static CBitmap *bmp1; // the texture 
static GLuint texHandle[2]; // the open gl texture handle

void readTextureFile()
{
bmp0= new CBitmap("metal.bmp");
printf("bmp size %d %d\n",bmp0->GetWidth(),bmp0->GetHeight());

bmp1= new CBitmap("starTexture3.bmp");
printf("bmp size %d %d\n",bmp1->GetWidth(),bmp1->GetHeight());

glGenTextures(2, &texHandle[0]);

// Texture handle will be bound to the BMP bitmap data just
// as it was read in...
glBindTexture(GL_TEXTURE_2D, texHandle[0]);
glTexImage2D(GL_TEXTURE_2D,
       0,                              // Mipmap level 0
       3,                              // 3 bytes [R,G,B] per pixel
       bmp0->GetWidth(),   // width of bitmap
       bmp0->GetHeight(),  // height of bitmap
       0,                              // no border pixels
       GL_RGBA,                     // bitmap pixel format
       GL_UNSIGNED_BYTE,               // format for GL to store it
       bmp0->GetBits()               // pointer to the pixel data
 );  

 glBindTexture(GL_TEXTURE_2D, texHandle[1]);
 glTexImage2D(GL_TEXTURE_2D,
       0,                              // Mipmap level 0
       3,                              // 3 bytes [R,G,B] per pixel
       bmp1->GetWidth(),   // width of bitmap
       bmp1->GetHeight(),  // height of bitmap
       0,                              // no border pixels
       GL_RGBA,                     // bitmap pixel format
       GL_UNSIGNED_BYTE,               // format for GL to store it
       bmp1->GetBits()               // pointer to the pixel data
);

}
// ------------------------- Cubes1Class -----------------------------

Cubes1Class::Cubes1Class() // constructor
{

}

void Cubes1Class::render()
{
for (int face = 0; face < 6; face++)
{
    if (face < 1)
    {
        glBindTexture(GL_TEXTURE_2D, texHandle[1]);
        glBegin(GL_QUADS);
        for (int vert = 0; vert <= 4; vert++)
        {
            glTexCoord2f(TexZ[vert][0], TexZ[vert][1]);
            glVertex3fv(VertsZ[FacesZ[face][vert]]);
        }
    }
    else
    {
        glBindTexture(GL_TEXTURE_2D, texHandle[0]);
        glBegin(GL_QUADS);
        for (int vert = 0; vert <= 4; vert++)
        {
            glTexCoord2f(TexZ[vert][0], TexZ[vert][1]);
            glVertex3fv(VertsZ[FacesZ[face][vert]]);
        }
    }
    glEnd();


}
}

// ------------------------- CubesClass -----------------------------

//Cubes1Class *lst[MAX_NUM_CUBES]; // the cubes
//GLfloat angle[MAX_NUM_CUBES]; // the cubes rotations

CubesClass::CubesClass(int num)   // constructor
{
    numOfCubes = 4;
    for (int i=0; i<numOfCubes; i++)
    {
        lst[i]=new Cubes1Class();
    }
}

void CubesClass::render()  // draw it
{
    for (int i=0; i<numOfCubes; i++)
    {
        glPushMatrix();
            glTranslatef(0.0, i, 0.0);

            lst[i]->render();
        glPopMatrix();
    }
}