2013-07-19 115 views
0

我目前正在嘗試瞭解3D,並試圖在3D數組和所有有趣的東西中體驗一些樂趣。我似乎無法理解爲什麼我的代碼不是呈現適當的立方體......我通過一個3D數組運行並分配隨機狀態(1繪製,2不繪製)我無法弄清楚爲什麼當我運行狀態時似乎只繪製所有的立方體或沒有。體素塊不呈現正確的塊?

任何幫助表示讚賞,原諒糟糕的代碼。

編輯:(我加了COUT文件,並固定了一些代碼)

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
#include <windows.h> 
#include <time.h> 


#include <SDL.h> 
#include <GL/gl.h> 
#include <GL/glu.h> 
using namespace std; 


float SCALE = 2; 
int i; 
int chunk[16][16][16]; 
int chunkSize = 16; 
time_t seconds; 
int seed = 0; 
int colors; 

void buildChunk(); 
void renderChunk(); 

void drawCube(float size,int x,int y,int z) 
{ 


    glBegin(GL_QUADS); 

    // front face 
    glColor3f(0,200,255); 
    glVertex3f(size/2*x,size/2+y,size/2+z); 
    glVertex3f(-size/2*x,size/2+y,size/2+z); 
    glVertex3f(-size/2*x,-size/2+y,size/2+z); 
    glVertex3f(size/2*x,-size/2+y,size/2+z); 
    // left face 
    glColor3f(0.0,1.0,0.0); 
    glVertex3f(-size/2*x,size/2+y,size/2+z); 
    glVertex3f(-size/2*x,-size/2+y,size/2+z); 
    glVertex3f(-size/2*x,-size/2+y,-size/2+z); 
    glVertex3f(-size/2*x,size/2+y,-size/2+z); 
    // back face 
    glColor3f(0.0,0.0,1.0); 
    glVertex3f(size/2*x,size/2+y,-size/2+z); 
    glVertex3f(-size/2*x,size/2+y,-size/2+z); 
    glVertex3f(-size/2*x,-size/2+y,-size/2+z); 
    glVertex3f(size/2*x,-size/2+y,-size/2+z); 
    // right face 
    glColor3f(1.0,1.0,0.0); 
    glVertex3f(size/2*x,size/2+y,size/2+z); 
    glVertex3f(size/2*x,-size/2+y,size/2+z); 
    glVertex3f(size/2*x,-size/2+y,-size/2+z); 
    glVertex3f(size/2*x,size/2+y,-size/2+z); 
    // top face 
    glColor3f(1.0,0.0,1.0); 
    glVertex3f(size/2*x,size/2+y,size/2+z); 
    glVertex3f(-size/2*x,size/2+y,size/2+z); 
    glVertex3f(-size/2*x,size/2+y,-size/2+z); 
    glVertex3f(size/2*x,size/2+y,-size/2+z); 
    // bottom face 
    glColor3f(0.0,1.0,1.0); 
    glVertex3f(size/2*x,-size/2+y,size/2+z); 
    glVertex3f(-size/2*x,-size/2+y,size/2+z); 
    glVertex3f(-size/2*x,-size/2+y,-size/2+z); 
    glVertex3f(size/2*x,-size/2+y,-size/2+z); 
    glEnd(); 
} 

float angle = 0.0; 
const int triangle = 1; 

void init() 
{ 

    glClearColor(0.0,0.0,0.0,1.0); //background color and alpha 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(45,640.0/480.0,1.0,500.0); 
    glMatrixMode(GL_MODELVIEW); 
    glEnable(GL_DEPTH_TEST); 
    buildChunk(); 
} 

void render() 
{ 

    renderChunk(); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 
    glTranslatef(0.0,0.0,-30.0); 
    glRotatef(angle,1.0,1.0,0.0); // angle, x-axis, y-axis, z-axis 
} 

int main(int argc, char** argv) 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 
    SDL_Surface *screen; 
    screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_OPENGL); 
//  screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_FULLSCREEN); 
    bool running = true; 
    const int FPS = 30; 
    Uint32 start; 
    SDL_Event event; 
    init(); 
    while(running) 
    { 
     start = SDL_GetTicks(); 
     while(SDL_PollEvent(&event)) 
     { 
      switch(event.type) 
      { 
      case SDL_QUIT: 
       running = false; 
       break; 
      } 
     } 

     render(); 
     SDL_GL_SwapBuffers(); 
     angle += 0.5; 
     if(angle > 360) 
      angle -= 360; 
     if(1000/FPS > SDL_GetTicks()-start) 
      SDL_Delay(1000/FPS-(SDL_GetTicks()-start)); 
    } 
    SDL_Quit(); 
    return 0; 
} 

void buildChunk() 
{ 
    seconds = time (NULL); 
    seed = seconds; 
    srand(seed); 
    /* Seed the random number generator with the specified seed */ 
    int x; 
    int y; 
    int z; 



    for(x=0; x < chunkSize; x++) 
    { 
     for(y=0; y < chunkSize; y++) 
     { 
      for(z=0; z < chunkSize; z++) 
      { 




       /* 50% chance for a cell to be alive */ 
       if(rand() % 100 < 50) 
       { 
        chunk[x][y][z] = 1; 
       } 
       else 
       { 
        chunk[x][y][z] = 0; 
       } 
       cout<< "chunk[" << x << "][" << y << "][" << z << "] state: " << chunk[x][y][z]<<endl; 

      } 
     } 
    } 
} 

void renderChunk() 
{ 
    int x; 
    int y; 
    int z; 

    for(x=0; x < chunkSize; x++) 
    { 
     for(y=0; y < chunkSize; y++) 
     { 
      for(z=0; z < chunkSize; z++) 
      { 

       if(chunk[x][y][z] == 1) 
       { 
        drawCube(1,x,y,z); 
       } 


      } 
     } 
    } 
} 

回答

2

請注意,您在您的所有glVertex3f呼叫的第一列中的「*」符號。這應該是一個'+'來抵消頂點x而不是縮放它。您可能還需要考慮按大小縮放每個多維數據集的大小。你會得到類似的東西:

 

    float halfSize = size/2; 
    float xSize = x * size; 
    float ySize = y * size; 
    float zSize = z * size; 

    // front face 
    glColor3f(0,200,255); 
    glVertex3f(xsize + halfSize, ysize + halfSize, zsize + halfSize); 
    glVertex3f(xsize - halfSize, ysize + halfSize, zsize + halfSize); 
    glVertex3f(xsize - halfSize, ysize - halfSize, zsize + halfSize); 
    glVertex3f(xsize + halfSize, ysize - halfSize, zsize + halfSize);