From 0c5f39a96f43cb86f4c5389a4cd3bc1b9f710ae8 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 1 Sep 2019 15:03:58 +0300 Subject: [PATCH] E --- light.cpp | 6 ++-- main.cpp | 25 +++++++++++----- material.cpp | 9 ++++-- material.h | 13 +++++---- mesh.cpp | 22 +++++--------- mesh.h | 22 +++++++------- model.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ model.h | 34 ++++++++++++++++++++++ node.cpp | 20 ++++++++----- node.h | 14 +++++---- root.cpp | 4 +-- root.h | 3 ++ texture.h | 1 + 13 files changed, 196 insertions(+), 59 deletions(-) create mode 100644 model.cpp create mode 100644 model.h diff --git a/light.cpp b/light.cpp index 219cf66..55c8c33 100644 --- a/light.cpp +++ b/light.cpp @@ -1,3 +1,4 @@ +#include"model.h" #include"light.h" #include"node.h" #include"root.h" @@ -23,8 +24,9 @@ namespace vb01{ int numLights=0,thisId=-1; for(Node *n : descendants){ std::vector lights=n->getLights(); - if(n->getMesh()){ - Material *mat=n->getMesh()->getMaterial(); + vector meshes=n->getMeshes(); + for(Mesh *mesh : meshes){ + Material *mat=mesh->getMaterial(); if(mat&&mat->isLightingEnabled()) materials.push_back(mat); } diff --git a/main.cpp b/main.cpp index 4ecd129..c992549 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include"node.h" #include"quad.h" #include"material.h" +#include"model.h" #include"light.h" #include @@ -16,17 +17,25 @@ int main(){ Vector3 v1[]={Vector3(2,2,.5),Vector3(.5,.5,.5)}; Vector3 v2[]={Vector3(-.0,0,2),Vector3(.5,0,-2)}; for(int i=0;i<1;i++){ - Quad *q=new Quad(v1[i]); - Node *node=new Node(v2[i]); - node->setObject(q); + Model *node=new Model("/home/dominykas/c++/FSim/jet00.obj"); + //node->setObject(q); Material *mat=new Material(); - mat->setDiffuseMap("/home/dominykas/c++/FSim/woodChips.jpg"); - mat->setLightingEnabled(true); - q->setMaterial(mat); - root->getRootNode()->attachChild(node); + mat->addDiffuseMap("/home/dominykas/c++/FSim/woodChips.jpg"); + mat->setLightingEnabled(false); + node->setMaterial(mat); + //root->getRootNode()->attachChild(node); } + Quad *q=new Quad(v1[0]); + Node *n=new Node(v2[0]); + n->attachMesh(q); + Material *mat=new Material(); + mat->addDiffuseMap("/home/dominykas/c++/FSim/woodChips.jpg"); + mat->setLightingEnabled(false); + q->setMaterial(mat); + root->getRootNode()->attachChild(n); + Camera *cam=Root::getSingleton()->getCamera(); - cam->setPosition(Vector3(0,0,-4)); + cam->setPosition(Vector3(0,0,-5)); Light *l1=new Light(); l1->setPosition(Vector3(-.5,.1,0)); l1->setColor(Vector3(0,1,0)); diff --git a/material.cpp b/material.cpp index 8a56693..d8364e7 100644 --- a/material.cpp +++ b/material.cpp @@ -14,8 +14,11 @@ namespace vb01{ void Material::update(){ shader->use(); shader->setBool(lightingEnabled,"lightingEnabled"); - if(diffuseMap)diffuseMap->select(); - if(normalMap)normalMap->select(); - if(specularMap)specularMap->select(); + for(Texture *t : diffuseMapTextures) + t->select(); + for(Texture *t : normalMapTextures) + t->select(); + for(Texture *t : specularMapTextures) + t->select(); } } diff --git a/material.h b/material.h index b54040c..81e57ff 100644 --- a/material.h +++ b/material.h @@ -1,24 +1,27 @@ #ifndef MATERIAL_H #define MATERIAL_H -#include"texture.h" #include"shader.h" +#include"texture.h" +#include namespace vb01{ + class Texture; + class Material{ public: Material(); ~Material(); void update(); - void setDiffuseMap(std::string diffuseMap){this->diffuseMap=new Texture(diffuseMap);} - void setNormalMap(std::string normalMap){this->normalMap=new Texture(normalMap);} - void setSpecularMap(std::string specularMap){this->specularMap=new Texture(specularMap);} + void addDiffuseMap(std::string diffuseMap){diffuseMapTextures.push_back(new Texture(diffuseMap));} + void addNormalMap(std::string normalMap){normalMapTextures.push_back(new Texture(normalMap));} + void addSpecularMap(std::string specularMap){specularMapTextures.push_back(new Texture(specularMap));} inline void setLightingEnabled(bool lighting){this->lightingEnabled=lighting;} inline Shader* getShader(){return shader;} inline bool isLightingEnabled(){return lightingEnabled;} private: bool lightingEnabled=false; - Texture *diffuseMap=nullptr,*normalMap=nullptr,*specularMap=nullptr; + std::vector diffuseMapTextures,normalMapTextures,specularMapTextures; Shader *shader; }; } diff --git a/mesh.cpp b/mesh.cpp index 58b31e7..c2fe7e6 100644 --- a/mesh.cpp +++ b/mesh.cpp @@ -4,30 +4,24 @@ #include #include #include -#include -#include #include #include using namespace std; using namespace glm; -using namespace Assimp; namespace vb01{ - Mesh::Mesh(string path){ - Importer importer; - const aiScene *scene=importer.ReadFile(path,aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals); - if(!scene||scene->mFlags&AI_SCENE_FLAGS_INCOMPLETE||!scene->mRootNode){ - cout<<"Failed to load model\n"; - exit(-1); - } - construct(); - } - Mesh::Mesh(){ } + Mesh::Mesh(Vertex *vertices, unsigned int *indices,int numTris){ + this->vertices=vertices; + this->indices=indices; + this->numTris=numTris; + construct(); + } + Mesh::~Mesh(){} void Mesh::construct(){ @@ -59,7 +53,7 @@ namespace vb01{ Vector3 dir=cam->getDirection(),up=cam->getUp(),camPos=cam->getPosition(); mat4 model=translate(mat4(1.),vec3(pos.x,pos.y,pos.z)); - model=rotate(model,38.f,vec3(1,0,0)); + //model=rotate(model,38.f,vec3(1,0,0)); mat4 view=lookAt(vec3(camPos.x,camPos.y,camPos.z),vec3(camPos.x+dir.x,camPos.y+dir.y,camPos.z+dir.z),vec3(up.x,up.y,up.z)); mat4 proj=perspective(radians(fov),width/height,nearPlane,farPlane); //proj=ortho(0.f,800.f,0.f,-600.f,nearPlane,farPlane); diff --git a/mesh.h b/mesh.h index 6ac28dd..ff9ba81 100644 --- a/mesh.h +++ b/mesh.h @@ -5,36 +5,36 @@ #include #include #include"material.h" -#include namespace vb01{ class Node; class Mesh{ - protected: + public: struct Vertex{ Vector3 pos,norm; Vector2 texCoords; }; - void construct(); - Vertex *vertices; - unsigned int *indices,VAO,VBO,EBO; - int numTris; - Node *node=nullptr; - public: - Mesh(std::string); Mesh(); + Mesh(Vertex*,unsigned int*,int); ~Mesh(); virtual void update(); void setMaterial(Material *mat){this->material=mat;} inline void setNode(Node *node){this->node=node;} inline Node* getNode(){return node;} inline Material* getMaterial(){return material;} + inline std::vector& getMeshes(){return meshes;} private: Material *material=nullptr; - void processNode(aiNode*,const aiScene*); - void processMesh(aiMesh*, const aiScene*); + Node *node=nullptr; + std::vector meshes; + std::string name=""; + protected: + void construct(); + Vertex *vertices; + unsigned int *indices,VAO,VBO,EBO; + int numTris; }; } diff --git a/model.cpp b/model.cpp new file mode 100644 index 0000000..dbce2ee --- /dev/null +++ b/model.cpp @@ -0,0 +1,82 @@ +#include"model.h" +#include"mesh.h" +#include"material.h" +#include"vector.h" +#include +#include +#include +#include +#include + +using namespace std; +using namespace Assimp; + +namespace vb01{ + void Model::processNode(aiNode *node, const aiScene *scene, Node *currentNode){ + for(int i=0;imNumMeshes;i++){ + aiMesh *mesh=scene->mMeshes[node->mMeshes[i]]; + processMesh(mesh,scene,currentNode); + } + for(int i=0;imNumChildren;i++){ + Node *childNode=new Node(); + currentNode->attachChild(childNode); + processNode(node->mChildren[i],scene,childNode); + } + } + + void Model::processMesh(aiMesh *mesh, const aiScene *scene, Node *currentNode){ + const int numTris=mesh->mNumFaces; + Mesh::Vertex vertices[3*numTris]; + unsigned int indices[3*numTris]; + for(int i=0;i<3*numTris;i++){ + Mesh::Vertex v; + v.pos.x=mesh->mVertices[i].x; + v.pos.y=mesh->mVertices[i].y; + v.pos.z=mesh->mVertices[i].z; + + v.norm.x=mesh->mNormals[i].x; + v.norm.y=mesh->mNormals[i].y; + v.norm.z=mesh->mNormals[i].z; + + if(mesh->mTextureCoords[0]){ + v.texCoords.x=mesh->mTextureCoords[0][i].x; + v.texCoords.y=mesh->mTextureCoords[0][i].y; + } + else + v.texCoords=Vector2::VEC_ZERO; + vertices[i]=v; + } + for(int i=0;imFaces[i].mNumIndices;j++){ + indices[i+j]=mesh->mFaces[i].mIndices[j]; + } + } + //if(mesh->mMaterialIndex>=0){ + // aiMaterial *material=scene->mMaterial[mesh->mMaterialIndex]; + //} + currentNode->attachMesh(new Mesh(vertices,indices,numTris)); + } + + Model::Model(string path) : Node(){ + Importer importer; + const aiScene *scene=importer.ReadFile(path,aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals); + if(!scene||scene->mFlags&AI_SCENE_FLAGS_INCOMPLETE||!scene->mRootNode){ + cout<<"Failed to load model:"<mRootNode,scene,this); + } + + Model::~Model(){} + + void Model::update(){ + Node::update(); + } + + void Model::setMaterial(Material *mat){ + for(Node *node : children) + for(Mesh *mesh : node->getMeshes()) + mesh->setMaterial(mat); + this->material=mat; + } +} diff --git a/model.h b/model.h new file mode 100644 index 0000000..018c4b5 --- /dev/null +++ b/model.h @@ -0,0 +1,34 @@ +#ifndef MODEL_H +#define MODEL_H + +#include +#include +#include"node.h" + +class aiScene; +class aiNode; +class aiMesh; +class aiMaterial; + +namespace vb01{ + class Mesh; + class Material; + class Texture; + + class Model : public Node{ + public: + Model(std::string); + ~Model(); + void update(); + void setMaterial(Material*); + private: + void processNode(aiNode*, const aiScene*, Node*); + void processMesh(aiMesh*, const aiScene*, Node*); +// std::vector processTexture(aiMaterial*, Assimp::aiTextureType); + void processMaterial(); + + Material* material; + }; +} + +#endif diff --git a/node.cpp b/node.cpp index cafd174..7f312e0 100644 --- a/node.cpp +++ b/node.cpp @@ -2,6 +2,7 @@ #include"node.h" #include"mesh.h" #include"light.h" +#include"material.h" using namespace std; @@ -13,8 +14,8 @@ namespace vb01{ Node::~Node(){} void Node::update(){ - if(mesh) - mesh->update(); + for(Mesh *m : meshes) + m->update(); for(Light *l : lights) l->update(); for(Node *c : children) @@ -31,8 +32,8 @@ namespace vb01{ descendants.push_back(child); } - void Node::setObject(Mesh *mesh){ - this->mesh=mesh; + void Node::attachMesh(Mesh *mesh){ + meshes.push_back(mesh); mesh->setNode(this); } @@ -46,9 +47,12 @@ namespace vb01{ int numLights=0; for(Node *n : descendants) numLights+=n->getNumLights(); - for(Node *n : descendants) - if(n->getMesh()) - if(n->getMesh()->getMaterial()) - n->getMesh()->getMaterial()->getShader()->setNumLights(numLights>0?numLights:1); + for(Node *n : descendants){ + vector meshes=n->getMeshes(); + for(Mesh *m : meshes){ + Material *mat=m->getMaterial(); + if(mat) mat->getShader()->setNumLights(numLights>0?numLights:1); + } + } } } diff --git a/node.h b/node.h index cc4673f..6370d82 100644 --- a/node.h +++ b/node.h @@ -7,26 +7,28 @@ namespace vb01{ class Mesh; + class Model; class Light; class Node{ public: - Node(Vector3); + Node(Vector3=Vector3::VEC_ZERO); ~Node(); - void setObject(Mesh*); - void update(); + void attachMesh(Mesh*); + virtual void update(); void attachChild(Node*); void addLight(Light*); - inline Mesh* getMesh(){return mesh;} + inline std::vector& getMeshes(){return meshes;} inline Node* getParent(){return parent;} inline Vector3 getPosition(){return pos;} inline std::vector& getDescendants(){return descendants;} + inline std::vector& getChildren(){return children;} inline std::vector& getLights(){return lights;} inline Light* getLight(int i){return lights[i];} inline int getNumLights(){return lights.size();} - private: + protected: Vector3 pos; - Mesh *mesh=nullptr; + std::vector meshes; std::vector children,descendants; std::vector lights; Node *parent=nullptr; diff --git a/root.cpp b/root.cpp index ebed2af..49cddcc 100644 --- a/root.cpp +++ b/root.cpp @@ -40,8 +40,8 @@ namespace vb01{ } glEnable(GL_DEPTH_TEST); glEnable(GL_STENCIL_TEST); - //glEnable(GL_CULL_FACE); - //glCullFace(GL_FRONT_AND_BACK); + glEnable(GL_CULL_FACE); + glCullFace(GL_FRONT_AND_BACK); } void Root::update(){ diff --git a/root.h b/root.h index c41d119..1dcbfdb 100644 --- a/root.h +++ b/root.h @@ -9,6 +9,8 @@ class GLFWwindow; namespace vb01{ void foo(); + class Mesh; + class Root{ public: Root(); @@ -25,6 +27,7 @@ namespace vb01{ GLFWwindow *window; Node *rootNode; Camera *camera; + std::vector meshes; void framebuffer_size_callback(GLFWwindow*,int,int); }; } diff --git a/texture.h b/texture.h index ffe08bc..1a2686f 100644 --- a/texture.h +++ b/texture.h @@ -11,6 +11,7 @@ namespace vb01{ private: unsigned int texture; int width,height,numChannels; + float weight; unsigned char *data; }; }