c++ - 体素 block 没有渲染正确的 block ?

标签 c++ rendering voxel

我目前正在尝试学习 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);
                }


            }
        }
    }
}

最佳答案

请注意,在所有 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);

关于c++ - 体素 block 没有渲染正确的 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17737439/

相关文章:

c++ - OpenGL:渲染大量立方体

javascript - 稀疏体素八叉树

c++ - 交换结构 vector 的内容 c++

C++ 和 Lua 集成参数 (...) (Lua 5.1)

html - 在 iOS 中的 UITableViewCell 上渲染 HTML

zend-framework - ZEND,用数据渲染不同的 View

c++ - 基于 btIDebugDraw 的 CGAL 渲染器(没有 QT)?

c++ - 错误: “expression must have class type” & “left of must have class/struct/union”

c++ - 是否允许 std::vector::size() 要求非平凡的计算?什么时候有意义?

python - 使用 numpy 数组中的索引执行计算和比较