-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
304 lines (258 loc) · 9.76 KB
/
main.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
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
/**************************************
* *
* Jeff Molofee's Basecode Example *
* nehe.gamedev.net *
* 2001 *
* *
**************************************/
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
//#include <gl\glaux.h> // Header File For The GLaux Library
#include <stdio.h>
#include "NeHeGL.h" // Header File For NeHeGL
#include "math.h" // NEW: Needed For Sqrtf
#include "ArcBall.h" // NEW: ArcBall Header
#include "block.hpp"
#include "sclog4c/sclog4c.h"
/*
#pragma comment( lib, "opengl32.lib" ) // Search For OpenGL32.lib While Linking
#pragma comment( lib, "glu32.lib" ) // Search For GLu32.lib While Linking
#pragma comment( lib, "glaux.lib" ) // Search For GLaux.lib While Linking
*/
#ifndef CDS_FULLSCREEN // CDS_FULLSCREEN Is Not Defined By Some
#define CDS_FULLSCREEN 4 // Compilers. By Defining It This Way,
#endif // We Can Avoid Errors
GL_Window* g_window;
Keys* g_keys;
// User Defined Variables
GLUquadricObj *quadratic; // Used For Our Quadric
const float PI2 = 2.0*3.1415926535f; // PI Squared
Matrix4fT Transform = { 1.0f, 0.0f, 0.0f, 0.0f, // NEW: Final Transform
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
Matrix3fT LastRot = { 1.0f, 0.0f, 0.0f, // NEW: Last Rotation
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
Matrix3fT ThisRot = { 1.0f, 0.0f, 0.0f, // NEW: This Rotation
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
ArcBallT ArcBall(640.0f, 480.0f); // NEW: ArcBall Instance
Point2fT MousePt; // NEW: Current Mouse Point
bool isClicked = false; // NEW: Clicking The Mouse?
bool isRClicked = false; // NEW: Clicking The Right Mouse Button?
bool isDragging = false; // NEW: Dragging The Mouse?
Block * blocks[DIM][DIM][DIM];
//enum TEXTURES
//{
// LAVA,
// NUM_TEXTURES
//}
GLuint texture[NUM_TEXTURES];
char * TexFileNames[]={
"lava.bmp",
"rock.bmp"
};
BOOL Initialize (GL_Window* window, Keys* keys) // Any GL Init Code & User Initialiazation Goes Here
{
g_window = window;
g_keys = keys;
// Start Of User Initialization
isClicked = false; // NEW: Clicking The Mouse?
isDragging = false; // NEW: Dragging The Mouse?
glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth (1.0f); // Depth Buffer Setup
glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
glEnable (GL_DEPTH_TEST); // Enable Depth Testing
glShadeModel (GL_FLAT); // Select Flat Shading (Nice Definition Of Objects)
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate
quadratic=gluNewQuadric(); // Create A Pointer To The Quadric Object
gluQuadricNormals(quadratic, GLU_SMOOTH); // Create Smooth Normals
gluQuadricTexture(quadratic, GL_TRUE); // Create Texture Coords
glEnable(GL_LIGHT0); // Enable Default Light
glEnable(GL_LIGHTING); // Enable Lighting
glEnable(GL_COLOR_MATERIAL); // Enable Color Material
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
freopen("log.txt","a",stderr);
sclog4c_level=SL4C_ALL;
for(int i=0;i<DIM;++i)
{
for(int j=0;j<DIM;++j)
{
for(int k=0;k<DIM;++k)
{
blocks[i][j][k]=new Block(ivec(i,j,k),(rand()%3==0)?Rock::air : Rock::granite);
if(blocks[i][j][k]->rock==Rock::air)
{
blocks[i][j][k]->temperature=1;
}
}
}
}
return TRUE; // Return TRUE (Initialization Successful)
}
void Deinitialize (void) // Any User DeInitialization Goes Here
{
gluDeleteQuadric(quadratic);
}
void Update (DWORD milliseconds) // Perform Motion Updates Here
{
if (g_keys->keyDown [VK_ESCAPE] == TRUE) // Is ESC Being Pressed?
TerminateApplication (g_window); // Terminate The Program
if (g_keys->keyDown [VK_F1] == TRUE) // Is F1 Being Pressed?
ToggleFullscreen (g_window); // Toggle Fullscreen Mode
for(int i=0;i<DIM;++i)
{
for(int j=0;j<DIM;++j)
{
for(int k=0;k<DIM;++k)
{
blocks[i][j][k]->update();
}
}
}
/*
if (isRClicked) // If Right Mouse Clicked, Reset All Rotations
{
Matrix3fSetIdentity(&LastRot); // Reset Rotation
Matrix3fSetIdentity(&ThisRot); // Reset Rotation
Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Reset Rotation
}
*/
/*
if (!isDragging) // Not Dragging
{
if (isClicked) // First Click
{
isDragging = true; // Prepare For Dragging
LastRot = ThisRot; // Set Last Static Rotation To Last Dynamic One
ArcBall.click(&MousePt); // Update Start Vector And Prepare For Dragging
}
}
else
{
if (isClicked) // Still Clicked, So Still Dragging
{
Quat4fT ThisQuat;
ArcBall.drag(&MousePt, &ThisQuat); // Update End Vector And Get Rotation As Quaternion
Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat); // Convert Quaternion Into Matrix3fT
Matrix3fMulMatrix3f(&ThisRot, &LastRot); // Accumulate Last Rotation Into This One
Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Set Our Final Transform's Rotation From This One
}
else // No Longer Dragging
isDragging = false;
}
*/
}
void Torus(float MinorRadius, float MajorRadius) // Draw A Torus With Normals
{
int i, j;
glBegin( GL_TRIANGLE_STRIP ); // Start A Triangle Strip
for (i=0; i<20; i++ ) // Stacks
{
for (j=-1; j<20; j++) // Slices
{
float wrapFrac = (j%20)/(float)20;
float phi = PI2*wrapFrac;
float sinphi = float(sin(phi));
float cosphi = float(cos(phi));
float r = MajorRadius + MinorRadius*cosphi;
glNormal3f(float(sin(PI2*(i%20+wrapFrac)/(float)20))*cosphi, sinphi, float(cos(PI2*(i%20+wrapFrac)/(float)20))*cosphi);
glVertex3f(float(sin(PI2*(i%20+wrapFrac)/(float)20))*r,MinorRadius*sinphi,float(cos(PI2*(i%20+wrapFrac)/(float)20))*r);
glNormal3f(float(sin(PI2*(i+1%20+wrapFrac)/(float)20))*cosphi, sinphi, float(cos(PI2*(i+1%20+wrapFrac)/(float)20))*cosphi);
glVertex3f(float(sin(PI2*(i+1%20+wrapFrac)/(float)20))*r,MinorRadius*sinphi,float(cos(PI2*(i+1%20+wrapFrac)/(float)20))*r);
}
}
glEnd(); // Done Torus
}
void Draw (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-10.0f,-10.0f,-40.0f); // Move Left 1.5 Units And Into The Screen 6.0
glColor3f(1,0,0);
for(int i=0;i<DIM;++i)
{
for(int j=0;j<DIM;++j)
{
for(int k=0;k<DIM;++k)
{
blocks[i][j][k]->draw(i,j,k);
}
}
}
//glPushMatrix(); // NEW: Prepare Dynamic Transform
// glMultMatrixf(Transform.M); // NEW: Apply Dynamic Transform
//glColor3f(0.75f,0.75f,1.0f);
//Torus(0.30f,1.00f);
//glPopMatrix(); // NEW: Unapply Dynamic Transform
//glLoadIdentity(); // Reset The Current Modelview Matrix
//glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 7.0
//glPushMatrix(); // NEW: Prepare Dynamic Transform
//glMultMatrixf(Transform.M); // NEW: Apply Dynamic Transform
//glColor3f(1.0f,0.75f,0.75f);
//gluSphere(quadratic,1.3f,20,20);
// glPopMatrix(); // NEW: Unapply Dynamic Transform
glFlush (); // Flush The GL Rendering Pipeline
}
GLubyte *LoadBmp(const char *Path, int *Width, int *Height);
int LoadGLTextures()
{
GLubyte *data=NULL;
int width=0,height=0;
//char FileName[64];
glGenTextures(NUM_TEXTURES,&texture[0]);
for(int i=0;i<NUM_TEXTURES;i++)
{
//sprintf(FileName,"Data\\%s\0",TexFileNames[i]);
//FileName[63]='\0';
if(data=LoadBmp(TexFileNames[i],&width,&height))
{
glBindTexture(GL_TEXTURE_2D,texture[i]);
glTexImage2D(GL_TEXTURE_2D,0,3,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
if(data)free(data);
data=NULL;
}
else
{
return FALSE;
}
}
return TRUE;
}
GLubyte *LoadBmp(const char *Path, int *Width, int *Height)
{
HANDLE hFile;
DWORD FileSize, dwRead;
BITMAPFILEHEADER *fh=NULL;
BITMAPINFOHEADER *ih;
BYTE *pRaster;
hFile=CreateFileA(Path,GENERIC_READ,0,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
return NULL;
}
FileSize=GetFileSize(hFile,NULL);
fh=(BITMAPFILEHEADER *)malloc(FileSize);
ReadFile(hFile,fh,FileSize,&dwRead,NULL);
CloseHandle(hFile);
int len = FileSize - fh->bfOffBits;
pRaster=(GLubyte *)malloc(len);
memcpy(pRaster, (BYTE *)fh+fh->bfOffBits, len); // RGB·Î ¼ø¼¸¦ ¹Ù²Û´Ù.
for (BYTE *p=pRaster;p < pRaster + len - 3;p+=3)
{
BYTE b = *p;
*p = *(p+2);
*(p+2) = b;
}
ih=(BITMAPINFOHEADER *)((PBYTE)fh+sizeof(BITMAPFILEHEADER));
*Width=ih->biWidth;
*Height=ih->biHeight;
free(fh);
return pRaster;
}