Skip to content

Commit

Permalink
add camera rotate support
Browse files Browse the repository at this point in the history
  • Loading branch information
jintaoyugithub committed Dec 8, 2024
1 parent 731cd72 commit 63596d3
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 30 deletions.
13 changes: 11 additions & 2 deletions examples/fluidSim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ bgfx_compile_shaders(
bgfx_compile_shaders(
TYPE VERTEX
SHADERS "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_ws_vs.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_varying.def.sc"
# VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_varying.def.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_objectInter_varying.def.sc"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/shaders/")

# cube
Expand All @@ -24,7 +25,14 @@ bgfx_compile_shaders(
bgfx_compile_shaders(
TYPE FRAGMENT
SHADERS "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_fs.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_varying.def.sc"
# VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_varying.def.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_objectInter_varying.def.sc"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/shaders/")

bgfx_compile_shaders(
TYPE FRAGMENT
SHADERS "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_objectInter_fs.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_objectInter_varying.def.sc"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/shaders/")

# cube
Expand Down Expand Up @@ -86,6 +94,7 @@ add_executable(
"${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_vs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_ws_vs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_fs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_objectInter_fs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/cube_vs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/cube_fs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/fluidSim/quad_advect_fs.sc"
Expand Down
74 changes: 51 additions & 23 deletions examples/fluidSim/fluidSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ bool DebugDispProject = false;
bool DebugDispAdvect = false;
float elapsed = 0;

float deltaX = 0.1;
float deltaY = 0.1;
float deltaZ = 0.1;
float deltaX = 0.01;
float deltaY = 0.01;
float deltaZ = 0.01;

struct testSphere
{
// center pos
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
float z = -0.1f;

float radius = 1.0f;
float radius = 5.0f;
};

class ExampleFluidSim : public shift::AppBaseGLFW
Expand Down Expand Up @@ -132,16 +132,11 @@ class ExampleFluidSim : public shift::AppBaseGLFW
// test cube rendering
// set all the matrices
glm::mat4 model = glm::identity<glm::mat4>();
model = glm::rotate(model, glm::radians(80.0f), glm::vec3(0.0, 1.0, 0.0));
// glm::mat4 view =
// glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f,
// 0.0f));
// glm::mat4 proj = glm::perspective(glm::radians(60.0f), float(getWidth()) / getHeight(), 0.1f,
// 100.0f);
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0, 0.0, 0.0));

glm::mat4 view = _camera->GetProperties<glm::mat4>(shift::Property::ViewMat);
glm::mat4 proj = _camera->GetProperties<glm::mat4>(shift::Property::ProjMat);
bgfx::setTransform(&model[0][0]);
// bgfx::setTransform(&model[0][0]);
bgfx::setViewTransform(0, &view[0][0], &proj[0][0]);

// set viewport
Expand All @@ -158,8 +153,16 @@ class ExampleFluidSim : public shift::AppBaseGLFW
switch (_event.type)
{
case GLEQ_BUTTON_PRESSED:
std::cout << "left button pressed" << std::endl;
isPressed = true;
switch (_event.mouse.button)
{
case GLFW_MOUSE_BUTTON_LEFT:
std::cout << "left button pressed" << std::endl;
isPressed = true;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
std::cout << "right button pressed" << std::endl;
break;
}
break;

case GLEQ_CURSOR_MOVED: {
Expand Down Expand Up @@ -194,8 +197,16 @@ class ExampleFluidSim : public shift::AppBaseGLFW
}

case GLEQ_BUTTON_RELEASED:
std::cout << "left button released" << std::endl;
isPressed = false;
switch (_event.mouse.button)
{
case GLFW_MOUSE_BUTTON_LEFT:
std::cout << "left button released" << std::endl;
isPressed = false;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
std::cout << "right button released" << std::endl;
break;
}
break;

case GLEQ_KEY_PRESSED:
Expand Down Expand Up @@ -244,33 +255,50 @@ class ExampleFluidSim : public shift::AppBaseGLFW
DebugDispProject = false;
DebugDispDiv = false;
}

break;

case GLEQ_KEY_REPEATED:
// control the sphere
if (_event.keyboard.key == GLFW_KEY_UP)
{
sphere.z -= deltaZ;
sphere.y += deltaZ;
std::cout << sphere.x << " " << sphere.y << " " << sphere.z << std::endl;
velocityCube->updateUniforms(UniformType::InterPosZ, sphere.z);

velocityGrid->updateUniforms(UniformType::InterPosY, sphere.y);
velocityGrid->updateUniforms(UniformType::InterVelX, 0.0f);
velocityGrid->updateUniforms(UniformType::InterVelY, 1.0f);

velocityGrid->dispatch(ProgramType::AddSource, 0);
}
if (_event.keyboard.key == GLFW_KEY_DOWN)
{
sphere.z += deltaZ;
sphere.y -= deltaZ;
std::cout << sphere.x << " " << sphere.y << " " << sphere.z << std::endl;
velocityCube->updateUniforms(UniformType::InterPosZ, sphere.z);

velocityGrid->updateUniforms(UniformType::InterPosY, sphere.y);
velocityGrid->updateUniforms(UniformType::InterVelX, 0.0f);
velocityGrid->updateUniforms(UniformType::InterVelY, -1.0f);
velocityGrid->dispatch(ProgramType::AddSource, 0);
}
if (_event.keyboard.key == GLFW_KEY_LEFT)
{
sphere.x -= deltaX;
std::cout << sphere.x << " " << sphere.y << " " << sphere.z << std::endl;
velocityCube->updateUniforms(UniformType::InterPosX, sphere.x);

velocityGrid->updateUniforms(UniformType::InterPosX, sphere.x);
velocityGrid->updateUniforms(UniformType::InterVelX, -1.0f);
velocityGrid->updateUniforms(UniformType::InterVelY, 0.0f);
velocityGrid->dispatch(ProgramType::AddSource, 0);
}
if (_event.keyboard.key == GLFW_KEY_RIGHT)
{
sphere.x += deltaX;
std::cout << sphere.x << " " << sphere.y << " " << sphere.z << std::endl;
velocityCube->updateUniforms(UniformType::InterPosX, sphere.x);

velocityGrid->updateUniforms(UniformType::InterPosX, sphere.x);
velocityGrid->updateUniforms(UniformType::InterVelX, 1.0f);
velocityGrid->updateUniforms(UniformType::InterVelY, 0.0f);
velocityGrid->dispatch(ProgramType::AddSource, 0);
}
break;

Expand Down
5 changes: 4 additions & 1 deletion examples/fluidSim/velocityFieldGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ VelocityFieldGrid::VelocityFieldGrid(int simResX, int simResY, int simResZ) : Ve
quadPosTexCoord::init();
_vbhQuad = bgfx::createVertexBuffer(bgfx::makeRef(quadVertices, sizeof(quadVertices)), quadPosTexCoord::_layout);
_ibhQuad = bgfx::createIndexBuffer(bgfx::makeRef(quadIndices, sizeof(quadIndices)));
_quadProgram = shift::loadProgram({"quad_ws_vs.sc", "quad_fs.sc"});
// Debug quads
//_quadProgram = shift::loadProgram({"quad_vs.sc", "quad_fs.sc"});
//_quadProgram = shift::loadProgram({"quad_ws_vs.sc", "quad_fs.sc"});
_quadProgram = shift::loadProgram({"quad_ws_vs.sc", "quad_objectInter_fs.sc"});

// init compute shaders
_csReset = shift::loadProgram({"velocity_reset_cs.sc"});
Expand Down
6 changes: 5 additions & 1 deletion include/utils/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/type_ptr.hpp>

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>

namespace shift
{
// TODO input class
Expand Down Expand Up @@ -93,7 +97,7 @@ class Camera

private:
/* Camera properties */
glm::vec3 _pos{glm::vec3(0.0, 0.0, 3.0)};
glm::vec3 _pos{glm::vec3(0.0, 0.0, 5.0)};
// camera coord system
// The camera coord system of OpenGL is right hand system
// But the positive view dir is negative Z
Expand Down
2 changes: 1 addition & 1 deletion shaders/examples/fluidSim/quad_fs.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$input v2f_texCoord0
$input v2f_texCoord0, v2f_posWS

#include "../../utils/bgfx_compute.sh"
#include "velocity_uniforms.sh"
Expand Down
81 changes: 81 additions & 0 deletions shaders/examples/fluidSim/quad_objectInter_fs.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
$input v2f_texCoord0, v2f_posWS

#include "../../utils/bgfx_compute.sh"
#include "velocity_uniforms.sh"

#define bufferSize 512

BUFFER_RO(_isFluid, float, 0);
BUFFER_RO(_curVelX, float, 1);
BUFFER_RO(_curVelY, float, 2);

vec3 HSVtoRGB(float H, float S, float V) {
// Normalize the hue to [0, 360)
if (H < 0.0) H += 360.0;
if (H >= 360.0) H -= 360.0;

float C = V * S;
float X = C * (1.0 - abs(mod(H / 60.0, 2.0) - 1.0));
float m = V - C;

float r, g, b;

if (H >= 0.0 && H < 60.0) {
r = C; g = X; b = 0.0;
} else if (H >= 60.0 && H < 120.0) {
r = X; g = C; b = 0.0;
} else if (H >= 120.0 && H < 180.0) {
r = 0.0; g = C; b = X;
} else if (H >= 180.0 && H < 240.0) {
r = 0.0; g = X; b = C;
} else if (H >= 240.0 && H < 300.0) {
r = X; g = 0.0; b = C;
} else {
r = C; g = 0.0; b = X;
}

// Add m to the final result to adjust brightness
r += m;
g += m;
b += m;

// Return the final color in RGB
return vec3(r, g, b);
}

float dir2Angle(float x, float y) {
float angle = atan2(x, y) * 180.0f / 3.14159;
if(angle < 0) angle += 360.0f;

return angle;
}

void main() {
int index = int(gl_FragCoord.x) + bufferSize * int(gl_FragCoord.y);

gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);

float dis = distance(v2f_posWS, vec3(uMousePosX, uMousePosY, uInterPosZ));

if(dis < uRadius) {
if (abs(_curVelX[index]) < 1e-5 && abs(_curVelY[index]) < 1e-5) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
} else {
vec2 velDir = vec2(_curVelX[index], _curVelY[index]);
float angle = dir2Angle(velDir.y, velDir.x);
float speed = length(velDir);

float H = angle;
float S = min(speed / 10.0f, 1.0f);
float V = min(speed / 10.0f, 1.0f);

vec3 color = HSVtoRGB(H, 1.0, 1.0);

gl_FragColor = vec4(color, 1.0);
}
} else {
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
}

//gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
5 changes: 5 additions & 0 deletions shaders/examples/fluidSim/quad_objectInter_varying.def.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vec3 v2f_posWS : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
vec2 v2f_texCoord0 : TEXCOORD0 = vec2(0.0, 0.0);

vec3 a_position : POSITION;
vec2 a_texcoord0 : TEXCOORD0;
3 changes: 2 additions & 1 deletion shaders/examples/fluidSim/quad_ws_vs.sc
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
$input a_position, a_texcoord0
$output v2f_texCoord0
$output v2f_texCoord0, v2f_posWS

#include "../../utils/bgfx_shader.sh"

void main() {
//gl_Position = vec4(a_position, 1.0);
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
v2f_texCoord0 = a_texcoord0;
v2f_posWS = mul(u_model[0], vec4(a_position, 1.0)).xyz;
}
1 change: 1 addition & 0 deletions shaders/examples/fluidSim/velocity_addSource_cs.sc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ BUFFER_RW(_curVelX, float, 2);
BUFFER_RW(_curVelY, float, 3);
BUFFER_WO(_isFluid, float, 4);


NUM_THREADS(8, 8, 1)
void main() {
uvec2 pos = gl_GlobalInvocationID.xy;
Expand Down
27 changes: 26 additions & 1 deletion src/utils/camera.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "glm/ext/matrix_transform.hpp"
#include <GLFW/glfw3.h>
#include <utils/camera.hpp>

Expand Down Expand Up @@ -31,6 +30,8 @@ void Camera::OnUpdate(float ts)
glm::vec2 mousePos = glm::vec2(x, y);
// get mouse delta pos
glm::vec2 deltaMousePos = (mousePos - _lastMousePos) * ts;
// update last mouse pos
_lastMousePos = mousePos;

// Every actions happen here need the right mouse button to be pressed
// Should be intergrated into input system
Expand Down Expand Up @@ -67,7 +68,31 @@ void Camera::OnUpdate(float ts)
isMoved = true;
}

if (isKeyDown(_window, GLFW_KEY_Q))
{
_pos += _upDir * _moveSpeed * ts;
isMoved = true;
}

if (isKeyDown(_window, GLFW_KEY_E))
{
_pos -= _upDir * _moveSpeed * ts;
isMoved = true;
}

// camera rotations
if (deltaMousePos.x != 0.0f || deltaMousePos.y != 0.0f)
{
float pitchDelta = deltaMousePos.y * _rotateSpped;
float yawDelta = deltaMousePos.x * _rotateSpped;

// TODO: figure out how quat works in graphics
glm::quat q = glm::normalize(
glm::cross(glm::angleAxis(-pitchDelta, _rightDir), glm::angleAxis(-yawDelta, glm::vec3(0.0f, 1.0f, 0.0f))));
_viewDir = glm::rotate(q, _viewDir);

isMoved = true;
}

// recalculate the view and proj matrices
if (isMoved)
Expand Down

0 comments on commit 63596d3

Please sign in to comment.