Skip to content

3d model loading #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS True)

# Tell CMake to ignore warnings from stuff in our dependencies. Surely GLM
# knows what it is doing... https://7tv.app/emotes/61e8fae862858c6406126ced
IF (NOT WIN32)
set_property(
DIRECTORY build
PROPERTY COMPILE_OPTIONS "-w"
)
ENDIF()

# Add google test to CMake
add_subdirectory(dependencies/google-test)
Expand Down Expand Up @@ -66,4 +72,9 @@ add_custom_target(lint
-i${CMAKE_SOURCE_DIR}/src/client/tests
-i${CMAKE_SOURCE_DIR}/src/server/tests
-i${CMAKE_SOURCE_DIR}/src/shared/tests
)
)

add_custom_target(pull_models
COMMAND
gdown 1N7a5cDgMcXbPO0RtgznnEo-1XUfdMScM -O ${CMAKE_SOURCE_DIR}/assets/graphics --folder
)
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ Depending on where you need to link the library (client, server, shared), you wi
- [C++ Intellisense](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)
- [General Productivity](https://marketplace.visualstudio.com/items?itemName=jirkavrba.subway-surfers)

## Models

You can download models from our Google Drive folder [here](https://drive.google.com/drive/folders/1N7a5cDgMcXbPO0RtgznnEo-1XUfdMScM?usp=sharing) and place them in `src/client/models`. Alternatively, you can install [gdown](https://github.com/wkentaro/gdown) and run `make pull_models` to automatically pull them.

## Documentation

View deployed documentation [here](https://cse125.ucsd.edu/2024/cse125g3/site/docs/html/)
Expand Down
2 changes: 2 additions & 0 deletions assets/graphics/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
15 changes: 15 additions & 0 deletions dependencies/assimp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include(FetchContent)

FetchContent_Declare(assimp
GIT_REPOSITORY https://github.com/assimp/assimp
GIT_TAG v5.3.1
GIT_PROGRESS TRUE
)

FetchContent_MakeAvailable(assimp)

set(ASSIMP_INCLUDE_DIRS
${CMAKE_BINARY_DIR}/_deps/assimp-src/include/
${CMAKE_BINARY_DIR}/_deps/assimp-build/include/
PARENT_SCOPE
)
6 changes: 3 additions & 3 deletions dependencies/glew/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ FetchContent_Declare(glew

FetchContent_MakeAvailable(glew)

# find_package(GLEW 2.0 REQUIRED)
# target_link_libraries(Starting GLEW::GLEW)

set(GLEW_INCLUDE_DIRS
${CMAKE_BINARY_DIR}/_deps/glew-src/include
PARENT_SCOPE)
11 changes: 11 additions & 0 deletions dependencies/stb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include(FetchContent)

FetchContent_Declare(stb
GIT_REPOSITORY https://github.com/nothings/stb
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(stb)

set(STB_INCLUDE_DIRS
${CMAKE_BINARY_DIR}/_deps/stb-src/
PARENT_SCOPE)
2 changes: 2 additions & 0 deletions include/client/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Camera {
glm::mat4 getViewProj();
void updatePos(glm::vec3 pos);

glm::vec3 getPos();

private:
// Perspective controls
float FOV; // Field of View Angle (degrees)
Expand Down
16 changes: 12 additions & 4 deletions include/client/client.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma once

#include "client/core.hpp"

#include <iostream>
Expand All @@ -12,6 +10,9 @@
#include <boost/filesystem.hpp>

#include "client/cube.hpp"
#include "client/lightsource.hpp"
#include "client/shader.hpp"
#include "client/model.hpp"
#include "client/util.hpp"
#include "client/lobbyfinder.hpp"
#include "client/camera.hpp"
Expand Down Expand Up @@ -57,10 +58,17 @@ class Client {

SharedGameState gameState;

float cubeMovementDelta = 0.05f;
std::shared_ptr<Shader> cube_shader;
std::shared_ptr<Shader> model_shader;
std::shared_ptr<Shader> light_source_shader;

std::unique_ptr<Model> player_model;
std::unique_ptr<Model> bear_model;
std::unique_ptr<LightSource> light_source;

float playerMovementDelta = 0.05f;

GLFWwindow *window;
GLuint cubeShaderProgram;

Camera *cam;

Expand Down
16 changes: 8 additions & 8 deletions include/client/cube.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "client/core.hpp"
#include "client/renderable.hpp"

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/transform.hpp>
Expand All @@ -11,23 +12,22 @@
#include <ostream>
#include <vector>

class Cube {
class Cube : public Renderable {
public:
Cube(glm::vec3 newColor, glm::vec3 scale);
explicit Cube(glm::vec3 newColor);
~Cube();

void draw(glm::mat4 viewProjMat, GLuint shader, bool fill);
void update(glm::vec3 new_pos);
void update_delta(glm::vec3 delta);

void draw(std::shared_ptr<Shader> shader,
glm::mat4 viewProj,
glm::vec3 camPos,
glm::vec3 lightPos,
bool fill) override;
private:
GLuint VAO;
GLuint VBO_positions, VBO_normals, EBO;

glm::mat4 model;
glm::vec3 color;


// Cube Information
std::vector<glm::vec3> positions;
std::vector<glm::vec3> normals;
Expand Down
34 changes: 34 additions & 0 deletions include/client/lightsource.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "client/core.hpp"
#include "client/shader.hpp"

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/transform.hpp>
#include <glm/gtx/string_cast.hpp>
#include <glm/gtx/euler_angles.hpp>

#include <iostream>
#include <ostream>
#include <vector>
#include <memory>

class LightSource {
public:
LightSource();
void draw(std::shared_ptr<Shader> shader, glm::mat4 viewProj);
void TranslateTo(const glm::vec3& new_pos);

glm::vec3 lightPos;
private:
GLuint VAO, VBO;

glm::mat4 model;
glm::vec3 color;

// Cube Information
std::vector<glm::vec3> positions;
std::vector<glm::vec3> normals;
std::vector<unsigned int> indices;

};
172 changes: 172 additions & 0 deletions include/client/model.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#pragma once

#include <vector>
#include <string>
#include <memory>
#include <optional>

#include <GL/glew.h>
#include <glm/glm.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

#include "assimp/material.h"
#include "client/renderable.hpp"
#include "client/shader.hpp"

/**
* Stores position, normal vector, and coordinates
* in texture map for every vertex in a mesh.
*/
struct Vertex {
glm::vec3 position;
glm::vec3 normal;
glm::vec2 textureCoords;
};

class Texture {
public:
/**
* Load a texture from a filepath.
*
* @param filepath is the file path to the texture
* @param type specifies the type of texture to load.
* Currently only aiTextureType_SPECULAR and aiTextureType_DIFFUSE
* are implemented.
*/
Texture(const std::string& filepath, const aiTextureType& type);

/**
* @return the texture's ID to be passed into OpenGL functions
*/
GLuint getID() const;

/*
* Get the type of texture. Either "texture_diffuse" or "texture_specular".
*/
std::string getType() const;
private:
GLuint ID;
std::string type;
};

struct Material {
glm::vec3 ambient;
glm::vec3 diffuse;
glm::vec3 specular;
float shininess;
};

/**
* Mesh holds the data needed to render a mesh (collection of triangles).
*
* A Mesh differs from a Model since a Model is typically made up of multiple,
* smaller meshes. This is useful for animating parts of model individual (ex: legs,
* arms, head)
*/
class Mesh : public Renderable {
public:
/**
* Creates a new mesh from a collection of vertices, indices and textures
*/
Mesh(
const std::vector<Vertex>& vertices,
const std::vector<unsigned int>& indices,
const std::vector<Texture>& textures,
const Material& material
);

/**
* Render the Mesh to the viewport using the provided shader program.
*
* @param shader to use when rendering the program. Determines position of
* vertices and their color/texture.
* @param modelView determines the scaling/rotation/translation of the
* mesh
*/
void draw(std::shared_ptr<Shader> shader,
glm::mat4 viewProj,
glm::vec3 camPos,
glm::vec3 lightPos,
bool fill) override;
private:
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
Material material;

// render data opengl needs
GLuint VAO, VBO, EBO;
};


class Model : public Renderable {
public:
/**
* Loads Model from a given filename. Can be of format
* .obj, .blend or any of the formats that assimp supports
* @see https://assimp-docs.readthedocs.io/en/latest/about/introduction.html?highlight=obj#introduction
*
* @param Filepath to model file.
*/
explicit Model(const std::string& filepath);

/**
* Draws all the meshes of a given model
*
* @param Shader to use while drawing all the
* meshes of the model
*/
void draw(std::shared_ptr<Shader> shader,
glm::mat4 viewProj,
glm::vec3 camPos,
glm::vec3 lightPos,
bool fill) override;

/**
* Sets the position of the Model to the given x,y,z
* values
*
* @param vector of x, y, z of the model's new position
*/
void translateAbsolute(const glm::vec3& new_pos) override;

/**
* Updates the position of the Model relative to it's
* previous position
*
* @param vector of x, y, z of the change in the Model's
* position
*/
void translateRelative(const glm::vec3& delta) override;

/**
* Scale the Model across all axes (x,y,z)
* by a factor
*
* @param new_factor describes how much to scale the model by.
* Ex: setting it to 0.5 will cut the model's rendered size
* in half.
*/
void scale(const float& new_factor) override;

/**
* Scale the model across all axes (x,y,z)
* by the scale factor in each axis.
*
* @param the scale vector describes how much to independently scale
* the model in each axis (x, y, z)
*/
void scale(const glm::vec3& scale) override;
private:
std::vector<Mesh> meshes;

void processNode(aiNode* node, const aiScene* scene);
Mesh processMesh(aiMesh* mesh, const aiScene* scene);
std::vector<Texture> loadMaterialTextures(aiMaterial* mat, const aiTextureType& type);

// store the directory of the model file so that textures can be
// loaded relative to the model file
std::string directory;
};
Loading