From 005fceafd96486a8ad2b50324372c8a4e4c783e3 Mon Sep 17 00:00:00 2001 From: Eero Holmala Date: Fri, 28 Apr 2023 17:44:09 +0300 Subject: [PATCH] App: WIP broken --- App/App/CMakeLists.txt | 2 + App/App/include/App.hh | 4 +- App/App/src/App.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++ App/App/src/main.cpp | 1 - 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/App/App/CMakeLists.txt b/App/App/CMakeLists.txt index 62d8b9d..9886560 100644 --- a/App/App/CMakeLists.txt +++ b/App/App/CMakeLists.txt @@ -13,11 +13,13 @@ add_executable(App find_package(SDL2 CONFIG REQUIRED) find_package(OpenGL REQUIRED) +find_package(GLEW REQUIRED) target_link_libraries(App PRIVATE AppLib SDL2::SDL2 SDL2::SDL2main OpenGL::GL + GLEW::GLEW ) target_include_directories(App PUBLIC "${CMAKE_SOURCE_DIR}/App/include") diff --git a/App/App/include/App.hh b/App/App/include/App.hh index c6fa761..3f0f0d7 100644 --- a/App/App/include/App.hh +++ b/App/App/include/App.hh @@ -2,8 +2,8 @@ #include "SDL2/SDL.h" // #include "SDL2/SDL_vulkan.h" -#include -#include +#include "SDL2/SDL_opengl.h" +#include "GL/glew.h" #include "Logger.hh" #include "Controller.hh" diff --git a/App/App/src/App.cpp b/App/App/src/App.cpp index 61f3d00..4c2714f 100644 --- a/App/App/src/App.cpp +++ b/App/App/src/App.cpp @@ -8,10 +8,89 @@ #define PLATFORM_SURFACE_EXTENSION_NAME VK_KHR_WIN32_SURFACE_EXTENSION_NAME #endif + +//TODO (Eero): https://github.com/theandrew168/sdl2-opengl-demo/blob/master/src/main.c namespace App { App::App() : m_ReturnCode(0), m_WindowWidth(800), m_WindowHeight(600), m_Quit(false), m_VSync(false) { + float vertices[] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; + + // printf("OpenGL Vendor: %s\n", glGetString(GL_VENDOR)); + // printf("OpenGL Renderer: %s\n", glGetString(GL_RENDERER)); + // printf("OpenGL Version: %s\n", glGetString(GL_VERSION)); + // printf("GLSL Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); + // Vertex Shader Part + unsigned int VBO; + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + 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"; + + 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; + } + + // Fragment Shader Part + unsigned int fragmentShader; + fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); + glCompileShader(fragmentShader); + + + glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); + if(!success) { + glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); + std::cout << "ERROR::SHADER::FRAGMET::COMPILATION_FAILED\n" << infoLog << std::endl; + } + + unsigned int shaderProgram; + shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + + glUseProgram(shaderProgram); + + // Oh yeah, and don't forget to delete the shader objects once we've linked them into the program object; we no longer need them anymore: + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + + + // // 0. copy our vertices array in a buffer for OpenGL to use + // glBindBuffer(GL_ARRAY_BUFFER, VBO); + // glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + // // 1. then set the vertex attributes pointers + // glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + // glEnableVertexAttribArray(0); + // // 2. use our shader program when we want to render an object + // glUseProgram(shaderProgram); + // // 3. now draw the object + // someOpenGLFunctionThatDrawsOurTriangle(); + std::cout << "Eternity Engine" << std::endl; m_Logger = std::make_shared(); m_Controller = std::make_shared(); @@ -81,6 +160,9 @@ namespace App glClearColor(0.39f, 0.58f, 0.93f, 1.f); // Clear color buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glUseProgram(shaderProgram); + glBindVertexArray(VAO); + // someOpenGLFunctionThatDrawsOurTriangle(); // Update window with OpenGL rendering SDL_GL_SwapWindow(m_Window); } @@ -92,6 +174,9 @@ namespace App App::~App() { + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glDeleteProgram(shaderProgram); SDL_DestroyWindow(m_Window); SDL_Quit(); m_Logger.reset(); diff --git a/App/App/src/main.cpp b/App/App/src/main.cpp index 4a21f8c..fa8d7bb 100644 --- a/App/App/src/main.cpp +++ b/App/App/src/main.cpp @@ -1,5 +1,4 @@ #include "App.hh" -#include "Math.hh" #include