SDL2+OpenGL 绘制多边形
Posted CodeMIRACLE
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SDL2+OpenGL 绘制多边形相关的知识,希望对你有一定的参考价值。
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 void Init()
10 {
11 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
12 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
13 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24);
14 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
15 SDL_Init(SDL_INIT_EVERYTHING);
16 window = SDL_CreateWindow("SDL+OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,400,300,SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
17 glContext = SDL_GL_CreateContext(window);
18 SDL_GL_SetSwapInterval(1);
19 glewInit();
20 }
21 void Draw()
22 {
23 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
24 glLoadIdentity();
25 glScaled(1.0f/10,1.0f/10,1.0f/10); //坐标系默认是[-1,1],这里先把坐标系拉伸一下,也可以用glViewport重置窗口大小
26 //绘制三角形
27 glTranslatef(-3.0f,0.0f,6.0f);
28 glBegin(GL_TRIANGLES);
29 glVertex3f(0.0f,1.0f,0.0f);
30 glVertex3f(-1.0f,-1.0f,0.0f);
31 glVertex3f(1.0f,-1.0f,0.0f);
32 glEnd();
33 //绘制矩形
34 glTranslatef(3.0f,0.0f,0.0f);
35 glBegin(GL_QUADS);
36 glVertex3f(-1.0f,1.0f,0.0f);
37 glVertex3f(1.0f,1.0f,0.0f);
38 glVertex3f(1.0f,-1.0f,0.0f);
39 glVertex3f(-1.0f,-1.0f,0.0f);
40 glEnd();
41 //绘制难看的多边形
42 glTranslatef(3.0f,0.0f,0.0f);
43 glBegin(GL_POLYGON);
44 glVertex3f(0.0f, 3.0f,0.0f);
45 glVertex3f(1.0f, 5.0f,0.0f);
46 glVertex3f(2.0f, 2.0f,0.0f);
47 glVertex3f(3.0f, 4.0f,0.0f);
48 glVertex3f(4.0f, 1.0f,0.0f);
49 glVertex3f(5.0f, 7.0f,0.0f);
50 glEnd();
51 //渲染
52 SDL_GL_SwapWindow(window);
53 }
54 int main(int argc, char *argv[])
55 {
56 quit = false;
57 Init();
58 while (!quit)
59 {
60 while(SDL_PollEvent(&sdlEvent))
61 {
62 if(sdlEvent.type == SDL_QUIT)
63 {
64 quit = true;
65 }
66 }
67 Draw();
68 SDL_Delay(16);
69 }
70 SDL_DestroyWindow(window);
71 window = NULL;
72 SDL_Quit();
73 return 0;
74 }
运行结果
给图形添加颜色
1 void Draw()
2 {
3 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
4 glLoadIdentity();
5 glScaled(1.0f/10,1.0f/10,1.0f/10);
6 glTranslatef(-3.0f,0.0f,6.0f);
7 glBegin(GL_TRIANGLES);
8 glColor3f(1.0f,0.0f,0.0f);
9 glVertex3f(0.0f,1.0f,0.0f);
10 glColor3f(0.0f,1.0f,0.0f);
11 glVertex3f(-1.0f,-1.0f,0.0f);
12 glColor3f(0.0f,0.0f,1.0f);
13 glVertex3f(1.0f,-1.0f,0.0f);
14 glEnd();
15 glColor3f(1.0f,1.0f,0.0f);
16 glTranslatef(3.0f,0.0f,0.0f);
17 glBegin(GL_QUADS);
18 glVertex3f(-1.0f,1.0f,0.0f);
19 glVertex3f(1.0f,1.0f,0.0f);
20 glVertex3f(1.0f,-1.0f,0.0f);
21 glVertex3f(-1.0f,-1.0f,0.0f);
22 glEnd();
23 glColor3f(0.0f,1.0f,1.0f);
24 glTranslatef(3.0f,0.0f,0.0f);
25 glBegin(GL_POLYGON);
26 glVertex3f(0.0f, 3.0f,0.0f);
27 glVertex3f(1.0f, 5.0f,0.0f);
28 glVertex3f(2.0f, 2.0f,0.0f);
29 glVertex3f(3.0f, 4.0f,0.0f);
30 glVertex3f(4.0f, 1.0f,0.0f);
31 glVertex3f(5.0f, 7.0f,0.0f);
32 glEnd();
33 SDL_GL_SwapWindow(window);
34 }
glRotatef旋转
1 void Draw()
2 {
3 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
4 glLoadIdentity();
5 glScaled(1.0f/10,1.0f/10,1.0f/10);
6
7 glRotatef(120,1.0f,0.0f,0.0f);
8
9 glTranslatef(-3.0f,0.0f,6.0f);
10 glBegin(GL_TRIANGLES);
11 glColor3f(1.0f,0.0f,0.0f);
12 glVertex3f(0.0f,1.0f,0.0f);
13 glColor3f(0.0f,1.0f,0.0f);
14 glVertex3f(-1.0f,-1.0f,0.0f);
15 glColor3f(0.0f,0.0f,1.0f);
16 glVertex3f(1.0f,-1.0f,0.0f);
17 glEnd();
18 glColor3f(1.0f,1.0f,0.0f);
19 glTranslatef(3.0f,0.0f,0.0f);
20 glBegin(GL_QUADS);
21 glVertex3f(-1.0f,1.0f,0.0f);
22 glVertex3f(1.0f,1.0f,0.0f);
23 glVertex3f(1.0f,-1.0f,0.0f);
24 glVertex3f(-1.0f,-1.0f,0.0f);
25 glEnd();
26 glColor3f(0.0f,1.0f,1.0f);
27 glTranslatef(3.0f,0.0f,0.0f);
28 glBegin(GL_POLYGON);
29 glVertex3f(0.0f, 3.0f,0.0f);
30 glVertex3f(1.0f, 5.0f,0.0f);
31 glVertex3f(2.0f, 2.0f,0.0f);
32 glVertex3f(3.0f, 4.0f,0.0f);
33 glVertex3f(4.0f, 1.0f,0.0f);
34 glVertex3f(5.0f, 7.0f,0.0f);
35 glEnd();
36 SDL_GL_SwapWindow(window);
37 }
以上是关于SDL2+OpenGL 绘制多边形的主要内容,如果未能解决你的问题,请参考以下文章