diff --git a/src/Engine/Engine.vcxproj b/src/Engine/Engine.vcxproj index 93be4d1..5f15786 100644 --- a/src/Engine/Engine.vcxproj +++ b/src/Engine/Engine.vcxproj @@ -142,8 +142,12 @@ - - + + Document + + + Document + diff --git a/src/Engine/Engine.vcxproj.filters b/src/Engine/Engine.vcxproj.filters index e12aec4..dda6039 100644 --- a/src/Engine/Engine.vcxproj.filters +++ b/src/Engine/Engine.vcxproj.filters @@ -43,9 +43,9 @@ - + + Resource Files - - + \ No newline at end of file diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index 2c6844e..69f6161 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -64,10 +64,10 @@ int main() glfwSetFramebufferSizeCallback(window, OnWindowResize); GLfloat vertices[] = { - 0.5f, 0.5f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f // top left + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left + -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f // top left }; GLuint faces[]{ 0, 1, 3, @@ -87,8 +87,12 @@ int main() glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(faces), faces, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + // position attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); + // colour (or more general, the second vertex Attribute) + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); // I could unbind the VAO, but this is usually not needed because when creating a new VAO we need to bind that to change it anyway, so there shouldn't be a problem std::string vertexPath = "shaders/default/default.vert"; @@ -104,7 +108,7 @@ int main() std::cerr << e.what(); return -1; } - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // main loop while(!glfwWindowShouldClose(window)) @@ -115,6 +119,10 @@ int main() ProcessInput(window); shader->Use(); + //float time = glfwGetTime(); + //float greenValue = (sin(time) / 2.0f) + 0.5f; + //shader->setFloat("ourColour", 0.0f, greenValue, 0.0f, 0.0f); + glBindVertexArray(vertexArrayObject); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/src/Engine/shaders/Shader.cpp b/src/Engine/shaders/Shader.cpp index bf9298f..72fd98e 100644 --- a/src/Engine/shaders/Shader.cpp +++ b/src/Engine/shaders/Shader.cpp @@ -8,18 +8,22 @@ namespace Nebulix { - - const char* vertexShaderSourceCode = "#version 330 core\n" + // debugging + const char* vertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" + "layout (location = 1) in vec3 aColor;\n" + "out vec3 ourColor;\n" "void main()\n" "{\n" - " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" + " gl_Position = vec4(aPos, 1.0);\n" + " ourColor = aColor;\n" "}\0"; - const char* fragmentShaderSourceCode = "#version 330 core\n" + const char* fragmentShaderSource = "#version 330 core\n" "out vec4 FragColor;\n" + "in vec3 ourColor;\n" "void main()\n" "{\n" - " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" + " FragColor = vec4(ourColor, 1.0f);\n" "}\n\0"; Shader::Shader(std::string& vertexShaderfile, std::string& fragmentShaderfile): _vertexFile{vertexShaderfile}, _fragmentFile{fragmentShaderfile} @@ -33,29 +37,29 @@ namespace Nebulix CreateProgram(vertexShader, fragmentShader); } - void Shader::CompileShadercode(std::ifstream& shaderSource, GLuint& shaderObject, ShaderType type) + void Shader::CompileShadercode(std::ifstream& shaderFile, GLuint& shaderObject, ShaderType type) { - if (!shaderSource.is_open()) + if (!shaderFile.is_open()) { throw IOException("ERROR::SHADER::CANNOT_OPEN_FILE"); } - const char* shader; - - shaderSource.exceptions(std::ifstream::failbit | std::ifstream::badbit); + std::string code; + const char* shaderCode; + + shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); try { - std::stringstream sourceCode; - sourceCode << shaderSource.rdbuf(); - shaderSource.close(); - std::string code = sourceCode.str(); - shader = code.c_str(); + std::stringstream shaderStream; + shaderStream << shaderFile.rdbuf(); + shaderFile.close(); + code = shaderStream.str(); } catch (const std::exception&) { throw Nebulix::IOException("ERROR::SHADER::READ_FILE"); } - + shaderCode = code.c_str(); if (type == ShaderType::Vertex) shaderObject = glCreateShader(GL_VERTEX_SHADER); @@ -64,7 +68,7 @@ namespace Nebulix else throw std::runtime_error("ERROR::SHADER::TYPE \nCannot create shader program with the given shader type '" + std::to_string((int)type) + "'"); - glShaderSource(shaderObject, 1, &shader, NULL); + glShaderSource(shaderObject, 1, &shaderCode, NULL); glCompileShader(shaderObject); int success; diff --git a/src/Engine/shaders/Shader.h b/src/Engine/shaders/Shader.h index dbb4c0f..ffe3b20 100644 --- a/src/Engine/shaders/Shader.h +++ b/src/Engine/shaders/Shader.h @@ -31,9 +31,21 @@ namespace Nebulix { glUniform1i(glGetUniformLocation(shaderId, name.c_str()), value); } - void setFloat(const std::string& name, float value) const + void setFloat(const std::string& name, float x) const { - glUniform1f(glGetUniformLocation(shaderId, name.c_str()), value); + glUniform1f(glGetUniformLocation(shaderId, name.c_str()), x); + } + void setFloat(const std::string& name, float x, float y) const + { + glUniform2f(glGetUniformLocation(shaderId, name.c_str()), x, y); + } + void setFloat(const std::string& name, float x, float y, float z) const + { + glUniform3f(glGetUniformLocation(shaderId, name.c_str()), x, y, z); + } + void setFloat(const std::string& name, float x, float y, float z, float w) const + { + glUniform4f(glGetUniformLocation(shaderId, name.c_str()), x, y, z, w); } private: diff --git a/src/Engine/shaders/default/default.frag b/src/Engine/shaders/default/default.frag index 2ccfb50..35a5ffc 100644 --- a/src/Engine/shaders/default/default.frag +++ b/src/Engine/shaders/default/default.frag @@ -1,7 +1,9 @@ #version 330 core out vec4 FragColor; +in vec3 ourColour; + void main() { - FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + FragColor = vec4(ourColour, 1.0); } \ No newline at end of file diff --git a/src/Engine/shaders/default/default.vert b/src/Engine/shaders/default/default.vert index d7bd59a..67ddd66 100644 --- a/src/Engine/shaders/default/default.vert +++ b/src/Engine/shaders/default/default.vert @@ -1,7 +1,11 @@ #version 330 core layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColour; + +out vec3 ourColour; void main() { - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + gl_Position = vec4(aPos, 1.0); + ourColour = aColour; }