-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcRenderClass.cpp
More file actions
348 lines (262 loc) · 9.23 KB
/
cRenderClass.cpp
File metadata and controls
348 lines (262 loc) · 9.23 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// cRenderClass.cpp
//
//////////////////////////////////////////////////////////////////////////////////////////
// includes
//////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <iostream>
using std::cout;
//////////////////////////////////////////////////////////////////////////////////////////
// Declarations
//////////////////////////////////////////////////////////////////////////////////////////
cRenderClass graphics( SCREEN_WIDTH, SCREEN_HEIGHT );
unsigned int m_tex[256];
//////////////////////////////////////////////////////////////////////////////////////////
// cRenderClass() - constructor
//////////////////////////////////////////////////////////////////////////////////////////
cRenderClass::cRenderClass(const int w, const int h)
{
m_sw = w;
m_sh = h;
int total_count = w*h*4;
m_buffer = new float[total_count];
for( int i=0; i<total_count; i++ )
{
m_buffer[i] = 1;
}
m_point_size = 1;
m_OPENGL_2_0_SUPPORTED = true;
// generate a number of textures...
glGenTextures(1, m_tex);
}
//////////////////////////////////////////////////////////////////////////////////////////
// cRenderClass() - destructor
//////////////////////////////////////////////////////////////////////////////////////////
cRenderClass::~cRenderClass()
{
}
//////////////////////////////////////////////////////////////////////////////////////////
// loop() - enters game loop
//////////////////////////////////////////////////////////////////////////////////////////
void cRenderClass::loop()
{
glutMainLoop();
}
extern cShaderInfo ShaderInfo;
//////////////////////////////////////////////////////////////////////////////////////////
// initShaders() - init GLSL shaders
//////////////////////////////////////////////////////////////////////////////////////////
void cRenderClass::initShaders()
{
GLenum err = glewInit();
if( GLEW_OK != err )
{
printf("Error: Glew is not supported\n\n");
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
if (glewIsSupported("GL_VERSION_2_0"))
{
printf("OpenGL 2.0 is supported\n\n");
}
else
{
printf("Error: OpenGL 2.0 is not supported\n\n");
m_OPENGL_2_0_SUPPORTED = false;
}
ShaderInfo.create();
}
//////////////////////////////////////////////////////////////////////////////////////////
// initialize glut stuff
//////////////////////////////////////////////////////////////////////////////////////////
void cRenderClass::create(int argc, _TCHAR* argv[])
{
// initialise the glut library
glutInit(&argc, argv);
// set up the initial display mode
// need both double buffering and z-buffering
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
// set the initial window position
glutInitWindowPosition(100, 100);
// set the initial window size
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
// create and name the window
glutCreateWindow("Using GLSL Shaders for Post Processing (Multi-Pass)!");
// reshape callback for current window
glutReshapeFunc(winReshapeFunc);
// set display callback for current window
glutDisplayFunc(renderScene);
// set up the global idle callback
glutIdleFunc(update);
initShaders();
// This block copied from HPC boids cpu
glutMouseFunc(click);
glutKeyboardFunc(key);
glutMotionFunc(mouse);
glutPassiveMotionFunc(mouse);
//note: do texture loading here
}
//////////////////////////////////////////////////////////////////////////////////////////
// drawPixel() - draw a pixel into an off-screen buffer
// if m_point_size>1 then draw a block of pixels
//////////////////////////////////////////////////////////////////////////////////////////
void cRenderClass::drawPixel(int x, int y)
{
int start, end;
start = -(m_point_size/2);
end = (int)((m_point_size/2.0f) + 0.5);
for( int i=start; i<end; i++ )
{
for( int j=start; j<end; j++ )
{
// reject values outside m_buffer range
if( (x+j < 0) || (x+j >= m_sw) )
continue;
if( (y+i < 0) || (y+i >= m_sh) )
continue;
m_buffer[(((y+i)*m_sw+(x+j))*4) + 0] = m_colour.r;
m_buffer[(((y+i)*m_sw+(x+j))*4) + 1] = m_colour.g;
m_buffer[(((y+i)*m_sw+(x+j))*4) + 2] = m_colour.b;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
// setViewport2D() - sets up the viewport ready for a screen grab
//////////////////////////////////////////////////////////////////////////////////////////
void cRenderClass::setViewport2D()
{
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, m_sw, 0, m_sh);
glMatrixMode(GL_MODELVIEW);
// set up the viewport
glViewport
(
0, // lower left x position
0, // lower left y position
m_sw, // viewport width
m_sh // viewport height
);
}
//////////////////////////////////////////////////////////////////////////////////////////
// render() -
//////////////////////////////////////////////////////////////////////////////////////////
void cRenderClass::render( )
{
// BEFORE SHADER
// disable shader program
// clear the back buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// copy contents from my own buffer
glDrawPixels( m_sw, m_sh, GL_RGBA, GL_FLOAT, m_buffer );
extern NA_HeatLamp heatLamp;
extern NA_Pig pig;
glUseProgram(0);
heatLamp.draw();
pig.draw();
glUseProgram(0);
// finally swap the buffers of the current window
glutSwapBuffers();
// clear out the temp buffer
clear();
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
// winReshapeFunc() - gets called initially and whenever the window get resized
// resizing has been locked
//////////////////////////////////////////////////////////////////////////////////////////
void winReshapeFunc(GLint w, GLint h)
{
// specify current matrix
glMatrixMode(GL_PROJECTION);
// load an identity matrix
glLoadIdentity();
// create a projection matrix... i.e. 2D projection onto xy plane
glOrtho( -SCREEN_WIDTH, SCREEN_WIDTH, -SCREEN_HEIGHT, SCREEN_HEIGHT, -100, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// set up the viewport
glViewport
(
0, // lower left x position
0, // lower left y position
(GLsizei) SCREEN_WIDTH, // viewport width
(GLsizei) SCREEN_HEIGHT // viewport height
);
}
// All of below copied from HPC boids cpu
void debugMouse()
{
extern cRenderClass graphics;
cout << "Mouse:\n";
cout << " raw pos : " << graphics.mouseRaw.x << ", " << graphics.mouseRaw.y << "\n";
cout << " adjusted pos : " << graphics.mousePos.x << ", " << graphics.mousePos.y << "\n";
/*
//draw a dot under the mouse for debugging
graphics.setColour(0.0f, 0.0f, 1.0f);
graphics.setPointSize(3);
graphics.drawPixel(graphics.mousePos.x, graphics.mousePos.y);
graphics.setColour(1.0f, 0.0f, 0.0f);
graphics.setPointSize(1);
graphics.drawPixel(graphics.mousePos.x, graphics.mousePos.y);
*/
}
void mouse(int x, int y)
{
graphics.mouseRaw.x = x;
graphics.mouseRaw.y = y;
//https://www.gamedev.net/topic/507797-moving-objects-with-the-mouse-in-3d/
//https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluUnProject.xml
// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glGet.xml
GLdouble modelMatix[16];
GLdouble projMatix[16];
GLint viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatix);
glGetIntegerv(GL_VIEWPORT, viewport);
GLdouble fixedX;
GLdouble fixedY;
GLdouble fixedZ;
if (gluUnProject(graphics.mouseRaw.x, graphics.mouseRaw.y, 0, modelMatix, projMatix, viewport, &fixedX, &fixedY, &fixedZ) == GL_FALSE)
{
cout << "cRenderClass - mouse func - gluUnProject returned false\n";
return;
}
graphics.mousePos.x = (float)fixedX;
graphics.mousePos.y = (float)fixedY*-1;
}
void key(unsigned char c, int x, int y)
{
// allow rotation of pig, this was helpful for texture debugging
extern NA_Pig pig;
switch (c)
{
case 'w':
case 'W':
pig.rotation.y = pig.rotation.y - 10.0f;
break;
case 's':
case 'S':
pig.rotation.y = pig.rotation.y + 10.0f;
break;
case 'a':
case 'A':
pig.rotation.x = pig.rotation.x + 10.0f;
break;
case 'd':
case 'D':
pig.rotation.x = pig.rotation.x - 10.0f;
break;
}
}
void click(int button, int state, int x, int y)
{
}