ソースを参照

Added rudimentary .obj parsing

angel 7 年 前
コミット
2c34e1cf3e
8 ファイル変更116 行追加28 行削除
  1. 2 0
      include/engine.h
  2. 12 0
      include/mesh.h
  3. 10 0
      include/modelLoader.h
  4. 19 0
      include/vector3.h
  5. 33 0
      models/cube.obj
  6. 38 0
      src/engine.cpp
  7. 2 6
      src/main.cpp
  8. 0 22
      src/renderManager.cpp

+ 2 - 0
include/engine.h

@@ -19,6 +19,8 @@ class Engine{
 
         void mainLoop();
 
+        void loadModels();
+
     private:
         WindowManager FEWindowManager;
         RenderManager FERenderManager;

+ 12 - 0
include/mesh.h

@@ -0,0 +1,12 @@
+#ifndef MESH_H
+#define MESH_H
+
+#include "vector3.h"
+#include <vector>
+
+
+struct Mesh{
+    std::vector<Vector3> vertices;
+};
+
+#endif

+ 10 - 0
include/modelLoader.h

@@ -0,0 +1,10 @@
+#ifndef MODELLOADER_H
+#define MODELLOADER_H
+
+class Model{
+    
+
+
+};
+
+#endif

+ 19 - 0
include/vector3.h

@@ -0,0 +1,19 @@
+#ifndef VECTOR3_H
+#define VECTOR3_H
+
+#include <string>
+
+struct Vector3{
+    float x = 0;
+    float y = 0;
+    float z = 0;
+
+    Vector3(float x1, float y1, float z1) : x(x1), y(y1), z(z1)
+    {}
+
+    Vector3(std::string x1, std::string y1, std::string z1):
+        x(std::stof(x1)), y(std::stof(x1)), z(std::stof(x1))
+    {}
+};
+
+#endif

+ 33 - 0
models/cube.obj

@@ -0,0 +1,33 @@
+# cube.obj
+#
+ 
+g cube
+ 
+v  0.0  0.0  0.0
+v  0.0  0.0  1.0
+v  0.0  1.0  0.0
+v  0.0  1.0  1.0
+v  1.0  0.0  0.0
+v  1.0  0.0  1.0
+v  1.0  1.0  0.0
+v  1.0  1.0  1.0
+
+vn  0.0  0.0  1.0
+vn  0.0  0.0 -1.0
+vn  0.0  1.0  0.0
+vn  0.0 -1.0  0.0
+vn  1.0  0.0  0.0
+vn -1.0  0.0  0.0
+ 
+f  1//2  7//2  5//2
+f  1//2  3//2  7//2 
+f  1//6  4//6  3//6 
+f  1//6  2//6  4//6 
+f  3//3  8//3  7//3 
+f  3//3  4//3  8//3 
+f  5//5  7//5  8//5 
+f  5//5  8//5  6//5 
+f  1//4  5//4  6//4 
+f  1//4  6//4  2//4 
+f  2//1  6//1  8//1 
+f  2//1  8//1  4//1 

+ 38 - 0
src/engine.cpp

@@ -3,6 +3,11 @@
 #include "renderManager.h"
 #include "inputManager.h"
 #include <stdio.h>
+#include <fstream>
+#include <sstream>
+#include <string>
+#include "vector3.h"
+#include "mesh.h"
 
 Engine::Engine(){
 
@@ -61,3 +66,36 @@ void Engine::mainLoop(){
         printf("Loop Iteration N:%d\n",count);
     }
 }
+
+void Engine::loadModels(){
+    printf("loading models");
+    std::fstream file;
+    std::string line, path, v, x ,y ,z;
+
+    Mesh cube;
+
+    path = "../models/cube.obj";
+    file.open(path.c_str());
+
+    //Get vertices into mesh
+    while(!file.eof()){
+
+        std::getline(file,line);
+        std::istringstream iss(line);
+        if(line[0] == 'v'){
+            iss >> v >> x >> y >> z;
+            Vector3 vertex(x,y,z);
+            cube.vertices.push_back(vertex);
+        }
+
+    }
+    file.close();
+    int meshSize = cube.vertices.size();
+    printf("Meshsize is: %d \n", meshSize);
+    for(int i = 0; i < meshSize; ++i){
+        Vector3 vertex = cube.vertices[i];
+        printf("Vertex  %2.1d: %f, %f, %f \n",i,vertex.x, vertex.y, vertex.z);
+    }
+}
+
+

+ 2 - 6
src/main.cpp

@@ -1,15 +1,11 @@
 #include "engine.h"
 
-//Global variables
-// SDL_Texture *gTexture   = nullptr;
-// Uint32 *gBuffer         = nullptr;
-// bool quitFlag           = false;
-
 int main(int argc, char *argv[]){
 
     Engine mainEngine;
     if(mainEngine.startUp()){
-        mainEngine.mainLoop();
+        mainEngine.loadModels();
+        //mainEngine.mainLoop();
     }
     else{
         printf("Engine could not initialize successfully. Shutting down.\n");

+ 0 - 22
src/renderManager.cpp

@@ -95,25 +95,3 @@ void RenderManager::clearScreen(){
     SDL_RenderClear(mainRenderer);
 }
 
-// void RenderManager::createPixelPattern(){
-//     //Get window pixel format
-//     SDL_PixelFormat *mappingFormat = SDL_AllocFormat (PIXEL_FORMAT);
-
-//     //Set color data
-//     Uint32 red = SDL_MapRGBA(mappingFormat, 0xFF,0x00,0x00,0x60);
-//     Uint32 green = SDL_MapRGBA(mappingFormat, 0x00,0xFF,0x00,0x80);
-//     Uint32 blue = SDL_MapRGBA(mappingFormat, 0x00,0x00,0xFF,0xFF);
-//     //Color in certain pixels
-//     for(int i = 0; i < pixelCount; ++i){
-//         if( (i % 50) == 0){
-//             buffer1[i] = red;
-//         }
-//         if((i % 1000) == 0){
-//             buffer1[i] = green;
-//         }
-//         if((i % 2000) == 0){
-//             buffer1[i] = blue;
-//         }
-//     }
-// }
-