working mouse picking

This commit is contained in:
2023-06-06 12:36:05 +02:00
parent a2b5424888
commit ccdcdac7c6
5 changed files with 38 additions and 21 deletions

View File

@@ -135,4 +135,12 @@ typedef Mat4<float> Mat4f;
typedef Mat4<int> Mat4i; typedef Mat4<int> Mat4i;
typedef Mat4<double> Mat4d; typedef Mat4<double> Mat4d;
template<typename T>
inline bool operator==(const Mat4<T>& mat, const Mat4<T>& other) {
return mat.x0 == other.x0 && mat.y0 == other.y0 && mat.z0 == other.z0 && mat.w0 == other.w0 &&
mat.x1 == other.x1 && mat.y1 == other.y1 && mat.z1 == other.z1 && mat.w1 == other.w1 &&
mat.x2 == other.x2 && mat.y2 == other.y2 && mat.z2 == other.z2 && mat.w2 == other.w2 &&
mat.x3 == other.x3 && mat.y3 == other.y3 && mat.z3 == other.z3 && mat.w3 == other.w3;
}
} // namespace td } // namespace td

View File

@@ -20,6 +20,11 @@ Vec3<T> operator- (const Vec3<T>& vect) {
return { -vect.x, -vect.y, -vect.z }; return { -vect.x, -vect.y, -vect.z };
} }
template<typename T>
Vec4<T> operator- (const Vec4<T>& vect) {
return { -vect.x, -vect.y, -vect.z, -vect.w };
}
template<typename T> template<typename T>
Vec3<T> operator+ (const Vec3<T>& vect, const Vec3<T>& other) { Vec3<T> operator+ (const Vec3<T>& vect, const Vec3<T>& other) {
return { vect.x + other.x, vect.y + other.y, vect.z + other.y }; return { vect.x + other.x, vect.y + other.y, vect.z + other.y };
@@ -70,10 +75,10 @@ T Dot(const Vec4<T>& vect, const Vec4<T>& other) {
template<typename T> template<typename T>
Vec4<T> Dot(const Mat4<T>& mat, const Vec4<T>& vect) { Vec4<T> Dot(const Mat4<T>& mat, const Vec4<T>& vect) {
return { return {
Dot(*reinterpret_cast<const Vec4<T>*>(&mat), vect), mat.x0 * vect.x + mat.x1 * vect.y + mat.x2 * vect.z + mat.x3 * vect.w,
Dot(*reinterpret_cast<const Vec4<T>*>(mat.data() + Mat4<T>::MATRIX_SIZE), vect), mat.y0 * vect.x + mat.y1 * vect.y + mat.y2 * vect.z + mat.y3 * vect.w,
Dot(*reinterpret_cast<const Vec4<T>*>(mat.data() + 2 * Mat4<T>::MATRIX_SIZE), vect), mat.z0 * vect.x + mat.z1 * vect.y + mat.z2 * vect.z + mat.z3 * vect.w,
Dot(*reinterpret_cast<const Vec4<T>*>(mat.data() + 3 * Mat4<T>::MATRIX_SIZE), vect), mat.w0 * vect.x + mat.w1 * vect.y + mat.w2 * vect.z + mat.w3 * vect.w
}; };
} }
@@ -84,7 +89,7 @@ Mat4<T> Dot(const Mat4<T>& mat, const Mat4<T>& other) {
for (std::size_t i = 0; i < Mat4<T>::MATRIX_SIZE; i++) { for (std::size_t i = 0; i < Mat4<T>::MATRIX_SIZE; i++) {
for (std::size_t j = 0; j < Mat4<T>::MATRIX_SIZE; j++) { for (std::size_t j = 0; j < Mat4<T>::MATRIX_SIZE; j++) {
for (std::size_t k = 0; k < Mat4<T>::MATRIX_SIZE; k++) { for (std::size_t k = 0; k < Mat4<T>::MATRIX_SIZE; k++) {
result.at(i, j) = mat.at(i, k) * other.at(k, j); result.at(i, j) += mat.at(i, k) * other.at(k, j);
} }
} }
} }

View File

@@ -17,8 +17,10 @@ struct Camera {
Vec3f CamPos; Vec3f CamPos;
Vec3f m_Front {0, -1, 0};
float m_Yaw = -3.141592653f / 2.0f; float m_Yaw = -3.141592653f / 2.0f;
constexpr static float m_Pitch = -3.141592653f / 2.0f + 0.0000001f; float m_Pitch = -3.141592653f / 2.0f - 0.0000001f;
}; };
class Renderer { class Renderer {

View File

@@ -91,9 +91,9 @@ void Renderer::SetZoom(float zoom) {
} }
void Renderer::SetCamMovement(const Vec2f& mov) { void Renderer::SetCamMovement(const Vec2f& mov) {
Vec2f cursor = {static_cast<float>(m_WindowSize.x) / 2.0f - mov.x, static_cast<float>(m_WindowSize.y) / 2.0f - mov.y}; m_Camera.m_Pitch -= mov.y / 50.0f;
Vec2f worldMovement = GetCursorWorldPos(cursor, m_WindowSize.x, m_WindowSize.y); m_Camera.m_Yaw += mov.x / 50.0f;
SetCamPos({worldMovement.x, m_Camera.CamPos.y, worldMovement.y}); SetCamPos(m_Camera.CamPos);
} }
void Renderer::SetCamPos(const Vec3f& newPos) { void Renderer::SetCamPos(const Vec3f& newPos) {
@@ -107,6 +107,7 @@ void Renderer::SetCamPos(const Vec3f& newPos) {
m_Camera.CamPos = newPos; m_Camera.CamPos = newPos;
m_Camera.viewMatrix = maths::Look(m_Camera.CamPos, front, { 0, 1, 0 }); m_Camera.viewMatrix = maths::Look(m_Camera.CamPos, front, { 0, 1, 0 });
m_Camera.InvViewMatrix = maths::Inverse(m_Camera.viewMatrix); m_Camera.InvViewMatrix = maths::Inverse(m_Camera.viewMatrix);
m_WorldShader->Start(); m_WorldShader->Start();
m_WorldShader->SetViewMatrix(m_Camera.viewMatrix); m_WorldShader->SetViewMatrix(m_Camera.viewMatrix);
m_EntityShader->Start(); m_EntityShader->Start();
@@ -115,15 +116,16 @@ void Renderer::SetCamPos(const Vec3f& newPos) {
Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight) { Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight) {
float relativeX = 1 - (cursorPos.x / windowWidth * 2); float relativeX = (cursorPos.x / windowWidth * 2) - 1.0f;
float relativeY = 1 - (cursorPos.y / windowHeight * 2); float relativeY = 1.0f - (cursorPos.y / windowHeight * 2);
Vec4f rayClip{ relativeX, relativeY, -1.0f, 1.0f }; Vec4f rayClip{ relativeX, relativeY, -1.0f, 1.0f };
Vec4f rayEye = maths::Dot(m_Camera.InvProjectionMatrix, rayClip); Vec4f rayEye = maths::Dot(m_Camera.InvProjectionMatrix, rayClip);
rayEye = { rayEye.x, rayEye.y, -1.0f, 0.0f }; rayEye = { rayEye.x, rayEye.y, -1.0f, 0.0f };
Vec4f rayWorld = maths::Dot(m_Camera.InvViewMatrix, rayEye); Vec4f rayWorld = maths::Dot(maths::Transpose(m_Camera.InvViewMatrix), rayEye); // why transpose ? I don't know
float lambda = -m_Camera.CamPos.y / rayWorld.y; float lambda = -m_Camera.CamPos.y / rayWorld.y;

View File

@@ -160,7 +160,7 @@ void WorldRenderer::Click() {
void WorldRenderer::SetCamPos(float camX, float camY) { void WorldRenderer::SetCamPos(float camX, float camY) {
m_CamPos = { camX, camY }; m_CamPos = { camX, camY };
m_Renderer->SetCamPos({ camX, 50, camY }); m_Renderer->SetCamPos({ camX, 25, camY });
} }
void WorldRenderer::DetectClick() { void WorldRenderer::DetectClick() {