Skip to content

Commit

Permalink
test rotations (doesnt work)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLastra8 committed Oct 30, 2024
1 parent 67542a8 commit 33d8bbc
Showing 1 changed file with 102 additions and 2 deletions.
104 changes: 102 additions & 2 deletions Maker/Editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ glm::dmat4 viewMatrix;
static bool middleMousePressed = false;
static ivec2 lastMousePosition;
static bool rightMousePressed = false;
static bool leftMousePressed = false;
static vector <GameObject> gameObjects;
GameObject* selectedObject = nullptr;
static double moveSpeed = 0.1;
float yaw = 0.0f;
float pitch = 0.0f;
const float MAX_PITCH = 89.0f;
bool altKeyPressed = false;
vec3 target;
ivec2 delta;
int lastMouseX;
int lastMouseY;


// Function to convert screen coordinates to world coordinates
Expand Down Expand Up @@ -104,6 +115,34 @@ bool rayIntersectsBoundingBox(const glm::vec3& rayOrigin, const glm::vec3& rayDi
return true;
}

glm::vec3 getRayFromMouse(int mouseX, int mouseY, const glm::mat4& projection, const glm::mat4& view, const glm::ivec2& viewportSize) {
float x = (2.0f * mouseX) / viewportSize.x - 1.0f;
float y = 1.0f - (2.0f * mouseY) / viewportSize.y;
glm::vec4 rayClip = glm::vec4(x, y, -1.0f, 1.0f);

glm::vec4 rayEye = glm::inverse(projection) * rayClip;
rayEye = glm::vec4(rayEye.x, rayEye.y, -1.0f, 0.0f);

glm::vec3 rayWorld = glm::normalize(glm::vec3(glm::inverse(view) * rayEye));
return rayWorld;
}

GameObject* raycastFromMouseToGameObject(int mouseX, int mouseY, const glm::mat4& projection, const glm::mat4& view, const glm::ivec2& viewportSize) {
glm::vec3 rayOrigin = glm::vec3(glm::inverse(view) * glm::vec4(0, 0, 0, 1));
glm::vec3 rayDirection = getRayFromMouse(mouseX, mouseY, projection, view, viewportSize);

GameObject* hitObject = nullptr;

for (auto& go : gameObjects) {
if (rayIntersectsBoundingBox(rayOrigin, rayDirection, go.boundingBox())) {
hitObject = &go;
break;
}
}

return hitObject;
}

static void drawFloorGrid(int size, double step) {
glBegin(GL_LINES);
for (double i = -size; i <= size; i += step) {
Expand Down Expand Up @@ -161,18 +200,60 @@ static void mouseWheel_func(int direction) {
camera.transform().translate(vec3(0, 0, direction * 0.1));
}

glm::vec2 getMousePosition() {
int x, y;
SDL_GetMouseState(&x, &y);
return glm::vec2(static_cast<float>(x), static_cast<float>(y));
}

void orbitCamera(const vec3& target, int deltaX, int deltaY) {
const float sensitivity = 0.1f;

yaw += deltaX * sensitivity;
pitch -= deltaY * sensitivity;
float distance = glm::length(camera.transform().pos() - target);
vec3 newPosition;
newPosition.x = target.x + distance * cos(glm::radians(yaw)) * cos(glm::radians(pitch));
newPosition.y = target.y + distance * sin(glm::radians(pitch));
newPosition.z = target.z + distance * sin(glm::radians(yaw)) * cos(glm::radians(pitch));

camera.transform().pos() = newPosition;
camera.transform().lookAt(target);
}

static void mouseButton_func(int button, int state, int x, int y) {
if (button == SDL_BUTTON_MIDDLE) {
middleMousePressed = (state == SDL_PRESSED);
if (middleMousePressed) {
lastMousePosition = ivec2(x, y);
lastMousePosition = ivec2(x, y);
}
}
if (button == SDL_BUTTON_RIGHT) {
rightMousePressed = (state == SDL_PRESSED);
}
if (button == SDL_BUTTON_LEFT) {
if (leftMousePressed) {
// Guarda la posición del ratón al hacer clic
lastMousePosition = ivec2(x, y);
glm::vec2 mouseScreenPos = getMousePosition();
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)WINDOW_SIZE.x / WINDOW_SIZE.y, 0.1f, 100.0f);
glm::mat4 view = camera.view();
selectedObject = raycastFromMouseToGameObject(mouseScreenPos.x, mouseScreenPos.y, projection, view, WINDOW_SIZE);

// Establece el objetivo de la órbita
if (selectedObject != nullptr) {
target = selectedObject->transform().pos();
}
else {
target = glm::vec3(0, 0, 0);
}
}
else {

leftMousePressed = false;
}
}
}

static void handleKeyboardInput() {
const Uint8* state = SDL_GetKeyboardState(NULL);
if (rightMousePressed) {
Expand All @@ -199,7 +280,21 @@ static void handleKeyboardInput() {
moveSpeed *= 2;
}
}
if (leftMousePressed) {

if (leftMousePressed && state[SDL_SCANCODE_LALT]) {
glm::vec2 mouseScreenPos = getMousePosition();
ivec2 delta = ivec2(mouseScreenPos) - lastMousePosition; // calcula el cambio
lastMousePosition = ivec2(mouseScreenPos); // actualiza la última posición del ratón

orbitCamera(target, delta.x, delta.y); // llama a la función de órbita
}
}

}



static void mouseMotion_func(int x, int y) {
if (middleMousePressed) {

Expand All @@ -210,8 +305,13 @@ static void mouseMotion_func(int x, int y) {
camera.transform().translate(vec3(delta.x * 0.01, 0, -delta.y * 0.01));
camera.transform().translate(vec3(0, delta.y * 0.01, 0));
}

if (leftMousePressed) {
lastMousePosition = ivec2(x, y);
}
}


std::string getFileExtension(const std::string& filePath) {
// Find the last dot in the file path
size_t dotPosition = filePath.rfind('.');
Expand Down

0 comments on commit 33d8bbc

Please sign in to comment.