两个立方体在 window 中看不到如何修复

two cubes dont see in window how to fix

我尝试绘制 2 个自动旋转的立方体,但我没有看到这个立方体。现在有人知道如何解决这个问题吗?

另外,我想给这个程序加一个摄像头。你能帮我做这个吗?

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cstdlib>
#define SCREEN_WIDTH 1920
#define SCREEN_HEIGHT 1080

void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
void DrawCube(GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength);

GLfloat rotationX = 0.0f;
GLfloat rotationY = 0.0f;
GLfloat rtri;               // Angle For The Triangle ( NEW )
GLfloat rquad;

int main(void)
{ 
    GLFWwindow *window;

    // Initialize the library
    if (!glfwInit())
    {
        return -1;
    }

    // Create a windowed mode window and its OpenGL context
    window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);

    glfwSetKeyCallback(window, keyCallback);
    glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);

    int screenWidth, screenHeight;
    glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    // Make the window's context current
    glfwMakeContextCurrent(window);

    glViewport(0.0f, 0.0f, screenWidth, screenHeight); // specifies the part of the window to which OpenGL will draw (in pixels), convert from normalised to pixels
    glMatrixMode(GL_PROJECTION); // projection matrix defines the properties of the camera that views the objects in the world coordinate frame. Here you typically set the zoom factor, aspect ratio and the near and far clipping planes
    glLoadIdentity(); // replace the current matrix with the identity matrix and starts us a fresh because matrix transforms such as glOrpho and glRotate cumulate, basically puts us at (0, 0, 0)
    glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1000); // essentially set coordinate system
    glMatrixMode(GL_MODELVIEW); // (default matrix mode) modelview matrix defines how your objects are transformed (meaning translation, rotation and scaling) in your world
    glLoadIdentity(); // same as above comment

    GLfloat halfScreenWidth = SCREEN_WIDTH / 2;
    GLfloat halfScreenHeight = SCREEN_HEIGHT / 2;


    // Loop until the user closes the window
    while (!glfwWindowShouldClose(window))
    {
        glShadeModel(GL_SMOOTH);
        glClearColor(0.6f, 1.0f, 1.0f, 0.1f);
        glClearDepth(1.0f);
        glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
        glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

        // Render OpenGL here
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glPushMatrix();
        glLoadIdentity();
        glTranslatef(-1.5f, 0.0f, -6.0f);   
        glRotatef(rtri, 0.0f, 1.0f, 0.0f);// Move Left 1.5 Units And Into The Screen 6.0
        DrawCube(halfScreenWidth, halfScreenHeight, -500, 200);
        glLoadIdentity();                                   // Reset The Current Modelview Matrix
        glTranslatef(1.5f, 0.0f, -7.0f);
        glRotatef(rquad, 1.0f, 1.0f, 1.0f);
        DrawCube(halfScreenWidth, halfScreenHeight, -500, 200);
        glPopMatrix();
        rtri += 0.2f;                                           // Increase The Rotation Variable For The Triangle ( NEW )
        rquad -= 0.15f;

        // Swap front and back buffers
        glfwSwapBuffers(window);

        // Poll for and process events
        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
}



void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
    //std::cout << key << std::endl;

    const GLfloat rotationSpeed = 10;

    // actions are GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT
    if (action == GLFW_PRESS || action == GLFW_REPEAT)
    {
        switch (key)
        {
        case GLFW_KEY_UP:
            rotationX -= rotationSpeed;
            break;
        case GLFW_KEY_DOWN:
            rotationX += rotationSpeed;
            break;
        case GLFW_KEY_RIGHT:
            rotationY += rotationSpeed;
            break;
        case GLFW_KEY_LEFT:
            rotationY -= rotationSpeed;
            break;
        }


    }
}


void DrawCube(GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength)
{
    GLfloat halfSideLength = edgeLength * 0.5f;

    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f,-1.0f,-1.0f, // triangle 1 : begin
        -1.0f,-1.0f, 1.0f,
        -1.0f, 1.0f, 1.0f, // triangle 1 : end
        1.0f, 1.0f,-1.0f, // triangle 2 : begin
        -1.0f,-1.0f,-1.0f,
        -1.0f, 1.0f,-1.0f, // triangle 2 : end
        1.0f,-1.0f, 1.0f,
        -1.0f,-1.0f,-1.0f,
        1.0f,-1.0f,-1.0f,
        1.0f, 1.0f,-1.0f,
        1.0f,-1.0f,-1.0f,
        -1.0f,-1.0f,-1.0f,
        -1.0f,-1.0f,-1.0f,
        -1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f,-1.0f,
        1.0f,-1.0f, 1.0f,
        -1.0f,-1.0f, 1.0f,
        -1.0f,-1.0f,-1.0f,
        -1.0f, 1.0f, 1.0f,
        -1.0f,-1.0f, 1.0f,
        1.0f,-1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f,-1.0f,-1.0f,
        1.0f, 1.0f,-1.0f,
        1.0f,-1.0f,-1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f,-1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f,-1.0f,
        -1.0f, 1.0f,-1.0f,
        1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f,-1.0f,
        -1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f, 1.0f,
        1.0f,-1.0f, 1.0f
    };
    static const GLfloat g_color_buffer_data[] = {
        0.583f,  0.771f,  0.014f,
        0.609f,  0.115f,  0.436f,
        0.327f,  0.483f,  0.844f,
        0.822f,  0.569f,  0.201f,
        0.435f,  0.602f,  0.223f,
        0.310f,  0.747f,  0.185f,
        0.597f,  0.770f,  0.761f,
        0.559f,  0.436f,  0.730f,
        0.359f,  0.583f,  0.152f,
        0.483f,  0.596f,  0.789f,
        0.559f,  0.861f,  0.639f,
        0.195f,  0.548f,  0.859f,
        0.014f,  0.184f,  0.576f,
        0.771f,  0.328f,  0.970f,
        0.406f,  0.615f,  0.116f,
        0.676f,  0.977f,  0.133f,
        0.971f,  0.572f,  0.833f,
        0.140f,  0.616f,  0.489f,
        0.997f,  0.513f,  0.064f,
        0.945f,  0.719f,  0.592f,
        0.543f,  0.021f,  0.978f,
        0.279f,  0.317f,  0.505f,
        0.167f,  0.620f,  0.077f,
        0.347f,  0.857f,  0.137f,
        0.055f,  0.953f,  0.042f,
        0.714f,  0.505f,  0.345f,
        0.783f,  0.290f,  0.734f,
        0.722f,  0.645f,  0.174f,
        0.302f,  0.455f,  0.848f,
        0.225f,  0.587f,  0.040f,
        0.517f,  0.713f,  0.338f,
        0.053f,  0.959f,  0.120f,
        0.393f,  0.621f,  0.362f,
        0.673f,  0.211f,  0.457f,
        0.820f,  0.883f,  0.371f,
        0.982f,  0.099f,  0.879f
    };


    //glColor3f( colour[0], colour[1], colour[2] );
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, g_vertex_buffer_data);
    glColorPointer(3, GL_FLOAT, 0, g_color_buffer_data);

    glDrawArrays(GL_TRIANGLES, 0, 36);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
}</i>

首先,我建议将 Orthographic Projection Matrix 的原点放在视口的中心:

glOrtho(-SCREEN_WIDTH*0.5,  SCREEN_WIDTH*0.5,
        -SCREEN_HEIGHT*0.5, SCREEN_HEIGHT*0.5,
        0.0, 1000.0);

模型-视图矩阵由视图矩阵和模型矩阵组成。 视图矩阵描述了您如何看待场景,而模型矩阵描述了场景中几何体的位置。

对于您选择单位矩阵的视图矩阵。

对于模型矩阵,您确实正确设置了旋转。您还计算了模型的平移和比例,但您从未使用过。 您错过了在绘图之前平移和缩放模型视图矩阵。

GLfloat halfScreenWidth = SCREEN_WIDTH * 0.5;
GLfloat halfScreenHeight = SCREEN_HEIGHT * 0.5;

GLfloat sideLenght = 200.0f;
GLfloat halfSideLenght = sideLenght * 0.5;

while ( ... )
{
    ...

    // backup the view matrix
    glPushMatrix();

    // set up the model matrix
    glTranslatef(-0.5f * halfScreenWidth, 0.0f, -6.0f);         // translate the model   
    glRotatef(rtri, 0.0f, 1.0f, 0.0f);                          // rotate the model
    glScalef( halfSideLenght, halfSideLenght, halfSideLenght ); // scale the model

    // draw the 1. cube
    DrawCube(/* I removed the parameter here, because they are not used */);

    // restor the view matrix
    glPopMatrix();

    // backup the view matrix
    glPushMatrix();

    // set up the model matrix
    glTranslatef(0.5f * halfScreenWidth, 0.0f, -7.0f);          // translate the model
    glRotatef(rquad, 1.0f, 1.0f, 1.0f);                         // rotate the model
    glScalef( halfSideLenght, halfSideLenght, halfSideLenght ); // scale the model

    // draw the 2. cube
    DrawCube(/* I removed the parameter here, because they are not used */);

    // restor the view matrix
    glPopMatrix();

  ...
}