diff --git a/OpenGLTemplate.sln b/OpenGLTemplate.sln index e1d5948..10f335d 100644 --- a/OpenGLTemplate.sln +++ b/OpenGLTemplate.sln @@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Dot", "Dot\Dot.vcxproj", "{ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Models", "Models\Models.vcxproj", "{6E813500-09F1-40C1-BF2A-E8C4D0DE1142}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TextureMapping", "TextureMapping\TextureMapping.vcxproj", "{609A0750-8502-4F60-8E54-AD032D6E216B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -41,6 +43,14 @@ Global {6E813500-09F1-40C1-BF2A-E8C4D0DE1142}.Release|x64.Build.0 = Release|x64 {6E813500-09F1-40C1-BF2A-E8C4D0DE1142}.Release|x86.ActiveCfg = Release|Win32 {6E813500-09F1-40C1-BF2A-E8C4D0DE1142}.Release|x86.Build.0 = Release|Win32 + {609A0750-8502-4F60-8E54-AD032D6E216B}.Debug|x64.ActiveCfg = Debug|x64 + {609A0750-8502-4F60-8E54-AD032D6E216B}.Debug|x64.Build.0 = Debug|x64 + {609A0750-8502-4F60-8E54-AD032D6E216B}.Debug|x86.ActiveCfg = Debug|Win32 + {609A0750-8502-4F60-8E54-AD032D6E216B}.Debug|x86.Build.0 = Debug|Win32 + {609A0750-8502-4F60-8E54-AD032D6E216B}.Release|x64.ActiveCfg = Release|x64 + {609A0750-8502-4F60-8E54-AD032D6E216B}.Release|x64.Build.0 = Release|x64 + {609A0750-8502-4F60-8E54-AD032D6E216B}.Release|x86.ActiveCfg = Release|Win32 + {609A0750-8502-4F60-8E54-AD032D6E216B}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/OpenGLTemplate/OpenGLTemplate.vcxproj b/OpenGLTemplate/OpenGLTemplate.vcxproj index 1fd98c9..a38fe64 100644 --- a/OpenGLTemplate/OpenGLTemplate.vcxproj +++ b/OpenGLTemplate/OpenGLTemplate.vcxproj @@ -153,6 +153,9 @@ + + + diff --git a/OpenGLTemplate/OpenGLTemplate.vcxproj.filters b/OpenGLTemplate/OpenGLTemplate.vcxproj.filters index 0769552..a93b22a 100644 --- a/OpenGLTemplate/OpenGLTemplate.vcxproj.filters +++ b/OpenGLTemplate/OpenGLTemplate.vcxproj.filters @@ -35,4 +35,9 @@ Resource Files + + + Resource Files + + \ No newline at end of file diff --git a/TextureMapping/ShaderError.h b/TextureMapping/ShaderError.h new file mode 100644 index 0000000..8ae7d47 --- /dev/null +++ b/TextureMapping/ShaderError.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include + +namespace ErrorCheck{ + inline void printShaderLog(GLuint shader) { + int len = 0; + int chWrittn = 0; + char *log; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len); + if (len > 0) { + log = (char *)malloc(len); + glGetShaderInfoLog(shader, len, &chWrittn, log); + std::cout << "Shader Info Log: " << log << std::endl; + free(log); + } } + inline void printProgramLog(int prog) { + int len = 0; + int chWrittn = 0; + char *log; + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len); + if (len > 0) { + log = (char *)malloc(len); + glGetProgramInfoLog(prog, len, &chWrittn, log); + std::cout << "Program Info Log: " << log << std::endl; + free(log); + } } + inline bool checkOpenGLError() { + bool foundError = false; + int glErr = glGetError(); + while (glErr != GL_NO_ERROR) { + std::cout << "glError: " << glErr << std::endl; + foundError = true; + glErr = glGetError(); + } + return foundError; + } +} \ No newline at end of file diff --git a/TextureMapping/TextureMapping.vcxproj b/TextureMapping/TextureMapping.vcxproj new file mode 100644 index 0000000..35b7b68 --- /dev/null +++ b/TextureMapping/TextureMapping.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {609a0750-8502-4f60-8e54-ad032d6e216b} + TextureMapping + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories) + + + Console + true + C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories) + glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories) + glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories) + + + Console + true + C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories) + glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories) + glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/TextureMapping/TextureMapping.vcxproj.filters b/TextureMapping/TextureMapping.vcxproj.filters new file mode 100644 index 0000000..5166fef --- /dev/null +++ b/TextureMapping/TextureMapping.vcxproj.filters @@ -0,0 +1,38 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + Header Files + + + Header Files + + + + + Resource Files + + + Resource Files + + + \ No newline at end of file diff --git a/TextureMapping/brick1.jpg b/TextureMapping/brick1.jpg new file mode 100644 index 0000000..dbcb80f Binary files /dev/null and b/TextureMapping/brick1.jpg differ diff --git a/TextureMapping/fragShader.glsl b/TextureMapping/fragShader.glsl new file mode 100644 index 0000000..66790d8 --- /dev/null +++ b/TextureMapping/fragShader.glsl @@ -0,0 +1,8 @@ +#version 430 +layout (binding=0) uniform sampler2D samp; +out vec4 color; +in vec2 uv; +void main(void) +{ + color=texture(samp,uv); +} \ No newline at end of file diff --git a/TextureMapping/glew32.dll b/TextureMapping/glew32.dll new file mode 100644 index 0000000..04f9381 Binary files /dev/null and b/TextureMapping/glew32.dll differ diff --git a/TextureMapping/ice.jpg b/TextureMapping/ice.jpg new file mode 100644 index 0000000..17804a3 Binary files /dev/null and b/TextureMapping/ice.jpg differ diff --git a/TextureMapping/main.cpp b/TextureMapping/main.cpp new file mode 100644 index 0000000..e16e003 --- /dev/null +++ b/TextureMapping/main.cpp @@ -0,0 +1,186 @@ +#include +#include +#include "utils.h" +#include +#include +#include +#include +#include +#include +#include + +#define numVAOs 1 +#define numVBOs 4 + +GLuint renderingProgram; +GLuint vao[numVAOs]; +GLuint vbo[numVBOs]; + +glm::vec3 camera; +GLuint mvLoc,projLoc; +int width, height; +float aspect; +glm::mat4 mMat, pMat, vMat, mvMat; +std::stacktransforms; + +GLuint pyrTex; + +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, + 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, + 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, + -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, + -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, + 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, + 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f + }; + float pyramidPositions[54]={ + -1.0f, -1.0f, 1.0f, 1.0f, - 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, + 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f, + 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f, + -1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.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, -1.0f + }; + float pyrTexCoords[36] = + { 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 1.0f, // top and right faces + 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 1.0f, // back and left faces + 0.0f, 0.0f, 200.0f, 200.0f, 0.0f, 200.0f, 200.0f, 200.0f, 0.0f, 0.0f, 200.0f, 0.0f }; // base triangles + glGenVertexArrays(1, vao); + glBindVertexArray(vao[0]); + glGenBuffers(numVBOs, vbo); + + glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); + glBufferData(GL_ARRAY_BUFFER, sizeof(cubePositions), cubePositions, GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); + glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidPositions), pyramidPositions, GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); + glBufferData(GL_ARRAY_BUFFER, sizeof(pyrTexCoords), pyrTexCoords, GL_STATIC_DRAW); +} + +void setupTextures(){ + pyrTex=utils::loadTexture("brick1.jpg"); + glBindTexture(GL_TEXTURE_2D,pyrTex); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +} + +void init(GLFWwindow* window) { + renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl"); + camera={0.0f,2.0f,-10.0f}; + setupVertices(); + setupTextures(); +} + +double lastTime=0; + +void DrawCube(){ + glUniformMatrix4fv(mvLoc,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 DrawTexturedSquarePyramid(){ + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D,pyrTex); + + glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mMat)); + glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat)); + glBindBuffer(GL_ARRAY_BUFFER,vbo[2]); + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); + glBindBuffer(GL_ARRAY_BUFFER,vbo[3]); + glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0); + + glDrawArrays(GL_TRIANGLES,0,18); +} + +void display(GLFWwindow* window, double currentTime) { + double elapsedTime=currentTime-lastTime; + lastTime=currentTime; + + glClear(GL_DEPTH_BUFFER_BIT); + glClearColor(0,0,0.2,1); + glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(renderingProgram); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + + mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix"); + projLoc=glGetUniformLocation(renderingProgram,"proj_matrix"); + + 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}); + transforms.push(vMat); + + mMat=glm::scale(transforms.top(),{200.f,1.f,200.f}); + mMat=glm::translate(mMat,{0.f,0.f,0.f}); + transforms.push(mMat); + mMat=glm::rotate(mMat,float(utils::degToRad(90)*currentTime/100),{0.f,1.f,0.f}); + mMat=glm::rotate(mMat,float(utils::degToRad(12)),{0.f,0.f,1.f}); + DrawTexturedSquarePyramid(); + mMat=glm::translate(transforms.top(),{0.f,0.f,0.f}); + mMat=glm::rotate(mMat,float(utils::degToRad(90)*currentTime/100),{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,4.f,0.f}); + mMat=glm::rotate(mMat,float(utils::degToRad(270)),{0.f,1.f,0.f}); + DrawTexturedSquarePyramid(); + + 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(); +} + +int main(void) { + if (!glfwInit()) { exit(EXIT_FAILURE); } + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + GLFWwindow* window = glfwCreateWindow(1920, 1080, "Texture Mapping!", NULL, NULL); + glfwSetWindowPos(window,1400,180); + glfwMakeContextCurrent(window); + if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); } + glfwSwapInterval(1); + + init(window); + + while (!glfwWindowShouldClose(window)) { + display(window,glfwGetTime()); + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwDestroyWindow(window); + glfwTerminate(); + exit(EXIT_SUCCESS); +} \ No newline at end of file diff --git a/TextureMapping/utils.h b/TextureMapping/utils.h new file mode 100644 index 0000000..e4c13b2 --- /dev/null +++ b/TextureMapping/utils.h @@ -0,0 +1,94 @@ +#pragma once +#include +#include +#include +#include "ShaderError.h" +#include + +#define PI 3.14159 + +class utils{ + inline static std::string readShaderSource(const char *filePath){ + std::string content; + std::ifstream fileStream(filePath, std::ios::in); + std::string line = ""; + if(fileStream.fail()){ + std::cout<<"Could not open shader "<