簡體   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