黑屏与GLUT?

时间:2017-07-12 23:05:26

标签: c++ opengl 3d glut volume

我正在尝试根据this code进行一些体积渲染。

我现在正在做的是我有一些示例3D数据(数组chBuffer本质上是一个4 * 4 * 4测试数据阵列),我正在将其转换为OpenGL 3D纹理。之后,我试图使用其他OpenGL命令渲染此纹理。但是,当我运行应用程序时,屏幕显示为完全黑色。是否有任何具体问题应该解决这个问题?

这是我的代码:

#include <glut.h>
#include <GL/gl.h>
#include <gl/glext.h>
#include <stdio.h>
#include <Windows.h>
GLfloat dOrthoSize = 1.0f;
GLuint mu3DTex;
#define MAP_3DTEXT( TexIndex ) \
    glTexCoord3f(0.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f);  \
    glVertex3f(-dOrthoSize,-dOrthoSize,TexIndex);\
    glTexCoord3f(1.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f);  \
    glVertex3f(dOrthoSize,-dOrthoSize,TexIndex);\
    glTexCoord3f(1.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f);  \
    glVertex3f(dOrthoSize,dOrthoSize,TexIndex);\
    glTexCoord3f(0.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f);  \
    glVertex3f(-dOrthoSize,dOrthoSize,TexIndex);
void main(int argc, char **argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Name");


    char* chBuffer = new char[64];
    for (int i = 0; i < 64; ++i)
        chBuffer[i] = ((i + (i / 8)) % 2) * 128 + 127;


    char* chRGBABuffer = new char[256];
    glGenTextures(1, (GLuint*)&mu3DTex);



    glBindTexture(GL_TEXTURE_3D, mu3DTex);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);



    for (int nIndx = 0; nIndx < 64; ++nIndx)
    {
        chRGBABuffer[nIndx * 4] = chBuffer[nIndx];
        chRGBABuffer[nIndx * 4 + 1] = chBuffer[nIndx];
        chRGBABuffer[nIndx * 4 + 2] = chBuffer[nIndx];
        chRGBABuffer[nIndx * 4 + 3] = chBuffer[nIndx];
    }

    glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 4, 4, 4, 0,
        GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)chRGBABuffer);
    glBindTexture(GL_TEXTURE_3D, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.03f);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();


    glEnable(GL_TEXTURE_3D);
    glBindTexture(GL_TEXTURE_3D, mu3DTex);

    for (float fIndx = -1.0f; fIndx <= 1.0f; fIndx += 0.003f)
    {
        glBegin(GL_QUADS);
        MAP_3DTEXT(fIndx);
        glEnd();
    }
    system("pause");


}

1 个答案:

答案 0 :(得分:0)

这是100%不是如何使用GLUT。

你需要:

  1. 设置glutDisplayFunc()回拨
  2. 在结尾处致电glFlush()
  3. glutMainLoop()中拨打main(),而不是在墙上投掷GL命令并希望获得最佳效果
  4. 像这样(未经测试):

    #include <glut.h>
    #include <GL/gl.h>
    
    GLfloat dOrthoSize = 1.0f;
    
    #define MAP_3DTEXT( TexIndex ) \
        glTexCoord3f(0.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f);  \
        glVertex3f(-dOrthoSize,-dOrthoSize,TexIndex);\
        glTexCoord3f(1.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f);  \
        glVertex3f(dOrthoSize,-dOrthoSize,TexIndex);\
        glTexCoord3f(1.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f);  \
        glVertex3f(dOrthoSize,dOrthoSize,TexIndex);\
        glTexCoord3f(0.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f);  \
        glVertex3f(-dOrthoSize,dOrthoSize,TexIndex);
    
    GLuint mu3DTex;
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glMatrixMode(GL_TEXTURE);
        glLoadIdentity();
    
        glEnable(GL_ALPHA_TEST);
        glAlphaFunc(GL_GREATER, 0.03f);
    
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
        glEnable(GL_TEXTURE_3D);
        glBindTexture(GL_TEXTURE_3D, mu3DTex);
    
        glBegin(GL_QUADS);
        for (float fIndx = -1.0f; fIndx <= 1.0f; fIndx += 0.003f)
        {
            MAP_3DTEXT(fIndx);
        }
        glEnd();
    
        glFlush();
    }
    
    void main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        glutInitWindowSize(500, 500);
        glutCreateWindow("Name");
    
        char* chBuffer = new char[64];
        for (int i = 0; i < 64; ++i)
            chBuffer[i] = ((i + (i / 8)) % 2) * 128 + 127;
    
        char* chRGBABuffer = new char[256];
        glGenTextures(1, (GLuint*)&mu3DTex);
    
        glBindTexture(GL_TEXTURE_3D, mu3DTex);
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    
        for (int nIndx = 0; nIndx < 64; ++nIndx)
        {
            chRGBABuffer[nIndx * 4] = chBuffer[nIndx];
            chRGBABuffer[nIndx * 4 + 1] = chBuffer[nIndx];
            chRGBABuffer[nIndx * 4 + 2] = chBuffer[nIndx];
            chRGBABuffer[nIndx * 4 + 3] = chBuffer[nIndx];
        }
    
        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 4, 4, 4, 0,
            GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)chRGBABuffer);
        glBindTexture(GL_TEXTURE_3D, 0);
    
        glutDisplayFunc( display );
        glutMainLoop();
    }