Can now load models from file, kinda
This commit is contained in:
parent
4c3c1687ef
commit
ac5bf9abdb
13 changed files with 10247 additions and 12 deletions
18
examples/model_loading/CMakeLists.txt
Normal file
18
examples/model_loading/CMakeLists.txt
Normal 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
|
||||
)
|
9965
examples/model_loading/assets/utah.obj
Normal file
9965
examples/model_loading/assets/utah.obj
Normal file
File diff suppressed because it is too large
Load diff
104
examples/model_loading/main.cpp
Normal file
104
examples/model_loading/main.cpp
Normal 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;
|
||||
}
|
9
examples/model_loading/shaders/fragmentShader.frag
Normal file
9
examples/model_loading/shaders/fragmentShader.frag
Normal file
|
@ -0,0 +1,9 @@
|
|||
#version 330 core
|
||||
in vec4 oCol;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = oCol;
|
||||
}
|
14
examples/model_loading/shaders/vertexShader.vert
Normal file
14
examples/model_loading/shaders/vertexShader.vert
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue