-
Notifications
You must be signed in to change notification settings - Fork 2
/
gl_framework.cpp
167 lines (148 loc) · 4.69 KB
/
gl_framework.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
#include "gl_framework.hpp"
// Load Bitmaps And Convert To Textures
int LoadGLTextures()
{
/* load an image file directly as a new OpenGL texture */
texture[0] = SOIL_load_OGL_texture
(
"textures/texture0.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
texture[1] = SOIL_load_OGL_texture
(
"textures/texture1.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
texture[2] = SOIL_load_OGL_texture
(
"textures/texture_ground.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
texture[3] = SOIL_load_OGL_texture
(
"textures/texture_night.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
texture[4] = SOIL_load_OGL_texture
(
"textures/texture_sea.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
texture[5] = SOIL_load_OGL_texture
(
"textures/night.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
texture[6] = SOIL_load_OGL_texture
(
"textures/cmap.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
texture[7] = SOIL_load_OGL_texture
(
"textures/tyre.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
if(texture[0] == 0 || texture[1] == 0 || texture[2] == 0 || texture[3] == 0 || texture[4] == 0 || texture[5] == 0 || texture[6] == 0 || texture[7] == 0)
return false;
// Return Success
return true;
}
void loadLightings()
{
/* Sun */
GLfloat LightAmbient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat LightDiffuse[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat LightSpecular[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat light_position[] = { 50.0, 50.0, 50.0, 0.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
/*Headlights*/
GLfloat LightAmbient1[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightDiffuse1[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightSpecular1[] = {0.0f, 0.0f, 0.0f, 1.0f };
GLfloat position1[] = {0.05, 0.075, 0.08, 1.0};
GLfloat spotDir1[] = {0.0, 0.0, -1.0};
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse1);
glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular1);
glLightfv(GL_LIGHT1, GL_POSITION, position1);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spotDir1);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 0);
glEnable(GL_LIGHT1);
glDisable(GL_LIGHT1);
/*Moon*/
GLfloat LightAmbient2[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat LightDiffuse2[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat LightSpecular2[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat position2[] = {50.0, 50.0, 50.0, 0.0};
glLightfv(GL_LIGHT2, GL_AMBIENT, LightAmbient2);
glLightfv(GL_LIGHT2, GL_DIFFUSE, LightDiffuse2);
glLightfv(GL_LIGHT2, GL_SPECULAR, LightSpecular2);
glLightfv(GL_LIGHT2, GL_POSITION, position2);
glEnable(GL_LIGHT2);
glDisable(GL_LIGHT2);
}
namespace transpace
{
//! Initialize GL State
void initGL(void)
{
LoadGLTextures();
//Set framebuffer clear color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//Set depth buffer furthest depth
glClearDepth(1.0);
//Set depth test to less-than
glDepthFunc(GL_LESS);
//Enable depth testing
glEnable(GL_DEPTH_TEST);
//Enable Gourard shading
glShadeModel(GL_SMOOTH);
//Initialise lights
loadLightings();
}
//!GLFW Error Callback
void error_callback(int error, const char* description)
{
std::cerr<<description<<std::endl;
}
//!GLFW framebuffer resize callback
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
//!Resize the viewport to fit the window size - draw to entire window
glViewport(0, 0, width, height);
}
//!GLFW keyboard callback
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
//printf("%d, %d, %d, %d\n", key, scancode, action, mods);
//!Close the window if the ESC key was pressed
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
else if(!b->playback)
control(key, scancode, action, mods);
}
};