diff --git a/LearningOpenGL.sln b/LearningOpenGL.sln new file mode 100644 index 0000000..faf659a --- /dev/null +++ b/LearningOpenGL.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LearningOpenGL", "LearningOpenGL.vcxproj", "{3C2529AD-C6AB-4D39-A409-EBB27FC9653A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3C2529AD-C6AB-4D39-A409-EBB27FC9653A}.Debug|x64.ActiveCfg = Debug|x64 + {3C2529AD-C6AB-4D39-A409-EBB27FC9653A}.Debug|x64.Build.0 = Debug|x64 + {3C2529AD-C6AB-4D39-A409-EBB27FC9653A}.Debug|x86.ActiveCfg = Debug|Win32 + {3C2529AD-C6AB-4D39-A409-EBB27FC9653A}.Debug|x86.Build.0 = Debug|Win32 + {3C2529AD-C6AB-4D39-A409-EBB27FC9653A}.Release|x64.ActiveCfg = Release|x64 + {3C2529AD-C6AB-4D39-A409-EBB27FC9653A}.Release|x64.Build.0 = Release|x64 + {3C2529AD-C6AB-4D39-A409-EBB27FC9653A}.Release|x86.ActiveCfg = Release|Win32 + {3C2529AD-C6AB-4D39-A409-EBB27FC9653A}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0FC02A26-2AB4-4E64-A0A0-0EB58E2AE683} + EndGlobalSection +EndGlobal diff --git a/LearningOpenGL.vcxproj b/LearningOpenGL.vcxproj new file mode 100644 index 0000000..91cc9c8 --- /dev/null +++ b/LearningOpenGL.vcxproj @@ -0,0 +1,148 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {3c2529ad-c6ab-4d39-a409-ebb27fc9653a} + LearningOpenGL + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + C:\Development Tools\glm-0.9.9.7\glm;C:\Development Tools\glad\include;C:\Development Tools\glfw-3.4.bin.WIN64\glfw-3.4.bin.WIN64\include;$(IncludePath) + C:\Development Tools\glfw-3.4.bin.WIN64\glfw-3.4.bin.WIN64\lib-vc2022;$(LibraryPath) + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Development Projects\LearningOpenGL;%(AdditionalIncludeDirectories) + + + Console + true + glfw3.lib;opengl32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LearningOpenGL.vcxproj.filters b/LearningOpenGL.vcxproj.filters new file mode 100644 index 0000000..d23d48a --- /dev/null +++ b/LearningOpenGL.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {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 + + + Source Files + + + + + + + + Header Files + + + \ No newline at end of file diff --git a/fragment.frag b/fragment.frag new file mode 100644 index 0000000..f0f2177 --- /dev/null +++ b/fragment.frag @@ -0,0 +1,8 @@ +#version 330 core +out vec4 FragColor; +in vec3 ourColor; + +void main() +{ + FragColor = vec4(ourColor, 1.0f); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4c96477 --- /dev/null +++ b/main.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include + + +void framebuffer_size_callback(GLFWwindow* window, int width, int height); +void processInput(GLFWwindow* window); + +int SCR_WIDTH = 800; +int SCR_HEIGHT = 800; + +float triangleVertices[] = { + -0.5f,-0.5f,0.0f, 1.0f,0.0f,0.0f, + 0.5f,-0.5f,0.0f, 0.0f,1.0f,0.0f, + 0.0f, 0.5f,0.0f, 0.0f,0.0f,1.0f +}; + +float squareVertices[] = { + -0.8f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, + -0.8f, 0.8f, 0.0f, 1.0f, 0.0f, 0.0f, + -1.0f, 0.8f, 0.0f, 0.0f, 1.0f, 0.0f, + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f +}; + +unsigned int indices[] = { + 0,1,3, + 1,2,3 +}; + +int main() { + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearningOpenGl", NULL, NULL); + if (window == NULL) { + std::cout << "Failed to create GLFW window" << std::endl; + glfwTerminate(); + return -1; + } + glfwMakeContextCurrent(window); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + std::cout << "Failed to initialize GLAD" << std::endl; + return -1; + } + + Shader ourShader("vertex.vert", "fragment.frag"); + + glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); + + unsigned int VAO_1, VAO_2, VBO_1, VBO_2, EBO; + glGenVertexArrays(1, &VAO_1); + glGenVertexArrays(1, &VAO_2); + glGenBuffers(1, &VBO_1); + glGenBuffers(1, &VBO_2); + glGenBuffers(1, &EBO); + + glBindVertexArray(VAO_1); + + glBindBuffer(GL_ARRAY_BUFFER, VBO_1); + glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); + + glBindVertexArray(VAO_2); + + glBindBuffer(GL_ARRAY_BUFFER, VBO_2); + glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + float r = 0.0f, g = 0.0f, b = 0.0f; + while (!glfwWindowShouldClose(window)) { + //input + processInput(window); + + //rendering + if (r < 1) { + r = r + 0.01f; + } + else if (g < 1) { + g = g + 0.01f; + } + else if (b < 1) { + b = b + 0.01f; + } + else { + r = 0; + g = 0; + b = 0; + } + + glClearColor(r, g, b, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + //transforms + glm::mat4 defaultTransform = glm::mat4(1.0f); + glm::mat4 rotatedTransform = glm::rotate(defaultTransform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f)); + + //draw + ourShader.use(); + unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform"); + glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(rotatedTransform)); + glBindVertexArray(VAO_1); + glDrawArrays(GL_TRIANGLES, 0, 3); + + glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(defaultTransform)); + glBindVertexArray(VAO_2); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + + //check and call + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwTerminate(); + return 0; +} + +void framebuffer_size_callback(GLFWwindow* window, int width, int height) { + glViewport(0, 0, width, height); +} + +void processInput(GLFWwindow* window) { + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { + glfwSetWindowShouldClose(window, true); + } +} \ No newline at end of file diff --git a/shader.h b/shader.h new file mode 100644 index 0000000..efe0cc5 --- /dev/null +++ b/shader.h @@ -0,0 +1,155 @@ +#pragma once +#ifndef SHADER_H +#define SHADER_H + +#include + +#include +#include +#include +#include + +class Shader +{ +public: + unsigned int ID; + + Shader(const char* vertexPath, const char* fragmentPath) + { + std::string vertexCode; + std::string fragmentCode; + std::ifstream vShaderFile; + std::ifstream fShaderFile; + + vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + try + { + vShaderFile.open(vertexPath); + fShaderFile.open(fragmentPath); + std::stringstream vShaderStream, fShaderStream; + vShaderStream << vShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); + vShaderFile.close(); + fShaderFile.close(); + vertexCode = vShaderStream.str(); + fragmentCode = fShaderStream.str(); + } + catch (std::ifstream::failure e) + { + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl; + } + const char* vShaderCode = vertexCode.c_str(); + const char* fShaderCode = fragmentCode.c_str(); + + unsigned int vertex, fragment; + + vertex = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex, 1, &vShaderCode, NULL); + glCompileShader(vertex); + checkCompileErrors(vertex, "VERTEX"); + + fragment = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment, 1, &fShaderCode, NULL); + glCompileShader(fragment); + checkCompileErrors(fragment, "FRAGMENT"); + + ID = glCreateProgram(); + glAttachShader(ID, vertex); + glAttachShader(ID, fragment); + glLinkProgram(ID); + checkCompileErrors(ID, "PROGRAM"); + + glDeleteShader(vertex); + glDeleteShader(fragment); + } + void use() + { + glUseProgram(ID); + } + // utility uniform functions + // ------------------------------------------------------------------------ + void setBool(const std::string& name, bool value) const + { + glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value); + } + // ------------------------------------------------------------------------ + void setInt(const std::string& name, int value) const + { + glUniform1i(glGetUniformLocation(ID, name.c_str()), value); + } + // ------------------------------------------------------------------------ + void setFloat(const std::string& name, float value) const + { + glUniform1f(glGetUniformLocation(ID, name.c_str()), value); + } + // ------------------------------------------------------------------------ + void setVec2(const std::string& name, const glm::vec2& value) const + { + glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec2(const std::string& name, float x, float y) const + { + glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y); + } + // ------------------------------------------------------------------------ + void setVec3(const std::string& name, const glm::vec3& value) const + { + glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec3(const std::string& name, float x, float y, float z) const + { + glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z); + } + // ------------------------------------------------------------------------ + void setVec4(const std::string& name, const glm::vec4& value) const + { + glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]); + } + void setVec4(const std::string& name, float x, float y, float z, float w) const + { + glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w); + } + // ------------------------------------------------------------------------ + void setMat2(const std::string& name, const glm::mat2& mat) const + { + glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); + } + // ------------------------------------------------------------------------ + void setMat3(const std::string& name, const glm::mat3& mat) const + { + glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); + } + // ------------------------------------------------------------------------ + void setMat4(const std::string& name, const glm::mat4& mat) const + { + glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]); + } + +private: + void checkCompileErrors(unsigned int shader, std::string type) + { + int success; + char infoLog[1024]; + if (type != "PROGRAM") + { + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(shader, 1024, NULL, infoLog); + std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- ---------------------------------------------------- -- " << std::endl; + } + } + else + { + glGetProgramiv(shader, GL_LINK_STATUS, &success); + if (!success) + { + glGetProgramInfoLog(shader, 1024, NULL, infoLog); + std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- ---------------------------------------------------- -- " << std::endl; + } + } + } +}; + +#endif diff --git a/vertex.vert b/vertex.vert new file mode 100644 index 0000000..c6e1603 --- /dev/null +++ b/vertex.vert @@ -0,0 +1,13 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; + +out vec3 ourColor; + +uniform mat4 transform; + +void main() +{ + gl_Position = transform * vec4(aPos, 1.0); + ourColor = aColor; +}