-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_BasicLighting.cpp
470 lines (382 loc) · 12.4 KB
/
9_BasicLighting.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <iostream>
#include <string>
#include <fstream>
#include <GL/glew.h>
#include <SDL.h>
#include <SDL_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define MATH_PI 3.14159265359f
using namespace std;
SDL_Window* window;
SDL_GLContext gl;
GLuint program[2];
GLuint vertexShader[2];
GLuint fragmentShader[2];
GLuint VAO, lightVAO, VBO;
GLuint gProj[2];
GLuint gView[2];
GLuint gModel[2];
GLuint gAmbientStrength;
GLuint gSpecularStrength;
GLuint gTexture1;
GLuint gObjectColor[2];
GLuint gLightColor;
GLuint gLightPosition;
GLuint gViewPosition;
unsigned int tex;
glm::vec3 posBox[10];
glm::vec3 camPos;
GLfloat camYaw, camPitch, lastX, lastY;
unsigned int LoadImage(string path)
{
SDL_Surface* img = IMG_Load(path.c_str());
unsigned int id;
if(img == NULL)
{
cout << "Can't Load Image : " << path << endl;
}else{
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
GLenum formatTexture;
switch(img->format->BytesPerPixel)
{
case 4:
if(img->format->Rmask == 0x000000ff)
{
formatTexture = GL_RGBA;
}else{
formatTexture = GL_BGRA;
}
break;
case 3:
if(img->format->Rmask == 0x000000ff)
{
formatTexture = GL_RGB;
}else{
formatTexture = GL_BGR;
}
break;
default:
cout << "The Image not Truecolor." << endl;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, formatTexture, img->w, img->h, 0, formatTexture, GL_UNSIGNED_BYTE, img->pixels);
SDL_FreeSurface(img);
img = NULL;
glBindTexture(GL_TEXTURE_2D, 0);
}
return id;
}
string LoadFile(string path)
{
string line, output;
output = "";
ifstream in(path.c_str());
if(!in.is_open())
{
cout << "Can't Load File " << path << endl;
return output;
}else{
while(in.good())
{
getline(in, line);
output.append(line+"\n");
}
}
return output;
}
GLuint CompileShader(string text, unsigned int shaderType)
{
GLuint shader = glCreateShader(shaderType);
const char* source[1];
source[0] = text.c_str();
GLint length[1];
length[0] = text.length();
glShaderSource(shader, 1, source, length);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if(!success)
{
GLchar error[1024];
glGetShaderInfoLog(shader, 1024, NULL, error);
cout << error << endl;
}
return shader;
}
void LoadShader(string _vertexShader, string _fragmentShader, int id)
{
program[id] = glCreateProgram();
vertexShader[id] = CompileShader(LoadFile(_vertexShader), GL_VERTEX_SHADER);
fragmentShader[id] = CompileShader(LoadFile(_fragmentShader), GL_FRAGMENT_SHADER);
if(vertexShader[id] == NULL && fragmentShader[id] == NULL)
{
cout << "Compile Shader Fail." << endl;
}else{
glAttachShader(program[id], vertexShader[id]);
glAttachShader(program[id], fragmentShader[id]);
glLinkProgram(program[id]);
GLint success;
glGetProgramiv(program[id], GL_LINK_STATUS, &success);
if(!success)
{
GLchar error[1024];
glGetProgramInfoLog(program[id], 1024, NULL, error);
cout << error << endl;
}
}
}
void Init()
{
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Basic Lighting", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
gl = SDL_GL_CreateContext(window);
glewInit();
glEnable(GL_DEPTH_TEST);
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
LoadShader("shader/Shader5.vs", "shader/Shader5.fs", 0);
LoadShader("shader/Shader5_2.vs", "shader/Shader5_2.fs", 1);
}
void Start()
{
gProj[0] = glGetUniformLocation(program[0], "gProjection");
gView[0] = glGetUniformLocation(program[0], "gView");
gModel[0] = glGetUniformLocation(program[0], "gModel");
gProj[1] = glGetUniformLocation(program[1], "gProjection");
gView[1] = glGetUniformLocation(program[1], "gView");
gModel[1] = glGetUniformLocation(program[1], "gModel");
gAmbientStrength = glGetUniformLocation(program[0], "ambientStrength");
gSpecularStrength = glGetUniformLocation(program[0], "specularStrength");
gTexture1 = glGetUniformLocation(program[0], "outTexture1");
gObjectColor[0] = glGetUniformLocation(program[0], "objectColor");
gObjectColor[1] = glGetUniformLocation(program[1], "objectColor");
gLightColor = glGetUniformLocation(program[0], "lightColor");
gLightPosition = glGetUniformLocation(program[0], "lightPosition");
gViewPosition = glGetUniformLocation(program[0], "viewPosition");
tex = LoadImage("source/box.png");
posBox[0] = glm::vec3( 0.0f, 0.0f, 0.0f);
posBox[1] = glm::vec3( 2.0f, 5.0f, -15.0f);
posBox[2] = glm::vec3(-1.5f, -2.2f, -2.5f);
posBox[3] = glm::vec3(-3.8f, -2.0f, -12.3f);
posBox[4] = glm::vec3( 2.4f, -0.4f, -3.5f);
posBox[5] = glm::vec3(-1.7f, 3.0f, -7.5f);
posBox[6] = glm::vec3( 1.3f, -2.0f, -2.5f);
posBox[7] = glm::vec3( 1.5f, 2.0f, -2.5f);
posBox[8] = glm::vec3( 1.5f, 0.2f, -1.5f);
posBox[9] = glm::vec3(-1.3f, 1.0f, -1.5f);
GLfloat vec[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.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.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
glGenVertexArrays(1, &VAO);
glGenVertexArrays(1, &lightVAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vec), vec, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vec), vec, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
camPos = glm::vec3(0.0f, 0.0f, 5.0f);
camYaw = 180.0f*(MATH_PI/180.0f);
camPitch = 0.0f;
lastX = 400.0f;
lastY = 300.0f;
SDL_WarpMouseInWindow(window, lastX, lastY);
}
void MouseController()
{
int mX, mY;
SDL_GetMouseState(&mX, &mY);
camYaw += 0.005f * (lastX-mX);
camPitch += 0.005f * (lastY-mY);
SDL_WarpMouseInWindow(window, lastX, lastY);
if(camYaw/(MATH_PI/180.0f) > 360.0f)
{
camYaw = 0.0f;
}
else if(camYaw/(MATH_PI/180.0f) <= 0.0f)
{
camYaw = 360.0f*(MATH_PI/180.0f);
}
if(camPitch/(MATH_PI/180.0f) > 90.0f)
{
camPitch = 90.0f*(MATH_PI/180.0f);
}
else if(camPitch/(MATH_PI/180.0f) < -90.0f)
{
camPitch = -90.0f*(MATH_PI/180.0f);
}
}
void KeyboardController(glm::vec3 dir, glm::vec3 right, GLfloat speed)
{
const Uint8* state = SDL_GetKeyboardState(NULL);
if(state[SDL_SCANCODE_UP] || state[SDL_SCANCODE_W])
{
camPos += speed * dir;
}
else if(state[SDL_SCANCODE_DOWN] || state[SDL_SCANCODE_S])
{
camPos -= speed * dir;
}
if(state[SDL_SCANCODE_LEFT] || state[SDL_SCANCODE_A])
{
camPos -= speed * right;
}
else if(state[SDL_SCANCODE_RIGHT] || state[SDL_SCANCODE_D])
{
camPos += speed * right;
}
}
void Update()
{
MouseController();
GLfloat lightX = sinf(SDL_GetTicks() * 0.0005f) * 10.0f;
GLfloat lightZ = cosf(SDL_GetTicks() * 0.0005f) * 10.0f;
GLfloat colorR = sinf(SDL_GetTicks() * 0.0002f);
GLfloat colorG = sinf(SDL_GetTicks() * 0.00007f);
GLfloat colorB = sinf(SDL_GetTicks() * 0.00013f);
glm::mat4 projection;
glm::mat4 view;
projection = glm::perspective(45.0f, 800.0f/600.0f, 0.1f, 100.0f);
glm::vec3 camDir(cosf(camPitch) * sinf(camYaw), sinf(camPitch), cosf(camPitch) * cosf(camYaw));
glm::vec3 camRight(sinf(camYaw - MATH_PI/2.0f), 0.0f, cosf(camYaw - MATH_PI/2.0f));
glm::vec3 camUp = cross(camRight, camDir);
view = glm::lookAt(camPos, camPos + camDir, camUp);
KeyboardController(camDir, camRight, 0.0025f);
glUseProgram(program[0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(gTexture1, 0);
glUniformMatrix4fv(gProj[0], 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(gView[0], 1, GL_FALSE, glm::value_ptr(view));
glUniform1f(gAmbientStrength, 0.1f);
glUniform1f(gSpecularStrength, 1.0f);
glUniform3f(gObjectColor[0], 0.8f, 0.8f, 0.8f);
glUniform3f(gLightColor, colorR, colorG, colorB);
glUniform3f(gLightPosition, lightX, 1.0f, lightZ);
glUniform3f(gViewPosition, camPos.x, camPos.y, camPos.z);
glBindVertexArray(VAO);
for(int i = 0; i < 10; i++)
{
glm::mat4 model;
model = glm::translate(model, posBox[i]);
model = glm::rotate(model, 20.0f * i, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(gModel[0], 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
glUseProgram(program[1]);
glUniformMatrix4fv(gProj[1], 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(gView[1], 1, GL_FALSE, glm::value_ptr(view));
glUniform3f(gObjectColor[1], colorR, colorG, colorB);
glBindVertexArray(lightVAO);
glm::mat4 light;
light = glm::translate(light, glm::vec3(lightX, 1.0f, lightZ));
light = glm::scale(light, glm::vec3(0.5f, 0.5f, 0.5f));
glUniformMatrix4fv(gModel[1], 1, GL_FALSE, glm::value_ptr(light));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}
void Close()
{
for(int i = 0; i < 2; i++)
{
glDetachShader(program[i], vertexShader[i]);
glDeleteShader(vertexShader[i]);
glDetachShader(program[i], fragmentShader[i]);
glDeleteShader(fragmentShader[i]);
glDeleteProgram(program[i]);
}
SDL_GL_DeleteContext(gl);
SDL_DestroyWindow(window);
window = NULL;
}
int main(int argc, char* argv[])
{
Init();
Start();
bool quit = false;
SDL_Event e;
while(!quit)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
{
quit = true;
break;
}
else if(e.type == SDL_KEYDOWN)
{
if(e.key.keysym.sym == SDLK_ESCAPE)
{
quit = true;
break;
}
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Update();
SDL_GL_SwapWindow(window);
}
Close();
return 0;
}