Skip to content

Commit

Permalink
Mouse hover
Browse files Browse the repository at this point in the history
Not perfectly accurate yet, need to find out why
  • Loading branch information
quadpixels committed Jun 17, 2024
1 parent dd72d26 commit de59594
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
22 changes: 22 additions & 0 deletions chunkindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,25 @@ void ChunkGrid::FromIX(int ix, int& x, int& y, int& z) {
y = ix / zdim;
z = ix % zdim;
}

bool AABB::IntersectRay(const glm::vec3& o, const glm::vec3& d) {
glm::vec3 inv_dir = 1.0f / d;
glm::vec3 t0 = (lb - o) * inv_dir;
glm::vec3 t1 = (ub - o) * inv_dir;

if (inv_dir.x < 0) {
std::swap(t0.x, t1.x);
}
if (inv_dir.y < 0) {
std::swap(t0.y, t1.y);
}
if (inv_dir.z < 0) {
std::swap(t0.z, t1.z);
}

float t00 = std::max(t0.z, std::max(t0.y, t0.x));
float t11 = std::min(t1.z, std::min(t1.y, t1.x));

if (0 <= t11 && t00 <= t11) return true;
else return false;
}
1 change: 1 addition & 0 deletions chunkindex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class AABB {
else if (ret.z > ub.z) ret.z = ub.z;
return ret;
}
bool IntersectRay(const glm::vec3& o, const glm::vec3& d);
};

// Indices for multiple Chunk's
Expand Down
15 changes: 15 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ char g_cam_dx = 0, g_cam_dy = 0, g_cam_dz = 0, // CONTROL axes, not OpenGL
g_arrow_dx = 0, g_arrow_dy = 0, g_arrow_dz = 0;
std::bitset<18> g_cam_flags;

glm::vec3 WindowCoordToPickRayDir(Camera* cam, int x, int y) {
glm::vec3 cam_right = glm::normalize(glm::cross(cam->lookdir, cam->up));
glm::vec3 cam_up = glm::normalize(cam->up);
float u = (x * 2.0f / WIN_W) - 1.0f;
float v = -((y * 2.0f / WIN_H) - 1.0f);
const float t = tanf(30.0f / (180.0f / 3.14159f));
v = v * t;
u = u * t * WIN_W / WIN_H;
glm::vec3 ret = cam_right * u + cam_up * v + cam->lookdir;
ret = glm::normalize(ret);
return ret;
}

// These are all scaffolds
Triangle *g_triangle[2];
ColorCube *g_colorcube[2];
Expand Down Expand Up @@ -105,6 +118,8 @@ bool g_aa = true, g_shadows = true;
unsigned g_test_tri_vao;
unsigned g_test_quad_vao;

int g_mouse_x = 0, g_mouse_y = 0;

void MyInit() {
{
float vertices[] = {
Expand Down
18 changes: 18 additions & 0 deletions main_d3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma comment(lib, "dxguid.lib")

#include <Windows.h>
#include <windowsx.h> // GET_{X,Y}_LPARAM
#undef max
#undef min

Expand Down Expand Up @@ -50,6 +51,9 @@ extern MainMenu* g_mainmenu;
extern Chunk* ALLNULLS[26];
extern TextMessage* g_textmessage;
extern Particles* g_particles;
extern int g_mouse_x, g_mouse_y;
extern glm::vec3 WindowCoordToPickRayDir(Camera* cam, int x, int y);
int g_titlebar_size;

bool init_done = false;
ID3D11Device *g_device11;
Expand Down Expand Up @@ -598,6 +602,12 @@ void CreateCyclimbWindow() {
WIN_W, WIN_H,
nullptr, nullptr,
hinstance, nullptr);

TITLEBARINFOEX* ptinfo = (TITLEBARINFOEX*)malloc(sizeof(TITLEBARINFOEX));
ptinfo->cbSize = sizeof(TITLEBARINFOEX);
SendMessage(g_hwnd, WM_GETTITLEBARINFOEX, 0, (LPARAM)ptinfo);
g_titlebar_size = ptinfo->rcTitleBar.bottom - ptinfo->rcTitleBar.top;
delete ptinfo;
}

int main_d3d11(int argc, char** argv) {
Expand Down Expand Up @@ -1022,6 +1032,11 @@ void OnKeyUp(WPARAM wParam, LPARAM lParam) {
else GetCurrentGameScene()->OnKeyReleased(char(tolower(wParam)));
}

void OnMouseMove(int x, int y) {
g_mouse_x = x;
g_mouse_y = y + g_titlebar_size;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
Expand All @@ -1048,6 +1063,9 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
}
update();
break;
case WM_MOUSEMOVE:
OnMouseMove(GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam));
break;
default:
return DefWindowProc(hwnd, message, wparam, lparam);
}
Expand Down
13 changes: 11 additions & 2 deletions scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ const float ClimbScene::INVERSE_INERTIA = 0.05f;
extern bool IsGL();
extern bool IsD3D11();
extern bool IsD3D12();
extern glm::vec3 WindowCoordToPickRayDir(Camera* cam, int x, int y);
extern int g_mouse_x, g_mouse_y;

const glm::vec3 ClimbScene::PLAYER_ROPE_ENDPOINT[8] =
{
Expand Down Expand Up @@ -161,8 +163,15 @@ void ClimbScene::PrepareSpriteListForRender() {
ChunkSprite* csp = dynamic_cast<ChunkSprite*>(sp);
if (csp) {
AABB aabb = csp->GetAABBInWorld();
if (campos.x >= aabb.lb.x && campos.x <= aabb.ub.x &&
campos.y >= aabb.lb.y && campos.y <= aabb.ub.y) {

glm::vec3 pickray_dir = WindowCoordToPickRayDir(camera, g_mouse_x, g_mouse_y);
glm::vec3 pickray_orig = camera->pos;

bool x1 = (campos.x >= aabb.lb.x && campos.x <= aabb.ub.x &&
campos.y >= aabb.lb.y && campos.y <= aabb.ub.y);
bool x2 = aabb.IntersectRay(pickray_orig, pickray_dir);

if (x1 || x2) {
highlight_sprite->pos = sp->pos;
float phase = fabs(sin(light_phase * 4));
highlight_sprite->scale.x = (aabb.ub.x - aabb.lb.x) + phase + 1;
Expand Down

0 comments on commit de59594

Please sign in to comment.