diff --git a/CMakeLists.txt b/CMakeLists.txt index da73a8b..22afc1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(vb01) include_directories(/usr/include/GLFW) include_directories(/usr/include/glm) include_directories(/usr/include/glad) +include_directories(/usr/include/assimp) set(CMAKE_BUILD_TYPE Debug) set(math quaternion.cpp vector.cpp) @@ -13,3 +14,4 @@ link_directories(/usr/lib) add_executable(vb01 main.cpp ${math} ${render}) target_link_libraries(vb01 glfw) target_link_libraries(vb01 glad) +target_link_libraries(vb01 assimp) diff --git a/light.cpp b/light.cpp index e69de29..219cf66 100644 --- a/light.cpp +++ b/light.cpp @@ -0,0 +1,66 @@ +#include"light.h" +#include"node.h" +#include"root.h" +#include"material.h" +#include"mesh.h" +#include + +using namespace glm; +using namespace std; + +namespace vb01{ + Light::Light(){ + + } + + Light::~Light(){} + + void Light::update(){ + Node *rootNode=Root::getSingleton()->getRootNode(); + std::vector descendants=rootNode->getDescendants(); + descendants.push_back(rootNode); + std::vector materials; + int numLights=0,thisId=-1; + for(Node *n : descendants){ + std::vector lights=n->getLights(); + if(n->getMesh()){ + Material *mat=n->getMesh()->getMaterial(); + if(mat&&mat->isLightingEnabled()) + materials.push_back(mat); + } + for(Light *l : lights){ + if(l){ + numLights++; + if(l==this) + thisId=numLights-1; + } + } + } + for(Material *m : materials){ + Shader *shader=m->getShader(); + shader->use(); + shader->setInt((int)type,"light["+to_string(thisId)+"].type"); + shader->setVec3(vec3(color.x,color.y,color.z),"light["+to_string(thisId)+"].color"); + switch(type){ + case POINT: + shader->setVec3(vec3(pos.x,pos.y,pos.z),"light["+to_string(thisId)+"].pos"); + shader->setFloat(attenuationValues.x,"light["+to_string(thisId)+"].a"); + shader->setFloat(attenuationValues.y,"light["+to_string(thisId)+"].b"); + shader->setFloat(attenuationValues.z,"light["+to_string(thisId)+"].c"); + break; + case DIRECTIONAL: + shader->setVec3(vec3(direction.x,direction.y,direction.z),"light["+to_string(thisId)+"].direction"); + break; + case SPOT: + shader->setVec3(vec3(pos.x,pos.y,pos.z),"light["+to_string(thisId)+"].pos"); + shader->setVec3(vec3(direction.x,direction.y,direction.z),"light["+to_string(thisId)+"].direction"); + shader->setFloat(attenuationValues.x,"light["+to_string(thisId)+"].a"); + shader->setFloat(attenuationValues.y,"light["+to_string(thisId)+"].b"); + shader->setFloat(attenuationValues.z,"light["+to_string(thisId)+"].c"); + shader->setFloat(innerAngle,"light["+to_string(thisId)+"].innerAngle"); + shader->setFloat(outerAngle,"light["+to_string(thisId)+"].outerAngle"); + break; + } + } + } +} diff --git a/light.h b/light.h index 39084ec..fe1b377 100644 --- a/light.h +++ b/light.h @@ -1,7 +1,11 @@ #ifndef LIGHT_H #define LIGHT_H +#include"vector.h" + namespace vb01{ + class Node; + class Light{ public: enum Type{POINT,DIRECTIONAL,SPOT,AMBIENT}; @@ -9,8 +13,27 @@ namespace vb01{ Light(); ~Light(); void update(); + inline void setNode(Node *node){this->node=node;} + inline void setPosition(Vector3 pos){this->pos=pos;} + inline void setColor(Vector3 color){this->color=color;} + inline void setDirection(Vector3 dir){this->direction=dir;} + inline void setAttenuationValues(float a, float b, float c){ + attenuationValues.x=a; + attenuationValues.y=b; + attenuationValues.z=c; + } + inline void setType(Type t){this->type=t;} + inline void setInnerAngle(float innerAngle){this->innerAngle=innerAngle;} + inline void setOuterAngle(float outerAngle){this->outerAngle=outerAngle;} + inline Vector3 getDirection(){return direction;} + inline Vector3 getAttenuationValues(){return attenuationValues;} + inline float getInnerAngle(){return innerAngle;} + inline float getOuterAngle(){return outerAngle;} private: Type type; + Node *node=nullptr; + Vector3 pos,color,attenuationValues=Vector3(1.8,.7,1),direction; + float innerAngle=.707,outerAngle=.714; }; } diff --git a/main.cpp b/main.cpp index a5d1b8f..4ecd129 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,8 @@ #include"node.h" #include"quad.h" #include"material.h" +#include"light.h" +#include using namespace vb01; using namespace std; @@ -11,14 +13,29 @@ int main(){ Root *root=Root::getSingleton(); root->start(800,600); - Quad *q=new Quad(Vector3(.5,.5,.5)); - Node *node=new Node(Vector3(0,0,2)); - 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); + 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); + Material *mat=new Material(); + mat->setDiffuseMap("/home/dominykas/c++/FSim/woodChips.jpg"); + mat->setLightingEnabled(true); + q->setMaterial(mat); + root->getRootNode()->attachChild(node); + } + Camera *cam=Root::getSingleton()->getCamera(); + cam->setPosition(Vector3(0,0,-4)); + Light *l1=new Light(); + l1->setPosition(Vector3(-.5,.1,0)); + l1->setColor(Vector3(0,1,0)); + l1->setAttenuationValues(.07,.14,1); + l1->setDirection(Vector3(0,-1,0)); + l1->setInnerAngle(glm::radians(40.)); + l1->setOuterAngle(glm::radians(60.)); + l1->setType(Light::POINT); + root->getRootNode()->addLight(l1); while(true) root->update(); return 0; diff --git a/material.h b/material.h index 5cc26a6..b54040c 100644 --- a/material.h +++ b/material.h @@ -15,7 +15,7 @@ namespace vb01{ void setSpecularMap(std::string specularMap){this->specularMap=new Texture(specularMap);} inline void setLightingEnabled(bool lighting){this->lightingEnabled=lighting;} inline Shader* getShader(){return shader;} - inline bool isLighting(){return lightingEnabled;} + inline bool isLightingEnabled(){return lightingEnabled;} private: bool lightingEnabled=false; Texture *diffuseMap=nullptr,*normalMap=nullptr,*specularMap=nullptr; diff --git a/mesh.cpp b/mesh.cpp index bc8538b..58b31e7 100644 --- a/mesh.cpp +++ b/mesh.cpp @@ -4,12 +4,23 @@ #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(); } @@ -48,12 +59,13 @@ 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,45.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); material->update(); + material->getShader()->setVec3(vec3(camPos.x,camPos.y,camPos.z),"camPos"); material->getShader()->setMat4(model,"model"); material->getShader()->setMat4(view,"view"); material->getShader()->setMat4(proj,"proj"); diff --git a/mesh.h b/mesh.h index ddb7b3c..6ac28dd 100644 --- a/mesh.h +++ b/mesh.h @@ -5,6 +5,8 @@ #include #include #include"material.h" +#include + namespace vb01{ class Node; @@ -28,8 +30,11 @@ namespace vb01{ 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;} private: Material *material=nullptr; + void processNode(aiNode*,const aiScene*); + void processMesh(aiMesh*, const aiScene*); }; } diff --git a/node.cpp b/node.cpp index 83c4c1b..cafd174 100644 --- a/node.cpp +++ b/node.cpp @@ -1,5 +1,9 @@ +#include"root.h" #include"node.h" #include"mesh.h" +#include"light.h" + +using namespace std; namespace vb01{ Node::Node(Vector3 pos){ @@ -11,16 +15,40 @@ namespace vb01{ void Node::update(){ if(mesh) mesh->update(); + for(Light *l : lights) + l->update(); for(Node *c : children) c->update(); } void Node::attachChild(Node *child){ + Node *parent=getParent(); + while(parent){ + parent->getDescendants().push_back(child); + parent=parent->getParent(); + } children.push_back(child); + descendants.push_back(child); } void Node::setObject(Mesh *mesh){ this->mesh=mesh; mesh->setNode(this); } + + void Node::addLight(Light *light){ + lights.push_back(light); + light->setNode(this); + + Node *rootNode=Root::getSingleton()->getRootNode(); + vector descendants=rootNode->getDescendants(); + descendants.push_back(rootNode); + 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); + } } diff --git a/node.h b/node.h index a8a6bf8..cc4673f 100644 --- a/node.h +++ b/node.h @@ -7,6 +7,7 @@ namespace vb01{ class Mesh; + class Light; class Node{ public: @@ -15,12 +16,19 @@ namespace vb01{ void setObject(Mesh*); void update(); void attachChild(Node*); + void addLight(Light*); + inline Mesh* getMesh(){return mesh;} inline Node* getParent(){return parent;} inline Vector3 getPosition(){return pos;} + inline std::vector& getDescendants(){return descendants;} + inline std::vector& getLights(){return lights;} + inline Light* getLight(int i){return lights[i];} + inline int getNumLights(){return lights.size();} private: Vector3 pos; Mesh *mesh=nullptr; - std::vector children; + std::vector children,descendants; + std::vector lights; Node *parent=nullptr; }; } diff --git a/quad.cpp b/quad.cpp index a9441ba..275d7ce 100644 --- a/quad.cpp +++ b/quad.cpp @@ -4,27 +4,27 @@ namespace vb01{ Quad::Quad(Vector3 size) : Mesh(){ Vertex v1,v2,v3,v4,v5,v6; v1.pos=Vector3(size.x/2,size.y/2,0); - v1.norm=Vector3(0,0,1); + v1.norm=Vector3(0,0,-1); v1.texCoords=Vector2(1,1); v2.pos=Vector3(-size.x/2,size.y/2,0); - v2.norm=Vector3(0,0,1); + v2.norm=Vector3(0,0,-1); v2.texCoords=Vector2(0,1); v3.pos=Vector3(-size.x/2,-size.y/2,0); - v3.norm=Vector3(0,0,1); + v3.norm=Vector3(0,0,-1); v3.texCoords=Vector2(0,0); v4.pos=Vector3(-size.x/2,-size.y/2,0); - v4.norm=Vector3(0,0,1); + v4.norm=Vector3(0,0,-1); v4.texCoords=Vector2(0,0); v5.pos=Vector3(size.x/2,-size.y/2,0); - v5.norm=Vector3(0,0,1); + v5.norm=Vector3(0,0,-1); v5.texCoords=Vector2(1,0); v6.pos=Vector3(size.x/2,size.y/2,0); - v6.norm=Vector3(0,0,1); + v6.norm=Vector3(0,0,-1); v6.texCoords=Vector2(1,1); numTris=2; diff --git a/root.cpp b/root.cpp index 47344c7..ebed2af 100644 --- a/root.cpp +++ b/root.cpp @@ -40,6 +40,8 @@ namespace vb01{ } glEnable(GL_DEPTH_TEST); glEnable(GL_STENCIL_TEST); + //glEnable(GL_CULL_FACE); + //glCullFace(GL_FRONT_AND_BACK); } void Root::update(){ diff --git a/shader.cpp b/shader.cpp index f3f1006..9793f45 100644 --- a/shader.cpp +++ b/shader.cpp @@ -23,11 +23,23 @@ namespace vb01{ vertShaderFile.close(); fragShaderFile.close(); - string v=vertShaderStream.str(); - string f=fragShaderStream.str(); + vString=vertShaderStream.str(); + fString=fragShaderStream.str(); + setNumLights(1); + } - const char *vertShaderSource=v.c_str(); - const char *fragShaderSource=f.c_str(); + void Shader::setNumLights(int numLights){ + int firstCharId=-1,secondCharId=-1; + for(int i=0;i children;$/;" m class:vb01::Node typeref:typename:std::vector +children node.h /^ std::vector children,descendants;$/;" m class:vb01::Node typeref:typename:std::vector code stb_image.h /^ stbi__uint16 code[256];$/;" m struct:__anon84e4e8860708 typeref:typename:stbi__uint16[256] code_bits stb_image.h /^ int code_bits; \/\/ number of valid bits$/;" m struct:__anon84e4e8860808 typeref:typename:int code_buffer stb_image.h /^ stbi__uint32 code_buffer; \/\/ jpeg entropy-coded buffer$/;" m struct:__anon84e4e8860808 typeref:typename:stbi__uint32 @@ -167,6 +177,7 @@ codes stb_image.h /^ stbi__gif_lzw codes[8192];$/;" m struct:__anon84e4e886130 coeff stb_image.h /^ short *coeff; \/\/ progressive only$/;" m struct:__anon84e4e8860808::__anon84e4e8860908 typeref:typename:short * coeff_h stb_image.h /^ int coeff_w, coeff_h; \/\/ number of 8x8 coefficient blocks$/;" m struct:__anon84e4e8860808::__anon84e4e8860908 typeref:typename:int coeff_w stb_image.h /^ int coeff_w, coeff_h; \/\/ number of 8x8 coefficient blocks$/;" m struct:__anon84e4e8860808::__anon84e4e8860908 typeref:typename:int +color light.h /^ Vector3 pos,color,attenuationValues=Vector3(1.8,.7,1),direction;$/;" m class:vb01::Light typeref:typename:Vector3 color_table stb_image.h /^ stbi_uc *color_table;$/;" m struct:__anon84e4e8861308 typeref:typename:stbi_uc * conj quaternion.h /^ inline vb01::quaternion conj(){return (*this+vb01::quaternion(0,1,0,0)*(*this)*vb01::quaterni/;" f class:vb01::quaternion typeref:typename:vb01::quaternion construct mesh.cpp /^ void Mesh::construct(){$/;" f class:vb01::Mesh typeref:typename:void @@ -202,14 +213,17 @@ delay stb_image.h /^ int delay;$/;" m struct:__anon84e4e8861308 typeref:typena delta stb_image.h /^ int delta[17]; \/\/ old 'firstsymbol' - old 'firstcode'$/;" m struct:__anon84e4e8860708 typeref:typename:int[17] depth stb_image.h /^ int depth;$/;" m struct:__anon84e4e8860e08 typeref:typename:int dequant stb_image.h /^ stbi__uint16 dequant[4][64];$/;" m struct:__anon84e4e8860808 typeref:typename:stbi__uint16[4][64] +descendants node.h /^ std::vector children,descendants;$/;" m class:vb01::Node typeref:typename:std::vector diffuseMap material.h /^ Texture *diffuseMap=nullptr,*normalMap=nullptr,*specularMap=nullptr;$/;" m class:vb01::Material typeref:typename:Texture * direction camera.h /^ Vector3 position=Vector3(0,0,0),direction=Vector3::VEC_K,left=Vector3::VEC_I,up=Vector3::VEC_/;" m class:vb01::Camera typeref:typename:Vector3 +direction light.h /^ Vector3 pos,color,attenuationValues=Vector3(1.8,.7,1),direction;$/;" m class:vb01::Light typeref:typename:Vector3 dot vector.h /^ float dot(Vector2 v){return x*v.x+y*v.y;}$/;" f struct:vb01::Vector2 typeref:typename:float dot vector.h /^ float dot(Vector3 v){return x*v.x+y*v.y+z*v.z;}$/;" f struct:vb01::Vector3 typeref:typename:float eflags stb_image.h /^ int flags, bgindex, ratio, transparent, eflags;$/;" m struct:__anon84e4e8861308 typeref:typename:int eob_run stb_image.h /^ int eob_run;$/;" m struct:__anon84e4e8860808 typeref:typename:int eof stb_image.h /^ int (*eof) (void *user); \/\/ returns nonzero if we are at end o/;" m struct:__anon84e4e8860208 typeref:typename:int (*)(void * user) expanded stb_image.h /^ stbi_uc *idata, *expanded, *out;$/;" m struct:__anon84e4e8860e08 typeref:typename:stbi_uc ** +fString shader.h /^ std::string vString,fString;$/;" m class:vb01::Shader typeref:typename:std::string farPlane camera.h /^ float fov=45,nearPlane=.1,farPlane=100;$/;" m class:vb01::Camera typeref:typename:float fast stb_image.h /^ stbi__uint16 fast[1 << STBI__ZFAST_BITS];$/;" m struct:__anon84e4e8860b08 typeref:typename:stbi__uint16[] fast stb_image.h /^ stbi_uc fast[1 << FAST_BITS];$/;" m struct:__anon84e4e8860708 typeref:typename:stbi_uc[] @@ -223,11 +237,15 @@ foo root.cpp /^ void foo(GLFWwindow *window, int width, int height){$/;" f names fov camera.h /^ float fov=45,nearPlane=.1,farPlane=100;$/;" m class:vb01::Camera typeref:typename:float getAngleBetween vector.h /^ float getAngleBetween(Vector2 v){return std::acos(dot(v));}$/;" f struct:vb01::Vector2 typeref:typename:float getAngleBetween vector.h /^ float getAngleBetween(Vector3 v){return std::acos(dot(v));}$/;" f struct:vb01::Vector3 typeref:typename:float +getAttenuationValues light.h /^ inline Vector3 getAttenuationValues(){return attenuationValues;}$/;" f class:vb01::Light typeref:typename:Vector3 getCamera root.h /^ inline Camera* getCamera(){return camera;}$/;" f class:vb01::Root typeref:typename:Camera * +getDescendants node.h /^ inline std::vector& getDescendants(){return descendants;}$/;" f class:vb01::Node typeref:typename:std::vector & getDirection camera.h /^ inline Vector3 getDirection(){return direction;}$/;" f class:vb01::Camera typeref:typename:Vector3 +getDirection light.h /^ inline Vector3 getDirection(){return direction;}$/;" f class:vb01::Light typeref:typename:Vector3 getFarPlane camera.h /^ inline float getFarPlane(){return farPlane;}$/;" f class:vb01::Camera typeref:typename:float getFov camera.h /^ inline float getFov(){return fov;}$/;" f class:vb01::Camera typeref:typename:float getHeight root.h /^ inline int getHeight(){return height;}$/;" f class:vb01::Root typeref:typename:int +getInnerAngle light.h /^ inline float getInnerAngle(){return innerAngle;}$/;" f class:vb01::Light typeref:typename:float getLeft camera.h /^ inline Vector3 getLeft(){return left;}$/;" f class:vb01::Camera typeref:typename:Vector3 getLength quaternion.h /^ inline float getLength(){return std::sqrt(getLengthSq());}$/;" f class:vb01::quaternion typeref:typename:float getLength vector.h /^ float getLength(){return std::sqrt(getLengthSq());}$/;" f struct:vb01::Vector2 typeref:typename:float @@ -235,8 +253,14 @@ getLength vector.h /^ float getLength(){return std::sqrt(getLengthSq());}$/;" f getLengthSq quaternion.h /^ inline float getLengthSq(){return w*w+x*x+y*y+z*z;}$/;" f class:vb01::quaternion typeref:typename:float getLengthSq vector.h /^ float getLengthSq(){return x*x+y*y+z*z;}$/;" f struct:vb01::Vector3 typeref:typename:float getLengthSq vector.h /^ float getLengthSq(){return x*x+y*y;}$/;" f struct:vb01::Vector2 typeref:typename:float +getLight node.h /^ inline Light* getLight(int i){return lights[i];}$/;" f class:vb01::Node typeref:typename:Light * +getLights node.h /^ inline std::vector& getLights(){return lights;}$/;" f class:vb01::Node typeref:typename:std::vector & +getMaterial mesh.h /^ inline Material* getMaterial(){return material;}$/;" f class:vb01::Mesh typeref:typename:Material * +getMesh node.h /^ inline Mesh* getMesh(){return mesh;}$/;" f class:vb01::Node typeref:typename:Mesh * getNearPlane camera.h /^ inline float getNearPlane(){return nearPlane;}$/;" f class:vb01::Camera typeref:typename:float getNode mesh.h /^ inline Node* getNode(){return node;}$/;" f class:vb01::Mesh typeref:typename:Node * +getNumLights node.h /^ inline int getNumLights(){return lights.size();}$/;" f class:vb01::Node typeref:typename:int +getOuterAngle light.h /^ inline float getOuterAngle(){return outerAngle;}$/;" f class:vb01::Light typeref:typename:float getParent node.h /^ inline Node* getParent(){return parent;}$/;" f class:vb01::Node typeref:typename:Node * getPosition camera.h /^ inline Vector3 getPosition(){return position;}$/;" f class:vb01::Camera typeref:typename:Vector3 getPosition node.h /^ inline Vector3 getPosition(){return pos;}$/;" f class:vb01::Node typeref:typename:Vector3 @@ -277,14 +301,16 @@ img_v_max stb_image.h /^ int img_h_max, img_v_max;$/;" m struct:__anon84e4e886 img_x stb_image.h /^ stbi__uint32 img_x, img_y;$/;" m struct:__anon84e4e8860308 typeref:typename:stbi__uint32 img_y stb_image.h /^ stbi__uint32 img_x, img_y;$/;" m struct:__anon84e4e8860308 typeref:typename:stbi__uint32 indices mesh.h /^ unsigned int *indices,VAO,VBO,EBO;$/;" m class:vb01::Mesh typeref:typename:unsigned int * +innerAngle light.h /^ float innerAngle=.707,outerAngle=.714;$/;" m class:vb01::Light typeref:typename:float io stb_image.h /^ stbi_io_callbacks io;$/;" m struct:__anon84e4e8860308 typeref:typename:stbi_io_callbacks io_user_data stb_image.h /^ void *io_user_data;$/;" m struct:__anon84e4e8860308 typeref:typename:void * -isLighting material.h /^ inline bool isLighting(){return lightingEnabled;}$/;" f class:vb01::Material typeref:typename:bool +isLightingEnabled material.h /^ inline bool isLightingEnabled(){return lightingEnabled;}$/;" f class:vb01::Material typeref:typename:bool jfif stb_image.h /^ int jfif;$/;" m struct:__anon84e4e8860808 typeref:typename:int left camera.h /^ Vector3 position=Vector3(0,0,0),direction=Vector3::VEC_K,left=Vector3::VEC_I,up=Vector3::VEC_/;" m class:vb01::Camera typeref:typename:Vector3 length stb_image.h /^ stbi__uint32 length;$/;" m struct:__anon84e4e8860d08 typeref:typename:stbi__uint32 lflags stb_image.h /^ int lflags;$/;" m struct:__anon84e4e8861308 typeref:typename:int lightingEnabled material.h /^ bool lightingEnabled=false;$/;" m class:vb01::Material typeref:typename:bool +lights node.h /^ std::vector lights;$/;" m class:vb01::Node typeref:typename:std::vector line0 stb_image.h /^ stbi_uc *line0,*line1;$/;" m struct:__anon84e4e8860a08 typeref:typename:stbi_uc * line1 stb_image.h /^ stbi_uc *line0,*line1;$/;" m struct:__anon84e4e8860a08 typeref:typename:stbi_uc ** line_size stb_image.h /^ int line_size;$/;" m struct:__anon84e4e8861308 typeref:typename:int @@ -305,6 +331,7 @@ mesh node.h /^ Mesh *mesh=nullptr;$/;" m class:vb01::Node typeref:typename:Mes mg stb_image.h /^ unsigned int mr,mg,mb,ma, all_a;$/;" m struct:__anon84e4e8861008 typeref:typename:unsigned int mr stb_image.h /^ unsigned int mr,mg,mb,ma, all_a;$/;" m struct:__anon84e4e8861008 typeref:typename:unsigned int nearPlane camera.h /^ float fov=45,nearPlane=.1,farPlane=100;$/;" m class:vb01::Camera typeref:typename:float +node light.h /^ Node *node=nullptr;$/;" m class:vb01::Light typeref:typename:Node * node mesh.h /^ Node *node=nullptr;$/;" m class:vb01::Mesh typeref:typename:Node * nomore stb_image.h /^ int nomore; \/\/ flag if we saw a marker so must stop$/;" m struct:__anon84e4e8860808 typeref:typename:int norm mesh.h /^ Vector3 pos,norm;$/;" m struct:vb01::Mesh::Vertex typeref:typename:Vector3 @@ -334,9 +361,11 @@ operator / vector.h /^ templateVector3 operator\/(T s){return Vecto order stb_image.h /^ int scan_n, order[4];$/;" m struct:__anon84e4e8860808 typeref:typename:int[4] out stb_image.h /^ stbi_uc *idata, *expanded, *out;$/;" m struct:__anon84e4e8860e08 typeref:typename:stbi_uc *** out stb_image.h /^ stbi_uc *out; \/\/ output buffer (always 4 components)$/;" m struct:__anon84e4e8861308 typeref:typename:stbi_uc * +outerAngle light.h /^ float innerAngle=.707,outerAngle=.714;$/;" m class:vb01::Light typeref:typename:float pal stb_image.h /^ stbi_uc pal[256][4];$/;" m struct:__anon84e4e8861308 typeref:typename:stbi_uc[256][4] parent node.h /^ Node *parent=nullptr;$/;" m class:vb01::Node typeref:typename:Node * parse stb_image.h /^ int parse, step;$/;" m struct:__anon84e4e8861308 typeref:typename:int +pos light.h /^ Vector3 pos,color,attenuationValues=Vector3(1.8,.7,1),direction;$/;" m class:vb01::Light typeref:typename:Vector3 pos mesh.h /^ Vector3 pos,norm;$/;" m struct:vb01::Mesh::Vertex typeref:typename:Vector3 pos node.h /^ Vector3 pos;$/;" m class:vb01::Node typeref:typename:Vector3 position camera.h /^ Vector3 position=Vector3(0,0,0),direction=Vector3::VEC_K,left=Vector3::VEC_I,up=Vector3::VEC_/;" m class:vb01::Camera typeref:typename:Vector3 @@ -363,16 +392,28 @@ s stb_image.h /^ stbi__context *s;$/;" m struct:__anon84e4e8860808 typeref:typ s stb_image.h /^ stbi__context *s;$/;" m struct:__anon84e4e8860e08 typeref:typename:stbi__context * scan_n stb_image.h /^ int scan_n, order[4];$/;" m struct:__anon84e4e8860808 typeref:typename:int select texture.cpp /^ void Texture::select(){$/;" f class:vb01::Texture typeref:typename:void +setAttenuationValues light.h /^ inline void setAttenuationValues(float a, float b, float c){$/;" f class:vb01::Light typeref:typename:void setBool shader.cpp /^ void Shader::setBool(bool b, string var){$/;" f class:vb01::Shader typeref:typename:void +setColor light.h /^ inline void setColor(Vector3 color){this->color=color;}$/;" f class:vb01::Light typeref:typename:void setDiffuseMap material.h /^ void setDiffuseMap(std::string diffuseMap){this->diffuseMap=new Texture(diffuseMap);}$/;" f class:vb01::Material typeref:typename:void +setDirection light.h /^ inline void setDirection(Vector3 dir){this->direction=dir;}$/;" f class:vb01::Light typeref:typename:void +setFloat shader.cpp /^ void Shader::setFloat(float fl, string var){$/;" f class:vb01::Shader typeref:typename:void +setInnerAngle light.h /^ inline void setInnerAngle(float innerAngle){this->innerAngle=innerAngle;}$/;" f class:vb01::Light typeref:typename:void +setInt shader.cpp /^ void Shader::setInt(int i, string var){$/;" f class:vb01::Shader typeref:typename:void setLightingEnabled material.h /^ inline void setLightingEnabled(bool lighting){this->lightingEnabled=lighting;}$/;" f class:vb01::Material typeref:typename:void setMat4 shader.cpp /^ void Shader::setMat4(mat4 mat, string var){$/;" f class:vb01::Shader typeref:typename:void setMaterial mesh.h /^ void setMaterial(Material *mat){this->material=mat;}$/;" f class:vb01::Mesh typeref:typename:void +setNode light.h /^ inline void setNode(Node *node){this->node=node;}$/;" f class:vb01::Light typeref:typename:void setNode mesh.h /^ inline void setNode(Node *node){this->node=node;}$/;" f class:vb01::Mesh typeref:typename:void setNormalMap material.h /^ void setNormalMap(std::string normalMap){this->normalMap=new Texture(normalMap);}$/;" f class:vb01::Material typeref:typename:void +setNumLights shader.cpp /^ void Shader::setNumLights(int numLights){$/;" f class:vb01::Shader typeref:typename:void setObject node.cpp /^ void Node::setObject(Mesh *mesh){$/;" f class:vb01::Node typeref:typename:void +setOuterAngle light.h /^ inline void setOuterAngle(float outerAngle){this->outerAngle=outerAngle;}$/;" f class:vb01::Light typeref:typename:void setPosition camera.h /^ void setPosition(Vector3 position){this->position=position;}$/;" f class:vb01::Camera typeref:typename:void +setPosition light.h /^ inline void setPosition(Vector3 pos){this->pos=pos;}$/;" f class:vb01::Light typeref:typename:void setSpecularMap material.h /^ void setSpecularMap(std::string specularMap){this->specularMap=new Texture(specularMap);}$/;" f class:vb01::Material typeref:typename:void +setType light.h /^ inline void setType(Type t){this->type=t;}$/;" f class:vb01::Light typeref:typename:void +setVec3 shader.cpp /^ void Shader::setVec3(vec3 vec, string var){$/;" f class:vb01::Shader typeref:typename:void shader material.h /^ Shader *shader;$/;" m class:vb01::Material typeref:typename:Shader * size stb_image.h /^ stbi_uc size[257];$/;" m struct:__anon84e4e8860708 typeref:typename:stbi_uc[257] size stb_image.h /^ stbi_uc size[288];$/;" m struct:__anon84e4e8860b08 typeref:typename:stbi_uc[288] @@ -660,17 +701,20 @@ texture texture.h /^ unsigned int texture;$/;" m class:vb01::Texture typeref:t todo stb_image.h /^ int restart_interval, todo;$/;" m struct:__anon84e4e8860808 typeref:typename:int tq stb_image.h /^ int tq;$/;" m struct:__anon84e4e8860808::__anon84e4e8860908 typeref:typename:int transparent stb_image.h /^ int flags, bgindex, ratio, transparent, eflags;$/;" m struct:__anon84e4e8861308 typeref:typename:int +type light.h /^ Type type;$/;" m class:vb01::Light typeref:typename:Type type stb_image.h /^ stbi__uint32 type;$/;" m struct:__anon84e4e8860d08 typeref:typename:stbi__uint32 type stb_image.h /^ stbi_uc size,type,channel;$/;" m struct:__anon84e4e8861108 typeref:typename:stbi_uc up camera.h /^ Vector3 position=Vector3(0,0,0),direction=Vector3::VEC_K,left=Vector3::VEC_I,up=Vector3::VEC_/;" m class:vb01::Camera typeref:typename:Vector3 update box.cpp /^ void Box::update(){$/;" f class:vb01::Box typeref:typename:void update camera.cpp /^ void Camera::update(){$/;" f class:vb01::Camera typeref:typename:void +update light.cpp /^ void Light::update(){$/;" f class:vb01::Light typeref:typename:void update material.cpp /^ void Material::update(){$/;" f class:vb01::Material typeref:typename:void update mesh.cpp /^ void Mesh::update(){$/;" f class:vb01::Mesh typeref:typename:void update node.cpp /^ void Node::update(){$/;" f class:vb01::Node typeref:typename:void update root.cpp /^ void Root::update(){$/;" f class:vb01::Root typeref:typename:void use shader.cpp /^ void Shader::use(){glUseProgram(id);}$/;" f class:vb01::Shader typeref:typename:void v stb_image.h /^ int h,v;$/;" m struct:__anon84e4e8860808::__anon84e4e8860908 typeref:typename:int +vString shader.h /^ std::string vString,fString;$/;" m class:vb01::Shader typeref:typename:std::string validate_uint32 stb_image.h /^typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];$/;" t typeref:typename:unsigned char[sizeof (stbi__uint32)==4?1:-1] value stb_image.h /^ stbi__uint16 value[288];$/;" m struct:__anon84e4e8860b08 typeref:typename:stbi__uint16[288] values stb_image.h /^ stbi_uc values[256];$/;" m struct:__anon84e4e8860708 typeref:typename:stbi_uc[256] @@ -678,6 +722,8 @@ vb01 box.cpp /^namespace vb01{$/;" n file: vb01 box.h /^namespace vb01{$/;" n vb01 camera.cpp /^namespace vb01{$/;" n file: vb01 camera.h /^namespace vb01{$/;" n +vb01 light.cpp /^namespace vb01{$/;" n file: +vb01 light.h /^namespace vb01{$/;" n vb01 material.cpp /^namespace vb01{$/;" n file: vb01 material.h /^namespace vb01{$/;" n vb01 mesh.cpp /^namespace vb01{$/;" n file: @@ -726,6 +772,7 @@ zout stb_image.h /^ char *zout;$/;" m struct:__anon84e4e8860c08 typeref:typena zout_end stb_image.h /^ char *zout_end;$/;" m struct:__anon84e4e8860c08 typeref:typename:char * zout_start stb_image.h /^ char *zout_start;$/;" m struct:__anon84e4e8860c08 typeref:typename:char * ~Camera camera.cpp /^ Camera::~Camera(){}$/;" f class:vb01::Camera +~Light light.cpp /^ Light::~Light(){}$/;" f class:vb01::Light ~Material material.cpp /^ Material::~Material(){}$/;" f class:vb01::Material ~Mesh mesh.cpp /^ Mesh::~Mesh(){}$/;" f class:vb01::Mesh ~Node node.cpp /^ Node::~Node(){}$/;" f class:vb01::Node diff --git a/texture.frag b/texture.frag new file mode 100644 index 0000000..40dde81 --- /dev/null +++ b/texture.frag @@ -0,0 +1,65 @@ +#version 330 core + +const int numLights=1; + +in vec3 fragPos; +in vec3 norm; +in vec2 texCoords; + +out vec4 FragColor; + +//0-POINT,1-DIRECTIONAL,2-SPOT + +struct Light{ + int type; + vec3 pos,color,direction; + float innerAngle,outerAngle; + float a,b,c; +}; + +uniform Light light[numLights]; +uniform sampler2D tex; +uniform bool lightingEnabled; +uniform vec3 camPos; + +void main(){ + vec4 finalColor=texture(tex,texCoords); + if(lightingEnabled){ + vec3 diffuseColor=vec3(0),specularColor; + for(int i=0;ilight[i].outerAngle){ + coef=0; + } + } + float specularStrength=.5; + reflectVec=reflect(lightDir,norm); + float spec=pow(max(dot(viewDir,reflectVec),0),32); + specularColor+=vec3(1)*spec*specularStrength; + + factor=max(dot(lightDir,norm),0.); + diffuseColor+=light[i].color*factor*attenuation*coef; + } + finalColor*=vec4(diffuseColor+specularColor,1); + } + FragColor=finalColor; +} diff --git a/texture.vert b/texture.vert new file mode 100644 index 0000000..6d35d27 --- /dev/null +++ b/texture.vert @@ -0,0 +1,20 @@ +#version 330 core + +layout (location=0) in vec3 aPos; +layout (location=1) in vec3 aNorm; +layout (location=2) in vec2 aTexCoords; + +out vec3 fragPos; +out vec3 norm; +out vec2 texCoords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 proj; + +void main(){ + gl_Position=proj*view*model*vec4(aPos,1); + fragPos=vec3(model*vec4(aPos,1)); + texCoords=aTexCoords; + norm=mat3(transpose(inverse(model)))*aNorm; +}