OpenGL-utility/examples/debug/main.cpp

117 lines
3.2 KiB
C++
Raw Normal View History

2021-01-19 19:25:46 +01:00
#include <iostream>
#include "openglu.hpp"
2021-01-22 01:13:01 +01:00
#include "transformable.hpp"
2021-01-19 19:25:46 +01:00
#include <glad/glad.h>
#include <GLFW/glfw3.h>
2021-01-22 01:13:01 +01:00
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
2021-01-19 19:25:46 +01:00
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
oglu::SetViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
int main(int argc, char** argv)
{
2021-01-21 01:31:23 +01:00
// Setup GLFW
2021-01-19 19:25:46 +01:00
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
const GLFWvidmode* screen = glfwGetVideoMode(glfwGetPrimaryMonitor());
int windowSize = screen->height / 4 * 3;
2021-01-21 01:31:23 +01:00
// Create Window
2021-01-19 19:25:46 +01:00
GLFWwindow* window = glfwCreateWindow(windowSize, windowSize, "Debug", NULL, NULL);
if (window == nullptr)
{
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
2021-01-21 01:31:23 +01:00
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
2021-01-19 19:25:46 +01:00
2021-01-21 01:31:23 +01:00
// glad stuff
2021-01-19 19:25:46 +01:00
oglu::LoadGLLoader((GLADloadproc)glfwGetProcAddress);
oglu::SetViewport(0, 0, windowSize, windowSize);
2021-01-21 01:31:23 +01:00
// Create vertices for square
2021-01-19 19:47:54 +01:00
float vertices[] = {
2021-01-21 12:07:43 +01:00
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
2021-01-19 19:47:54 +01:00
};
2021-01-20 16:51:55 +01:00
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
2021-01-19 19:47:54 +01:00
};
2021-01-20 16:51:55 +01:00
oglu::VertexAttribute topology[] = {
2021-01-21 12:07:43 +01:00
{ 0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0 },
{ 1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)) },
{ 2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)) }
2021-01-20 16:51:55 +01:00
};
2021-01-21 01:31:23 +01:00
// Make a square
2021-01-22 01:29:50 +01:00
oglu::Object square(vertices, sizeof(vertices), indices, sizeof(indices), topology, sizeof(topology));
2021-01-21 01:31:23 +01:00
// Create a shader
oglu::Shader shader;
2021-01-21 01:31:23 +01:00
try
2021-01-20 16:51:55 +01:00
{
shader = oglu::MakeShader("shaders/vertexShader.vert", "shaders/fragmentShader.frag");
2021-01-20 16:51:55 +01:00
}
2021-01-21 15:48:31 +01:00
catch (const std::runtime_error& e)
2021-01-20 16:51:55 +01:00
{
std::cerr << e.what() << std::endl;
return -1;
}
2021-01-19 19:47:54 +01:00
2021-01-21 12:07:43 +01:00
// Create a texture
2021-01-21 16:11:41 +01:00
oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg");
oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png");
2021-01-21 12:07:43 +01:00
2021-01-22 01:13:01 +01:00
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.f), 1.0f, 0.1f, 100.0f);
2021-01-21 01:31:23 +01:00
// Window loop
2021-01-19 19:25:46 +01:00
while (!glfwWindowShouldClose(window))
{
processInput(window);
2021-01-22 01:13:01 +01:00
oglu::ClearScreen(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f));
2021-01-22 01:29:50 +01:00
square.Rotate(6.0f, 0.0f, 0.0f);
2021-01-19 19:25:46 +01:00
2021-01-20 16:51:55 +01:00
shader->Use();
shader->SetUniform("texture1", crate, 0);
shader->SetUniform("texture2", opengl, 1);
2021-01-22 01:29:50 +01:00
shader->SetUniformMatrix4fv("model", 1, GL_FALSE, square.GetMatrix());
2021-01-22 01:13:01 +01:00
shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view));
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection));
2021-01-21 16:11:41 +01:00
2021-01-22 01:29:50 +01:00
square.Render();
2021-01-20 16:51:55 +01:00
2021-01-19 19:25:46 +01:00
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}