繁体   English   中英

为什么我在opengl中的3D形状表现得很奇怪?

[英]Why are my 3D shapes in opengl acting weird?

我尝试编写一个“引擎”来使用OpenGL渲染形状。 我们的想法是你可以编写“渲染器”,这些渲染器只是渲染形状的函数,并将它们添加到要显示的Engine类的列表中。

所以我添加了一个立方体和一个金字塔(我刚刚从互联网上复制的代码) - 我让它们旋转。

正如您在问题底部的图像中看到的那样 - 形状很奇怪 - 您可以从正面看到形状的背面等。

现在,我明白OpenGL只是按照我告诉它渲染它们的顺序渲染东西 - 导致首先写入的东西首先被渲染 - 但是我使用了glDepthFunc ,它应该使得东西按“深度”呈现而不是顺序写作

#include <GL/glut.h>
#include <iostream>
#include <list>

namespace Graphics
{
    class Engine
    {
    public:
        static void Init();
        static void Display();
        static void Reshape(GLsizei width, GLsizei height);
        static void Timer(int value);
        static bool Run(int argc, char** argv);

        // A renderer is just a method that does stuff and return a boolean
        using renderer_t = bool(*)();

        static void AddRenderer(renderer_t renderer);

    private:
        static std::list<renderer_t> renderers;
    };
}

namespace Graphics
{
    std::list<Engine::renderer_t> Engine::renderers;

    void Engine::Init()
    {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
        glClearDepth(1.0f);                   // Set background depth to farthest
        glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
        glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
        glShadeModel(GL_SMOOTH);   // Enable smooth shading
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections
    }

    void Engine::Display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
        glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix

        for (auto renderer : Engine::renderers)
        {
            if (!renderer())
            {
                std::cout << "A renderer has failed rendering something... :(" << std::endl;
            }
        }

        glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
    }

    void Engine::Reshape(GLsizei width, GLsizei height)
    {  // GLsizei for non-negative integer
        // Compute aspect ratio of the new window
        if (height == 0) height = 1;                // To prevent divide by 0
        GLfloat aspect = (GLfloat)width / (GLfloat)height;

        // Set the viewport to cover the new window
        glViewport(0, 0, width, height);

        // Set the aspect ratio of the clipping volume to match the viewport
        glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
        glLoadIdentity();             // Reset
        // Enable perspective projection with fovy, aspect, zNear and zFar
        gluPerspective(45.0f, aspect, 0.1f, 100.0f);
    }

    void Engine::Timer(int value)
    {
        glutPostRedisplay();      // Post re-paint request to activate display()
        glutTimerFunc(15, Engine::Timer, 0); // next timer call milliseconds later
    }

    bool Engine::Run(int argc, char** argv)
    {
        glutInit(&argc, argv);            // Initialize GLUT
        glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
        glutInitWindowSize(640, 480);   // Set the window's initial width & height
        glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
        glutCreateWindow("FML");          // Create window with the given title
        glutDisplayFunc(Engine::Display);       // Register callback handler for window re-paint event
        glutReshapeFunc(Engine::Reshape);       // Register callback handler for window re-size event
        Engine::Init();                       // Our own OpenGL initialization
        glutTimerFunc(0, Engine::Timer, 0);     // Call the next display immediately
        glutMainLoop();                 // Enter the infinite event-processing loop
        return true;
    }

    void Engine::AddRenderer(renderer_t renderer)
    {
        Engine::renderers.push_back(renderer);
    }
}

using namespace Graphics;

static bool RenderCube()
{
    static auto angleCube = 0.0f;

    // Render a color-cube consisting of 6 quads with different colors
    glLoadIdentity();                 // Reset the model-view matrix
    glTranslatef(1.5f, 0.0f, -7.0f);  // Move right and into the screen
    glRotatef(angleCube, 1.0f, 1.0f, 1.0f);

    glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
    // Top face (y = 1.0f)
    // Define vertices in counter-clockwise (CCW) order with normal pointing out
    glColor3f(0.0f, 1.0f, 0.0f);     // Green
    glVertex3f( 1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f,  1.0f);
    glVertex3f( 1.0f, 1.0f,  1.0f);

    // Bottom face (y = -1.0f)
    glColor3f(1.0f, 0.5f, 0.0f);     // Orange
    glVertex3f( 1.0f, -1.0f,  1.0f);
    glVertex3f(-1.0f, -1.0f,  1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f( 1.0f, -1.0f, -1.0f);

    // Front face  (z = 1.0f)
    glColor3f(1.0f, 0.0f, 0.0f);     // Red
    glVertex3f( 1.0f,  1.0f, 1.0f);
    glVertex3f(-1.0f,  1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f( 1.0f, -1.0f, 1.0f);

    // Back face (z = -1.0f)
    glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
    glVertex3f( 1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f,  1.0f, -1.0f);
    glVertex3f( 1.0f,  1.0f, -1.0f);

    // Left face (x = -1.0f)
    glColor3f(0.0f, 0.0f, 1.0f);     // Blue
    glVertex3f(-1.0f,  1.0f,  1.0f);
    glVertex3f(-1.0f,  1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f,  1.0f);

    // Right face (x = 1.0f)
    glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
    glVertex3f(1.0f,  1.0f, -1.0f);
    glVertex3f(1.0f,  1.0f,  1.0f);
    glVertex3f(1.0f, -1.0f,  1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);
    glEnd();

    angleCube += 0.2f;

    return true;
}

static bool RenderPyramid()
{
    static auto anglePyramid = 0.0f;

    // Render a pyramid consists of 4 triangles
    glLoadIdentity();                  // Reset the model-view matrix
    glTranslatef(-1.5f, 0.0f, -6.0f);  // Move left and into the screen
    glRotatef(anglePyramid, 1.0f, 1.0f, 1.0f);

    glBegin(GL_TRIANGLES);           // Begin drawing the pyramid with 4 triangles
    // Front
    glColor3f(1.0f, 0.0f, 0.0f);     // Red
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);     // Green
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glColor3f(0.0f, 0.0f, 1.0f);     // Blue
    glVertex3f(1.0f, -1.0f, 1.0f);

    // Right
    glColor3f(1.0f, 0.0f, 0.0f);     // Red
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 0.0f, 1.0f);     // Blue
    glVertex3f(1.0f, -1.0f, 1.0f);
    glColor3f(0.0f, 1.0f, 0.0f);     // Green
    glVertex3f(1.0f, -1.0f, -1.0f);

    // Back
    glColor3f(1.0f, 0.0f, 0.0f);     // Red
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);     // Green
    glVertex3f(1.0f, -1.0f, -1.0f);
    glColor3f(0.0f, 0.0f, 1.0f);     // Blue
    glVertex3f(-1.0f, -1.0f, -1.0f);

    // Left
    glColor3f(1.0f,0.0f,0.0f);       // Red
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glColor3f(0.0f,0.0f,1.0f);       // Blue
    glVertex3f(-1.0f,-1.0f,-1.0f);
    glColor3f(0.0f,1.0f,0.0f);       // Green
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glEnd();   // Done drawing the pyramid

    anglePyramid += 0.25f;

    return true;
}

int main(int argc, char** argv)
{
    Engine::AddRenderer(RenderCube);
    Engine::AddRenderer(RenderPyramid);
    return Engine::Run(argc, argv);
}

所得形状的图像旋转

第一张图片 第二张图片 第三张图片

正如你所看到的那样,形状很奇怪 - 即使我使用了glDepthFunc你也可以看到它们的glDepthFunc

除非您特别要求,否则OS /驱动程序没有义务为您提供任何深度缓冲位; 零位,无深度缓冲。

正如@BDL指出的那样,对于GLUT,这意味着GLUT_DEPTH中的ORing与您的glutInitDisplayMode()参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM