diff --git a/CMakeLists.txt b/CMakeLists.txt index 550fb89..ca62ae7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 17) find_package(OpenGL REQUIRED) find_package(glfw3 REQUIRED) -add_executable(ecsgame glad.c main.cpp Shader.cpp Shader.h) +add_executable(ecsgame glad.c Shader.cpp Shader.h main.cpp) include_directories(${OPENGL_INCLUDE_DIRS}) diff --git a/Shader.h b/Shader.h index b99a9bb..fbc8954 100644 --- a/Shader.h +++ b/Shader.h @@ -9,20 +9,23 @@ class Shader { public: - // the program ID + /// Program ID unsigned int ID; - // constructor reads and builds the shader + /// Read and build the shader from the files at vertexPath and fragmentPath. + /// Note that an OpenGL context has to be initialized before calling this! Otherwise a SIGSEGV will be thrown. Shader(const char *vertexPath, const char *fragmentPath); - // use/activate the shader + /// Activate the shader - usually called before rendering. void use(); - // utility uniform functions + /// Set a uniform boolean in the shader void setBool(const std::string &name, bool value) const; + /// Set a uniform int in the shader void setInt(const std::string &name, int value) const; + /// Set a uniform float in the shader void setFloat(const std::string &name, float value) const; private: diff --git a/Shaders/default-fragment.fs b/Shaders/default-fragment.fs new file mode 100644 index 0000000..98fbdbe --- /dev/null +++ b/Shaders/default-fragment.fs @@ -0,0 +1,8 @@ +#version 320 es +out mediump vec4 FragColor; +in mediump vec3 COLOR; + +void main() +{ + FragColor = vec4(COLOR, 1.0); +} \ No newline at end of file diff --git a/Shaders/default-vertex.vs b/Shaders/default-vertex.vs new file mode 100644 index 0000000..7b1b9df --- /dev/null +++ b/Shaders/default-vertex.vs @@ -0,0 +1,11 @@ +#version 320 es +layout (location = 0) in vec3 aPos; // the position variable has attribute position 0 +layout (location = 1) in vec3 aColor; // the color variable has attribute position 1 + +out vec3 COLOR; // output a color to the fragment shader + +void main() +{ + gl_Position = vec4(aPos, 1.0); + COLOR = aColor; +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index b659fb0..8792ed4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,10 @@ -#include -#include "ECS.h" #include #include -#include + +#include + +#include "Shader.h" +#include "ECS.h" using namespace ECS; @@ -134,19 +136,6 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action, world->emit({key, action}); } -const char *vertexShaderSource = "#version 330 core\n" - "layout (location = 0) in vec3 aPos;\n" - "void main()\n" - "{\n" - " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" - "}\0"; -const char *fragmentShaderSource = "#version 330 core\n" - "out vec4 FragColor;\n" - "void main()\n" - "{\n" - " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" - "}\n\0"; - int main() { world->registerSystem(new GravitySystem(-9.8f)); world->registerSystem(new PositionDebugOutputSystem()); @@ -184,57 +173,18 @@ int main() { return -1; } - // build and compile our shader program - // ------------------------------------ - // vertex shader - int vertexShader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); - glCompileShader(vertexShader); - // check for shader compile errors - int success; - char infoLog[512]; - glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; - } - // fragment shader - int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); - glCompileShader(fragmentShader); - // check for shader compile errors - glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; - } - // link shaders - int shaderProgram = glCreateProgram(); - glAttachShader(shaderProgram, vertexShader); - glAttachShader(shaderProgram, fragmentShader); - glLinkProgram(shaderProgram); - // check for linking errors - glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); - if (!success) { - glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; - } - glDeleteShader(vertexShader); - glDeleteShader(fragmentShader); + Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ float 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 + // positions // colors + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top }; unsigned int indices[] = { // note that we start from 0! - 0, 1, 3, // first Triangle - 1, 2, 3 // second Triangle + 0, 1, 2 // first Triangle }; unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); @@ -249,7 +199,13 @@ int main() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, 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); + + // color attribute + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float))); + glEnableVertexAttribArray(1); glEnableVertexAttribArray(0); // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind @@ -283,10 +239,9 @@ int main() { glClear(GL_COLOR_BUFFER_BIT); // draw our first triangle - glUseProgram(shaderProgram); + defaultShader.use(); glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized - //glDrawArrays(GL_TRIANGLES, 0, 6); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES, 0, 3); // glBindVertexArray(0); // no need to unbind it every time /* Swap front and back buffers */