过剩问题

发布于 2024-10-16 00:51:18 字数 2036 浏览 2 评论 0原文

当我编译后尝试运行此代码时,我只得到一个没有内容的窗口边框,那么错误在哪里?

注意:我使用 ubuntu 和 gcc 来编译

gcc -lglut  Simple.c -o Simple

输出

#include <GL/glut.h> // Header File For The GLUT Library 
#include <GL/gl.h> // Header File For The OpenGL Library 
#include <GL/glu.h> // Header File For The GLu Library

void SetupRC(void);
void RenderScene(void);
void ChangeSize(GLsizei w, GLsizei h);

// Called to draw scene
void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT);

    // set the color to these values
    //          R    G     B
    glColor3f(56.0f, 19.0f,68.0f);

    // Draw a filled rectangle with current color
    glRectf(-25.0f, 50.0f, 50.0f, -25.0f);

    // Flush drawing commands
    glFlush();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(640,468);
    glutCreateWindow("Simple");
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);

    SetupRC();
    glutMainLoop();

    return 0;
}

// Setup the rendering state
void SetupRC(void)
{
    glClearColor(0.0f, 2.0f, 1.0f, 0.0f);
}

// Handling window resizing
void ChangeSize(GLsizei w, GLsizei h)
{
    GLfloat aspectRatio;

    // Prevent divide by zero
    if (h == 0) 
        h = 1;

    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Establish clipping volume (left, right, bottom, top, near, far)
    aspectRatio = (GLfloat) w / (GLfloat) h;
    if (w <= h) {
        glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
    }
    else
        glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

When i try to run this code after compiling it, i got only a window border without content, so where is the error?

Note: i am using ubuntu and gcc for compiling

gcc -lglut  Simple.c -o Simple

The output

#include <GL/glut.h> // Header File For The GLUT Library 
#include <GL/gl.h> // Header File For The OpenGL Library 
#include <GL/glu.h> // Header File For The GLu Library

void SetupRC(void);
void RenderScene(void);
void ChangeSize(GLsizei w, GLsizei h);

// Called to draw scene
void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT);

    // set the color to these values
    //          R    G     B
    glColor3f(56.0f, 19.0f,68.0f);

    // Draw a filled rectangle with current color
    glRectf(-25.0f, 50.0f, 50.0f, -25.0f);

    // Flush drawing commands
    glFlush();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowPosition(400,200);
    glutInitWindowSize(640,468);
    glutCreateWindow("Simple");
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);

    SetupRC();
    glutMainLoop();

    return 0;
}

// Setup the rendering state
void SetupRC(void)
{
    glClearColor(0.0f, 2.0f, 1.0f, 0.0f);
}

// Handling window resizing
void ChangeSize(GLsizei w, GLsizei h)
{
    GLfloat aspectRatio;

    // Prevent divide by zero
    if (h == 0) 
        h = 1;

    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Establish clipping volume (left, right, bottom, top, near, far)
    aspectRatio = (GLfloat) w / (GLfloat) h;
    if (w <= h) {
        glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
    }
    else
        glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾其所爱 2024-10-23 00:51:18

尝试将 glutSwapBuffers() 添加到 RenderScene() 的末尾。

Try adding a glutSwapBuffers() to the end of RenderScene().

当爱已成负担 2024-10-23 00:51:18

一件事。您有一个深度缓冲区,因此您还应该将 GL_DEPTH_BUFFER_BIT 添加到清除调用中。

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

One thing. You have a depth buffer so you should also add GL_DEPTH_BUFFER_BIT to the clear call.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文