Skip to content

Commit

Permalink
Merge pull request #4 from devprofile98/physic
Browse files Browse the repository at this point in the history
Physic
  • Loading branch information
devprofile98 authored Sep 20, 2021
2 parents bbb0670 + bcbb737 commit 702a3eb
Show file tree
Hide file tree
Showing 36 changed files with 6,097 additions and 8,581 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_subdirectory(Engine)
add_subdirectory(shm-physics)
add_executable(playground PlayGround/main.cpp)
target_include_directories(playground PUBLIC Engine/include/ Engine/openGLRenderer/ shm-physics/include/cyclon/)
target_include_directories(playground PUBLIC Engine/include/ Engine/openGLRenderer/ shm-physics/include/cyclon/ cyclone-physics/include/cyclone)


if (WIN32)
Expand Down
8,478 changes: 0 additions & 8,478 deletions CMakeLists.txt.user

This file was deleted.

25 changes: 22 additions & 3 deletions Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

set(shm_src
# F:/project/SHM/PlayGround/world.cpp
F:/project/SHM/PlayGround/gpc.cpp
# F:/project/SHM/PlayGround/gpc.cpp
F:/project/SHM/PlayGround/flappybird.cpp
source/glad.c
source/Engine.cpp
source/context_manager.cpp
Expand All @@ -33,10 +34,28 @@ set(cyclon_src
../shm-physics/src/pfgen.cpp
../shm-physics/src/plinks.cpp
../shm-physics/src/pcontacts.cpp
)

set(physic_src
../cyclone-physics/src/body.cpp
../cyclone-physics/src/collide_coarse.cpp
../cyclone-physics/src/collide_fine.cpp
../cyclone-physics/src/contacts.cpp
../cyclone-physics/src/core.cpp
../cyclone-physics/src/fgen.cpp
../cyclone-physics/src/joints.cpp
../cyclone-physics/src/particle.cpp
../cyclone-physics/src/pcontacts.cpp
../cyclone-physics/src/pfgen.cpp
../cyclone-physics/src/plinks.cpp
../cyclone-physics/src/pworld.cpp
../cyclone-physics/src/random.cpp
../cyclone-physics/src/world.cpp
)


add_subdirectory(include/shys)
add_library(shm-engine SHARED ${opengl_renderer_src} ${shm_src} ${cyclon_src} )
add_library(shm-engine SHARED ${opengl_renderer_src} ${shm_src} ${cyclon_src} ${physic_src} )
target_link_libraries(shm-engine shys)
if (WIN32)
target_link_libraries(shm-engine "F:/project/SHM/Engine/include/GLFW/glfw3.lib")
Expand All @@ -48,5 +67,5 @@ elseif(UNIX)
endif()

target_include_directories(shm-engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/openGLRenderer/)
target_include_directories(shm-engine PUBLIC include/ ../shm-physics/include/cyclon/)
target_include_directories(shm-engine PUBLIC include/ ../shm-physics/include/cyclon/ ../cyclone-physics/include/)

54 changes: 53 additions & 1 deletion Engine/include/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <vector>


namespace SHM {
/**
represent a RGB color
*/
struct Color{
Color(){};
Color(float red, float green, float blue):r(red), g(green), b(blue)
Expand All @@ -16,7 +20,7 @@ namespace SHM {
}
float r, g, b;

// call normalize function if values are in range 0-255
/** call normalize function if values are in range 0-255*/
void normalize(){
if (r >1 || g>1 || b>1){
r = r/255.0f;
Expand All @@ -26,6 +30,10 @@ namespace SHM {
}
};

/** represent a basic 3D vector
* use shm-physic Vector3 for more usage and support
*/

class Vector3{
public:
Vector3(float x=0, float y=0, float z=0):x(x), y(y), z(z)
Expand All @@ -42,6 +50,50 @@ namespace SHM {

float x, y, z;
private:
};

/**
* @brief linear interpolation between start to end by duration
*/
class Lerp{
public:
Lerp(float* ref_value, float duration, float start, float end)
:duration(duration), start(start), end(end), m_ref_value(ref_value)
{
step = (end - start) / duration;
}


float tick(){
if ((*m_ref_value) + step < end){
float temp = start;
temp += step;
start = temp;
(*m_ref_value) += step;
return temp;
}
return end;
}

private:
float duration = 0;
float step, start, end;
float* m_ref_value;
};


class LerpManager{
public:
LerpManager() = delete;
void add(Lerp p){
wait_list.push_back(p);
}
static inline std::vector<Lerp> wait_list;
static void exec(){
for (Lerp& l: wait_list){
l.tick();
}
};

};
}
Expand Down
1 change: 1 addition & 0 deletions Engine/include/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

// include common header files
#include "BaseRenderer.hpp"
#include "Common.hpp"

// #include "openGLRenderer.hpp"
#include "context_manager.hpp"
Expand Down
12 changes: 7 additions & 5 deletions Engine/include/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Camera.hpp"
#include "Handler.hpp"
#include "openGLRenderer.hpp"
#include <string_view>


namespace SHM
Expand All @@ -19,7 +20,7 @@ namespace SHM
OPENGL,
VULKAN
};

// class Application;
class SHM_EXPORT Engine{
public:
Engine(const char* project_name, API_TYPE api_type);
Expand All @@ -28,6 +29,7 @@ namespace SHM
static std::shared_ptr<BaseRenderer> getRenderer();
static std::shared_ptr<Camera> getCamera();
void MainRenderLoop();
static Handler * getHandler();

static std::shared_ptr<shader> CreateShader(const char *vertex_code, const char *fragment_code);

Expand All @@ -37,19 +39,19 @@ namespace SHM
bool firstMouse=true;

bool InitWorld() const;
void outLoop();
void outLoop(GLFWwindow* window=nullptr);
void inLoop();
void saveImage(char* file_path);

// void setKeyBinding(Application *app);
ContextManager* context_manager;


private:
ContextManager* context_manager;
void setRenderer(API_TYPE api_type);
static std::shared_ptr<BaseRenderer> m_renderer;

static std::shared_ptr<Camera > m_camera;
Handler* m_handler;
static inline Handler* m_handler;
};

}
Expand Down
22 changes: 22 additions & 0 deletions Engine/include/Handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

namespace SHM{

//void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
//{
// if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
// {
// currentXPos--;
// if (currentXPos < 0)
// currentXPos = 0;
// }
//}

class SHM_EXPORT Command{
public:
virtual ~Command(){};
virtual void execute(){};
};


class SHM_EXPORT Handler{
public:
Handler(GLFWwindow *window, std::shared_ptr<Camera> camera);
Expand All @@ -20,6 +37,11 @@ namespace SHM{
static inline std::shared_ptr<Camera> camera;
float last_frame_time;

// buttons
Command* space_btn;
// void execspace(){
// space_btn->execute();
// }

};

Expand Down
4 changes: 2 additions & 2 deletions Engine/include/context_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SHM{
class SHM_EXPORT ContextManager{
public:
ContextManager(const char* window_name);
~ContextManager();
virtual ~ContextManager();

static void FrameBufferCallback(GLFWwindow* window, int width, int height);
GLFWwindow* GetWindow() const;
Expand All @@ -20,10 +20,10 @@ namespace SHM{
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 10.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
GLFWwindow* window;

private:
void createWindow(uint32_t width, uint32_t height, const char* window_name);
GLFWwindow* window;

};

Expand Down
2 changes: 1 addition & 1 deletion Engine/openGLRenderer/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ void Mesh::setupMesh()

// always good practice to set everything back to defaults once configured.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
// glBindTexture(GL_TEXTURE_2D, 0);
}
14 changes: 11 additions & 3 deletions Engine/openGLRenderer/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void Model::Draw(std::shared_ptr<shader> sh){
glm::mat4 model{1.0f};
model = glm::translate(model, m_position);
model = glm::scale(model, m_scale);
model = glm::rotate(model, glm::radians(90.0f), m_rotation);
model = glm::rotate(model, glm::radians(1.5f), m_rotation);

if (sh){
// calculate for shadow map
Expand All @@ -88,6 +88,14 @@ void Model::Draw(std::shared_ptr<shader> sh){
}
}

void Model::DrawInstances(std::vector<glm::vec3> &positions, std::shared_ptr<shader> sh)
{
for(glm::vec3& pos: positions){
m_position = pos;
this->Draw(sh);
}
}

std::shared_ptr<shader> Model::getShader()
{
return shader_program;
Expand All @@ -101,11 +109,11 @@ void Model::setPosition(const glm::vec3 &pos)
void Model::setScale(const glm::vec3 &scale)
{
this->m_scale = scale;
std::cout << m_scale.x << m_scale.y <<m_scale.z<<std::endl;
}

void Model::setRotation(const glm::vec3 &rot)
void Model::setRotation(const glm::vec3 &rot, float radians)
{
this->m_rotation_radians = radians;
this->m_rotation = rot;
}

Expand Down
5 changes: 4 additions & 1 deletion Engine/openGLRenderer/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <GLFW/glfw3.h>
//gl math
#include <glm/glm.hpp>
#include <glm/gtx/string_cast.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>

Expand All @@ -24,13 +25,14 @@ class Model{
public:
Model(const char* path, std::shared_ptr<shader> shader_program);
void Draw(std::shared_ptr<shader> sh=nullptr);
void DrawInstances(std::vector<glm::vec3>& positions, std::shared_ptr<shader> shader_program);
std::vector<Texture_INT> textures_loaded;
std::vector<Mesh> meshes;
std::shared_ptr<shader> getShader();

void setPosition(const glm::vec3& pos);
void setScale(const glm::vec3& scale);
void setRotation(const glm::vec3& rot);
void setRotation(const glm::vec3& rot, float radians=0);

const glm::vec3 *getPosition() const;
const glm::vec3 *getScale() const;
Expand All @@ -41,6 +43,7 @@ class Model{
std::shared_ptr<shader> shader_program;
void loadModel(std::string path);
glm::vec3 m_position, m_rotation, m_scale;
float m_rotation_radians;
void processNode(aiNode *node, const aiScene *scene);
Mesh processMesh(aiMesh *mesh, const aiScene *scene);
std::vector<Texture_INT> loadMaterialTexture(aiMaterial *mat, aiTextureType type, std::string typeName);
Expand Down
3 changes: 0 additions & 3 deletions Engine/source/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ namespace SHM{
m_up = up;
m_fov = 45.0f;
}



}
Loading

0 comments on commit 702a3eb

Please sign in to comment.