Browse Source

Fixed slow rasterizer.

angel 7 years ago
parent
commit
d606425999
4 changed files with 10 additions and 16 deletions
  1. 1 1
      include/model.h
  2. BIN
      models/.teapot.obj.swp
  3. 2 2
      src/model.cpp
  4. 7 13
      src/rasterizer.cpp

+ 1 - 1
include/model.h

@@ -9,7 +9,7 @@ class Model{
     public:
     public:
         Model(std::string path); 
         Model(std::string path); 
         void describeMesh();
         void describeMesh();
-        Mesh getMesh();
+        Mesh *getMesh();
 
 
     private:
     private:
         void buildMesh(std::string path);
         void buildMesh(std::string path);

BIN
models/.teapot.obj.swp


+ 2 - 2
src/model.cpp

@@ -9,8 +9,8 @@ Model::Model(std::string path){
     buildMesh(path);
     buildMesh(path);
 }
 }
 
 
-Mesh Model::getMesh(){
-    return mMesh;
+Mesh * Model::getMesh(){
+    return &mMesh;
 }
 }
 
 
 void Model::describeMesh(){
 void Model::describeMesh(){

+ 7 - 13
src/rasterizer.cpp

@@ -34,10 +34,13 @@ void Rasterizer::drawModels(Model * models){
     Uint32 red = SDL_MapRGBA(mappingFormat, 0xFF,0x00,0x00,0xFF);
     Uint32 red = SDL_MapRGBA(mappingFormat, 0xFF,0x00,0x00,0xFF);
     Uint32 green = SDL_MapRGBA(mappingFormat, 0x00,0xFF,0x00,0xFF);
     Uint32 green = SDL_MapRGBA(mappingFormat, 0x00,0xFF,0x00,0xFF);
     Uint32 blue = SDL_MapRGBA(mappingFormat, 0x00,0x00,0xFF,0xFF);
     Uint32 blue = SDL_MapRGBA(mappingFormat, 0x00,0x00,0xFF,0xFF);
-    for (Vector3 f : (models->getMesh()).faces ){
-        Vector3 v1 = (models->getMesh()).vertices[f.x-1];
-        Vector3 v2 = (models->getMesh()).vertices[f.y-1];
-        Vector3 v3 = (models->getMesh()).vertices[f.z-1];
+    Mesh * modelMesh = models->getMesh();
+    std::vector<Vector3> * faces = &modelMesh->faces;
+    std::vector<Vector3> * vertices = &modelMesh->vertices;
+    for (Vector3 f : *faces ){
+        Vector3 v1 = (*vertices)[f.x-1];
+        Vector3 v2 = (*vertices)[f.y-1];
+        Vector3 v3 = (*vertices)[f.z-1];
         drawLine(v1, v2, red);
         drawLine(v1, v2, red);
         drawLine(v2, v3, green);
         drawLine(v2, v3, green);
         drawLine(v1, v3, blue);
         drawLine(v1, v3, blue);
@@ -99,12 +102,3 @@ void Rasterizer::setPixelColor(Uint32 color, int x, int y){
 int Rasterizer::convertCoordinates(int x, int y){
 int Rasterizer::convertCoordinates(int x, int y){
     return ((y * mCanvas->mWidth) + x);
     return ((y * mCanvas->mWidth) + x);
 }
 }
-
-// std::vector<Vector3> vertices = (models->getMesh()).vertices;
-//     for (Vector3 v : vertices ){
-//         int x = (v.x * 50 ) + mCanvas->mWidth/2;
-//         int y = (-v.y * 50 ) + mCanvas->mHeight/2;
-//         if( (x >= 0 ) && (y >= 0) && (x < mCanvas->mWidth) && (y < mCanvas->mHeight) ){
-//             setPixelColor(white,x,y);
-//         }
-//     }