Can now load models from file, kinda

This commit is contained in:
Robert 2021-01-22 13:37:57 +01:00
parent 4c3c1687ef
commit ac5bf9abdb
13 changed files with 10247 additions and 12 deletions

View file

@ -47,10 +47,10 @@ int main(int argc, char** argv)
// Create vertices for square
float vertices[] = {
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
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
@ -59,9 +59,8 @@ int main(int argc, char** argv)
};
oglu::VertexAttribute topology[] = {
{ 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)) }
{ 0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0 },
{ 1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)) }
};
// Make a square

View file

@ -1,5 +1,4 @@
#version 330 core
in vec3 oCol;
in vec2 oUV;
out vec4 FragColor;

View file

@ -1,9 +1,7 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aCol;
layout (location = 2) in vec2 aUV;
layout (location = 1) in vec2 aUV;
out vec3 oCol;
out vec2 oUV;
uniform mat4 model;
@ -12,7 +10,6 @@ uniform mat4 projection;
void main()
{
oCol = aCol;
oUV = aUV;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}

View file

@ -0,0 +1,18 @@
add_executable(model_loading "main.cpp")
find_package(glfw3 REQUIRED)
target_include_directories(model_loading PRIVATE
glfw
)
target_link_libraries(model_loading PRIVATE
"$<TARGET_FILE_DIR:openglu>/$<TARGET_FILE_BASE_NAME:openglu>.lib"
glfw
)
add_custom_command(TARGET model_loading POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:openglu> $<TARGET_FILE:glfw> $<TARGET_FILE_DIR:model_loading>
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/shaders $<TARGET_FILE_DIR:model_loading>/shaders
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/assets $<TARGET_FILE_DIR:model_loading>/assets
)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,104 @@
#include <iostream>
#include "openglu.hpp"
#include "transformable.hpp"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
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)
{
// Setup GLFW
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;
// Create Window
GLFWwindow* window = glfwCreateWindow(windowSize, windowSize, "Utah teapot", NULL, NULL);
if (window == nullptr)
{
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad stuff
oglu::LoadGLLoader((GLADloadproc)glfwGetProcAddress);
oglu::SetViewport(0, 0, windowSize, windowSize);
oglu::VertexArray utah_model;
try {
utah_model = oglu::MakeVertexArray("assets/utah.obj");
}
catch (const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
return -1;
}
oglu::Object utah(utah_model);
// Create a shader
oglu::Shader shader;
try
{
shader = oglu::MakeShader("shaders/vertexShader.vert", "shaders/fragmentShader.frag");
}
catch (const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
return -1;
}
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, -2.0f, -10.0f));
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.f), 1.0f, 0.1f, 100.0f);
// Window loop
oglu::Enable(GL_DEPTH_TEST);
while (!glfwWindowShouldClose(window))
{
processInput(window);
oglu::ClearScreen(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f));
// view = glm::rotate(view, glm::radians(1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
utah.Rotate(0.0f, 1.0f, 0.0f);
shader->Use();
shader->SetUniform("model", utah);
shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view));
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection));
oglu::PolygonMode(GL_FRONT_AND_BACK, GL_LINE);
utah.Render();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}

View file

@ -0,0 +1,9 @@
#version 330 core
in vec4 oCol;
out vec4 FragColor;
void main()
{
FragColor = oCol;
}

View file

@ -0,0 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
out vec4 oCol;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
oCol = vec4(0.5, 0.5, 0.5, 1.0);
gl_Position = projection * view * model * vec4(aPos, 1.0);
}