diff --git a/CMakeLists.txt b/CMakeLists.txt index dfe8f3c..b826eef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ link_directories(/usr/lib/) set(CMAKE_BUILD_TYPE Debug) set(math quaternion.cpp vector.cpp) -set(render camera.cpp light.cpp material.cpp mesh.cpp model.cpp node.cpp quad.cpp root.cpp shader.cpp texture.cpp) +set(render camera.cpp light.cpp material.cpp mesh.cpp model.cpp node.cpp quad.cpp box.cpp root.cpp shader.cpp texture.cpp) add_executable(vb01 main.cpp ${render} ${math}) target_link_libraries(vb01 glfw) diff --git a/box.cpp b/box.cpp index 7a9adcf..d805d39 100644 --- a/box.cpp +++ b/box.cpp @@ -1,73 +1,63 @@ #include"box.h" namespace vb01{ - Box::Box(){ - float size=.1; - float vertData[]={ - 0,1,0,1,1, - 0,1,0,0,1, - 0,1,0,0,0, - - 0,1,0,0,0, - 0,1,0,1,0, - 0,1,0,1,1, - - 0,-1,0,1,1, - 0,-1,0,0,1, - 0,-1,0,0,0, - - 0,-1,0,0,0, - 0,-1,0,1,0, - 0,-1,0,1,1, - - 0,0,1,1,1, - 0,0,1,0,1, - 0,0,1,0,0, - - 0,0,1,0,0, - 0,0,1,1,0, - 0,0,1,1,1, - - 0,0,-1,1,1, - 0,0,-1,0,1, - 0,0,-1,0,0, - - 0,0,-1,0,0, - 0,0,-1,1,0, - 0,0,-1,1,1, - - 1,0,0,1,1, - 1,0,0,0,1, - 1,0,0,0,0, - - 1,0,0,0,0, - 1,0,0,1,0, - 1,0,0,1,1, - - -1,0,0,1,1, - -1,0,0,0,1, - -1,0,0,0,0, - - -1,0,0,0,0, - -1,0,0,1,0, - -1,0,0,1,1 - }; - Vector3 p[]={ - Vector3(size.x/2,size.y/2,size.z/2), - Vector3(-size.x/2,size.y/2,size.z/2), - Vector3(-size.x/2,size.y/2,-size.z/2), + Box::Box(Vector3 size){ + Vector3 pos[]={ + Vector3(size.x/2,size.y/2,size.z/2), + Vector3(-size.x/2,size.y/2,size.z/2), Vector3(-size.x/2,size.y/2,-size.z/2), + Vector3(size.x/2,size.y/2,-size.z/2), Vector3(size.x/2,-size.y/2,size.z/2), Vector3(-size.x/2,-size.y/2,size.z/2), Vector3(-size.x/2,-size.y/2,-size.z/2), - Vector3(-size.x/2,-size.y/2,-size.z/2) + Vector3(size.x/2,-size.y/2,-size.z/2) }; - int indices[]={ - + Vector3 norm[]={ + Vector3(0,0,1), + Vector3(0,1,0), + Vector3(1,0,0), + + Vector3(0,0,-1), + Vector3(0,-1,0), + Vector3(-1,0,0) }; - } - void Box::update(){ - + Vector2 tex[]={ + Vector2(1,1), + Vector2(0,1), + Vector2(0,0), + Vector2(1,0) + }; + unsigned int data[]={ + 0,3,1, 4,3,0, 1,3,2, + 5,3,3, 1,3,2, 4,3,0, + + 2,0,1, 6,0,0, 3,0,2, + 6,0,2, 7,0,0, 3,0,3, + + 3,2,0, 7,2,1, 4,2,2, + 4,2,2, 0,2,3, 3,2,0, + + 1,5,0, 5,5,1, 6,5,2, + 6,5,2, 2,5,3, 1,5,0, + + 0,1,0, 1,1,1, 2,1,2, + 2,1,2, 3,1,3, 0,1,0, + + 4,4,3, 6,4,1, 5,4,2, + 6,4,1, 4,4,3, 7,4,0 + }; + numTris=12; + indices=new unsigned int[3*numTris]; + vertices=new Vertex[3*numTris]; + for(int i=0;i<3*numTris;i++){ + Vertex v; + v.pos=pos[data[3*i]]; + v.norm=norm[data[3*i+1]]; + v.texCoords=tex[data[3*i+2]]; + vertices[i]=v; + indices[i]=i; + } + construct(); } } diff --git a/box.h b/box.h index 6a8fee6..88b8cad 100644 --- a/box.h +++ b/box.h @@ -6,9 +6,7 @@ namespace vb01{ class Box : public Mesh{ public: - Box(); - void update(); - private: + Box(Vector3); }; } diff --git a/main.cpp b/main.cpp index 3af31c3..7dfc92f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,10 @@ +#include #include +#include #include"root.h" #include"node.h" #include"quad.h" +#include"box.h" #include"material.h" #include"model.h" #include"light.h" @@ -14,20 +17,31 @@ int main(){ Root *root=Root::getSingleton(); root->start(800,600); - Vector3 v1[]={Vector3(.2,.2,.2),Vector3(.5,.5,.5)}; - Vector3 v2[]={Vector3(-.5,0,-2),Vector3(.0,0,0)}; + Vector3 v1[]={Vector3(.2,.2,.2),Vector3(1,1,1)}; + Vector3 v2[]={Vector3(0,0,0),Vector3(.0,0,0)}; - //Quad *q=new Quad(v1[1]); - Model *n=new Model("/home/dominykas/c++/FSim/jet00.obj"); + string t[]={ + "/home/dominykas/c++/FSim/left.jpg", + "/home/dominykas/c++/FSim/right.jpg", + "/home/dominykas/c++/FSim/top.jpg", + "/home/dominykas/c++/FSim/bottom.jpg", + "/home/dominykas/c++/FSim/front.jpg", + "/home/dominykas/c++/FSim/back.jpg" + }; + root->createSkybox(t); + + Box *b=new Box(Vector3(1,1,1)); + Node *n=new Node(v2[0]); Material *mat=new Material(); mat->addDiffuseMap("/home/dominykas/c++/FSim/defaultTexture.jpg"); mat->setLightingEnabled(false); - n->setMaterial(mat); - //n->attachMesh(q); + b->setMaterial(mat); + n->attachMesh(b); root->getRootNode()->attachChild(n); Camera *cam=Root::getSingleton()->getCamera(); cam->setPosition(Vector3(0,0,-5)); + cam->lookAt(Vector3(1,0,1).norm(),Vector3(0,1,0)); Light *l1=new Light(); l1->setPosition(Vector3(-.5,.1,0)); l1->setColor(Vector3(0,1,0)); @@ -37,7 +51,8 @@ int main(){ l1->setOuterAngle(glm::radians(60.)); l1->setType(Light::POINT); //root->getRootNode()->addLight(l1); - while(true) + while(true){ root->update(); + } return 0; } diff --git a/material.cpp b/material.cpp index d8364e7..d12fe47 100644 --- a/material.cpp +++ b/material.cpp @@ -4,9 +4,19 @@ using namespace std; namespace vb01{ - Material::Material(){ - string basePath="/home/dominykas/c++/FSim/"; - shader=new Shader(basePath+"texture.vert",basePath+"texture.frag"); + Material::Material(Type type){ + this->type=type; + + string basePath="/home/dominykas/c++/FSim/",shaderName; + switch(type){ + case MATERIAL_2D: + shaderName="texture."; + break; + case MATERIAL_SKYBOX: + shaderName="skybox."; + break; + } + shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag"); } Material::~Material(){} diff --git a/material.h b/material.h index 2bf2cea..190cb59 100644 --- a/material.h +++ b/material.h @@ -6,20 +6,22 @@ #include namespace vb01{ - class Texture; - class Material{ public: - Material(); + enum Type{MATERIAL_2D,MATERIAL_SKYBOX}; + + Material(Type=MATERIAL_2D); ~Material(); void update(); void addDiffuseMap(std::string diffuseMap){diffuseMapTextures.push_back(new Texture(diffuseMap));} + void addDiffuseMap(std::string diffuseMap[6]){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: + Type type; bool lightingEnabled=false; std::vector diffuseMapTextures,normalMapTextures,specularMapTextures; Shader *shader=nullptr; diff --git a/mesh.cpp b/mesh.cpp index 76e7d0d..72d27c7 100644 --- a/mesh.cpp +++ b/mesh.cpp @@ -49,11 +49,11 @@ namespace vb01{ Camera *cam=root->getCamera(); float fov=cam->getFov(),width=root->getWidth(),height=root->getHeight(); float nearPlane=cam->getNearPlane(),farPlane=cam->getFarPlane(); - Vector3 pos=node->getPosition(); + Vector3 pos=(node?node->getPosition():Vector3::VEC_ZERO); 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,13.f,vec3(0,1,0)); + model=rotate(model,radians(0.f),vec3(0,1,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 ff9ba81..ad645c0 100644 --- a/mesh.h +++ b/mesh.h @@ -16,7 +16,6 @@ namespace vb01{ Vector3 pos,norm; Vector2 texCoords; }; - Mesh(); Mesh(Vertex*,unsigned int*,int); ~Mesh(); virtual void update(); @@ -31,6 +30,7 @@ namespace vb01{ std::vector meshes; std::string name=""; protected: + Mesh(); void construct(); Vertex *vertices; unsigned int *indices,VAO,VBO,EBO; diff --git a/node.cpp b/node.cpp index 7f312e0..48d33f5 100644 --- a/node.cpp +++ b/node.cpp @@ -51,7 +51,8 @@ namespace vb01{ vector meshes=n->getMeshes(); for(Mesh *m : meshes){ Material *mat=m->getMaterial(); - if(mat) mat->getShader()->setNumLights(numLights>0?numLights:1); + string str=to_string(numLights>0?numLights:1); + if(mat) mat->getShader()->editShader(false,'=',';',str); } } } diff --git a/quad.cpp b/quad.cpp index 275d7ce..d4dfbd4 100644 --- a/quad.cpp +++ b/quad.cpp @@ -27,8 +27,9 @@ namespace vb01{ v6.norm=Vector3(0,0,-1); v6.texCoords=Vector2(1,1); + int numPolyVerts=3; numTris=2; - indices=new unsigned int[numTris*3]; + indices=new unsigned int[numTris*numPolyVerts]; indices[0]=0; indices[1]=1; indices[2]=2; @@ -36,7 +37,7 @@ namespace vb01{ indices[4]=4; indices[5]=5; - vertices=new Vertex[numTris*3]; + vertices=new Vertex[numTris*numPolyVerts]; vertices[0]=v1; vertices[1]=v2; vertices[2]=v3; diff --git a/root.cpp b/root.cpp index 2fe1daa..b24e92c 100644 --- a/root.cpp +++ b/root.cpp @@ -1,8 +1,13 @@ +#include"stb_image.h" #include"root.h" #include #include #include #include +#include"shader.h" +#include"box.h" + +using namespace std; namespace vb01{ static Root *root=new Root(); @@ -41,7 +46,6 @@ namespace vb01{ glEnable(GL_DEPTH_TEST); glEnable(GL_STENCIL_TEST); glEnable(GL_CULL_FACE); - glCullFace(GL_FRONT); } void Root::update(){ @@ -49,9 +53,21 @@ namespace vb01{ glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glDepthMask(GL_FALSE); + glCullFace(GL_BACK); + skybox->update(); + glDepthMask(GL_TRUE); + glCullFace(GL_FRONT); rootNode->update(); glfwSwapBuffers(window); } } + + void Root::createSkybox(string textures[6]){ + skybox = new Box(Vector3(10,10,10)); + Material *skyboxMat=new Material(Material::Type::MATERIAL_SKYBOX); + skyboxMat->addDiffuseMap(textures); + skybox->setMaterial(skyboxMat); + } } diff --git a/root.h b/root.h index 1dcbfdb..fab5c4f 100644 --- a/root.h +++ b/root.h @@ -3,6 +3,7 @@ #include"node.h" #include"camera.h" +#include class GLFWwindow; @@ -10,6 +11,8 @@ namespace vb01{ void foo(); class Mesh; + class Box; + class Shader; class Root{ public: @@ -20,8 +23,10 @@ namespace vb01{ inline Node* getRootNode(){return rootNode;} inline int getWidth(){return width;} inline int getHeight(){return height;} + void createSkybox(std::string[6]); static Root* getSingleton(); private: + Box *skybox=nullptr; bool running=false; int width,height; GLFWwindow *window; diff --git a/shader.cpp b/shader.cpp index 9793f45..d28be51 100644 --- a/shader.cpp +++ b/shader.cpp @@ -25,19 +25,26 @@ namespace vb01{ vString=vertShaderStream.str(); fString=fragShaderStream.str(); - setNumLights(1); + + loadShaders(); } - void Shader::setNumLights(int numLights){ + void Shader::editShader(bool vertexShader, char firstChar, char secondChar, string insertion){ int firstCharId=-1,secondCharId=-1; for(int i=0;imaterial=mat;}$/;" setMaterial model.cpp /^ void Model::setMaterial(Material *mat){$/;" f class:vb01::Model 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 -setNumLights shader.cpp /^ void Shader::setNumLights(int numLights){$/;" f class:vb01::Shader 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 @@ -431,6 +445,7 @@ size stb_image.h /^ stbi_uc size[257];$/;" m struct:__anon84e4e8860708 typere size stb_image.h /^ stbi_uc size[288];$/;" m struct:__anon84e4e8860b08 typeref:typename:stbi_uc[288] size stb_image.h /^ stbi_uc size,type,channel;$/;" m struct:__anon84e4e8861108 typeref:typename:stbi_uc skip stb_image.h /^ void (*skip) (void *user,int n); \/\/ skip the next 'n' bytes, or 'unget/;" m struct:__anon84e4e8860208 typeref:typename:void (*)(void * user,int n) +skybox root.h /^ Box *skybox=nullptr;$/;" m class:vb01::Root typeref:typename:Box * spec_end stb_image.h /^ int spec_end;$/;" m struct:__anon84e4e8860808 typeref:typename:int spec_start stb_image.h /^ int spec_start;$/;" m struct:__anon84e4e8860808 typeref:typename:int specularMapTextures material.h /^ std::vector diffuseMapTextures,normalMapTextures,specularMapTextures;$/;" m class:vb01::Material typeref:typename:std::vector @@ -714,10 +729,11 @@ todo stb_image.h /^ int restart_interval, todo;$/;" m struct:__anon84e4e886080 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 material.h /^ Type type;$/;" m class:vb01::Material 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 +type texture.h /^ TextureType type;$/;" m class:vb01::Texture typeref:typename:TextureType up camera.h /^ Vector3 position=Vector3(0,0,0),direction=Vector3(0,0,1),left=Vector3(1,0,0),up=Vector3(0,1,0/;" 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 diff --git a/texture.cpp b/texture.cpp index aeb2921..b38708c 100644 --- a/texture.cpp +++ b/texture.cpp @@ -3,11 +3,14 @@ #include #include #include"texture.h" +#include using namespace std; namespace vb01{ Texture::Texture(string path){ + this->type=TextureType::TEXTURE_2D; + glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D,texture); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); @@ -22,7 +25,31 @@ namespace vb01{ stbi_image_free(data); } + Texture::Texture(string paths[6]){ + this->type=TextureType::TEXTURE_CUBEMAP; + + glGenTextures(1,&texture); + glBindTexture(GL_TEXTURE_CUBE_MAP,texture); + for(int i=0;i<6;i++){ + data=stbi_load(paths[i].c_str(),&width,&height,&numChannels,0); + if(data){ + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+i,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,data); + stbi_image_free(data); + } + else{ + cout<<"Failed to read cubemap texture "<