-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleRenderer3D.cpp
More file actions
283 lines (219 loc) · 7.34 KB
/
ModuleRenderer3D.cpp
File metadata and controls
283 lines (219 loc) · 7.34 KB
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
#include "Globals.h"
#include "Application.h"
#include "ModuleRenderer3D.h"
#include "ComponentCamera.h"
#include "glew-2.1.0\include\GL\glew.h"
#include "SDL\include\SDL_opengl.h"
#include <gl/GL.h>
#include <gl/GLU.h>
#pragma comment (lib, "glu32.lib") /* link OpenGL Utility lib */
#pragma comment (lib, "opengl32.lib") /* link Microsoft OpenGL lib */
#pragma comment( lib, "glew-2.1.0/lib/Release/Win32/glew32.lib")
#pragma comment( lib, "glew-2.1.0/lib/Release/Win32/glew32s.lib")
ModuleRenderer3D::ModuleRenderer3D(Application* app, bool start_enabled) : Module(app, start_enabled)
{
}
// Destructor
ModuleRenderer3D::~ModuleRenderer3D()
{}
// Called before render is available
bool ModuleRenderer3D::Init()
{
App->gui->app_log.AddLog("Creating 3D Renderer context\n");
bool ret = true;
//Create context
context = SDL_GL_CreateContext(App->window->window);
if(context == NULL)
{
App->gui->app_log.AddLog("OpenGL context could not be created! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
GLenum err = glewInit();
App->gui->app_log.AddLog("Using Glew %s\n", glewGetString(GLEW_VERSION));
App->gui->app_log.AddLog("Vendor: %s\n", glGetString(GL_VENDOR));
App->gui->app_log.AddLog("Renderer: %s\n", glGetString(GL_RENDERER));
App->gui->app_log.AddLog("OpenGL version supported %s\n", glGetString(GL_VERSION));
App->gui->app_log.AddLog("GLSL: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
// (config.ReadBool("renderer.vsync")
if(ret)
{
//Use Vsync
if(vsync && SDL_GL_SetSwapInterval(1) < 0)
App->gui->app_log.AddLog("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
//Initialize Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Check for error
GLenum error = glGetError();
if(error != GL_NO_ERROR)
{
App->gui->app_log.AddLog("Error initializing OpenGL! %s\n", gluErrorString(error));
ret = false;
}
//Initialize Modelview Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Check for error
error = glGetError();
if(error != GL_NO_ERROR)
{
App->gui->app_log.AddLog("Error initializing OpenGL! %s\n", gluErrorString(error));
ret = false;
}
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearDepth(1.0f);
//Clear color
glClearColor(clear_color.r, clear_color.g, clear_color.b, clear_color.a);
//Check for error
error = glGetError();
if(error != GL_NO_ERROR)
{
App->gui->app_log.AddLog("Error initializing OpenGL! %s\n", gluErrorString(error));
ret = false;
}
GLfloat LightModelAmbient[] = { light_ambient.r, light_ambient.g, light_ambient.b, light_ambient.a };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, LightModelAmbient);
lights[0].ref = GL_LIGHT0;
lights[0].ambient.Set(0.25f, 0.25f, 0.25f, 1.0f);
lights[0].diffuse.Set(main_light.r, main_light.g, main_light.b, main_light.a);
lights[0].SetPos(0.0f, 0.0f, 2.5f);
lights[0].Init();
GLfloat MaterialAmbient[] = {1.0f, 1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, MaterialAmbient);
GLfloat MaterialDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialDiffuse);
glEnable(GL_DEPTH_TEST);
lights[0].Active(true);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
if(face_culling)
glEnable(GL_CULL_FACE);
}
// Projection matrix for
OnResize(App->window->screen_width, App->window->screen_height);
return ret;
}
// PreUpdate: clear buffer
update_status ModuleRenderer3D::PreUpdate(float dt)
{
dt = (float)ms_timer.Read() / 1000.0f;
App->gui->Fps_renderer_data(dt);
ms_timer.Start();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
if (App->camera->cam->proj_update) {
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(App->camera->cam->ProjectionMatrix());
App->camera->cam->proj_update = false;
}
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(App->camera->cam->ViewMatrix());
// light 0 on cam pos
lights[0].SetPos(App->camera->cam->transform->position.x, App->camera->cam->transform->position.y, App->camera->cam->transform->position.z);
for(uint i = 0; i < MAX_LIGHTS; ++i)
lights[i].Render();
return UPDATE_CONTINUE;
}
// PostUpdate present buffer to screen
update_status ModuleRenderer3D::PostUpdate(float dt)
{
//first draw scene
App->scene_intro->DrawScene();
//then debug features
if (debug_draw)
DebugDraw();
//and editor last
App->gui->Draw();
SDL_GL_SwapWindow(App->window->window);
return UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleRenderer3D::CleanUp()
{
App->gui->app_log.AddLog("Destroying 3D Renderer\n");
SDL_GL_DeleteContext(context);
return true;
}
void ModuleRenderer3D::Save(JSON_file& config)
{
config.WriteBool("renderer.vsync", vsync);
config.WriteBool("renderer.face_cull", face_culling);
config.WriteColor("renderer.clear_color", clear_color);
config.WriteColor("renderer.light_ambient", light_ambient);
config.WriteColor("renderer.main_light", main_light);
}
void ModuleRenderer3D::Load(JSON_file& config)
{
vsync = config.ReadBool("renderer.vsync");
face_culling = config.ReadBool("renderer.face_cull");
clear_color = config.ReadColor("renderer.clear_color");
light_ambient = config.ReadColor("renderer.light_ambient");
main_light = config.ReadColor("renderer.main_light");
}
void ModuleRenderer3D::OnResize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
ProjectionMatrix = perspective(60.0f, (float)width / (float)height, 0.125f, 512.0f);
glLoadMatrixf(&ProjectionMatrix);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void ModuleRenderer3D::DebugDraw() {}
//Direct draw of a cube using TRIS
/*glLineWidth(2.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.f, 1.f, 0.f); //C
glVertex3f(0.f, 0.f, 0.f); //A
glVertex3f(1.f, 0.f, 0.f); //B
glVertex3f(0.f, 1.f, 0.f); //C
glVertex3f(1.f, 0.f, 0.f); //B
glVertex3f(1.f, 1.f, 0.f); //D
glVertex3f(1.f, 1.f, 0.f); //D
glVertex3f(1.f, 0.f, 0.f); //B
glVertex3f(1.f, 0.f, -1.f); //F
glVertex3f(1.f, 1.f, 0.f); //D
glVertex3f(1.f, 0.f, -1.f); //F
glVertex3f(1.f, 1.f, -1.f); //H
glVertex3f(0.f, 1.f, -1.f); //G
glVertex3f(0.f, 1.f, 0.f); //C
glVertex3f(1.f, 1.f, 0.f); //D
glVertex3f(0.f, 1.f, -1.f); //G
glVertex3f(1.f, 1.f, 0.f); //D
glVertex3f(1.f, 1.f, -1.f); //H
glVertex3f(1.f, 1.f, -1.f); //H
glVertex3f(1.f, 0.f, -1.f); //F
glVertex3f(0.f, 0.f, -1.f); //E
glVertex3f(0.f, 1.f, -1.f); //G
glVertex3f(1.f, 1.f, -1.f); //H
glVertex3f(0.f, 0.f, -1.f); //E
glVertex3f(0.f, 0.f, 0.f); //A
glVertex3f(0.f, 0.f, -1.f); //E
glVertex3f(1.f, 0.f, 0.f); //B
glVertex3f(0.f, 0.f, -1.f); //E
glVertex3f(1.f, 0.f, -1.f); //F
glVertex3f(1.f, 0.f, 0.f); //B
glVertex3f(0.f, 1.f, 0.f); //C
glVertex3f(0.f, 0.f, -1.f); //E
glVertex3f(0.f, 0.f, 0.f); //A
glVertex3f(0.f, 1.f, 0.f); //C
glVertex3f(0.f, 1.f, -1.f); //G
glVertex3f(0.f, 0.f, -1.f); //E
glEnd();
*/
/* Coordinates cube
glVertex3f(0.f, 0.f, 0.f); //A
glVertex3f(1.f, 0.f, 0.f); //B
glVertex3f(0.f, 1.f, 0.f); //C
glVertex3f(1.f, 1.f, 0.f); //D
glVertex3f(0.f, 0.f, -1.f); //E
glVertex3f(1.f, 0.f, -1.f); //F
glVertex3f(0.f, 1.f, -1.f); //G
glVertex3f(1.f, 1.f, -1.f); //H
glLineWidth(1.0f);*/
//glEnableClientState(GL_VERTEX_ARRAY);
//glVertexPointer(3, GL_FLOAT, 0, NULL);
//// … draw other buffers
//glDrawArrays(GL_TRIANGLES, 0, VERT_NUM * 3);
//glDisableClientState(GL_VERTEX_ARRAY);