-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22_assimp.cpp
274 lines (240 loc) · 6.42 KB
/
22_assimp.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
#include <assert.h>
#include <math.h>
#include "utils/utils.h"
#include "utils/camera.h"
#include "utils/pipeline.h"
#include "utils/light.h"
#include "utils/mesh.h"
#include <iostream>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#define WINDOW_WIDTH 1024
#define WINDOW_HEIGHT 768
#define PLANE_WIDTH 5.0f
#define PLANE_HEIGHT 5.0f
#define MAX_LIGHTS 2
const char* WINDOW_NAME = "Tutorial 22";
DirectionLight directionLight;
Camera* camera = NULL;
LightProgram* program = NULL;
Mesh* mesh = 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 = ""
"const int MAX_LIGHTS = 2;"
"struct BaseLight"
"{"
" vec3 Color;"
" float AmbientIntensity;"
" float DiffuseIntensity;"
"};"
"struct DirectionalLight"
"{"
" BaseLight Base;"
" vec3 Direction;"
"};"
"struct Attenuation"
"{"
" float Constant;"
" float Linear;"
" float Exp;"
"};"
"struct PointLight"
"{"
" BaseLight Base;"
" vec3 Position;"
" Attenuation Atten;"
"};"
"struct SpotLight"
"{"
" PointLight Base;"
" vec3 Direction;"
" float Cutoff;"
"};"
"uniform DirectionalLight directionLight;"
"uniform PointLight pointLights[MAX_LIGHTS];"
"uniform SpotLight spotLights[MAX_LIGHTS];"
"uniform int numPointLights;"
"uniform int numSpotLights;"
"uniform sampler2D sampler;"
"varying vec2 uvCoord;"
"varying vec3 normal0;"
"varying vec3 worldPos0;"
"vec4 CalcLightInternal(BaseLight Light, vec3 LightDirection, vec3 Normal)"
"{"
" vec4 AmbientColor = vec4(Light.Color * Light.AmbientIntensity, 1.0);"
" float DiffuseFactor = dot(Normal, -LightDirection);"
" vec4 DiffuseColor = vec4(0, 0, 0, 0);"
" if (DiffuseFactor > 0.0) {"
" DiffuseColor = vec4(Light.Color * Light.DiffuseIntensity * DiffuseFactor, 1.0);"
" }"
" return (AmbientColor + DiffuseColor);"
"}"
"vec4 CalcDirectionalLight(vec3 Normal)"
"{"
" return CalcLightInternal(directionLight.Base, directionLight.Direction, Normal);"
"}"
"vec4 CalcPointLight(PointLight l, vec3 Normal)"
"{"
" vec3 LightDirection = worldPos0 - l.Position;"
" float Distance = length(LightDirection);"
" LightDirection = normalize(LightDirection);"
" vec4 Color = CalcLightInternal(l.Base, LightDirection, Normal);"
" float Atn = l.Atten.Constant + l.Atten.Linear * Distance + l.Atten.Exp * Distance * Distance;"
" return Color / Atn;"
"}"
"vec4 CalcSpotLight(SpotLight l, vec3 Normal)"
"{"
" vec3 LightToPixel = normalize(worldPos0 - l.Base.Position);"
" float SpotFactor = dot(LightToPixel, l.Direction);"
" if (SpotFactor > l.Cutoff) {"
" vec4 Color = CalcPointLight(l.Base, Normal);"
" return Color * (1.0 - (1.0 - SpotFactor) * 1.0/(1.0 - l.Cutoff));"
" } else {"
" return vec4(0.0, 0.0, 0.0, 0.0);"
" }"
"}"
"void main()"
"{"
" vec3 Normal = normalize(normal0);"
" vec4 TotalLight = CalcDirectionalLight(Normal);"
" for (int i = 0; i < numPointLights; i++) {"
" TotalLight += CalcPointLight(pointLights[i], Normal);"
" }"
" for (int i = 0; i < numSpotLights; i++) {"
" TotalLight += CalcSpotLight(spotLights[i], Normal);"
" }"
" gl_FragColor = texture2D(sampler, uvCoord) * TotalLight;"
"}";
void render ()
{
camera->OnRender();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
static float scale = 0.0f;
scale += 0.1f;
Pipeline p;
p.Scale(0.1f, 0.1f, 0.1f);
p.Rotate(0.0f, scale, 0.0f);
p.Pos(0.0f, 0.0f, 10.0f);
p.SetPerspectiveProj(60.0f, WINDOW_WIDTH, WINDOW_HEIGHT, 1.0f, 100.0f);
p.SetCamera(*camera);
program->SetWVP(p.GetTrans());
program->SetWorld(p.GetWorldTrans());
program->SetDirectionLight(directionLight);
program->SetPointLights(0, NULL);
program->SetSpotLights(0, NULL);
mesh->Render();
}
void createShaderProgram ()
{
program = new LightProgram();
if (!program->Init()) {
exit(1);
}
program->AddShader(GL_VERTEX_SHADER, vertex_shader.c_str());
program->AddShader(GL_FRAGMENT_SHADER, fragment_shader.c_str());
program->Compile();
program->Link();
glUniform1i(program->GetUniformLocation("sampler"), 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);
}
}
int main (int argc, char *argv[])
{
init();
Vector3f pos(3.0f, 7.0f, -10.0f);
Vector3f target(0.0f, -0.2f, 1.0f);
Vector3f up(0.0f, 1.0f, 0.0f);
camera = new Camera(WINDOW_WIDTH, WINDOW_HEIGHT, pos, target, up);
mesh = new Mesh();
mesh->LoadMesh("content/phoenix_ugv.md2");
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
directionLight.Color = Vector3f(1.0f, 1.0f, 1.0f);
directionLight.AmbientIntensity = 1.0f;
directionLight.DiffuseIntensity = 0.01f;
directionLight.Direction = Vector3f(1.0f, -1.0f, 0.0f);
createShaderProgram();
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_q:
directionLight.AmbientIntensity += 0.1f;
break;
case SDLK_w:
directionLight.AmbientIntensity -= 0.1f;
break;
case SDLK_ESCAPE:
running = false;
break;
}
}
break;
}
}
render();
SDL_GL_SwapWindow(window);
}
SDL_Quit();
return 0;
}