簡體   English   中英

在 opengl 中使用 for 循環創建多維數據集

[英]creating cubes with for loop in opengl

我正在嘗試使用 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 當前為空

它現在有效,不知道為什么如果我浪費時間它只是不抱歉:/

這是結束代碼:

#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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM