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 6 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
15 changes: 15 additions & 0 deletions 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 @@ -67,3 +73,12 @@ add_custom_target(lint
-i${CMAKE_SOURCE_DIR}/src/server/tests
-i${CMAKE_SOURCE_DIR}/src/shared/tests
)

add_custom_target(pull_models
COMMAND
gdown 133bVNM4_27hg_VoZGo9EUfCn7n6VXzOU -O ${CMAKE_SOURCE_DIR}/src/client/models/Player1-fire.obj &&
gdown 1v3XO_E1ularO5Ku2WaA8O9GqE402a8cx -O ${CMAKE_SOURCE_DIR}/src/client/models/bear-sp22.obj &&
gdown 1hHK-0iKMT6uboFUl3DXC9JcbnfsNCNF4 -O ${CMAKE_SOURCE_DIR}/src/client/models/cube.obj &&
gdown 1mEWRgBP7G-s3XOr6NO9yichD4_eKc3JH -O ${CMAKE_SOURCE_DIR}/src/client/models/teapot.obj
)

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,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
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)
11 changes: 9 additions & 2 deletions include/client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/filesystem.hpp>

#include "client/cube.hpp"
#include "client/shader.hpp"
#include "client/model.hpp"
#include "client/util.hpp"
#include "client/lobbyfinder.hpp"

Expand Down Expand Up @@ -51,8 +54,10 @@ class Client {

SharedGameState gameState;

GLuint cubeShaderProgram;
float cubeMovementDelta = 0.05f;
std::shared_ptr<Shader> cubeShader;

std::unique_ptr<Model> playerModel;
float playerMovementDelta = 0.05f;

GLFWwindow *window;

Expand All @@ -69,5 +74,7 @@ class Client {
/// @brief Generate endpoints the client can connect to
basic_resolver_results<class boost::asio::ip::tcp> endpoints;
std::shared_ptr<Session> session;

boost::filesystem::path root_path;
};

130 changes: 130 additions & 0 deletions include/client/model.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#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/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;
};

/**
* 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:
/**
* 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);

/**
* 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 modelView) const;
private:
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;

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


class Model {
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);

/**
* 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 TranslateTo(const glm::vec3& new_pos);

/**
* 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);
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);

glm::mat4 modelView;
};
60 changes: 60 additions & 0 deletions include/client/shader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <GL/glew.h>

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>


class Shader {
public:
/**
* Create a shader program from filepaths to a vertex and fragment shader.
* Use getID() to access the shader program's ID.
*/
Shader(const std::string& vertexPath, const std::string& fragmentPath);
~Shader();

/*
* @return the shader program's ID which can be passed into OpenGL functions
*/
GLuint getID();

/*
* Activate the shader program. Must be called before drawing.
* Calls glUseProgram under the hood.
*/
void use();

/*
* Sets a boolean unform variable of the shader program
* with the specified value
* @param name is the name of the uniform variable as written
* in the shader program
* @param value is the boolean value to write to that variable
*/
void setBool(const std::string &name, bool value) const;

/*
* Sets an integer unform variable of the shader program
* with the specified value
* @param name is the name of the uniform variable as written
* in the shader program
* @param value is the integer value to write to that variable
*/
void setInt(const std::string &name, int value) const;

/*
* Sets a float unform variable of the shader program
* with the specified value
* @param name is the name of the uniform variable as written
* in the shader program
* @param value is the float value to write to that variable
*/
void setFloat(const std::string &name, float value) const;
private:
// the shader program ID
unsigned int ID;
};
5 changes: 0 additions & 5 deletions include/client/shaders.hpp

This file was deleted.

17 changes: 0 additions & 17 deletions include/client/util.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,2 @@
#pragma once

#include <stdio.h>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

// #include <GL/glew.h>

// #include <GLFW/glfw3.h>
// #include <glm/glm.hpp>

#include "client/core.hpp"


GLuint LoadShaders(const std::string& vertex_file_path, const std::string& fragment_file_path);
3 changes: 3 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mkShell {
gnumake
gcc13
gdb
zlib

wayland
wayland-scanner
Expand All @@ -29,6 +30,8 @@ mkShell {
doxygen
clang-tools_14
cppcheck

python310Packages.gdown
];
nativeBuildInputs = with pkgs; [
pkg-config
Expand Down
Loading