-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
114 lines (96 loc) · 2.43 KB
/
main.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cstdlib>
void drawTriangle();
void drawCube();
int main()
{
if(SDL_Init(SDL_INIT_VIDEO))
{
std::cerr<<"error init SDL\n";
exit(EXIT_FAILURE);
}
SDL_Rect screenSize;
SDL_GetDisplayBounds(0,&screenSize);
SDL_Window *window = SDL_CreateWindow(
"OpenGL Demo",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
screenSize.w/2,
screenSize.h/2,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
SDL_GLContext glContext;
// set OpenGL attributes
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,0);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
glContext=SDL_GL_CreateContext(window);
// now make this the active context
SDL_GL_MakeCurrent(window,glContext);
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f,(float)screenSize.w/screenSize.h,
0.5,100
);
glMatrixMode(GL_MODELVIEW);
gluLookAt(2,2,2,0,0,0,0,1,0);
glEnable(GL_DEPTH_TEST);
bool quit=false;
SDL_Event event;
while(!quit)
{
while(SDL_PollEvent(&event))
{
switch( event.type)
{
case SDL_QUIT : quit =true; break;
case SDL_KEYDOWN :
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE : quit= true; break;
case SDLK_w : glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); break;
case SDLK_s : glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); break;
}
break;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//drawTriangle();
drawCube();
SDL_GL_SwapWindow(window);
} // end quit loop
}
void drawTriangle()
{
static int rot=0;
glPushMatrix();
glRotated(++rot,0,1,0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glEnd();
glPopMatrix();
}
void drawCube()
{
static int rot=0;
glPushMatrix();
glRotated(++rot,0,1,0);
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0);
glVertex3f(-1,-1,1);
glVertex3f(-1,1,1);
glVertex3f(1,1,1);
glVertex3f(1,-1,1);
glEnd();
glPopMatrix();
}