C++ OpenGL 我在屏幕上看不到一个立方体

huangapple go评论64阅读模式
英文:

C++ OpenGL I can't see a cube on the screen

问题

抱歉,我无法提供代码翻译,以下是你的代码的描述:

你在屏幕上看不到一个立方体。你使用glTranslatef定位了它,然后用LoadIdentity使其停在某个位置。你想通过改变矩阵模式来得到一个摄像头,但是立方体并没有出现。

你的代码包括一个立方体的顶点和颜色数组,以及用于绘制立方体的函数drawCube()。在主函数main()中,你初始化了窗口和OpenGL环境,设置了键盘回调函数,启用了深度测试,并在一个循环中进行绘制操作。

在循环中,你设置了视口,清除颜色和深度缓冲区,设置了投影矩阵,并使用gluLookAt()设置了视图矩阵。然后通过glTranslatef()将立方体移动到指定位置,并调用drawCube()函数进行绘制。最后交换缓冲区并处理事件。

你怀疑无法绘制立方体,但检查代码后发现绘制立方体的部分没有问题。可能的原因需要进一步检查OpenGL的状态设置、摄像机位置、视角设置或者渲染管线的配置。

英文:

I can't see a cube on the screen.I located it with glTranslatef. I got it to stop somewhere with LoadIdentity. I wanted to get a camera by changing the Matrix Modes, but the cube does not appear.

Code:

#include <Windows.h>

#include <gl/GL.h>
#include <gl/GLU.h>
#include <GLFW/glfw3.h>

#include <cstdio>

int width = 1280;
int height = 720;

GLfloat vertices[] = {
    -1, -1, -1,   -1, -1,  1,   -1,  1,  1,   -1,  1, -1,
    1, -1, -1,    1, -1,  1,    1,  1,  1,    1,  1, -1,
    -1, -1, -1,   -1, -1,  1,    1, -1,  1,    1, -1, -1,
    -1,  1, -1,   -1,  1,  1,    1,  1,  1,    1,  1, -1,
    -1, -1, -1,   -1,  1, -1,    1,  1, -1,    1, -1, -1,
    -1, -1,  1,   -1,  1,  1,    1,  1,  1,    1, -1,  1
};

GLfloat colors[] =
{
    0, 1, 0,   0, 1, 0,   0, 1, 0,   0, 1, 0,
    0, 1, 0,   0, 1, 0,   0, 1, 0,   0, 1, 0,
    0, 1, 0,   0, 1, 0,   0, 1, 0,   0, 1, 0,
    0, 1, 0,   0, 1, 0,   0, 1, 0,   0, 1, 0,
    0, 1, 0,   0, 1, 0,   0, 1, 0,   0, 1, 0,
    0, 1, 0,   0, 1, 0,   0, 1, 0,   0, 1, 0
};

void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
    if (action == GLFW_PRESS)
        if (key == GLFW_KEY_ESCAPE)
            glfwSetWindowShouldClose(window, GL_TRUE);
}

void drawCube() {

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glColorPointer(3, GL_FLOAT, 0, colors);
    
    glDrawArrays(GL_QUADS, 0, 24);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

}


int main(void)
{
    GLFWwindow* window;

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

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(width, height, "C++ OpenGL Test Area", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

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

    glfwSetKeyCallback(window, keyboard);


    glEnable(GL_DEPTH_TEST); 
    
    


    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        glViewport(0, 0, width, height);

        /* Render here */
        glClearColor(0.0, 192/256, 1, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();


        gluLookAt(1, 1, 1, 0, 0, 0, 0, 1, 0);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glTranslatef(0, 0, 5);
        drawCube();


        glFlush();

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

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

    glfwTerminate();
    return 0;
}

I thought it was because I couldn't draw the cube. I checked the codes again, but there is no problem drawing the cube.

答案1

得分: 1

立方体不在视图体积内。如果你没有指定投影矩阵,视图体积将是围绕相机位置对齐在视线上的一个唯一的立方体。不在视图体积内的任何几何图形都会被裁剪掉。你必须设置一个投影矩阵。投影矩阵定义了在场景中投射到视口上的体积。可以使用 glOrtho 设置正交投影矩阵。可以使用 glFrustumgluPerspective 设置透视投影矩阵。透视投影矩阵定义了一个视景体。例如:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)width / (float)height;
gluPerspective(90.0, aspect, 0.1, 10.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4, 4, 4, 0, 0, 0, 0, 1, 0);

drawCube();
英文:

The cube is not in the viewing volume. If you do not specify a projection matrix, the viewing volume is a unique cube around the camera position, aligned along the line of sight. Any geometry that is not in the viewing volume is clipped. You must set up a projection matrix. The projection matrix defines the volume in the scene that is projected onto the viewport. An orthographic projection matrix can be set with glOrtho. A perspective projection matrix can be set with glFrustum or gluPerspective. The perspective projection matrix defines a Viewing frustum. e.g.:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)width / (float)height;
gluPerspective(90.0, aspect, 0.1, 10.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4, 4, 4, 0, 0, 0, 0, 1, 0);

drawCube();

huangapple
  • 本文由 发表于 2023年2月13日 23:12:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定