绘制正四面体,即从四个面依次绘制三角形,其他立体图形方法类似
1 #include <GL/glew.h>
2 #include <SDL2/SDL.h>
3 #include <SDL2/SDL_opengl.h>
4
5 bool quit;
6 SDL_Window* window;
7 SDL_GLContext glContext;
8 SDL_Event sdlEvent;
9 GLfloat rtri=0;
10 void Init()
11 {
12 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
13 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
14 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24);
15 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
16 SDL_Init(SDL_INIT_EVERYTHING);
17 window = SDL_CreateWindow("SDL+OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,400,300,SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
18 glContext = SDL_GL_CreateContext(window);
19 SDL_GL_SetSwapInterval(1);
20 glewInit();
21 }
22 void Draw()
23 {
24 rtri++;
25 if(rtri==360)
26 rtri=0;
27 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
28 glLoadIdentity();
29 glScaled(1.0f/10,1.0f/10,1.0f/10);
30
31 glRotatef(rtri,0.0f,1.0f,0.0f);
32 glBegin(GL_TRIANGLES);
33 glColor3f(1.0f,0.0f,0.0f);
34 glVertex3f( 0.0f, 4.0f, 0.0f);
35 glColor3f(0.0f,1.0f,0.0f);
36 glVertex3f(-4.0f,-4.0f, 4.0f);
37 glColor3f(0.0f,0.0f,1.0f);
38 glVertex3f( 4.0f,-4.0f, 4.0f);
39 glColor3f(1.0f,0.0f,0.0f);
40 glVertex3f( 0.0f, 4.0f, 0.0f);
41 glColor3f(0.0f,0.0f,1.0f);
42 glVertex3f( 4.0f,-4.0f, 4.0f);
43 glColor3f(0.0f,1.0f,0.0f);
44 glVertex3f( 4.0f,-4.0f, -4.0f);
45 glColor3f(1.0f,0.0f,0.0f);
46 glVertex3f( 0.0f, 4.0f, 0.0f);
47 glColor3f(0.0f,1.0f,0.0f);
48 glVertex3f( 4.0f,-4.0f, -4.0f);
49 glColor3f(0.0f,0.0f,1.0f);
50 glVertex3f(-4.0f,-4.0f, -4.0f);
51 glColor3f(1.0f,0.0f,0.0f);
52 glVertex3f( 0.0f, 4.0f, 0.0f);
53 glColor3f(0.0f,0.0f,1.0f);
54 glVertex3f(-4.0f,-4.0f,-4.0f);
55 glColor3f(0.0f,1.0f,0.0f);
56 glVertex3f(-4.0f,-4.0f, 4.0f);
57 glEnd();
58
59 SDL_GL_SwapWindow(window);
60 }
61 int main(int argc, char *argv[])
62 {
63 quit = false;
64 Init();
65 while (!quit)
66 {
67 while(SDL_PollEvent(&sdlEvent))
68 {
69 if(sdlEvent.type == SDL_QUIT)
70 {
71 quit = true;
72 }
73 }
74 Draw();
75 SDL_Delay(16);
76 }
77 SDL_DestroyWindow(window);
78 window = NULL;
79 SDL_Quit();
80 return 0;
81 }