c - OpenGL 黑屏/未绘制任何内容?

标签 c opengl glut

为什么这段代码会产生黑屏(什么都没画...)?

我正在创建一个 Pong 克隆,但如果不让它工作我就无法继续。

#include <GL/glut.h>

struct Rectangle {
    int x;
    int y;
    int width;
    int height;
};

struct Ball {
    int x;
    int y;
    int radius;
};

typedef struct Rectangle Rectangle;
typedef struct Ball Ball;

Rectangle rOne, rTwo;
Ball ball;

void display(void);
void reshape(int w, int h);
void drawRectangle(Rectangle *r);

int main(int argc, char* argv[]) {

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(800,600);
    glutCreateWindow("Pong");
    gluOrtho2D(0,0,800.0,600.0);

    rOne.x = 100;
    rOne.y = 100;
    rOne.width = 100;
    rOne.height = 50;   

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();

    return 0;

}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    drawRectangle(&rOne);

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h) {
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,0,(GLfloat)w,(GLfloat)h);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void drawRectangle(Rectangle *r) {
    glBegin(GL_QUADS);
        glVertex2i(r->x,r->y);
    glVertex2i(r->x+(r->width-1),r->y);
    glVertex2i(r->x+(r->width-1),r->y+(r->height-1));
    glVertex2i(r->x,r->y+(r->height-1));
    glEnd();
}

最佳答案

您请求宽度为零的正射投影:

gluOrtho2D(0,0,(GLfloat)w,(GLfloat)h);

这可能是你的意思:

gluOrtho2D(0,(GLfloat)w,0,(GLfloat)h);

例子:

#include <GL/glut.h>

typedef struct {
    int x;
    int y;
    int width;
    int height;
} Rect;

typedef struct {
    int x;
    int y;
    int radius;
} Ball;

Rect rOne, rTwo;
Ball ball;

void display(void);
void reshape(int w, int h);
void drawRectangle(Rect *r);

int main(int argc, char* argv[]) 
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800,600);
    glutCreateWindow("Pong");

    rOne.x = 100;
    rOne.y = 100;
    rOne.width = 100;
    rOne.height = 50;   

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();

    return 0;
}

void display(void) 
{
    glClear(GL_COLOR_BUFFER_BIT);

    drawRectangle(&rOne);

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,(GLfloat)w,0,(GLfloat)h);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void drawRectangle(Rect *r) 
{
    glBegin(GL_QUADS);
    glVertex2i(r->x,r->y);
    glVertex2i(r->x+(r->width-1),r->y);
    glVertex2i(r->x+(r->width-1),r->y+(r->height-1));
    glVertex2i(r->x,r->y+(r->height-1));
    glEnd();
}

关于c - OpenGL 黑屏/未绘制任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8609539/

相关文章:

c - 将 memcpy 与十六进制值结合使用

c - 是否保证全局变量总是用c99初始化为0?

c - 动态调度,C

尝试打印二叉树根部的值时崩溃

c++ - 四边形输出纹理不正确

c++ - 如何组织要渲染的对象

c++ - 一起使用 SDL、OpenGL 和 Qt

c++ - QML 下的 OpenGL

c++ - 如何在蓝色中没有所有感觉颜色的情况下将 QUADS 涂成蓝色

c++ - 如何将 "camera"放入 OpenGL 的立方体中