forked from PWATT200/GP2BaseCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
202 lines (166 loc) · 5.12 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "Common.h"
#include "Graphics.h"
#include "Vertices.h"
Vertex verts[]={
//Front Face
{-0.5f, -0.5f, 0.5f},
{0.5f, -0.5f, 0.5f},
{0.5f, 0.5f, 0.5f},
{ -0.5f, -0.5f, 0.5f },
{-0.5f, 0.5f, 0.5f},
{ 0.5f, 0.5f, 0.5f },
//Back Face
{ -0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
{ -0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
//Left Face
{ -0.5f, -0.5f, -0.5f },
{ -0.5f, 0.5f, -0.5f },
{-0.5f, -0.5f, 0.5f },
{ -0.5f, 0.5f, -0.5f },
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, -0.5f, 0.5f },
//Right Face
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{0.5f, -0.5f, 0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, -0.5f, 0.5f },
//top Face
{0.5f, 0.5f, 0.5f},
{0.5f, 0.5f, -0.5f},
{-0.5f, 0.5f, -0.5f},
{-0.5f, 0.5f, -0.5f},
{0.5f, 0.5f, 0.5f},
{-0.5f, 0.5f, 0.5f},
//bottom Face
{0.5f, -0.5f, 0.5f},
{0.5f, -0.5f, -0.5f},
{-0.5f, -0.5f, -0.5f},
{-0.5f, -0.5f, -0.5f},
{0.5f, -0.5f, 0.5f},
{-0.5f, -0.5f, 0.5f},
};
float xRotation=0.0f;
float yRotation=0.0f;
float zRotation=0.0f;
GLuint vertexBuffer;
void initScene()
{
vertexBuffer=createAndFillBuffer(verts,30);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
void cleanUp()
{
glDeleteBuffers(1,&vertexBuffer);
}
void render()
{
//old imediate mode!
//Set the clear colour(background)
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
//clear the colour and depth buffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//Swith to ModelView
glMatrixMode( GL_MODELVIEW );
//Reset using the Indentity Matrix
glLoadIdentity( );
setCameraProperties(0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glRotatef(xRotation,1.0f,0.0f,0.0f);
glRotatef(yRotation,0.0f,1.0f,0.0f);
glRotatef(zRotation,0.0f,0.0f,1.0f);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 30);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
int main(int argc, char * arg[])
{
//Controls the game loop
bool run=true;
bool pause=false;
// init everyting - SDL, if it is nonzero we have a problem
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cout << "ERROR SDL_Init " <<SDL_GetError()<< std::endl;
return -1;
}
//Request opengl 4.1 context, Core Context
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_CORE);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
//Create a window
SDL_Window * window = SDL_CreateWindow(
"SDL", // window title
SDL_WINDOWPOS_CENTERED, // x position, centered
SDL_WINDOWPOS_CENTERED, // y position, centered
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags
);
// Create an OpenGL context associated with the window.
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
//Call our InitOpenGL Function
initOpenGL();
//Set our viewport
setViewport(640,480);
initScene();
//Value to hold the event generated by SDL
SDL_Event event;
//Game Loop
while(run)
{
//While we still have events in the queue
while (SDL_PollEvent(&event)) {
//Get event type
if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) {
//set our boolean which controls the loop to false
run = false;
}
if (event.type==SDL_WINDOWEVENT)
{
switch (event.window.event) {
case SDL_WINDOWEVENT_MINIMIZED:
pause=true;
break;
}
}
if (event.type==SDL_KEYDOWN){
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
yRotation-=1.0f;
break;
case SDLK_RIGHT:
yRotation+=1.0f;
break;
case SDLK_UP:
xRotation-=1.0f;
break;
case SDLK_DOWN:
xRotation+=1.0f;
break;
default:
break;
}
}
}
//init Scene
//render
render();
//Call swap so that our GL back buffer is displayed
SDL_GL_SwapWindow(window);
}
// clean up, reverse order!!!
cleanUp();
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}