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
|
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
#include<MMSystem.h>
#include <conio.h>
#include<vector>
#include <fstream>
#include<time.h>
#include<math.h>
#include "ImageLoader.h"
#include "Texture.h"
#include<glut.h>
#include "ModelData.h"
#include "ModelParser.h"
#include "FuncandVar.h"
using namespace std;
class Model
{
private:
// Model Info
vector<Vertex3D> Vertices;
vector<Texture2D> Textures;
vector<Normal3D> Normals;
vector<Face> Faces;
public:
float X, Y,Z;
Model()
{
}
Model(float x, float y, float z)
{
this->X = x;
this->Y = y;
this->Z = z;
}
void Load(const char *OBJFile) {
ifstream ifs(OBJFile);
cout << OBJFile << endl;
while (!ifs.eof()) {
char data[100000];
for (int Dataindx = 0; Dataindx < 100000; Dataindx++)
data[Dataindx] = '\0';
ifs >> data; //Extract 1 word from the file
if (strcmp(data, "v") == 0) {
float x, y, z; //Vertex data
//Extract x, y, z components
ifs >> x;
ifs >> y;
ifs >> z;
Vertex3D vert; //Create Vertex3D object
vert.x = x;
vert.y = y;
vert.z = z;
//Add the vertex into the vector
this->Vertices.push_back(vert);
} else if(strcmp(data, "vt") == 0) {
float x, y; // Texture Data
// Extract x, y components
ifs >> x;
ifs >> y;
Texture2D text;
text.x = x;
text.y = y;
//cout << ": X: " << x << ", Y: " << y << endl;
// Add the texture into the vector
this->Textures.push_back(text);
} else if(strcmp(data, "vn")==0) {
float x, y, z; //Vertex normal
//Extract vertex normal
ifs >> x;
ifs >> y;
ifs >> z;
Normal3D norm; //Create Normal3D object
norm.x = x;
norm.y = y;
norm.z = z;
//Add this normal into the vector
this->Normals.push_back(norm);
} else if(strcmp(data, "f")==0) {
if (this->Faces.size() == 2)
int qwe = 0;
char faceData[100000]; //Face data
Face myFace;
while (true) {
ifs >> faceData; //Extract next data
if (ContainSlash(faceData,100000)) { //Check if data is valid
char Vertexindx[15];
char Textureindx[15];
char Normalindx[15];
ExtractVertexIdx(faceData, Vertexindx);
ExtractTextureIdx(faceData, Textureindx);
ExtractNormalIdx(faceData, Normalindx);
int vIndex = atoi(Vertexindx);
//cout << "VIndex: " << atoi(Vertexindx) << endl;
int tIndex = atoi(Textureindx);
//cout << "TIndex: " << atoi(Textureindx) << endl;
int nIndex = atoi(Normalindx);
//cout << "NIndex: " << atoi(Normalindx) << endl << endl;
myFace.VertexIndex.push_back(vIndex);
myFace.TextureIndex.push_back(tIndex);
myFace.NormalIndex.push_back(nIndex);
} else {
this->Faces.push_back(myFace); //INVALID face data
ifs.unget();
break;
}
}
}
}
}
void Render(GLuint _Texture)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPushMatrix();
glTranslatef(this->X, this->Y, this->Z);
glColor3f(1.0, 1.0, 1.0);
glLineWidth(1);
for (int i = 0; i < Faces.size(); i++)
{
glBegin(GL_POLYGON);
for (int j = 0; j < Faces[i].NormalIndex.size(); j++)
{
int Normalindx = Faces[i].NormalIndex[j]-1; //Get the normal
float x, y, z;
x = Normals[Normalindx].x;
y = Normals[Normalindx].y;
z = Normals[Normalindx].z;
glNormal3f(x, y, z);
int Textureindx = this->Faces[i].TextureIndex[j] - 1;
x = this->Textures[Textureindx].x;
y = this->Textures[Textureindx].y;
glTexCoord2f(x,y);
//Get the vertex
int Vertexindx = Faces[i].VertexIndex[j]-1;
x = Vertices[Vertexindx].x;
y = Vertices[Vertexindx].y;
z = Vertices[Vertexindx].z;
glVertex3f(x, y, z);
}
glEnd();
}
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
};
Model model1;
Model model2;
Model model3;
Model model4;
Model model5;
Model model6;
Model model7;
Model model8;
Model model9;
Model model10;
float Angle = 0;
void GameScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
Camera();
CameraMovement();
CameraRaiser();
KeyboardControls();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glColor3f(1, 1, 1);
//glNormal3f(0, 0, 1);
///*glBegin(GL_QUADS);*/
//glTexCoord2f(0.0f, 1.0f);
//glVertex3f(-100, 50, -15);
//glTexCoord2f(0.0f, 0.0f);
//glVertex3f(-100, -50, -15);
//glTexCoord2f(1.0f, 0.0f);
//glVertex3f(100, -50, -15);
//glTexCoord2f(1.0f, 1.0f);
//glVertex3f(100, 50, -15);
//glEnd();
/*glRotatef(Angle, 0, 1, 0);*/
model1.Render( _textureId[0]);
model2.Render(_textureId[1]);
model3.Render(_textureId[1]);
model4.Render(_textureId[1]);
glutSwapBuffers();
Angle+= 0.01;
glutPostRedisplay();
}
void ReShape(int w, int h)
{
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
void Keyboard (unsigned char keydown, int x, int y)
{
switch(keydown)
{
case'w': _Keys['w'] = PRESSED; break;
case 'a':_Keys['a'] = PRESSED; break;
case 's': _Keys['s'] = PRESSED; break;
case 'd': _Keys['d'] = PRESSED; break;
case 't': _SpeedModifier +=1; cout << _SpeedModifier << endl; break;
case 'y': _SpeedModifier -=1; cout << _SpeedModifier << endl; break;
case 'q': _Keys['q'] = PRESSED; break;
case 'e': _Keys['e'] = PRESSED; break;
}
}
void KeyboardRelease(unsigned char keyup, int x, int y)
{
switch(keyup)
{
case'w': _Keys['w'] = NOTPRESSED; break;
case 'a':_Keys['a'] = NOTPRESSED; break;
case 's': _Keys['s'] = NOTPRESSED; break;
case 'd': _Keys['d'] = NOTPRESSED; break;
case 'q': _Keys['q'] = NOTPRESSED; break;
case 'e': _Keys['e'] = NOTPRESSED; break;
}
}
int main(int ArgC, char **ArgV)
{
model1.Load("cube.obj");
model2.Load("FLOORFINAL.obj");
model3.Load("FIRSTpanel.obj");
// OpenGL Setup
glutInit(&ArgC, ArgV);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
// Setup Drawing Window
glutInitWindowPosition(150, 80);
glutInitWindowSize(1024, 576); // 1024, 576
glutCreateWindow("Image Loading");
LoadTextures("bg.bmp", 0);
LoadTextures("Grass.bmp", 1);
Enablers();
glutDisplayFunc(GameScene);
glutIdleFunc(GameScene);
glutReshapeFunc(ReShape);
glutPassiveMotionFunc(mouseMovement);
glutKeyboardFunc(Keyboard);
glutKeyboardUpFunc(KeyboardRelease);
KeyboardInit();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
|