Can now load models from file, kinda
This commit is contained in:
parent
4c3c1687ef
commit
ac5bf9abdb
|
@ -47,10 +47,10 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
// Create vertices for square
|
// Create vertices for square
|
||||||
float vertices[] = {
|
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, 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, 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, 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, 0.0f, 1.0f // top left
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned int indices[] = {
|
unsigned int indices[] = {
|
||||||
|
@ -59,9 +59,8 @@ int main(int argc, char** argv)
|
||||||
};
|
};
|
||||||
|
|
||||||
oglu::VertexAttribute topology[] = {
|
oglu::VertexAttribute topology[] = {
|
||||||
{ 0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0 },
|
{ 0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0 },
|
||||||
{ 1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)) },
|
{ 1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)) }
|
||||||
{ 2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)) }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Make a square
|
// Make a square
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
in vec3 oCol;
|
|
||||||
in vec2 oUV;
|
in vec2 oUV;
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec3 aCol;
|
layout (location = 1) in vec2 aUV;
|
||||||
layout (location = 2) in vec2 aUV;
|
|
||||||
|
|
||||||
out vec3 oCol;
|
|
||||||
out vec2 oUV;
|
out vec2 oUV;
|
||||||
|
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
@ -12,7 +10,6 @@ uniform mat4 projection;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
oCol = aCol;
|
|
||||||
oUV = aUV;
|
oUV = aUV;
|
||||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
}
|
}
|
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);
|
||||||
|
}
|
|
@ -12,7 +12,10 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#ifdef OGLU_BUILD_DLL
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef OGLU_WIN32
|
#ifdef OGLU_WIN32
|
||||||
#ifdef OGLU_BUILD_DLL
|
#ifdef OGLU_BUILD_DLL
|
||||||
|
|
|
@ -45,6 +45,44 @@ namespace oglu
|
||||||
* @param[in] clearColor Color to clear the screen with
|
* @param[in] clearColor Color to clear the screen with
|
||||||
*/
|
*/
|
||||||
OGLU_API void ClearScreen(GLbitfield mask, Color clearColor);
|
OGLU_API void ClearScreen(GLbitfield mask, Color clearColor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Wrapper for glEnable.
|
||||||
|
*
|
||||||
|
* @param[in] cap A GL capability
|
||||||
|
*/
|
||||||
|
OGLU_API void Enable(GLenum cap);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Wrapper for glDisable.
|
||||||
|
*
|
||||||
|
* @param[in] cap A GL capability
|
||||||
|
*/
|
||||||
|
OGLU_API void Disable(GLenum cap);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Wrapper for glEnablei.
|
||||||
|
*
|
||||||
|
* @param[in] cap A GL capability
|
||||||
|
* @param[in] index Index of the switch
|
||||||
|
*/
|
||||||
|
OGLU_API void Enable(GLenum cap, GLuint index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Wrapper for glDisablei.
|
||||||
|
*
|
||||||
|
* @param[in] cap A GL capability
|
||||||
|
* @param[in] index Index of the switch
|
||||||
|
*/
|
||||||
|
OGLU_API void Disable(GLenum cap, GLuint index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Wrapper for glPolygonMode.
|
||||||
|
*
|
||||||
|
* @param[in] face Type of polygon to apply @p mode to
|
||||||
|
* @param[in] mode Type of rasterization for polygons
|
||||||
|
*/
|
||||||
|
OGLU_API void PolygonMode(GLenum face, GLenum mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //OPENGLU_HPP
|
#endif //OPENGLU_HPP
|
||||||
|
|
|
@ -54,6 +54,15 @@ namespace oglu
|
||||||
*/
|
*/
|
||||||
friend std::shared_ptr<AbstractVertexArray> OGLU_API MakeVertexArray(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
|
friend std::shared_ptr<AbstractVertexArray> OGLU_API MakeVertexArray(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Constructs a new VAO.
|
||||||
|
*
|
||||||
|
* Reads an .obj file and converts it into a VAO
|
||||||
|
*
|
||||||
|
* @param[in] filepath Path to the .obj file
|
||||||
|
*/
|
||||||
|
friend std::shared_ptr<AbstractVertexArray> OGLU_API MakeVertexArray(const char* filepath);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Copy constructor.
|
* @brief Copy constructor.
|
||||||
*
|
*
|
||||||
|
|
|
@ -23,4 +23,29 @@ namespace oglu
|
||||||
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
|
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
|
||||||
glClear(mask);
|
glClear(mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Enable(GLenum cap)
|
||||||
|
{
|
||||||
|
glEnable(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Disable(GLenum cap)
|
||||||
|
{
|
||||||
|
glDisable(cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enable(GLenum cap, GLuint index)
|
||||||
|
{
|
||||||
|
glEnablei(cap, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Disable(GLenum cap, GLuint index)
|
||||||
|
{
|
||||||
|
glDisablei(cap, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PolygonMode(GLenum face, GLenum mode)
|
||||||
|
{
|
||||||
|
glPolygonMode(face, mode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
#include "vertexArray.hpp"
|
#include "vertexArray.hpp"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace oglu
|
namespace oglu
|
||||||
{
|
{
|
||||||
AbstractVertexArray::AbstractVertexArray(const AbstractVertexArray& other) :
|
AbstractVertexArray::AbstractVertexArray(const AbstractVertexArray& other) :
|
||||||
|
@ -22,6 +27,56 @@ namespace oglu
|
||||||
return VertexArray(obj);
|
return VertexArray(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<AbstractVertexArray> MakeVertexArray(const char* filepath)
|
||||||
|
{
|
||||||
|
std::ifstream file(filepath);
|
||||||
|
if (!file.good())
|
||||||
|
{
|
||||||
|
file.close();
|
||||||
|
throw std::runtime_error("Missing file: " + std::string(filepath));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<GLfloat> vertices;
|
||||||
|
std::vector<GLuint> indices;
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
while(!file.eof())
|
||||||
|
{
|
||||||
|
std::getline(file, line);
|
||||||
|
switch (line[0])
|
||||||
|
{
|
||||||
|
case 'v':
|
||||||
|
{
|
||||||
|
GLfloat x, y, z;
|
||||||
|
int shut_up_vs = sscanf(line.c_str(), "v %f %f %f", &x, &y, &z);
|
||||||
|
vertices.push_back(x);
|
||||||
|
vertices.push_back(y);
|
||||||
|
vertices.push_back(z);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case 'f':
|
||||||
|
{
|
||||||
|
GLuint a, b, c;
|
||||||
|
int shut_up_vs = sscanf(line.c_str(), "f %u %u %u", &a, &b, &c);
|
||||||
|
indices.push_back(a - 1);
|
||||||
|
indices.push_back(b - 1);
|
||||||
|
indices.push_back(c - 1);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttribute topology[] = {
|
||||||
|
{0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 },
|
||||||
|
};
|
||||||
|
|
||||||
|
return MakeVertexArray(vertices.data(), sizeof(GLfloat) * vertices.size(),
|
||||||
|
indices.data(), sizeof(GLuint) * indices.size(),
|
||||||
|
topology, sizeof(topology)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
AbstractVertexArray::AbstractVertexArray(const GLfloat* vertices, size_t verticesSize,
|
AbstractVertexArray::AbstractVertexArray(const GLfloat* vertices, size_t verticesSize,
|
||||||
const GLuint* indices, size_t indicesSize,
|
const GLuint* indices, size_t indicesSize,
|
||||||
const VertexAttribute* topology, size_t topologySize) :
|
const VertexAttribute* topology, size_t topologySize) :
|
||||||
|
|
Loading…
Reference in a new issue