c++ - OpenGL 和 Glut 中的空白屏幕

标签 c++ opengl glut

尝试绘制立方体时,我得到的是空白屏幕。这是一个带有我的 glClearColor 背景的屏幕。

我的代码在很多文件中,所以我不能在这里全部发布,所以我把它放在了 on Github .你会想看看它。这是一个 visual studio 2012 文件,但所有源代码都适用于任何 IDE。虽然非常不完整,但这里有一些片段:

编辑:我现在可以看到我的形状,但它们完全没有光。即使当我在 glutSolidTeapot 中使用已经制作好的法线时,它看起来也很平坦。此外,即使我将远切割平面指定为 1000,我的形状也会在此之前消失。我已经更新了我的代码。

CodeIndigo.cpp

// Entry point!
// Code indigo is a 3d mystery game. Readme for more information.

#include "CodeIndigo.h"
#include <iostream>


void box_update (const int& frame, const Object& self)
{
    return;
}

void update (int frame)
{
    if (Indigo::keys ['w'])
    {
        Indigo::Current_World.camera.Move (0.05);
    }
    if (Indigo::keys ['s'])
    {
        Indigo::Current_World.camera.Move (-0.05);
    }
    if (Indigo::keys ['a'])
    {
        Indigo::Current_World.camera.Move (0.0, -0.05);
    }
    if (Indigo::keys ['d'])
    {
        Indigo::Current_World.camera.Move (0.0, 0.05);
    }
    if (GL_NO_ERROR != glGetError ())
    {
        std::cout << "Error: " << glGetError () << std::endl;
    }
    if (Indigo::keys ['3'])
    {
        Camera camera = Indigo::Current_World.camera;
        std::cout << camera.X << ", " << camera.Y << ", " << camera.Z << " looking at "
            << camera.eye.Get_X () << ", " << camera.eye.Get_Y () << ", " << camera.eye.Get_Z () << std::endl;
    }
}

void mouse_moved (int x, int y)
{
    static const float sensitivity = 0.5;
    Indigo::Current_World.camera.eye.Add_Direction (0.0, x * sensitivity,
        y * -1 * sensitivity);
    std::cout << x << ", " << y << std::endl;
}

int main(int argc, char ** argv)
{
    Indigo::Initialize (argc, argv, " ~ Code Indigo",
        800, 600, true, 60, Indigo::Sky_Color, 60);
    Mesh box = Mesh::Sphere (0.5);
    Object add = Object(0.0, 0.0, -1.0, box, Indigo::White_Color, 40.0f, box_update);
    int object = Indigo::Current_World.Add_Object (add);
    Indigo::Update_Function = update;
    Indigo::Relative_Mouse_Moved_Function = mouse_moved;
    Indigo::Current_World.lighting.Add_Light (0.0, 2.0, 0.0);
    Indigo::Current_World.camera.Place (0.0, 0.0, 0.0);
    Indigo::Current_World.camera.eye.Set_Direction (1.0, 90.0, -2.8);
    Indigo::Run ();
    return (0);
}

靛蓝::初始化

// Initializes window and rendering matrices.
void Initialize (int argc, char ** argv, const char * window_name,
    const int& window_width, const int& window_height, const bool& fullscreen,
    int field_of_view, float * background, int max_framerate)
{
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize (window_width, window_height);
    glutCreateWindow (window_name);
    if (fullscreen)
    {
        glutFullScreen ();
    }
    if (background)
    {
        glClearColor (background [0], background [1], background [2], 1.0);
    }
    else
    {
        glClearColor (Sky_Color [0], Sky_Color [1], Sky_Color [2], 1.0);
    }
    Frame_Length_Minimum = 1000 / max_framerate;
    glutSetKeyRepeat (GLUT_KEY_REPEAT_OFF);
    glutDisplayFunc (Render);
    glutTimerFunc (10, Update, 0);
    glutReshapeFunc (Reshape);
    glutPassiveMotionFunc (Mouse_Moved);
    glutMouseFunc (Mouse_Button);
    glutKeyboardFunc (Key_Pressed);
    glutKeyboardUpFunc (Key_Released);
    glMatrixMode (GL_PROJECTION);
    Reshape ();
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    glShadeModel (GL_SMOOTH);
    glEnable (GL_DEPTH_TEST);
    return;
}

靛蓝:: reshape

// Acts for when the window reshapes
void Reshape (int width, int height)
{
    bool viewport = true;
    if (0 == width)
    {
        width = glutGet (GLUT_WINDOW_WIDTH);
        viewport = false;
    }
    if (0 == height)
    {
        height = glutGet (GLUT_WINDOW_HEIGHT);
        viewport = false;
    }
    if (viewport)
    {
        glViewport (0, 0, width, height);
    }
    glLoadIdentity ();
    gluPerspective (Field_Of_View,
        (float) width / (float) height,
        0.5, 1000.0);
}

世界::渲染

void World::Render (void) const
{
    // Renders every object in the world
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode (GL_PROJECTION);
    Indigo::Reshape ();
    glMatrixMode (GL_MODELVIEW);
    camera.Look ();
    lighting.Position_Lights ();
    // <DELETE>
    float full_array [] = {1.0, 1.0, 1.0, 1.0};
    glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, full_array);
    glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, full_array);
    glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 60.0);
    glutSolidTeapot (0.3);
    // </DELETE>
    for (int Object_ID=0; Object_ID<objects.size (); ++Object_ID)
    {
        const_cast <Object&> (objects [Object_ID]).Render ();
    }
    glutSwapBuffers ();
    return;
}

对象::渲染

// Renders the object
void Object::Render (void) const
{
    float full_array [] = {1.0, 1.0, 1.0, 1.0};
    glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, object_color ? object_color : full_array);
    glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, full_array);
    glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, object_shine);
    glPushMatrix ();
    glTranslatef (X, Y, Z);
    std::vector <Vertex> points = Data.Get_Vertices ();
    glBegin (Render_Types [Data.Group_Size]);
    for (int Point=0; Point<points.size (); Point++)
    {
        // When each polygon is finished, calculate a light normal
        if ((Point + 1) % (Data.Group_Size == 0 ? 3 : Data.Group_Size) == 0)
        {
            Vertex two = points [Point - 1] - points [Point - 2];
            Vertex three = points [Point] - points [Point - 2];
            glNormal3f (two.Z * three.Y - two.Y * three.Z,
                two.X * three.Z - two.Z * three.X,
                two.Y * three.X - two.X * three.Y);
        }
        Vertex Cursor = points [Point];
        glVertex3f (Cursor.X, Cursor.Y, Cursor.Z);
    }
    glEnd ();
    glPopMatrix ();
    return;
}

对不起,代码太多了!

困难在于我不知道问题是否在于我:

  • 使用gluPerspective设置投影矩阵
  • 一些我不知道的 Glut 事情
  • 画出形状
  • 还有什么

最佳答案

我同意 pwny。太多未经测试的代码。常见原因:

  1. 相机/物体位置不佳
  2. 错误的截头体/视角(错误的 znearzfar)
  3. 启用纹理但不使用它们
  4. 不当的光照/ Material 参数
  5. 忘记设置glColor

检查错误

  1. 禁用 GL_CULL_FACE,GL_DEPTH_TEST,GL_TEXTURE_1D,2D,...lighting,material,blend

  2. 将投影 (GL_PROJECTION)、相机和模型 (GL_MODELVIEW) 设置为定义的状态(例如单位矩阵)

  3. 尝试在没有 GL_CULL_FACE 的情况下以正确的位置和大小呈现 GL_QUAD 以使其可见

  4. 成功后设置投影矩阵为透视

    调整 znear,zfar 和四边形位置以匹配您的场景

  5. 用你的立方体替换四边形

  6. 成功后重新启用所有您需要的增量

PS. 还有一个好主意是检查 glerrors(可能完全是其他错误)

关于c++ - OpenGL 和 Glut 中的空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18194445/

相关文章:

c++ - 获取引用指向的对象的地址

c++ - 请解释这个表达

c++ - std::vector 到 GLvoid*?

opengl - glm 结合旋转和平移

c++ - 碰撞检测有效但有小故障?

C++ u8 literal char 覆盖

c++ - C++ 行换列

c++ - 为什么我的顶点缓冲区对象出现访问冲突错误?

c++ - 如何用假参数初始化过剩?

c++ - 如何设置opengl对象的大小?