2
我想收到一個四個三角形的小動畫。我編寫了幾個版本的代碼,但這些版本都不起作用。結果我有一個空白的黑屏。 我的代碼:OpenGL with GLUT - 黑色屏幕
#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;
}
我想創建一個動畫在我的rectange從traingles建造將爲每一些小時間2度旋轉。我想旋轉它像鐘錶的作品。我知道問題不及時(不小),但在glRotatef - 我不知道它應該採取什麼參數給我一個適當的效果。
在此先感謝! :)
您好,感謝響應;)我已經改變了我的代碼,現在它看起來像 - http://pastebin.com/gNcjWLad - 現在,它推出後 - 它凍結..:< – user3025978
該死的:P首先用glutSwapBuffers()替換glflush()(後者在沒有調用的情況下導致刷新)。替換main中的init()調用,並將其作爲glutReshapeFunc(init)中的一個參數。在draw()(函數的line1)中刪除第一個glLoadIdentity()。 Draw_triangle應該有一個開頭(GL_TRIANGLE)跟着一個glEnd()。 – KostasRim
嗨! :D這仍然不好 - http://pastebin.com/CfEqHKzW或mabye我做錯了什麼..?在繪製三角形之前使用glEnd()前面的 – user3025978