-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_control_key.cpp
239 lines (187 loc) · 5.09 KB
/
14_control_key.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <assert.h>
#include <math.h>
#include "util_3d.h"
#include <iostream>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
GLuint ibo;
GLuint vbo;
GLuint uWorldLocation;
Camera *camera = NULL;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_NAME = "Tutorial 14";
static const GLfloat vertices[] = {
-1.0f, -1.0f, 0.0f,
0.0f, -1.0f, 1.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
static const unsigned int indices[] = {
0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2,
};
static std::string vertex_shader = ""
"attribute vec3 position;"
"uniform mat4 world;"
"varying vec4 color;"
"void main()"
"{"
" gl_Position = world * vec4(position, 1.0);"
" color = vec4(clamp(position, 0.0, 1.0), 1.0);"
"}";
static std::string fragment_shader = ""
"varying vec4 color;"
"void main()"
"{"
" gl_FragColor = color;"
"}";
void render ()
{
glClear(GL_COLOR_BUFFER_BIT);
static float scale = 0.0f;
scale += 0.1f;
Pipeline p;
p.Rotate(0.0f, scale, 0.0f);
p.Pos(0.0f, 0.0f, 3.0f);
p.SetPerspectiveProj(30.0f, WINDOW_WIDTH, WINDOW_HEIGHT, 1.0f, 1000.0f);
p.SetCamera(*camera);
glUniformMatrix4fv(uWorldLocation, 1, GL_TRUE, (const GLfloat*)p.GetTrans());
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
}
SDL_Window *window;
void init()
{
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Unable to init SDL, error: '%s'\n", SDL_GetError());
exit(1);
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
window = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
if (window == NULL)
{
fprintf(stderr, "Unable to create SDL Context");
exit(1);
}
glewExperimental = GL_TRUE;
GLenum res = glewInit();
if (res != GLEW_OK)
{
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
exit(1);
}
}
void createContext ()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glGenVertexArrays(1, &vbo); // may use another name variable instead vbo
glBindVertexArray(vbo);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
void addShader (GLuint program, const char* shaderText, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &shaderText, NULL);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(shader, 1024, NULL, InfoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", shaderType, InfoLog);
exit(1);
}
glAttachShader(program, shader);
}
void createShaderProgram ()
{
GLuint program = glCreateProgram();
addShader(program, vertex_shader.c_str(), GL_VERTEX_SHADER);
addShader(program, fragment_shader.c_str(), GL_FRAGMENT_SHADER);
GLint success = 0;
GLchar ErrorLog[1024] = { 0 };
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (success == 0)
{
glGetProgramInfoLog(program, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
exit(1);
}
glValidateProgram(program);
glGetProgramiv(program, GL_VALIDATE_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(program, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
exit(1);
}
glUseProgram(program);
uWorldLocation = glGetUniformLocation(program, "world");
assert(uWorldLocation != 0xFFFFFFFF);
}
int main (int argc, char *argv[])
{
init();
createContext();
createShaderProgram();
camera = new Camera();
bool running = true;
while(running)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = false;
break;
case SDLK_LEFT:
camera->OnKeyboard(2);
break;
case SDLK_RIGHT:
camera->OnKeyboard(3);
break;
case SDLK_UP:
camera->OnKeyboard(0);
break;
case SDLK_DOWN:
camera->OnKeyboard(1);
break;
}
break;
}
}
render();
SDL_GL_SwapWindow(window);
}
SDL_Quit();
return 0;
}