【问题标题】:OpenGL with GLUT - black screenOpenGL 与 GLUT - 黑屏
【发布时间】:2015-10-31 17:33:09
【问题描述】:

我想接收四个三角形的小动画。我写了几个版本的代码,但这些版本都不起作用。结果我有一个空白的黑屏。 我的代码:

#define GLUT_DISABLE_ATEXIT_HACK
#define TIMERSECS 20

#include <windows.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/GL.H>
#include <stdlib.h>

float arc = 0.0f;

void draw_traingle(float x0, float y0, float x1, float y1, float x2, float y2) {
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f); //Blue
    glVertex2f(x0, y0);
    glVertex2f(x1, y1);
    glVertex2f(x2, y2);
}

void draw(void) {
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
    glRotatef(arc, 1.0f,0.0f,0.0f);

    glBegin(GL_POLYGON);

    float center_x = 300.0f;
    float center_y = 300.0f;

    float up_x = center_x;
    float up_y = 250.0f;

    float down_x = center_x;
    float down_y = 350.0f;

    float right_x = 350.0f;
    float right_y = center_y;

    float left_x = 250.0f;
    float left_y = center_y;
    glPushMatrix();
    draw_traingle(up_x, up_y, right_x, right_y, center_x, center_y);
    draw_traingle(right_x, right_y, down_x, down_y, center_x, center_y);
    draw_traingle(down_x, down_y, left_x, left_y, center_x, center_y);
    draw_traingle(left_x, left_y, up_x, up_y, center_x, center_y);
    glPopMatrix();
    glEnd();
    glFlush();
    glutSwapBuffers();
}

void handleKeypress(unsigned char key, int x, int y) {
    switch (key) {
    case 27:
        exit(0);
    }
}
void init(void) {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glViewport(0, 0, 600, 600);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); //=1
    gluOrtho2D(0.0, 600.0, 0.0, 600.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); //=1
}
void update(int value) {
    arc += 2.0f;
    if (arc > 360.f) {
        arc -= 360;
    }
    glutPostRedisplay();
    glutTimerFunc(TIMERSECS, update, 0);
}
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //single buffer and RGBA
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Window");
    init();
    glutDisplayFunc(draw);
    glutTimerFunc(TIMERSECS, update, 0);
    glutKeyboardFunc(handleKeypress);
    glutMainLoop();
    return 0;
}

我想创建一个动画,在其中我从三角形构建的矩形将每隔一小段时间旋转 2 度。我想像时钟一样旋转它。我知道问题不是及时(不是小),而是在 glRotatef - 我不知道应该需要什么参数才能给我适当的效果。

提前致谢! :)

【问题讨论】:

    标签: c++ opengl glut


    【解决方案1】:
    #include <GL/glut.h>
    #include <GL/gl.h>
    #include <stdlib.h>
    //#define TIMERSECS 200 prefer not to use preprocessor macros
    //instead you can use:
    static const int TIMERSECS = 200;
    
    float arc = 0.0f;
    
    void draw_traingle(float x0, float y0, float x1, float y1, float x2,     float y2) {
        glBegin(GL_POLYGON);
        glColor4f(0.0f, 0.0f, 1.0f, 1.0f); //Blue
        glVertex2f(x0, y0);
        glVertex2f(x1, y1);
        glVertex2f(x2, y2);
        glEnd();
    }
    
    void draw(void) {
        glClear(GL_COLOR_BUFFER_BIT);
        glTranslatef(300, 300, 0);
        glRotatef(arc, 0.0f,0.0f,1.0f);
        glTranslatef(-300, -300, 0);
    
        float center_x = 300.0f;
        float center_y = 300.0f;
    
        float up_x = center_x;
        float up_y = 250.0f;
    
        float down_x = center_x;
        float down_y = 350.0f;
    
        float right_x = 350.0f;
        float right_y = center_y;
    
        float left_x = 250.0f;
        float left_y = center_y;
        draw_traingle(up_x, up_y, right_x, right_y, center_x,     center_y);
        draw_traingle(right_x, right_y, down_x, down_y, center_x, center_y);
        draw_traingle(down_x, down_y, left_x, left_y, center_x, center_y);
        draw_traingle(left_x, left_y, up_x, up_y, center_x, center_y);
    
        glutSwapBuffers();
    }
    
    void handleKeypress(unsigned char key, int x, int y) {
        switch (key) {
        case 27:
                exit(0);
        }
    }
    
    void init() {
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glViewport(0, 0, 600, 600);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity(); //=1
        gluOrtho2D(0.0, 600.0, 0.0, 600.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity(); //=1
    }
    void update(int value) {
        arc += 2.0f;
        if (arc > 360.f) {
                arc -= 360;
        }
        glutPostRedisplay();
        glutTimerFunc(200, update, 1);
    }
    int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE  | GLUT_RGBA); //single buffer and RGBA
        glutInitWindowSize(600, 600);
        glutCreateWindow("Window");
        init();
        glutDisplayFunc(draw);
        glutTimerFunc(TIMERSECS, update, 1);
        glutKeyboardFunc(handleKeypress);
        glutMainLoop();
        return 0;
    }
    

    旋转总是相对于您使用的坐标系。变换总是以相反的顺序应用。当我们使用 rotatef 函数旋转对象时,它总是相对于坐标系的中心。所以首先我们需要将它翻译到由 translatef(-300,-300,0) 完成的中心。现在对象位于中心。然后我们使用 rotatef 将对象旋转到 x、y 或 z 轴。然后我们再次将对象平移到变换之前的位置。最后我们以相反的顺序编写函数。你很高兴:)

    【讨论】:

    • 嗨,感谢您的回复 ;) 我已经更改了我的代码,现在它看起来像 - pastebin.com/gNcjWLad - 现在,在启动它之后 - 它冻结了.. :
    • 该死的 :P 首先将 glflush() 替换为 glutSwapBuffers() (后者导致无需调用即可刷新)。替换 main 中的 init() 调用并将其作为 glutReshapeFunc(init) 中的一个参数。删除 draw() 中的第一个 glLoadIdentity()(函数的 line1)。 Draw_triangle 应该有 begin(GL_TRIANGLE) 后跟 glEnd()。
    • 嗨! :D 这仍然不好 - pastebin.com/CfEqHKzW 或者也许我做错了什么......?
    • 在绘制三角形之前使用 glEnd()。你想创建什么形状?
    • 还是不行。好的..当我将计时器更改为 2000 时,我可以看到我的静态图像:imgur.com/sobMlFM 我想做一个动画,让我的三角形像时钟一样旋转,但更平滑。换句话说,我想做一个由三角形创建的正方形,它将像时钟的指针一样旋转;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多