diff --git a/old/entry.cpp b/old/entry.cpp deleted file mode 100644 index 1b120d7..0000000 --- a/old/entry.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#define SDL_MAIN_HANDLED -#include "SDL2/SDL.h" - -#ifdef __WIN32__ -// FIXME: OpenGL Headers - -constexpr Uint32 window_flags = SDL_WINDOW_OPENGL; - -#elif __APPLE__ - #include "SDL2/SDL_metal.h" - - #define NS_PRIVATE_IMPLEMENTATION - #define CA_PRIVATE_IMPLEMENTATION - #define MTL_PRIVATE_IMPLEMENTATION - - #include "Foundation/Foundation.hpp" - #include "Metal/Metal.hpp" - #include "QuartzCore/QuartzCore.hpp" - #include "metal/renderer.hpp" - -constexpr Uint32 window_flags = SDL_WINDOW_METAL; - -#endif - -int main() -{ - if (SDL_InitSubSystem(SDL_INIT_TIMER | SDL_INIT_VIDEO) < 0) - { - SDL_Log("%s", SDL_GetError()); - return -1; - } - - SDL_Window* window = - SDL_CreateWindow("Visualizer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); - - if (!window) - { - SDL_Log("%s", SDL_GetError()); - SDL_Quit(); - return -1; - } - -#ifdef __WIN32__ - // FIXME: OpenGL Context - -#elif __APPLE__ - - SDL_MetalView metal_view = SDL_Metal_CreateView(window); - - CA::MetalLayer* Layer = (CA::MetalLayer*)SDL_Metal_GetLayer(metal_view); - - Layer->setDevice(MTL::CreateSystemDefaultDevice()); - - int w, h; - SDL_Metal_GetDrawableSize(window, &w, &h); - - MTL::Device* GPU = Layer->device(); - - MTL::CommandQueue* Queue = GPU->newCommandQueue(); - - CA::MetalDrawable* drawable{}; - - MTL::RenderPassDescriptor* rpDescriptor{ MTL::RenderPassDescriptor::alloc()->init() }; - - Pipeline pipeline; - - pipeline.LoadShader(GPU); - pipeline.LoadBuffers(GPU); - -#endif - - SDL_bool state{ SDL_TRUE }; - - SDL_Event evnt{}; - while (state) - { - while (SDL_PollEvent(&evnt)) - { - if (evnt.type == SDL_QUIT || evnt.key.keysym.scancode == SDL_SCANCODE_ESCAPE) - state = SDL_FALSE; - } -#ifdef __WIN32__ - // Rendering - } -#elif __APPLE__ - - drawable = Layer->nextDrawable(); - - rpDescriptor->colorAttachments()->object(0)->setTexture(drawable->texture()); - - MTL::CommandBuffer* cmdBuffer = Queue->commandBuffer(); - - MTL::RenderCommandEncoder* cmdEncoder = cmdBuffer->renderCommandEncoder(rpDescriptor); - - cmdEncoder->setRenderPipelineState(pipeline.GetRenderPipeline()); - - cmdEncoder->setVertexBuffer(pipeline.Position, 0, 0); - cmdEncoder->setVertexBuffer(pipeline.Color, 0, 1); - - cmdEncoder->drawPrimitives(MTL::PrimitiveType::PrimitiveTypeTriangle, NS::UInteger(0), NS::UInteger(3)); - - cmdEncoder->endEncoding(); - cmdBuffer->presentDrawable(drawable); - cmdBuffer->commit(); - } - SDL_Metal_DestroyView(metal_view); - -#endif - - SDL_DestroyWindow(window); - SDL_Quit(); -} diff --git a/old/metal/CMakeLists.txt b/old/metal/CMakeLists.txt deleted file mode 100644 index 290c333..0000000 --- a/old/metal/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -find_package(SDL2 CONFIG REQUIRED) - -if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/metal-cpp) - file( - DOWNLOAD https://developer.apple.com/metal/cpp/files/metal-cpp_macOS14.2_iOS17.2.zip - ${CMAKE_CURRENT_SOURCE_DIR}/metal-cpp_macOS14.2_iOS17.2.zip SHOW_PROGRESS - ) - message("-- Downloaded metal-cpp") - execute_process( - COMMAND ${CMAKE_COMMAND} -E tar x metal-cpp_macOS14.2_iOS17.2.zip - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - ) - execute_process( - COMMAND ${CMAKE_COMMAND} -E rm metal-cpp_macOS14.2_iOS17.2.zip - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - ) -endif() - -add_library(metal-build STATIC renderer.cpp) - -target_include_directories(metal-build PUBLIC metal-cpp) -target_include_directories(metal-build PUBLIC ${SDL2_INCLUDE_DIRS}) - -target_compile_features(metal-build PUBLIC cxx_std_17) - -target_link_libraries(metal-build PUBLIC SDL2::SDL2 - "-framework Metal" - "-framework MetalFX" - "-framework Foundation" - "-framework QuartzCore" -) diff --git a/old/metal/renderer.cpp b/old/metal/renderer.cpp deleted file mode 100644 index b327e53..0000000 --- a/old/metal/renderer.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "renderer.hpp" -#include "SDL2/SDL_log.h" -#include -#include - -namespace -{ - std::string ShaderContent{}; - - void read_contents(std::string name) - { - std::ifstream filename{ name }; - - if (!filename.is_open()) - { - SDL_Log("Failed to open file"); - return; - } - - std::string line; - while (std::getline(filename, line)) - { - ShaderContent += line + "\n"; - } - - filename.close(); - } -} // namespace - -void Pipeline::LoadShader(MTL::Device* gpu_device) -{ - NS::Error* error{}; - - read_contents("shader.metal"); - - MTL::Library* shaders = gpu_device->newLibrary( - NS::String::string(ShaderContent.c_str(), NS::StringEncoding::UTF8StringEncoding), nullptr, &error); - - if (!shaders) - { - SDL_Log("%s", error->localizedDescription()->utf8String()); - assert(false); - } - - // vertex shader - MTL::Function* vertexFunc = shaders->newFunction(NS::String::string("VertexFunc", NS::UTF8StringEncoding)); - - // fragment shader - MTL::Function* fragmentFunc = shaders->newFunction(NS::String::string("FragmentFunc", NS::UTF8StringEncoding)); - - MTL::RenderPipelineDescriptor* rPipelineDescriptor{ MTL::RenderPipelineDescriptor::alloc()->init() }; - - rPipelineDescriptor->setLabel(NS::String::string("test", NS::UTF8StringEncoding)); - rPipelineDescriptor->setVertexFunction(vertexFunc); - rPipelineDescriptor->setFragmentFunction(fragmentFunc); - rPipelineDescriptor->colorAttachments()->object(0)->setPixelFormat(MTL::PixelFormat::PixelFormatBGRA8Unorm_sRGB); - - this->custom_pipeline = gpu_device->newRenderPipelineState(rPipelineDescriptor, &error); - - if (!this->custom_pipeline) - { - SDL_Log("%s", error->localizedDescription()->utf8String()); - assert(false); - } - - shaders->release(); - vertexFunc->release(); - fragmentFunc->retain(); - - rPipelineDescriptor->release(); -} - -typedef struct -{ - float x, y, z; -} float3; - -void Pipeline::LoadBuffers(MTL::Device* gpu_device) -{ - int num_of_vertices = 3; - - const float position[] = { 0.0f, 0.25f, 0.0f, 0.0f, -0.25f, -0.25f, 0.0f, 0.0f, 0.25f, -0.25f, 0.0f, 0.0f }; - - const float colors[] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f }; - - uint possize = sizeof(position); - uint colsize = sizeof(colors); - - MTL::Buffer* BufferOne = gpu_device->newBuffer(possize, MTL::ResourceStorageModeManaged); - MTL::Buffer* BufferTwo = gpu_device->newBuffer(colsize, MTL::ResourceStorageModeManaged); - - memcpy(BufferOne->contents(), position, possize); - memcpy(BufferTwo->contents(), colors, colsize); - - BufferOne->didModifyRange(NS::Range::Make(0, BufferOne->length())); - BufferTwo->didModifyRange(NS::Range::Make(0, BufferTwo->length())); - - this->Position = BufferOne; - this->Color = BufferTwo; - - this->tVertices = num_of_vertices; -} diff --git a/old/metal/renderer.hpp b/old/metal/renderer.hpp deleted file mode 100644 index f3f61c2..0000000 --- a/old/metal/renderer.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include - -class Pipeline -{ -public: - inline MTL::RenderPipelineState* GetRenderPipeline() { return this->custom_pipeline; } - - void LoadShader(MTL::Device* gpu_device); - void LoadBuffers(MTL::Device* gpu_device); - - MTL::Buffer* Position{}; - MTL::Buffer* Color{}; - int tVertices{}; - -private: - MTL::RenderPipelineState* custom_pipeline; -}; - -class Renderer -{ -}; diff --git a/old/opengl/Application.cpp b/old/opengl/Application.cpp deleted file mode 100644 index eb08a41..0000000 --- a/old/opengl/Application.cpp +++ /dev/null @@ -1,329 +0,0 @@ -#include "../Header/Application.h" -#include - -Application::Application() - : window(nullptr) - , gContext(nullptr) - , shader(nullptr) - , startTime(0.0f) - , endTime(0.0f) - , model(glm::mat4(1.0f)) // Identity matrix -{ - cube = new Cube(); -} - -Application::~Application() -{ - close(); -} - -bool Application::initSDL() -{ - // Initialization flag - - if (SDL_Init(SDL_INIT_VIDEO) < 0) - { - printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); - sdlSuccess = false; - } - else - { - // Use OpenGL 3.1 core - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - - window = SDL_CreateWindow("Visualizer-SDL2OpenGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); - - if (window == NULL) - { - printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); - sdlSuccess = false; - } - else - { - // Create context - gContext = SDL_GL_CreateContext(window); - if (gContext == NULL) - { - printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError()); - sdlSuccess = false; - } - else - { - // Initialize GLEW - glewExperimental = GL_TRUE; - glewError = glewInit(); - if (glewError != GLEW_OK) - { - printf("Error initializing GLEW! %s\n", glewGetErrorString(glewError)); - } - - // Use Vsync - if (SDL_GL_SetSwapInterval(1) < 0) - { - printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError()); - } - - // Initialize OpenGL - shader = new Shader("Resources/Shader.vert", "Resources/Shader.frag"); - if (!shader || shader->getShader() == 0) - { - printf("Failed to create Shader!\n"); - sdlSuccess = false; - } - if (!initGL()) - { - printf("Unable to initialize OpenGL!\n"); - } - // Initialize ImGui - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); - (void)io; - ImGui_ImplSDL2_InitForOpenGL(window, gContext); - ImGui_ImplOpenGL3_Init("#version 330"); - } - } - } - return sdlSuccess; -} - -bool Application::initGL() -{ - glEnable(GL_DEPTH_TEST); - glDisable(GL_BLEND); - glDisable(GL_CULL_FACE); - // glCullFace(GL_BACK); // Cull back faces - glFrontFace(GL_CCW); // Counter-clockwise winding considered front - - // Initialize the shader - shader->use(); - gProgramID = shader->getShader(); - - // Get attribute and uniform locations - modelLoc = glGetUniformLocation(gProgramID, "model"); - viewLoc = glGetUniformLocation(gProgramID, "view"); - projectionLoc = glGetUniformLocation(gProgramID, "projection"); - - // Initialize clear color - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - - // Set the projection and view matrices - projection = glm::perspective(glm::radians(45.0f), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f); - - shader->use(); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - - view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.0f)); - - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - shader->use(); - - cube->initialize(); - - return true; -} - -void Application::run() -{ - if (!initSDL()) - { - printf("Failed to initialize!\n"); - } - else - { - // Deltatime calci - startTime = static_cast(SDL_GetTicks()); - - // Enable text input - SDL_StartTextInput(); - - // While application is running - while (isWindowOpen) - { - endTime = static_cast(SDL_GetTicks()); - float deltTime = endTime - startTime; - startTime = endTime; - - // Handle events on queue - handleEvents(); - - // Updates - update(deltTime); - - // Render cube - render(); - - // Update screen - SDL_GL_SwapWindow(window); - } - - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplSDL2_Shutdown(); - ImGui::DestroyContext(); - - // Disable text input - SDL_StopTextInput(); - } - - // Free resources and close SDL - close(); -} - -void Application::handleEvents() -{ - SDL_Event e; - while (SDL_PollEvent(&e) != 0) - { - ImGui_ImplSDL2_ProcessEvent(&e); - - // User requests quit - if (e.type == SDL_QUIT) - { - isWindowOpen = false; - } - // Handle keypress - else if (e.type == SDL_TEXTINPUT) - { - if (e.text.text[0] == 'q') - { - gRenderCube = !gRenderCube; - } - } - } -} - -void Application::update(float deltaTime) -{ - // Calculate time elapsed - accumulatedTime += deltaTime; - float time = accumulatedTime / 1000.0f; - - // Cube update - cube->update(deltaTime); - - // Update the shader uniform - shader->use(); -} - -void Application::render() -{ - // Clear color and depth buffer - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - // Render cube - if (gRenderCube) - { - shader->use(); - - cube->render(gProgramID); - - shader->use(); - } - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplSDL2_NewFrame(); - ImGui::NewFrame(); - - handleImGui(); - - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); -} - -void Application::close() -{ - // Deallocate program - glDeleteProgram(gProgramID); - - if (shader) - { - delete shader; - shader = nullptr; - } - if (cube) - { - delete cube; - cube = nullptr; - } - - // Destroy window - SDL_DestroyWindow(window); - window = NULL; - - // Quit SDL subsystems - SDL_Quit(); -} - -void Application::initImGui() -{ -} - -void Application::handleImGui() -{ - if (ImGui::Begin("Shader Loader")) - { - if (ImGui::Button("Load Vertex Shader")) - { - ImGuiFileDialog::Instance()->OpenDialog("ChooseVertexShader", "Choose Vertex Shader", ".vert,.glsl,.txt"); - } - - if (ImGui::Button("Load Fragment Shader")) - { - ImGuiFileDialog::Instance()->OpenDialog("ChooseFragmentShader", "Choose Fragment Shader", - ".frag,.glsl,.txt"); - } - - if (ImGuiFileDialog::Instance()->Display("ChooseVertexShader")) - { - if (ImGuiFileDialog::Instance()->IsOk()) - { - std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName(); - strcpy_s(vertexPathAdd, 256, filePathName.c_str()); - } - ImGuiFileDialog::Instance()->Close(); - } - - if (ImGuiFileDialog::Instance()->Display("ChooseFragmentShader")) - { - if (ImGuiFileDialog::Instance()->IsOk()) - { - std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName(); - strcpy_s(fragmentPathAdd, 256, filePathName.c_str()); - } - ImGuiFileDialog::Instance()->Close(); - } - - if (ImGui::Button("Load Shaders")) - { - if (strlen(vertexPathAdd) > 0 && strlen(fragmentPathAdd) > 0) - { - fileSelectSucceed = true; - - shader = new Shader(std::filesystem::relative(vertexPathAdd).string(), - std::filesystem::relative(fragmentPathAdd).string()); - - if (!shader || shader->getShader() == 0) - { - printf("Failed to create Shader!\n"); - sdlSuccess = false; - } - else - { - printf("Shader created SuccessFully!\n"); - } - if (!initGL()) - { - printf("Unable to initialize OpenGL!\n"); - } - } - else - { - std::cerr << "Please select both vertex and fragment shader files." << std::endl; - } - } - - ImGui::End(); - } -} diff --git a/old/opengl/Application.h b/old/opengl/Application.h deleted file mode 100644 index 5f0f002..0000000 --- a/old/opengl/Application.h +++ /dev/null @@ -1,87 +0,0 @@ -#pragma once - -#include -#include -#include - -// IMGUI -#include "ImGuiFileDialog.h" -#include "imgui.h" -#include "imgui_impl_opengl3.h" -#include "imgui_impl_sdl2.h" - -// Using SDL, SDL OpenGL, GLEW, standard IO, and strings -#include -#undef main -#include "Cube.h" -#include "Shader.h" -#include -#include -#include -#include -#include -#include - -// Screen dimension constants -const int SCREEN_WIDTH = 1200; -const int SCREEN_HEIGHT = 800; - -class Application -{ -public: - Application(); - ~Application(); - void run(); - char* vertexPathAdd = new char[256]{ "" }; - char* fragmentPathAdd = new char[256]{ "" }; - -private: - bool initSDL(); - bool initGL(); - void handleEvents(); - void update(float deltaTime); - void render(); - void close(); - - // IMGUI - void initImGui(); - void handleImGui(); - - float accumulatedTime = 0.0f; - - bool sdlSuccess = true; - bool openGLSuccess = true; - bool isWindowOpen = true; - bool fileSelectSucceed = false; - SDL_Window* window; - SDL_GLContext gContext; - - GLenum glewError; - - // Graphics program - GLuint gProgramID = 0; - - // SHADER - Shader* shader; - - Cube* cube; - - // uniform locations - GLint modelLoc; - GLint viewLoc; - GLint projectionLoc; - - // projection matrix - glm::mat4 projection; - - // Set the view matrix - glm::mat4 view; - - // Set model matrix (rotation) - glm::mat4 model; - - bool gRenderCube = true; - - float startTime; - float endTime; -}; diff --git a/old/opengl/CMakeLists.txt b/old/opengl/CMakeLists.txt deleted file mode 100644 index 4e34392..0000000 --- a/old/opengl/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -# OpenGL dependencies -find_package(GLEW CONFIG REQUIRED) -find_package(SDL2 CONFIG REQUIRED) -find_package(imgui CONFIG REQUIRED) -find_package(glm CONFIG REQUIRED) - -add_library(GLBuild STATIC - Application.cpp - Shader.cpp - Cube.cpp -) - -target_compile_features(GLBuild PRIVATE cxx_std_17) - -target_link_libraries(GLBuild PUBLIC GLEW::glew SDL2::SDL2 glm::glm-header-only imgui::imgui) diff --git a/old/opengl/Cube.cpp b/old/opengl/Cube.cpp deleted file mode 100644 index f34bbd6..0000000 --- a/old/opengl/Cube.cpp +++ /dev/null @@ -1,284 +0,0 @@ -#include "../Header/Cube.h" - -Cube::Cube() - : vboID(0), iboID(0), position(0.0f), scale(1.0f), rotationAngle(0.0f), axis(1.0f), model(glm::mat4(1.0f)) -// Initialize vertex and index data -{ - // Color definitions - GLfloat redColor[] = { 1.0f, 0.0f, 0.0f }; - GLfloat greenColor[] = { 0.0f, 1.0f, 0.0f }; - GLfloat blueColor[] = { 0.0f, 0.0f, 1.0f }; - GLfloat yellowColor[] = { 1.0f, 1.0f, 0.0f }; - GLfloat cyanColor[] = { 0.0f, 1.0f, 1.0f }; - GLfloat magentaColor[] = { 1.0f, 0.0f, 1.0f }; - // VBO data for a cube - vertexData = { - // Front face (Blue) - -0.5f, - -0.5f, - 0.5f, - blueColor[0], - blueColor[1], - blueColor[2], - 0.5f, - -0.5f, - 0.5f, - blueColor[0], - blueColor[1], - blueColor[2], - 0.5f, - 0.5f, - 0.5f, - blueColor[0], - blueColor[1], - blueColor[2], - -0.5f, - 0.5f, - 0.5f, - blueColor[0], - blueColor[1], - blueColor[2], - - // Back face (Red) - -0.5f, - -0.5f, - -0.5f, - redColor[0], - redColor[1], - redColor[2], - 0.5f, - -0.5f, - -0.5f, - redColor[0], - redColor[1], - redColor[2], - 0.5f, - 0.5f, - -0.5f, - redColor[0], - redColor[1], - redColor[2], - -0.5f, - 0.5f, - -0.5f, - redColor[0], - redColor[1], - redColor[2], - - // Left face (Green) - -0.5f, - -0.5f, - -0.5f, - greenColor[0], - greenColor[1], - greenColor[2], - -0.5f, - -0.5f, - 0.5f, - greenColor[0], - greenColor[1], - greenColor[2], - -0.5f, - 0.5f, - 0.5f, - greenColor[0], - greenColor[1], - greenColor[2], - -0.5f, - 0.5f, - -0.5f, - greenColor[0], - greenColor[1], - greenColor[2], - - // Right face (Cyan) - 0.5f, - -0.5f, - -0.5f, - cyanColor[0], - cyanColor[1], - cyanColor[2], - 0.5f, - -0.5f, - 0.5f, - cyanColor[0], - cyanColor[1], - cyanColor[2], - 0.5f, - 0.5f, - 0.5f, - cyanColor[0], - cyanColor[1], - cyanColor[2], - 0.5f, - 0.5f, - -0.5f, - cyanColor[0], - cyanColor[1], - cyanColor[2], - - // Top face (Yellow) - -0.5f, - 0.5f, - -0.5f, - yellowColor[0], - yellowColor[1], - yellowColor[2], - 0.5f, - 0.5f, - -0.5f, - yellowColor[0], - yellowColor[1], - yellowColor[2], - 0.5f, - 0.5f, - 0.5f, - yellowColor[0], - yellowColor[1], - yellowColor[2], - -0.5f, - 0.5f, - 0.5f, - yellowColor[0], - yellowColor[1], - yellowColor[2], - - // Bottom face (Magenta) - -0.5f, - -0.5f, - -0.5f, - magentaColor[0], - magentaColor[1], - magentaColor[2], - 0.5f, - -0.5f, - -0.5f, - magentaColor[0], - magentaColor[1], - magentaColor[2], - 0.5f, - -0.5f, - 0.5f, - magentaColor[0], - magentaColor[1], - magentaColor[2], - -0.5f, - -0.5f, - 0.5f, - magentaColor[0], - magentaColor[1], - magentaColor[2], - }; - - // IBO data for a cube - indexData = { - 0, 1, 2, // first triangle - 2, 3, 0, // second triangle - - // top - 4, 5, 6, // first triangle - 6, 7, 4, // second triangle - - // left - 8, 9, 10, // first triangle - 10, 11, 8, // second triangle - - // right - 14, 13, 12, // 12, 13, 14, // first triangle - 12, 15, 14, // 14, 15, 12, // second triangle - - // back - 18, 17, 16, // 16, 17, 18, // first triangle - 16, 19, 18, // 18, 19, 16, // second triangle - - // bottom - 20, 21, 22, // first triangle - 22, 23, 20 // second triangle - }; -} -Cube::~Cube() -{ - glDeleteBuffers(1, &vboID); - glDeleteBuffers(1, &iboID); -} - -void Cube::initialize() -{ - // Generate and bind VBO - glGenBuffers(1, &vboID); - glBindBuffer(GL_ARRAY_BUFFER, vboID); - glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(GLfloat), vertexData.data(), GL_STATIC_DRAW); - - // Generate and bind IBO - glGenBuffers(1, &iboID); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.size() * sizeof(GLuint), indexData.data(), GL_STATIC_DRAW); -} - -void Cube::update(float deltaTime) -{ - accumulatedTime += deltaTime; - // float timeElapsed = accumulatedTime / 1000.0f; - - // Does scaling, translation, rotation, & axis change lerp based on time - applySimpleTransformation(deltaTime); - - model = glm::translate(glm::mat4(0.1f), position) * glm::scale(glm::mat4(0.1f), scale) * - glm::rotate(glm::mat4(0.1f), rotationAngle, axis); -} - -void Cube::render(GLuint gProgramID) -{ - glUseProgram(gProgramID); - - // Bind VBO and IBO - glBindBuffer(GL_ARRAY_BUFFER, vboID); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID); - - // Set vertex attribute pointers - gVertexPos3DLocation = glGetAttribLocation(gProgramID, "LVertexPos3D"); - vertexColorLocation = glGetAttribLocation(gProgramID, "LVertexColor"); - - glEnableVertexAttribArray(gVertexPos3DLocation); - glVertexAttribPointer(gVertexPos3DLocation, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); - - glEnableVertexAttribArray(vertexColorLocation); - glVertexAttribPointer(vertexColorLocation, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), - (GLvoid*)(3 * sizeof(GLfloat))); - - // Set the model matrix uniform - glUniformMatrix4fv(glGetUniformLocation(gProgramID, "model"), 1, GL_FALSE, glm::value_ptr(model)); - glDrawElements(GL_TRIANGLES, static_cast(indexData.size()), GL_UNSIGNED_INT, 0); - - glDisableVertexAttribArray(gVertexPos3DLocation); - glDisableVertexAttribArray(vertexColorLocation); -} - -void Cube::setPosition(const glm::vec3 pos) -{ - position = pos; -} - -void Cube::setScale(const glm::vec3 scale) -{ - this->scale = scale; -} - -void Cube::setRotationAngle(float rotationAngle) -{ - this->rotationAngle = rotationAngle; -} - -void Cube::setAxis(const glm::vec3 axis) -{ - this->axis = axis; -} - -void Cube::applySimpleTransformation(float deltaTime) -{ - float time = accumulatedTime / 1000.0f; - setAxis(glm::vec3(sin(time), cos(time), 0.0f)); - setPosition(glm::vec3(2.0f * cos(time), sin(2.0f * time), cos(2.0f * time))); - setScale(glm::vec3(sin((time) + 1.0f))); - setRotationAngle(time); -} diff --git a/old/opengl/Cube.h b/old/opengl/Cube.h deleted file mode 100644 index 548e07b..0000000 --- a/old/opengl/Cube.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -class Cube -{ -public: - Cube(); - ~Cube(); - - void initialize(); - void update(float deltaTime); - void render(GLuint ProgramID); - - void setPosition(const glm::vec3 pos); - void setScale(const glm::vec3 scale); - void setRotationAngle(float rotationAngle); - void setAxis(const glm::vec3 axis); - - // Does scaling, translation, rotation, & axis change lerp based on time - void applySimpleTransformation(float deltaTime); - -private: - GLuint vboID; - GLuint iboID; - GLint vertexColorLocation; - GLint gVertexPos3DLocation = -1; - - std::vector vertexData; - std::vector indexData; - - glm::mat4 model; - glm::vec3 position; - glm::vec3 scale; - float rotationAngle; - glm::vec3 axis; - - float accumulatedTime = 0.0f; -}; diff --git a/old/opengl/Shader.cpp b/old/opengl/Shader.cpp deleted file mode 100644 index e4234a8..0000000 --- a/old/opengl/Shader.cpp +++ /dev/null @@ -1,145 +0,0 @@ -#include "../Header/Shader.h" - -Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) -{ - // Get vertex source - vertexFilename = vertexPath; - vertexFileData = readFile(vertexFilename); - vertexSource = vertexFileData.c_str(); - - // Get fragment source - fragmentFilename = fragmentPath; - fragmentFileData = readFile(fragmentFilename); - fragmentSource = fragmentFileData.c_str(); - - vertexShaderID = compileShader(vertexSource, GL_VERTEX_SHADER); - fragmentShaderID = compileShader(fragmentSource, GL_FRAGMENT_SHADER); - - shaderProgram = linkShaders(vertexShaderID, fragmentShaderID); -} - -Shader::~Shader() -{ - glDeleteShader(vertexShaderID); - glDeleteShader(fragmentShaderID); - glDeleteProgram(shaderProgram); -} - -void Shader::use() -{ - glUseProgram(shaderProgram); -} - -GLuint Shader::getShader() const -{ - return shaderProgram; -} - -std::string Shader::readFile(const std::string& filePath) -{ - std::ifstream file(filePath); - if (!file.is_open()) - { - std::cerr << "Failed to open file: " << filePath << std::endl; - return ""; - } - std::stringstream buffer; - buffer << file.rdbuf(); - return buffer.str(); -} - -GLuint Shader::compileShader(const char* source, GLuint shaderID) -{ - GLint shader; - shader = glCreateShader(shaderID); - - const char* shaderSource = source; - glShaderSource(shader, 1, &shaderSource, NULL); - glCompileShader(shader); - - GLint success; - char infoLog[512]; - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - - switch (shaderID) - { - case GL_VERTEX_SHADER: - if (!success) - { - glGetShaderInfoLog(shader, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; - } - break; - case GL_FRAGMENT_SHADER: - if (!success) - { - glGetShaderInfoLog(shader, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; - } - break; - } - - return shader; -} - -GLuint Shader::linkShaders(GLuint vertexShaderID, GLuint fragmentShaderID) -{ - GLuint shaderProgram; - shaderProgram = glCreateProgram(); - glAttachShader(shaderProgram, vertexShaderID); - glAttachShader(shaderProgram, fragmentShaderID); - glLinkProgram(shaderProgram); - - GLint success; - char infoLog[512]; - - glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); - if (!success) - { - glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::VERTEX::LINKING_FAILED\n" << infoLog << std::endl; - } - return shaderProgram; -} - -void Shader::printShaderLog(GLuint shader) -{ - if (glIsShader(shader)) - { - int infoLogLength = 0; - int maxLength = infoLogLength; - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); - char* infoLog = new char[maxLength]; - glGetShaderInfoLog(shader, maxLength, &infoLogLength, infoLog); - if (infoLogLength > 0) - { - std::cout << infoLog << std::endl; - } - delete[] infoLog; - } - else - { - std::cout << "Name " << shader << " is not a shader" << std::endl; - } -} - -void Shader::printProgramLog(GLuint program) -{ - if (glIsProgram(program)) - { - int infoLogLength = 0; - int maxLength = infoLogLength; - glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); - char* infoLog = new char[maxLength]; - glGetProgramInfoLog(program, maxLength, &infoLogLength, infoLog); - if (infoLogLength > 0) - { - std::cout << infoLog << std::endl; - } - delete[] infoLog; - } - else - { - std::cout << "Name " << program << " is not a program" << std::endl; - } -} diff --git a/old/opengl/Shader.h b/old/opengl/Shader.h deleted file mode 100644 index 3143486..0000000 --- a/old/opengl/Shader.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -class Shader -{ -public: - Shader(const std::string& vertexPath, const std::string& fragmentPath); - ~Shader(); - - void use(); - GLuint getShader() const; - -private: - // unsigned int vertexShaderID; - // unsigned int fragmentShaderID; - GLuint shaderProgram; - - // Vertext Shader - GLuint vertexShaderID; - std::string vertexFilename; - std::string vertexFileData; - const char* vertexSource; - - // Fragment Shader - GLuint fragmentShaderID; - std::string fragmentFilename; - std::string fragmentFileData; - const char* fragmentSource; - - std::string readFile(const std::string& filePath); - GLuint compileShader(const char* source, GLuint shaderID); - GLuint linkShaders(GLuint vertexShaderID, GLuint fragmentShaderID); - - void printShaderLog(GLuint shader); - void printProgramLog(GLuint program); -};