-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.cpp
175 lines (135 loc) · 3.81 KB
/
Renderer.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
/*
* Renderer.cpp
*
* Created on: Apr 4, 2012
* Author: iraklis
*/
#include "Renderer.h"
#include "MAHeaders.h"
void Renderer::init(GLView *glView)
{
mGLView = glView;
mGLView->addGLViewListener(this);
}
void Renderer::glViewReady(GLView *glView)
{
//Set this GLView to receive OpenGL commands
mGLView->bind();
// Create the texture we will use for rendering.
createTexture();
// Initialize OpenGL.
initGL();
}
void Renderer::setLandscape(landscape *ls)
{
mLandscape = ls;
}
void Renderer::setCamera(camera *c)
{
mCamera = c;
}
void Renderer::createTexture()
{
// Create an OpenGL 2D texture from the image resource.
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &mLunarTexture);
glBindTexture(GL_TEXTURE_2D, mLunarTexture);
maOpenGLTexImage2D(LUNAR_TEXTURE);
// Set texture parameters.
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
/**
* Standard OpenGL initialization.
*/
void Renderer::initGL()
{
//Configure the viewport
setViewport(mGLView->getWidth(), mGLView->getHeight());
// Enable texture mapping.
glEnable(GL_TEXTURE_2D);
// Enable smooth shading.
glShadeModel(GL_SMOOTH);
// Set the depth value used when clearing the depth buffer.
glClearDepthf(1.0f);
//glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
mEnvironmentInitialized = true;
}
/**
* Setup the projection matrix.
*/
void Renderer::setViewport(int width, int height)
{
// Set the viewport to fill the GLView
glViewport(0, 0, (GLint)width, (GLint)height);
// Select the projection matrix.
glMatrixMode(GL_PROJECTION);
// Reset the projection matrix.
glLoadIdentity();
GLfloat ratio = (GLfloat)width / (GLfloat)height;
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
}
/**
* Standard OpenGL utility function for setting up the
* perspective projection matrix.
*/
void Renderer::gluPerspective(
GLfloat fovy,
GLfloat aspect,
GLfloat zNear,
GLfloat zFar)
{
//const float M_PI = 3.14159;
GLfloat ymax = zNear * tan(fovy * M_PI / 360.0);
GLfloat ymin = -ymax;
GLfloat xmin = ymin * aspect;
GLfloat xmax = ymax * aspect;
glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
}
void Renderer::draw()
{
if(mEnvironmentInitialized)
{
// Set the background color to be used when clearing the screen.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Clear the screen and the depth buffer.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use the model matrix.
glMatrixMode(GL_MODELVIEW);
// Reset the model matrix.
glLoadIdentity();
renderLandscape();
// Wait (blocks) until all GL drawing commands to finish.
glFinish();
mGLView->redraw();
}
}
void Renderer::renderLandscape()
{
// Array used to convert from QUAD to TRIANGLE_STRIP.
// QUAD is not available on the OpenGL implementation
// we are using.
GLubyte indices[4] = {0, 1, 3, 2};
// Select the texture to use when rendering the box.
glBindTexture(GL_TEXTURE_2D, mLunarTexture);
glPushMatrix();
glRotatef(180.0f * asin(mCamera->facing.y)/M_PI, 1.0f, 0.0f, 0.0f);
glRotatef(180.0f * asin(mCamera->facing.x)/M_PI, 0.0f, 1.0f, 0.0f);
glTranslatef(-mCamera->position.x, -mCamera->position.y, -mCamera->position.z);
//glScalef(20.0f, 20.0f, 0.0f);
// Enable texture and vertex arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
for(int i = 0; i < mLandscape->numSegments; i++) {
// Set pointers to vertex coordinates and texture coordinates.
glVertexPointer(3, GL_FLOAT, 0, mLandscape->segments[i].vcoords);
glTexCoordPointer(2, GL_FLOAT, 0, mLandscape->segments[i].tcoords);
// This draws the segment.
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices);
}
glPopMatrix();
// Disable texture and vertex arrays
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}