【问题标题】:C++ OpenGL, GLFW Drawing a simple cubeC++ OpenGL, GLFW 绘制一个简单的立方体
【发布时间】:2014-07-25 07:30:16
【问题描述】:

所以,我正在尝试在 openGL 和 GLFW 中绘制一个简单的立方体。

在下面的代码中,我可以绘制立方体,但它只是显示为一个简单的矩形。这里发生了什么?

我试过“glTransformf(0,0,-10);”,但如果我做的任何事情小于 -2,立方体就会消失。在 -2 处,出现正面。在默认位置 0 处,我可以看到立方体的背面。

另外,当我尝试旋转它时,显示的只是一个从窗口顶部移动到底部的矩形。看起来很奇怪。

谁能帮我找出程序为什么会这样?

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>

#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>

const char* gameTitle = "TEST";
GLFWwindow* window;

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, 0, 0,   0, 0, 1,   0, 1, 1,   0, 1, 0,
1, 0, 0,   1, 0, 1,   1, 1, 1,   1, 1, 0,
0, 0, 0,   0, 0, 1,   1, 0, 1,   1, 0, 0,
0, 1, 0,   0, 1, 1,   1, 1, 1,   1, 1, 0,
0, 0, 0,   0, 1, 0,   1, 1, 0,   1, 0, 0,
0, 0, 1,   0, 1, 1,   1, 1, 1,   1, 0, 1
};

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

bool initWindow(const int resX, const int resY)
{
if(!glfwInit())
{
    fprintf(stderr, "Failed to initialize GLFW\n");
    return false;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing

// Open a window and create its OpenGL context
window = glfwCreateWindow(resX, resY, gameTitle, NULL, NULL);

if(window == NULL)
{
    fprintf(stderr, "Failed to open GLFW window.\n");
    glfwTerminate();
    return false;
}

glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, controls);

// Get info of GPU and supported OpenGL version
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("OpenGL version supported %s\n", glGetString(GL_VERSION));

glEnable(GL_DEPTH_TEST); // Depth Testing
glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
glCullFace(GL_BACK);
return true;
}

static void drawCube()
{
static float alpha = 0;
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
glTranslatef(0,0,-2);
glMatrixMode(GL_MODELVIEW_MATRIX);
//attempt to rotate cube
//glRotatef(alpha, 1, 0, 0);

/* We have a color array and a vertex array */
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);

/* Send data : 24 vertices */
glDrawArrays(GL_QUADS, 0, 24);

/* Cleanup states */
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
alpha += 0.1;
}

static void display()
{
glClearColor(0.0, 0.8, 0.3, 1.0);
while(!glfwWindowShouldClose(window))
{
    // Draw stuff
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawCube();

    // Update Screen
    //glFlush();
    glfwSwapBuffers(window);

    // Check for any input, or window movement
    glfwPollEvents();

    // Scale to window size
    GLint windowWidth, windowHeight;
    glfwGetWindowSize(window, &windowWidth, &windowHeight);
    glViewport(0, 0, windowWidth, windowHeight);
}
}

int main(int argc, char** argv)
{
if(initWindow(1024, 620))
{
    display();
}
printf("Goodbye!\n");
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

【问题讨论】:

  • 如果您好奇,您将直接绘制到剪辑空间,一个以原点为中心的 2x2x2 立方体,如下所述:stackoverflow.com/a/23187139/755921(这就是为什么您观察到边“消失”的原因,因为它们移出该视图体积)

标签: c++ opengl glfw


【解决方案1】:
  1. 您永远不会设置(有意义的)投影矩阵。
  2. Abuse not the projection matrix stack
  3. 不要在drawCube()、单一职责原则等中设置矩阵。
  4. 在尝试绘制之前设置您的视口。
  5. C++ 有 C 头文件的 c 前缀版本 (stdio.h -> cstdio)。改用这些。

大家一起:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <cstdio>

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

GLFWwindow* initWindow(const int resX, const int resY)
{
    if(!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return NULL;
    }
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing

    // Open a window and create its OpenGL context
    GLFWwindow* window = glfwCreateWindow(resX, resY, "TEST", NULL, NULL);

    if(window == NULL)
    {
        fprintf(stderr, "Failed to open GLFW window.\n");
        glfwTerminate();
        return NULL;
    }

    glfwMakeContextCurrent(window);
    glfwSetKeyCallback(window, controls);

    // Get info of GPU and supported OpenGL version
    printf("Renderer: %s\n", glGetString(GL_RENDERER));
    printf("OpenGL version supported %s\n", glGetString(GL_VERSION));

    glEnable(GL_DEPTH_TEST); // Depth Testing
    glDepthFunc(GL_LEQUAL);
    glDisable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    return window;
}

void drawCube()
{
    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, 0, 0,   0, 0, 1,   0, 1, 1,   0, 1, 0,
        1, 0, 0,   1, 0, 1,   1, 1, 1,   1, 1, 0,
        0, 0, 0,   0, 0, 1,   1, 0, 1,   1, 0, 0,
        0, 1, 0,   0, 1, 1,   1, 1, 1,   1, 1, 0,
        0, 0, 0,   0, 1, 0,   1, 1, 0,   1, 0, 0,
        0, 0, 1,   0, 1, 1,   1, 1, 1,   1, 0, 1
    };

    static float alpha = 0;
    //attempt to rotate cube
    glRotatef(alpha, 0, 1, 0);

    /* We have a color array and a vertex array */
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glColorPointer(3, GL_FLOAT, 0, colors);

    /* Send data : 24 vertices */
    glDrawArrays(GL_QUADS, 0, 24);

    /* Cleanup states */
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
    alpha += 1;
}

void display( GLFWwindow* window )
{
    while(!glfwWindowShouldClose(window))
    {
        // Scale to window size
        GLint windowWidth, windowHeight;
        glfwGetWindowSize(window, &windowWidth, &windowHeight);
        glViewport(0, 0, windowWidth, windowHeight);

        // Draw stuff
        glClearColor(0.0, 0.8, 0.3, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_PROJECTION_MATRIX);
        glLoadIdentity();
        gluPerspective( 60, (double)windowWidth / (double)windowHeight, 0.1, 100 );

        glMatrixMode(GL_MODELVIEW_MATRIX);
        glTranslatef(0,0,-5);

        drawCube();

        // Update Screen
        glfwSwapBuffers(window);

        // Check for any input, or window movement
        glfwPollEvents();
    }
}

int main(int argc, char** argv)
{
    GLFWwindow* window = initWindow(1024, 620);
    if( NULL != window )
    {
        display( window );
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

【讨论】:

  • 非常有帮助!非常感谢。奇怪的一件事是,窗口的水平尺寸越小,立方体旋转得越快。这只是 FPS 问题,还是导致它的原因?
  • 听起来您的填充率有限。 GPU 必须触摸的像素越少,它绘制帧的速度就越快。
  • 从 GLFW 版本 3.0 开始,不会自动包含 GLU 标头,因此如果您想在应用程序中使用 glu 函数,您必须先定义它,然后再包含像 #define GLFW_INCLUDE_GLU 这样的 glfw3 标头文件,然后在下一行添加这个#include &lt;GLFW/glfw3.h&gt;
【解决方案2】:

我相信你的问题是你基本上使用的是正交投影,或者肯定不是透视的东西,这会给立方体带来更多我认为你正在寻找的“3D”外观。

尝试以下方法来设置正确的透视投影矩阵:

glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective(45, windowWidth / windowHeight, 0.1f, 100.0f);

// Draw calls.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多