OpenGL 黑屏/没有绘制?

发布于 2024-12-22 08:25:29 字数 1448 浏览 3 评论 0原文

为什么这段代码会产生黑屏(没有绘制任何内容......)?

我正在创建一个 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();
}

Why does this piece of code produce a black screen (nothing is drawn...)?

I'm creating a Pong clone but I can't proceed without getting this to work.

#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();
}

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

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

发布评论

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

评论(2

当梦初醒 2024-12-29 08:25:30

你的问题就在这里:

gluOrtho2D(0,0,800.0,600.0);

gluOrtho2D 不像 glViewport。 gluOrtho2D 的参数是:

gluOrtho2D(left, right, bottom, top);

所以你必须像这样调用它

gluOrtho(0, w, 0, h);

Your problem lies here:

gluOrtho2D(0,0,800.0,600.0);

gluOrtho2D is not like glViewport. The parameter of gluOrtho2D are :

gluOrtho2D(left, right, bottom, top);

So you must call it like

gluOrtho(0, w, 0, h);
滿滿的愛 2024-12-29 08:25:29

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

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();
}

You're requesting an ortho projection with zero width:

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

This is probably what you meant:

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

Example:

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