Initial OpenGL commit

This commit is contained in:
Corentin 2021-07-13 01:02:17 +09:00
commit 1ae16b306f
18 changed files with 25863 additions and 0 deletions

9398
src/glad/glad.cpp Normal file

File diff suppressed because one or more lines are too long

26
src/test/example_gl.cpp Normal file
View file

@ -0,0 +1,26 @@
#include <iostream>
#include "uui/opengl/application.hpp"
using namespace std;
int main()
{
cout << "Example Staring" << endl;
try
{
uui::GlApplication app;
app.add_window(800, 600, "Vulkan!");
app.add_window(600, 600, "Vulkan 2!");
app.run();
}
catch(const std::exception& error)
{
std::cerr << error.what() << std::endl;
return EXIT_FAILURE;
}
cout << "Example done" << endl;
return 0;
}

View file

@ -0,0 +1,72 @@
#include "uui/opengl/application.hpp"
uui::GlApplication::GlApplication()
{
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
uui::GlApplication::~GlApplication()
{
glfwTerminate();
}
void uui::GlApplication::run()
{
std::cout << "Starting the Application" << std::endl;
auto start_time = std::chrono::high_resolution_clock::now();
auto end_time = start_time;
float render_duration;
float frame_count = 0.0f;
std::cout << "\n";
while(!windows.empty())
{
end_time = std::chrono::high_resolution_clock::now();
render_duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
// std::cout << "Frame " << frame_count << ", duration: " << render_duration << std::endl;
if(frame_count > 0.0f && render_duration > 500.0f)
{
std::cout << "\rFPS : " << (frame_count * 1000.0f) / render_duration << " "<< std::flush;
frame_count = 0.0f;
start_time = end_time;
}
glfwPollEvents();
// Destroying windows
for(auto it = windows.begin(); it != windows.end(); it += 1)
{
auto& window = *it;
if(glfwWindowShouldClose(window->glfw_window))
{
if(it == (windows.end() - 1) && windows.size() > 1)
{
glfwMakeContextCurrent((*(windows.end() - 2))->glfw_window);
glfwSwapInterval(1);
}
windows.erase(it);
it -= 1;
}
else
window->draw();
}
frame_count += 1;
}
std::cout << "Ending the Application" << std::endl;
}
void uui::GlApplication::add_window(int width, int height, const std::string& title)
{
windows.push_back(std::make_unique<GlWindow>(*this, width, height, title));
glfwSwapInterval(1);
if(windows.size() > 1)
{
glfwMakeContextCurrent((*(windows.end() - 2))->glfw_window);
glfwSwapInterval(0);
}
}

33
src/uui/opengl/window.cpp Normal file
View file

@ -0,0 +1,33 @@
#include "uui/opengl/window.hpp"
#include <iostream>
uui::GlWindow::GlWindow(const uui::GlApplication& app, int width, int height, const std::string& title): app(app)
{
std::cout << "Creating a Window" << std::endl;
this->title = title;
glfw_window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if(glfw_window == NULL)
throw std::runtime_error("GLWindow : failed to create GLFW window");
glfwMakeContextCurrent(glfw_window);
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
throw std::runtime_error("GLWindow : Failed to initialize OpenGL context");
glViewport(0, 0, width, height);
}
uui::GlWindow::~GlWindow()
{
std::cout << "Closing a Window" << std::endl;
glfwMakeContextCurrent(glfw_window);
glfwDestroyWindow(glfw_window);
}
void uui::GlWindow::draw()
{
glfwMakeContextCurrent(glfw_window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(glfw_window);
}

20
src/uui/shader.cpp Normal file
View file

@ -0,0 +1,20 @@
#include "uui/shader.hpp"
#include <fstream>
std::vector<char> uui::readFile(const std::string& filename)
{
std::ifstream file(filename, std::ios::ate | std::ios::binary);
if(!file.is_open())
throw std::runtime_error("failed to open file!");
size_t fileSize = (size_t)file.tellg();
std::vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), fileSize);
file.close();
return buffer;
}