From fb8b5089378b3fd994eb9770aebecb017b0e62c6 Mon Sep 17 00:00:00 2001 From: 4VRDriver <44267643+4VRDriver@users.noreply.github.com> Date: Mon, 31 Aug 2020 14:09:54 +0200 Subject: [PATCH] First triangle --- .vscode/settings.json | 47 +++++++++++++++++++++++++++++- Controller.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++- Controller.h | 15 +++++++++- 3 files changed, 126 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 29fe99b..9029c4a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,51 @@ { "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", "files.associations": { - "ostream": "cpp" + "ostream": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ranges": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" } } \ No newline at end of file diff --git a/Controller.cpp b/Controller.cpp index bda5e4a..a2a0c8b 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -1,5 +1,6 @@ #include #include +#include #ifdef __linux__ #include @@ -30,7 +31,66 @@ Controller::~Controller() { } void Controller::run() { - glClearColor(0.341f, 0.678f, 0.408f, 1.0f); + glClearColor(0.241f, 0.578f, 0.308f, 1.0f); + + unsigned int vertexShader; + vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); + glCompileShader(vertexShader); + + 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; + } + + unsigned int fragmentShader; + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); + glCompileShader(fragmentShader); + glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); + if(!success) { + glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); + std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; + } + + unsigned int shaderProgram; + shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + 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); + + unsigned int VBO, VAO; + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + + glBindVertexArray(VAO); + + float vertices[] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + // This is the game loop while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) { // Timing @@ -41,6 +101,11 @@ void Controller::run() { // Render and buffer swap glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(shaderProgram); + glBindVertexArray(VAO); + glDrawArrays(GL_TRIANGLES, 0, 3); + glfwSwapBuffers(gameWindow->getGLFWwindow()); diff --git a/Controller.h b/Controller.h index 7b4f4f8..fdc2ba0 100644 --- a/Controller.h +++ b/Controller.h @@ -24,6 +24,19 @@ private: const uint16_t MAX_FPS = 60; - double deltaTime; + double deltaTime; + + const char *vertexShaderSource = "#version 330 core\n" + "layout (location = 0) in vec3 aPos;\n" + "void main() {\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" + " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" + "}\0"; };