00001 #include <windows.h>
00002 #include "shared.h"
00003 #include "polygon.h"
00004 #include "mmgr.h"
00005
00006 void SetGLScene(POLYGON* polygon)
00007 {
00008
00009 polygon[0].Vertex[0].x = -1.0;
00010 polygon[0].Vertex[0].y = -1.0;
00011 polygon[0].Vertex[0].z = 1.0;
00012 polygon[0].Vertex[1].x = 1.0;
00013 polygon[0].Vertex[1].y = -1.0;
00014 polygon[0].Vertex[1].z = 1.0;
00015 polygon[0].Vertex[2].x = 1.0;
00016 polygon[0].Vertex[2].y = 1.0;
00017 polygon[0].Vertex[2].z = 1.0;
00018
00019 polygon[1].Vertex[0].x = -1.0;
00020 polygon[1].Vertex[0].y = -1.0;
00021 polygon[1].Vertex[0].z = 1.0;
00022 polygon[1].Vertex[1].x = 1.0;
00023 polygon[1].Vertex[1].y = 1.0;
00024 polygon[1].Vertex[1].z = 1.0;
00025 polygon[1].Vertex[2].x = -1.0;
00026 polygon[1].Vertex[2].y = 1.0;
00027 polygon[1].Vertex[2].z = 1.0;
00028
00029 polygon[2].Vertex[0].x = 1.0;
00030 polygon[2].Vertex[0].y = -1.0;
00031 polygon[2].Vertex[0].z = -1.0;
00032 polygon[2].Vertex[1].x = -1.0;
00033 polygon[2].Vertex[1].y = -1.0;
00034 polygon[2].Vertex[1].z = -1.0;
00035 polygon[2].Vertex[2].x = -1.0;
00036 polygon[2].Vertex[2].y = 1.0;
00037 polygon[2].Vertex[2].z = -1.0;
00038
00039 polygon[3].Vertex[0].x = 1.0;
00040 polygon[3].Vertex[0].y = -1.0;
00041 polygon[3].Vertex[0].z = -1.0;
00042 polygon[3].Vertex[1].x = -1.0;
00043 polygon[3].Vertex[1].y = 1.0;
00044 polygon[3].Vertex[1].z = -1.0;
00045 polygon[3].Vertex[2].x = 1.0;
00046 polygon[3].Vertex[2].y = 1.0;
00047 polygon[3].Vertex[2].z = -1.0;
00048
00049 polygon[4].Vertex[0].x = -1.0;
00050 polygon[4].Vertex[0].y = -1.0;
00051 polygon[4].Vertex[0].z = -1.0;
00052 polygon[4].Vertex[1].x = -1.0;
00053 polygon[4].Vertex[1].y = -1.0;
00054 polygon[4].Vertex[1].z = 1.0;
00055 polygon[4].Vertex[2].x = -1.0;
00056 polygon[4].Vertex[2].y = 1.0;
00057 polygon[4].Vertex[2].z = 1.0;
00058
00059 polygon[5].Vertex[0].x = -1.0;
00060 polygon[5].Vertex[0].y = -1.0;
00061 polygon[5].Vertex[0].z = -1.0;
00062 polygon[5].Vertex[1].x = -1.0;
00063 polygon[5].Vertex[1].y = 1.0;
00064 polygon[5].Vertex[1].z = 1.0;
00065 polygon[5].Vertex[2].x = -1.0;
00066 polygon[5].Vertex[2].y = 1.0;
00067 polygon[5].Vertex[2].z = -1.0;
00068
00069 polygon[6].Vertex[0].x = 1.0;
00070 polygon[6].Vertex[0].y = -1.0;
00071 polygon[6].Vertex[0].z = 1.0;
00072 polygon[6].Vertex[1].x = 1.0;
00073 polygon[6].Vertex[1].y = -1.0;
00074 polygon[6].Vertex[1].z = -1.0;
00075 polygon[6].Vertex[2].x = 1.0;
00076 polygon[6].Vertex[2].y = 1.0;
00077 polygon[6].Vertex[2].z = -1.0;
00078
00079 polygon[7].Vertex[0].x = 1.0;
00080 polygon[7].Vertex[0].y = -1.0;
00081 polygon[7].Vertex[0].z = 1.0;
00082 polygon[7].Vertex[1].x = 1.0;
00083 polygon[7].Vertex[1].y = 1.0;
00084 polygon[7].Vertex[1].z = -1.0;
00085 polygon[7].Vertex[2].x = 1.0;
00086 polygon[7].Vertex[2].y = 1.0;
00087 polygon[7].Vertex[2].z = 1.0;
00088
00089 polygon[8].Vertex[0].x = 1.0;
00090 polygon[8].Vertex[0].y = 1.0;
00091 polygon[8].Vertex[0].z = 1.0;
00092 polygon[8].Vertex[1].x = 1.0;
00093 polygon[8].Vertex[1].y = 1.0;
00094 polygon[8].Vertex[1].z = -1.0;
00095 polygon[8].Vertex[2].x = -1.0;
00096 polygon[8].Vertex[2].y = 1.0;
00097 polygon[8].Vertex[2].z = -1.0;
00098
00099 polygon[9].Vertex[0].x = 1.0;
00100 polygon[9].Vertex[0].y = 1.0;
00101 polygon[9].Vertex[0].z = 1.0;
00102 polygon[9].Vertex[1].x = -1.0;
00103 polygon[9].Vertex[1].y = 1.0;
00104 polygon[9].Vertex[1].z = -1.0;
00105 polygon[9].Vertex[2].x = -1.0;
00106 polygon[9].Vertex[2].y = 1.0;
00107 polygon[9].Vertex[2].z = 1.0;
00108
00109 polygon[10].Vertex[0].x = -1.0;
00110 polygon[10].Vertex[0].y = -1.0;
00111 polygon[10].Vertex[0].z = 1.0;
00112 polygon[10].Vertex[1].x = -1.0;
00113 polygon[10].Vertex[1].y = -1.0;
00114 polygon[10].Vertex[1].z = -1.0;
00115 polygon[10].Vertex[2].x = 1.0;
00116 polygon[10].Vertex[2].y = -1.0;
00117 polygon[10].Vertex[2].z = -1.0;
00118
00119 polygon[11].Vertex[0].x = -1.0;
00120 polygon[11].Vertex[0].y = -1.0;
00121 polygon[11].Vertex[0].z = 1.0;
00122 polygon[11].Vertex[1].x = 1.0;
00123 polygon[11].Vertex[1].y = -1.0;
00124 polygon[11].Vertex[1].z = -1.0;
00125 polygon[11].Vertex[2].x = 1.0;
00126 polygon[11].Vertex[2].y = -1.0;
00127 polygon[11].Vertex[2].z = 1.0;
00128
00129 polygon[0].SetNormal();
00130 polygon[1].SetNormal();
00131 polygon[2].SetNormal();
00132 polygon[3].SetNormal();
00133 polygon[4].SetNormal();
00134 polygon[5].SetNormal();
00135 polygon[6].SetNormal();
00136 polygon[7].SetNormal();
00137 polygon[8].SetNormal();
00138 polygon[9].SetNormal();
00139 polygon[10].SetNormal();
00140 polygon[11].SetNormal();
00141 }
00142
00143 void SetGLProperties()
00144 {
00145 glCullFace(GL_BACK);
00146 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
00147 glClearDepth(1.0);
00148 glDepthFunc(GL_LESS);
00149 glEnable(GL_DEPTH_TEST);
00150 glShadeModel(GL_SMOOTH);
00151 glEnable(GL_NORMALIZE);
00152 glEnable(GL_CULL_FACE);
00153 }
00154
00155 void SetGLProjection(int Width, int Height)
00156 {
00157 if (Height==0)
00158 Height=1;
00159 glViewport(0, 0, Width, Height);
00160 glMatrixMode(GL_PROJECTION);
00161 glLoadIdentity();
00162 gluPerspective(45.0, (float)Width / (float)Height, 1.0, 200.0);
00163 }
00164
00165 void SetGLView(int Width, int Height)
00166 {
00167 SetGLProjection(Width, Height);
00168 glMatrixMode(GL_MODELVIEW);
00169 glLoadIdentity();
00170 }
00171
00172 void SetGLMaterial()
00173 {
00174 float mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
00175 float mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00176 float mat_specular[] = { 0.9, 0.9, 0.9, 1.0 };
00177 float mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
00178 float mat_shininess[] = { 80.0 };
00179
00180 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00181 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00182 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00183 glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
00184 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
00185 }
00186
00187 void SetGLLighting()
00188 {
00189 float light_ambient[] = {0.2, 0.2, 0.2, 1.0};
00190 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00191 float light_position[] = {-3.0, 2.0, 1.0, 1.0};
00192 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00193 glEnable(GL_LIGHTING);
00194 glEnable(GL_LIGHT0);
00195 }
00196
00197 void SetGLTexture(TEXTURE* texture)
00198 {
00199 sprintf(texture[0].TexName, "%s", "tile1.tga");
00200 texture[0].LoadTGA();
00201 }
00202
00203 void DrawGrid()
00204 {
00205 glDisable(GL_TEXTURE_2D);
00206 glDisable(GL_LIGHTING);
00207 glPushMatrix();
00208 glTranslatef(0,-2.0,0);
00209 float Line = -10;
00210 int Grid;
00211 glBegin(GL_LINES);
00212 for(Grid = 0; Grid <= 20; Grid += 1)
00213 {
00214 glColor3f(0.0f,1.0f,0.0f);
00215 glVertex3f(Line + Grid, 0, -10);
00216 glVertex3f(Line + Grid, 0, 10);
00217 glVertex3f(-10, 0, Line + Grid);
00218 glVertex3f(10, 0, Line + Grid);
00219 }
00220 glEnd();
00221 glPopMatrix();
00222 glEnable(GL_TEXTURE_2D);
00223 glEnable(GL_LIGHTING);
00224 }
00225
00226 void DrawCube(POLYGON* polygon, TEXTURE* texture)
00227 {
00228 float mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
00229 float mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00230 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00231 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00232
00233 glBindTexture(GL_TEXTURE_2D, texture[0].TexID);
00234
00235 glPushMatrix();
00236 glTranslatef(0.0f,0.0f,-5.0f);
00237 glBegin(GL_TRIANGLES);
00238
00239 glNormal3fv(&polygon[0].Vertex[0].nx);
00240 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[0].x);
00241 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[0].Vertex[1].x);
00242 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[0].Vertex[2].x);
00243
00244 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[1].Vertex[0].x);
00245 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[1].x);
00246 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[1].Vertex[2].x);
00247
00248 glNormal3fv(&polygon[2].Vertex[0].nx);
00249 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[0].x);
00250 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[2].Vertex[1].x);
00251 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[2].Vertex[2].x);
00252
00253 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[3].Vertex[0].x);
00254 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[1].x);
00255 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[3].Vertex[2].x);
00256
00257 glNormal3fv(&polygon[4].Vertex[0].nx);
00258 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[0].x);
00259 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[4].Vertex[1].x);
00260 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[4].Vertex[2].x);
00261
00262 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[5].Vertex[0].x);
00263 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[1].x);
00264 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[5].Vertex[2].x);
00265
00266 glNormal3fv(&polygon[6].Vertex[0].nx);
00267 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[0].x);
00268 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[6].Vertex[1].x);
00269 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[6].Vertex[2].x);
00270
00271 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[7].Vertex[0].x);
00272 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[1].x);
00273 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[7].Vertex[2].x);
00274
00275 glNormal3fv(&polygon[8].Vertex[0].nx);
00276 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[0].x);
00277 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[8].Vertex[1].x);
00278 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[8].Vertex[2].x);
00279
00280 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[9].Vertex[0].x);
00281 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[1].x);
00282 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[9].Vertex[2].x);
00283
00284 glNormal3fv(&polygon[10].Vertex[0].nx);
00285 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[0].x);
00286 glTexCoord2f(1.0f, 0.0f); glVertex3fv(&polygon[10].Vertex[1].x);
00287 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[10].Vertex[2].x);
00288
00289 glTexCoord2f(0.0f, 0.0f); glVertex3fv(&polygon[11].Vertex[0].x);
00290 glTexCoord2f(1.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[1].x);
00291 glTexCoord2f(0.0f, 1.0f); glVertex3fv(&polygon[11].Vertex[2].x);
00292 glEnd();
00293 glPopMatrix();
00294 }
00295
00296 void DrawSphere()
00297 {
00298 float mat_ambient[] = { 0.8, 0.5, 0.1, 1.0 };
00299 float mat_diffuse[] = { 0.8, 0.5, 0.1, 1.0 };
00300 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00301 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00302
00303 glDisable(GL_TEXTURE_2D);
00304 glPushMatrix();
00305 glTranslatef(-3.0f,-1.0f,-8.0f);
00306 GLUquadricObj * sphere = gluNewQuadric();
00307 gluQuadricOrientation(sphere, GLU_OUTSIDE);
00308 gluSphere(sphere,1.0,50,50);
00309 glPopMatrix();
00310 glEnable(GL_TEXTURE_2D);
00311 }
00312
00313 void DrawCone()
00314 {
00315 float mat_ambient[] = { 0.1, 0.5, 1.0, 1.0 };
00316 float mat_diffuse[] = { 0.1, 0.5, 1.0, 1.0 };
00317 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00318 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00319
00320 glDisable(GL_TEXTURE_2D);
00321 glDisable(GL_CULL_FACE);
00322 glPushMatrix();
00323 glTranslatef(3.0f,-2.0f,-8.0f);
00324 glRotatef(-90,1,0,0);
00325 GLUquadricObj * cylinder = gluNewQuadric();
00326 gluQuadricOrientation(cylinder, GLU_OUTSIDE);
00327 gluCylinder(cylinder,1.0,0.0,2.0,20,20);
00328 glPopMatrix();
00329 glEnable(GL_CULL_FACE);
00330 glEnable(GL_TEXTURE_2D);
00331 }