diff --git a/Models/Models.vcxproj b/Models/Models.vcxproj index 33cd84e..2415bb3 100644 --- a/Models/Models.vcxproj +++ b/Models/Models.vcxproj @@ -147,7 +147,9 @@ + + diff --git a/Models/Models.vcxproj.filters b/Models/Models.vcxproj.filters index 7b704e7..ef3dad2 100644 --- a/Models/Models.vcxproj.filters +++ b/Models/Models.vcxproj.filters @@ -26,6 +26,12 @@ Resource Files + + Resource Files + + + Resource Files + diff --git a/Models/cupyay4.png b/Models/cupyay4.png new file mode 100644 index 0000000..f2738c4 Binary files /dev/null and b/Models/cupyay4.png differ diff --git a/Models/fragShader2.glsl b/Models/fragShader2.glsl new file mode 100644 index 0000000..18087e9 --- /dev/null +++ b/Models/fragShader2.glsl @@ -0,0 +1,11 @@ +#version 430 +layout (binding=0) uniform sampler2D samp; +out vec4 color; +in vec2 uv; +void main(void) +{ + color=texture(samp,uv); + if(color.a<0.1){ + discard; + } +} \ No newline at end of file diff --git a/Models/main.cpp b/Models/main.cpp index 4c155b2..aaaa778 100644 --- a/Models/main.cpp +++ b/Models/main.cpp @@ -10,19 +10,26 @@ #include #define numVAOs 1 -#define numVBOs 2 +#define numVBOs 3 -GLuint renderingProgram; +GLuint renderingProgram,expandingPlaneProgram; GLuint vao[numVAOs]; GLuint vbo[numVBOs]; glm::vec3 camera; -GLuint mvLoc,projLoc; +GLuint vLoc,projLoc; int width, height; float aspect; glm::mat4 mMat, pMat, vMat, mvMat; std::stacktransforms; +GLuint cupYayTex; + +void setupTextures(void){ + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D,cupYayTex); +} + void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin float cubePositions[108] = { -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, @@ -46,6 +53,14 @@ void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed -1.0f, -1.0f, - 1.0f, 1.0f, - 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f }; + float plane[6*3]={ + -1,1,0,1,1,0,-1,-1,0, + -1,-1,0,1,1,0,1,-1,0 + }; + float planeUVs[6*2]={ + 0,1,1,1,0,0, + 0,0,1,1,1,0 + }; glGenVertexArrays(1, vao); glBindVertexArray(vao[0]); glGenBuffers(numVBOs, vbo); @@ -54,32 +69,43 @@ void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositions), cubePositions, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); - glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidPositions), pyramidPositions, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(plane), plane, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); + glBufferData(GL_ARRAY_BUFFER, sizeof(planeUVs), planeUVs, GL_STATIC_DRAW); } void init(GLFWwindow* window) { renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl"); - camera={0.0f,0.0f,-12.0f}; + expandingPlaneProgram=utils::createShaderProgram("vertShader2.glsl","fragShader2.glsl"); + camera={0.0f,0.0f,-10.0f}; setupVertices(); + + cupYayTex=utils::loadTexture("cupyay4.png"); + setupTextures(); } double lastTime=0; void DrawCube(){ - glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mMat)); + glUniformMatrix4fv(vLoc,1,GL_FALSE,glm::value_ptr(mMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); glBindBuffer(GL_ARRAY_BUFFER,vbo[0]); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glDrawArrays(GL_TRIANGLES,0,36); } void DrawSquarePyramid(){ - glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mMat)); + glUniformMatrix4fv(vLoc,1,GL_FALSE,glm::value_ptr(mMat)); glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); glBindBuffer(GL_ARRAY_BUFFER,vbo[1]); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glDrawArrays(GL_TRIANGLES,0,18); } +GLuint timeLoc; + +GLuint mvLoc; +float size=0; + void display(GLFWwindow* window, double currentTime) { double elapsedTime=currentTime-lastTime; lastTime=currentTime; @@ -92,9 +118,11 @@ void display(GLFWwindow* window, double currentTime) { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); - mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix"); + vLoc=glGetUniformLocation(renderingProgram,"v_matrix"); projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); + timeLoc=glGetUniformLocation(renderingProgram,"time"); glfwGetFramebufferSize(window,&width,&height); aspect=(float)width/(float)height; @@ -104,34 +132,45 @@ void display(GLFWwindow* window, double currentTime) { transforms.push(vMat); mMat=glm::translate(transforms.top(),{0.f,0.f,0.f}); - transforms.push(mMat); - mMat=glm::rotate(mMat,float(utils::degToRad(90)*currentTime),{0.f,1.f,0.f}); - mMat=glm::rotate(mMat,float(utils::degToRad(12)),{0.f,0.f,1.f}); - DrawSquarePyramid(); - mMat=glm::translate(transforms.top(),{0.f,0.f,0.f}); - mMat=glm::rotate(mMat,float(utils::degToRad(90)*currentTime),{0.f,1.f,0.f}); - mMat=glm::rotate(mMat,float(utils::degToRad(192)),{0.f,0.f,1.f}); - mMat=glm::translate(mMat,{0.f,2.f,0.f}); - mMat=glm::rotate(mMat,float(utils::degToRad(270)),{0.f,1.f,0.f}); - DrawSquarePyramid(); - - mMat=glm::translate(transforms.top(),{sin(currentTime*0.5)*6,sin(currentTime*0.5)*0.8-1,cos(currentTime*0.5)*6}); - transforms.push(mMat); - mMat=glm::rotate(mMat,float(utils::degToRad(24)-28),{0,0,1}); - mMat=glm::rotate(mMat,float(utils::degToRad(24)*currentTime+28),{0,1,0}); - DrawCube(); - - mMat=glm::rotate(transforms.top(),utils::degToRad(90),{1,0,0}); - mMat=glm::translate(mMat,{sin(currentTime*3)*3,0,cos(currentTime*3)*3}); - mMat=glm::scale(mMat,{0.3,0.3,0.3}); - mMat=glm::rotate(mMat,float(-utils::degToRad(220)*currentTime+4),{0,1,0}); - DrawCube(); - - mMat=glm::rotate(transforms.top(),utils::degToRad(90),{1,1,0}); - mMat=glm::translate(mMat,{sin(currentTime*3)*3,0,cos(currentTime*3)*3}); - mMat=glm::scale(mMat,{0.3,0.3,0.3}); - mMat=glm::rotate(mMat,float(utils::degToRad(120)*currentTime),{0,1,0}); - DrawCube(); + glUniformMatrix4fv(vLoc,1,GL_FALSE,glm::value_ptr(vMat)); + glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); + glUniform1f(timeLoc,currentTime); + glBindBuffer(GL_ARRAY_BUFFER,vbo[0]); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + glDrawArraysInstanced(GL_TRIANGLES,0,36,100000); + + size+=currentTime/1000; + glUseProgram(expandingPlaneProgram); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glfwGetFramebufferSize(window,&width,&height); + aspect=(float)width/(float)height; + pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f); + vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z}); + + mMat=glm::translate(glm::mat4{1.0},{0,0,0}); + mMat=glm::scale(mMat,{size,size,size}); + + mvMat=vMat*mMat; + + mvLoc=glGetUniformLocation(expandingPlaneProgram,"mv_matrix"); + projLoc=glGetUniformLocation(expandingPlaneProgram,"proj_matrix"); + glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat)); + glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); + glBindBuffer(GL_ARRAY_BUFFER,vbo[1]); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + glBindBuffer(GL_ARRAY_BUFFER,vbo[2]); + glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0); + + glActiveTexture(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D,cupYayTex); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + + glDrawArrays(GL_TRIANGLES,0,6); } int main(void) { diff --git a/Models/utils.h b/Models/utils.h index 1162823..e4c13b2 100644 --- a/Models/utils.h +++ b/Models/utils.h @@ -3,6 +3,7 @@ #include #include #include "ShaderError.h" +#include #define PI 3.14159 @@ -80,4 +81,14 @@ public: inline static float radToDeg(float rad){ return rad*57.2957795130823208767; } + + inline static GLuint loadTexture(const char*file){ + GLuint textureID; + textureID=SOIL_load_OGL_texture(file,SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y); + if(!textureID){ + std::cout<<"Could not load texture file "<