Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Jul 31st, 2010 | Syntax: None | Size: 3.71 KB | Hits: 9 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. #include "glen.h"
  2.  
  3. Model::Model() { }
  4.  
  5. Model::Model(char *filename) {
  6.         FILE *model;
  7.         model = fopen(filename, "r");
  8.  
  9.         if (model) {
  10.                 std::vector<Vector3> vertices;
  11.                 std::vector<Vector3> normals;
  12.                 std::vector<Vector2> uvs;
  13.  
  14.                 this->vertex_buffer.setNormal(0.0f, 0.0f, 1.0f);
  15.                
  16.                 while (!feof(model)) {
  17.                         char type[7];
  18.                         fscanf(model, "%s ", type);
  19.  
  20.                         if (!strcmp("#", type)) {
  21.                                 while (fgetc(model) != '\n');
  22.                         } else if (!strcmp("v", type)) {
  23.                                 Vector3 vertex;
  24.  
  25.                                 fscanf(model, "%f %f %f", &vertex.x, &vertex.y, &vertex.z);
  26.                                 vertices.push_back(vertex);
  27.  
  28.                                 continue;
  29.                         } else if (!strcmp("vn", type)) {
  30.                                 Vector3 normal;
  31.  
  32.                                 fscanf(model, "%f %f %f", &normal.x, &normal.y, &normal.z);
  33.                                 normals.push_back(normal);
  34.  
  35.                                 continue;
  36.                         } else if (!strcmp("vt", type)) {
  37.                                 Vector2 uv;
  38.  
  39.                                 fscanf(model, "%f %f", &uv.x, &uv.y);
  40.                                 uvs.push_back(uv);
  41.  
  42.                         } else if (!strcmp("s", type)) {
  43.                                 char shade[4];
  44.  
  45.                                 fscanf(model, "%s", &shade);
  46.                         } else if (!strcmp("usemtl", type)) {
  47.                                 char material[50];
  48.  
  49.                                 fscanf(model, "%s", &material);
  50.                         } else if (!strcmp("mtllib", type)) {
  51.                                 char material[50];
  52.  
  53.                                 fscanf(model, "%s", &material);
  54.                         } else if (!strcmp("f", type)) {
  55.                                 int v[4], vt[4], vn[4];
  56.  
  57.                                 if (uvs.size() > 0) {
  58.                                         fscanf(model, "%d/%d/%d ", &v[0], &vt[0], &vn[0]);
  59.                                         fscanf(model, "%d/%d/%d ", &v[1], &vt[1], &vn[1]);
  60.                                         fscanf(model, "%d/%d/%d", &v[2], &vt[2], &vn[2]);
  61.                                 } else {
  62.                                         fscanf(model, "%d//%d ", &v[0], &vn[0]);
  63.                                         fscanf(model, "%d//%d ", &v[1], &vn[1]);
  64.                                         fscanf(model, "%d//%d", &v[2], &vn[2]);
  65.                                 }
  66.  
  67.                                 if (uvs.size() > 0)
  68.                                         this->vertex_buffer.setUV(uvs.at(vt[0] - 1).x, uvs.at(vt[0] - 1).y);
  69.                                 else
  70.                                         this->vertex_buffer.setUV(0.0f, 0.0f);
  71.  
  72.                                 this->vertex_buffer.setNormal(normals.at(vn[0] - 1).y, normals.at(vn[0] - 1).x, normals.at(vn[0] - 1).z);
  73.                                 this->vertex_buffer.addVertex(vertices.at(v[0] - 1).x, vertices.at(v[0] - 1).y, vertices.at(v[0] - 1).z);
  74.  
  75.                                 if (uvs.size() > 0)
  76.                                         this->vertex_buffer.setUV(uvs.at(vt[1] - 1).x, uvs.at(vt[1] - 1).y);
  77.  
  78.                                 this->vertex_buffer.setNormal(normals.at(vn[1] - 1).y, normals.at(vn[1] - 1).x, normals.at(vn[1] - 1).z);
  79.                                 this->vertex_buffer.addVertex(vertices.at(v[1] - 1).x, vertices.at(v[1] - 1).y, vertices.at(v[1] - 1).z);
  80.  
  81.                                 if (uvs.size() > 0)
  82.                                         this->vertex_buffer.setUV(uvs.at(vt[2] - 1).x, uvs.at(vt[2] - 1).y);
  83.  
  84.                                 this->vertex_buffer.setNormal(normals.at(vn[2] - 1).y, normals.at(vn[2] - 1).x, normals.at(vn[2] - 1).z);
  85.                                 this->vertex_buffer.addVertex(vertices.at(v[2] - 1).x, vertices.at(v[2] - 1).y, vertices.at(v[2] - 1).z);
  86.  
  87.                                 continue;
  88.                         }
  89.                 }
  90.  
  91.                 fclose(model);
  92.         } else {
  93. #ifdef _WIN32
  94.                 MessageBox(NULL, "Couldn't load model!", "Error!", MB_OK);
  95. #else
  96.                 printf("Error: Couldn't load model!\n");
  97. #endif
  98.         }
  99. }
  100.  
  101. VertexBuffer Model::getBuffer() {
  102.         return this->vertex_buffer;
  103. }
  104.  
  105. ModelInstance::ModelInstance(Model model, Texture2D texture, Matrix matrix) {
  106.         this->model = model;
  107.         this->texture = texture;
  108.         this->matrix = matrix;
  109. }
  110.  
  111. Matrix ModelInstance::getMatrix() {
  112.         return this->matrix;
  113. }
  114.  
  115. void ModelInstance::setMatrix(Matrix matrix) {
  116.         this->matrix = matrix;
  117. }
  118.  
  119. void ModelInstance::setTexture(Texture2D texture) {
  120.         this->texture = texture;
  121. }
  122.  
  123. void ModelInstance::render() {
  124.         glLoadIdentity();
  125.  
  126.         glTranslatef(this->matrix.translation.x, this->matrix.translation.y, this->matrix.translation.z);
  127.         glScalef(this->matrix.scale.x, this->matrix.scale.y, this->matrix.scale.z);
  128.         glRotatef(this->matrix.rotation.x, 1.0f, 0.0f, 0.0f);
  129.         glRotatef(this->matrix.rotation.y, 0.0f, 1.0f, 0.0f);
  130.         glRotatef(this->matrix.rotation.z, 0.0f, 0.0f, 1.0f);
  131.  
  132.         this->texture.use();
  133.         this->model.getBuffer().render();
  134. }