-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_specular.cpp
379 lines (317 loc) · 10.3 KB
/
19_specular.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
#include <assert.h>
#include <math.h>
#include "utils/util_3d.h"
#include "utils/util_camera.h"
#include "utils/util_pipeline.h"
#include "utils/util_texture.h"
#include <iostream>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#define ARRAY_SIZE_IN_ELEMENTS(a) (sizeof(a)/sizeof(a[0]))
#define WINDOW_WIDTH 1024
#define WINDOW_HEIGHT 768
const char* WINDOW_NAME = "Tutorial 19";
GLuint ibo;
GLuint vbo;
// UNIFORMS FOR SHADERS
GLuint uWVP;
GLuint uWorld;
GLuint uSampler;
GLuint uLightColor;
GLuint uLightDirection;
GLuint uEyeWorldPos;
GLuint uAmbientIntensity;
GLuint uDiffuseIntensity;
GLuint uSpecularIntensity;
GLuint uSpecularPower;
Camera *camera = NULL;
Texture *texture = NULL;
static std::string vertex_shader = ""
"attribute vec3 position;"
"attribute vec2 texCoord;"
"attribute vec3 normal;"
"uniform mat4 WVP;"
"uniform mat4 world;"
"varying vec2 uvCoord;"
"varying vec3 normal0;"
"varying vec3 worldPos0;"
"void main()"
"{"
" gl_Position = WVP * vec4(position, 1.0);"
" uvCoord = texCoord;"
" normal0 = (world * vec4(normal, 0.0)).xyz;"
" worldPos0 = (world * vec4(position, 1.0)).xyz;"
"}";
static std::string fragment_shader = ""
"uniform sampler2D sampler;"
"uniform vec3 lightColor;"
"uniform vec3 lightDirection;"
"uniform vec3 eyeWorldPos;"
"uniform float ambientIntensity;"
"uniform float diffuseIntensity;"
"uniform float specularIntensity;"
"uniform float specularPower;"
"varying vec2 uvCoord;"
"varying vec3 normal0;"
"varying vec3 worldPos0;"
"void main()"
"{"
" vec4 ambient = vec4(lightColor * ambientIntensity, 1.0);"
" vec3 normal = normalize(normal0);"
" float diffuseFactor = dot(normal, -lightDirection);"
" vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);"
" vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);"
" if (diffuseFactor > 0.0) {"
" diffuse = vec4(lightColor * diffuseIntensity * diffuseFactor, 1.0);"
" vec3 vertexToEye = normalize(eyeWorldPos - worldPos0);"
" vec3 lightReflect = normalize(reflect(lightDirection, normal));"
" float specularFactor = dot(vertexToEye, lightReflect);"
" if (specularFactor > 0.0) {"
" specularFactor = pow(specularFactor, specularPower);"
" specular = vec4(lightColor * specularIntensity * specularFactor, 1.0);"
" }"
" }"
" gl_FragColor = texture2D(sampler, uvCoord) * (ambient + diffuse + specular);"
"}";
static float ambientIntensity = 0.5f;
static float diffuseIntensity = 0.5f;
static float specularIntensity = 0.5f;
static float specularPower = 0.5f;
void render ()
{
camera->OnRender();
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(60.0f, WINDOW_WIDTH, WINDOW_HEIGHT, 1.0f, 100.0f);
p.SetCamera(*camera);
Vector3f cameraPos = camera->GetPos();
glUniform3f(uLightColor, 1.0f, 1.0f, 1.0f);
glUniform3f(uLightDirection, 1.0f, 0.0f, 0.0f);
glUniform3f(uEyeWorldPos, cameraPos.x, cameraPos.y, cameraPos.z);
glUniform1f(uAmbientIntensity, ambientIntensity);
glUniform1f(uDiffuseIntensity, diffuseIntensity);
glUniform1f(uSpecularIntensity, specularIntensity);
glUniform1f(uSpecularPower, specularPower);
glUniformMatrix4fv(uWorld, 1, GL_TRUE, (const GLfloat*)p.GetWorldTrans());
glUniformMatrix4fv(uWVP, 1, GL_TRUE, (const GLfloat*)p.GetTrans());
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)20);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
texture->Bind(GL_TEXTURE0);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
}
void CalcNormals(const unsigned int* pIndices, unsigned int IndexCount, Vertex* pVertices, unsigned int VertexCount)
{
for (unsigned int i = 0 ; i < IndexCount ; i += 3) {
unsigned int Index0 = pIndices[i];
unsigned int Index1 = pIndices[i + 1];
unsigned int Index2 = pIndices[i + 2];
Vector3f v1 = pVertices[Index1].pos - pVertices[Index0].pos;
Vector3f v2 = pVertices[Index2].pos - pVertices[Index0].pos;
Vector3f Normal = v1.Cross(v2);
Normal.Normalize();
pVertices[Index0].normal += Normal;
pVertices[Index1].normal += Normal;
pVertices[Index2].normal += Normal;
}
for (unsigned int i = 0 ; i < VertexCount ; i++) {
pVertices[i].normal.Normalize();
}
}
void createContext ()
{
unsigned int indices[] = { 0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2 };
unsigned int indexCount = ARRAY_SIZE_IN_ELEMENTS(indices);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
Vertex vertices[4] = { Vertex(Vector3f(-1.0f, -1.0f, 0.5773f), Vector2f(0.0f, 0.0f)),
Vertex(Vector3f(0.0f, -1.0f, -1.15475f), Vector2f(0.5f, 0.0f)),
Vertex(Vector3f(1.0f, -1.0f, 0.5773f), Vector2f(1.0f, 0.0f)),
Vertex(Vector3f(0.0f, 1.0f, 0.0f), Vector2f(0.5f, 1.0f)) };
unsigned int vertexCount = ARRAY_SIZE_IN_ELEMENTS(vertices);
CalcNormals(indices, indexCount, vertices, vertexCount);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, 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;
glBindAttribLocation(program, 0, "position");
glBindAttribLocation(program, 1, "texCoord");
glBindAttribLocation(program, 2, "normal");
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &success);
GLchar ErrorLog[1024] = { 0 };
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);
uWVP = glGetUniformLocation(program, "WVP");
assert(uWVP != 0xFFFFFFFF);
uWorld = glGetUniformLocation(program, "world");
assert(uWorld != 0xFFFFFFFF);
uSampler = glGetUniformLocation(program, "sampler");
assert(uSampler != 0xFFFFFFFF);
uLightColor = glGetUniformLocation(program, "lightColor");
assert(uLightColor != 0xFFFFFFFF);
uLightDirection = glGetUniformLocation(program, "lightDirection");
assert(uLightDirection != 0xFFFFFFFF);
uEyeWorldPos = glGetUniformLocation(program, "eyeWorldPos");
assert(uEyeWorldPos != 0xFFFFFFFF);
uAmbientIntensity = glGetUniformLocation(program, "ambientIntensity");
assert(uAmbientIntensity != 0xFFFFFFFF);
uDiffuseIntensity = glGetUniformLocation(program, "diffuseIntensity");
assert(uDiffuseIntensity != 0xFFFFFFFF);
uSpecularIntensity = glGetUniformLocation(program, "specularIntensity");
assert(uSpecularIntensity != 0xFFFFFFFF);
uSpecularPower = glGetUniformLocation(program, "specularPower");
assert(uSpecularPower != 0xFFFFFFFF);
}
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);
}
}
int main (int argc, char *argv[])
{
init();
camera = new Camera(WINDOW_WIDTH, WINDOW_HEIGHT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
createContext();
createShaderProgram();
glUniform1i(uSampler, 0);
texture = new Texture(GL_TEXTURE_2D, "content/uvLayoutGrid.jpg");
if (!texture->Load()) {
return 1;
}
bool running = true;
while(running)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEMOTION:
camera->OnMouse(event.motion.x, event.motion.y);
break;
case SDL_KEYDOWN:
{
camera->OnKeyboard(event.key.keysym.sym);
switch(event.key.keysym.sym)
{
case SDLK_s:
ambientIntensity += 0.1f;
break;
case SDLK_a:
ambientIntensity -= 0.1f;
break;
case SDLK_x:
diffuseIntensity += 0.1f;
break;
case SDLK_z:
diffuseIntensity -= 0.1f;
break;
case SDLK_q:
specularIntensity += 0.1f;
break;
case SDLK_w:
specularIntensity -= 0.1f;
break;
case SDLK_e:
specularPower += 0.1f;
break;
case SDLK_r:
specularPower -= 0.1f;
break;
case SDLK_ESCAPE:
running = false;
break;
}
}
break;
}
}
render();
SDL_GL_SwapWindow(window);
}
SDL_Quit();
return 0;
}