-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleRenderer3D.cpp
152 lines (119 loc) · 3.5 KB
/
ModuleRenderer3D.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
#include "Globals.h"
#include "Application.h"
#include "ModuleRenderer3D.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 */
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");
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());
//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;
}
//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);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
lights[0].Active(true);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}
// 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);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(App->camera->GetViewMatrix());
// 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)
{
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();
ProjectionMatrix = perspective(60.0f, (float)width / (float)height, 0.125f, 512.0f);
glLoadMatrixf(&ProjectionMatrix);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}