opengl画颜色色块,一块红色区,一块绿色区。
opengl画颜色色块代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <windows.h> #include <stdio.h> #include <GL/glut.h> void reshape(int w, int h); void display(void); void reshape(int w, int h) { glViewport(0, 0, w, h); } void display(void) { float mat[16]; int i; glEnable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glGetFloatv(GL_PROJECTION_MATRIX, mat); for (i = 0; i < 16; i++) { printf("%10.7f", mat[i]); if ((i + 1) % 4) printf(" "); else printf("\n"); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glColor3f(1.0f, 0.0f, 0.0f); //在右上角画红色平面:应该在后面 glBegin(GL_POLYGON); glVertex3f(0.0f, 0.0f, -1.0f + 0.001f); glVertex3f(1.0f, 0.0f, -1.0f + 0.001f); glVertex3f(1.0f, 1.0f, -1.0f + 0.001f); glVertex3f(0.0f, 1.0f, -1.0f + 0.001f); glEnd(); glColor3f(0.0f, 1.0f, 0.0f); //在左下角画绿色的平面:应该在前面 glBegin(GL_POLYGON); glVertex3f(-1.0f, -1.0f, 1.0f - 0.001f); glVertex3f(0.0f + 0.5f, -1.0f, 1.0f - 0.001f); glVertex3f(0.0f + 0.5f, 0.0f + 0.5f, 1.0f - 0.001f); glVertex3f(-1.0f, 0.0f + 0.5f, 1.0f - 0.001f); glEnd(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow("No Transform"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } |
opengl画颜色色块测试结果: