-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleRenderer3D.cpp
More file actions
218 lines (168 loc) · 4.75 KB
/
ModuleRenderer3D.cpp
File metadata and controls
218 lines (168 loc) · 4.75 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
#include "Globals.h"
#include "Application.h"
#include "ModuleRenderer3D.h"
#include "SDL\include\SDL_opengl.h"
#include <gl/GL.h>
#include <gl/GLU.h>
#include "C_Camera.h"
#include "DevIL/include/IL/ilut.h"
#pragma comment (lib, "lib/DevIL/lib/x86/Release/DevIL.lib")
#pragma comment (lib, "lib/DevIL/lib/x86/Release/ILU.lib")
#pragma comment (lib, "lib/DevIL/lib/x86/Release/ILUT.lib")
#include "SDL.h"
#pragma comment( lib, "lib/SDL/libx86/SDL2.lib" )
#pragma comment( lib, "lib/SDL/libx86/SDL2main.lib" )
#pragma comment (lib, "glu32.lib") /* link OpenGL Utility lib */
#pragma comment (lib, "opengl32.lib") /* link Microsoft OpenGL lib */
ModuleRenderer3D::ModuleRenderer3D(Application* app, bool start_enabled) : Module(app, start_enabled)
{
}
// Destructor
ModuleRenderer3D::~ModuleRenderer3D()
{}
// Called before render is available
bool ModuleRenderer3D::Init()
{
LOG("Creating 3D Renderer context");
LOGC("Starting Render module");
bool ret = true;
//Create context
context = SDL_GL_CreateContext(App->window->window);
if (context == NULL)
{
LOG("OpenGL context could not be created! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
if (ret == true)
{
//Use Vsync
if (VSYNC && SDL_GL_SetSwapInterval(1) < 0)
LOG("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
//Init OpenGL
glewInit();
LOGC("GLEW Initialized");
// Initialize IL
ilInit();
// Initialize ILU
iluInit();
// Initialize ILUT with OpenGL support.
ilutRenderer(ILUT_OPENGL);
LOGC("DevIL Initialized");
//Initialize Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Check for error
GLenum error = glGetError();
if (error != GL_NO_ERROR)
{
LOG("Error initializing OpenGL! %s\n", gluErrorString(error));
ret = false;
}
//Init FrameBuffer
//Initialize Modelview Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Check for error
error = glGetError();
if (error != GL_NO_ERROR)
{
LOG("Error initializing OpenGL! %s\n", gluErrorString(error));
ret = false;
}
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearDepth(1.0f);
//Initialize clear color
glClearColor(0.f, 0.f, 0.f, 1.f);
//Check for error
error = glGetError();
if (error != GL_NO_ERROR)
{
LOG("Error initializing OpenGL! %s\n", gluErrorString(error));
ret = false;
}
GLfloat LightModelAmbient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
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(0.75f, 0.75f, 0.75f, 1.0f);
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);
lights[0].Active(true);
SetSDLIcon(App->window->window);
}
// Projection matrix for
OnResize(SCREEN_WIDTH, SCREEN_HEIGHT);
return ret;
}
// PreUpdate: clear buffer
update_status ModuleRenderer3D::PreUpdate(float dt)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
checkRenderFilters();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(App->camera->camera->ViewMatrix());
// light 0 on cam pos
lights[0].SetPos(App->camera->Position.x, App->camera->Position.y, App->camera->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)
{
App->gui->Draw();//RECHECK
SDL_GL_SwapWindow(App->window->window);
return UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleRenderer3D::CleanUp()
{
LOG("Destroying 3D Renderer");
SDL_GL_DeleteContext(context);
return true;
}
void ModuleRenderer3D::OnResize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
App->camera->camera->SetAspectRatio((float)width / (float)height);
glLoadMatrixf(App->camera->camera->ProjectionMatrix());
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void ModuleRenderer3D::checkRenderFilters() {
if(depth_active)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
if(cullface_active)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
if(lighting_active)
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
if(color_active)
glEnable(GL_COLOR_MATERIAL);
else
glDisable(GL_COLOR_MATERIAL);
if(texture_active)
glEnable(GL_TEXTURE_2D);
else
glDisable(GL_TEXTURE_2D);
if(wireframe_active)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// //wireframe
}
void ModuleRenderer3D::SetSDLIcon(SDL_Window* window)
{
}