I am trying to see which object is clicked for a fixed Z position. I am using perspective projection.
For the rendering of my objects, I use:
glm::mat4 mvp = m_ProjectionView * model; // Inversed
For the calculation of the world position of the mouse, I have tried the following, but it is not converting correctly.
glm::mat4 projViewInverse = glm::inverse( projection * view );
glm::vec4 cursorWorldPosition = glm::vec4( projViewInverse * glm::vec4( ( mousePosX / scrWidth ) * 2.0f - 1.0f, ( mousePosY / scrHeight ) * 2.0f - 1.0f, -1, 1 ) );
cursorWorldPosition.w = 1.0f / cursorWorldPosition.w;
cursorWorldPosition.x *= cursorWorldPosition.w;
cursorWorldPosition.y *= cursorWorldPosition.w;
cursorWorldPosition.z *= cursorWorldPosition.w;
Related
I'm trying to use bullet physics to draw a ray to hit a game object in the scene so I can select it, i am using the camera matrix to draw a ray and then pick an object in space and then look through a list of game objects and look for the same location.
On Mouse press I have the following code, it seems to be off and only picks the items some times:
glm::vec4 lRayStart_NDC(
((float)lastX / (float)RECT_WIDTH - 0.5f) * 2.0f,
((float)lastY / (float)RECT_HEIGHT - 0.5f) * 2.0f,
-1.0,
1.0f
);
glm::vec4 lRayEnd_NDC(
((float)lastX / (float)RECT_WIDTH - 0.5f) * 2.0f,
((float)lastY / (float)RECT_HEIGHT - 0.5f) * 2.0f,
0.0,
1.0f
);
projection = glm::perspective(glm::radians(SceneManagement::getInstance()->MainCamera->GetVOW()), (float)RECT_WIDTH / (float)RECT_HEIGHT, 0.1f, 100.0f);
glm::mat4 InverseProjectionMatrix = glm::inverse(projection);
view = SceneManagement::getInstance()->MainCamera->GetViewMatrix();
glm::mat4 InverseViewMatrix = glm::inverse(view);
glm::vec4 lRayStart_camera = InverseProjectionMatrix * lRayStart_NDC;
lRayStart_camera /= lRayStart_camera.w;
glm::vec4 lRayStart_world = InverseViewMatrix * lRayStart_camera;
lRayStart_world /= lRayStart_world.w;
glm::vec4 lRayEnd_camera = InverseProjectionMatrix * lRayEnd_NDC;
lRayEnd_camera /= lRayEnd_camera.w;
glm::vec4 lRayEnd_world = InverseViewMatrix * lRayEnd_camera;
lRayEnd_world /= lRayEnd_world.w;
glm::vec3 lRayDir_world(lRayEnd_world - lRayStart_world);
lRayDir_world = glm::normalize(lRayDir_world);
glm::vec3 out_end = SceneManagement::getInstance()->MainCamera->GetCamPosition() + SceneManagement::getInstance()->MainCamera->GetCamFront() * 1000.0f;
btCollisionWorld::ClosestRayResultCallback RayCallback(
btVector3(SceneManagement::getInstance()->MainCamera->GetCamPosition().x, SceneManagement::getInstance()->MainCamera->GetCamPosition().y, SceneManagement::getInstance()->MainCamera->GetCamPosition().z),
btVector3(out_end.x, out_end.y, out_end.z)
);
PhysicsManager::getInstance()->dynamicsWorld->rayTest(
btVector3(SceneManagement::getInstance()->MainCamera->GetCamPosition().x, SceneManagement::getInstance()->MainCamera->GetCamPosition().y, SceneManagement::getInstance()->MainCamera->GetCamPosition().z),
btVector3(out_end.x, out_end.y, out_end.z),
RayCallback
);
if (RayCallback.hasHit())
{
btTransform position = RayCallback.m_collisionObject->getInterpolationWorldTransform();
printf("Collision \n");
for (int i = 0; i < SceneManagement::getInstance()->gObjects.size(); i++)
{
if (SceneManagement::getInstance()->gObjects.at(i)->transform.Position.x == position.getOrigin().getX() &&
SceneManagement::getInstance()->gObjects.at(i)->transform.Position.y == position.getOrigin().getY() &&
SceneManagement::getInstance()->gObjects.at(i)->transform.Position.z == position.getOrigin().getZ())
{
int select = i;
SceneManagement::getInstance()->SelectedGameObject = SceneManagement::getInstance()->gObjects.at(select);
SceneManagement::getInstance()->SelectedGameObject->DisplayInspectorUI();
return;
}
}
}
This check
SceneManagement::getInstance()->gObjects.at(i)->transform.Position.x == position.getOrigin().getX() &&
SceneManagement::getInstance()->gObjects.at(i)->transform.Position.y == position.getOrigin().getY() &&
SceneManagement::getInstance()->gObjects.at(i)->transform.Position.z == position.getOrigin().getZ()
fails due to numerical precision issues.
You should check the "equality" of vectors only up to some precision.
Please, use some distance function and the following check instead of your if() condition:
const double EPSILON = 1e-4; // experiment with this value
auto objPos = SceneManagement::getInstance()->gObjects.at(i)->transform.Position;
bool isWithinRange = distance3D(objPos, position.getOrigin()) < EPSILON;
if (isWithinRange)
{
int select = i;
...
}
The distance3D function is simply the euclidean distance in 3D. The glm::distance function should do, just convert both vectors to glm::vec3 format.
I'm trying to get the coordinates (x,y) of the grid (z = 0) using only the cursor coordinates. After a long search I found this way to do that using the glm::unproject.
First I'm getting the cursor coordinates using the callback:
void cursorCallback(GLFWwindow *window, double x, double y)
{
this->cursorCoordinate = glm::vec3(x, (this->windowHeight - y - 1.0f), 0.0f);
}
an then converting these coordinates:
glm::vec3 cursorCoordinatesToWorldCoordinates()
{
glm::vec3 pointInitial = glm::unProject(
glm::vec3(this->cursorCoordinate.x, this->cursorCoordinate.y, 0.0),
this->modelMatrix * this->viewMatrix,
this->projectionMatrix,
this->viewPort
);
glm::vec3 pointFinal = glm::unProject(
glm::vec3(this->cursorCoordinate.x, this->cursorCoordinate.y, 1.0),
this->modelMatrix * this->viewMatrix,
this->projectionMatrix,
this->viewPort
);
glm::vec3 vectorDirector = pointFinal - pointInitial;
double lambda = (-pointInitial.y) / vectorDirector.y;
double x = pointInitial.x + lambda * vectorDirector.x;
double y = pointInitial.z + lambda * vectorDirector.z;
return glm::vec3(x, y, 0.0f);
}
I use an ArcBall camera to rotate the world around specified axis, so that is how I generate the MVP matrixes:
this->position = glm::vec3(0.0f, 10.0f, 5.0f);
this->up = glm::vec3(0.0f, 1.0f, 0.0f);
this->lookAt = glm::vec3(0.0f, 0.0f, 0.0f);
this->fieldView = 99.0f;
this->farDistance = 100.0f;
this->nearDistance = 0.1f;
this->modelMatrix = glm::mat4(1.0f);
this->viewMatrix = glm::lookAt(this->position, this->lookAt, this->up) * glm::rotate(glm::degrees(this->rotationAngle) * this->dragSpeed, this->rotationAxis);
this->projectionMatrix = glm::perspective(glm::radians(this->fieldView), 1.0f, this->nearDistance, this->farDistance);
But something is going wrong because I'm not getting the right results. Look this print of the application:
each square is 1 unit, the cube is rendered at position (0, 0, 0). With rotationAngle = 0 when a put the cursor at (0,0), (1,1), (2,2), (3,3), (4,4), (5,5) I get (0, 5.7), (0.8, 6.4), (1.6, 6.9), (2.4, 7.6), (3.2, 8.2), (4.2, 8.8) respectivally. That's not expected.
Why y is delayed by 6 units?
It's necessary rotate the result cursorCoordinatesToWorldCoordinates based on rotationAngle isn't?
--
That I already did:
Checked if the viewport match with glViewport - OK
Checked the opengl coordinates (Y is up, not Z) - OK
You want to intersect the ray from glm::vec3(this->cursorCoordinate.x, this->cursorCoordinate.y, 0.0) to glm::vec3(this->cursorCoordinate.x, this->cursorCoordinate.y, 1.0) with the grid in world space, rather than model space (of the cuboid).
You've to skip this.modelMatrix:
glm::vec3 pointInitial = glm::unProject(
glm::vec3(this->cursorCoordinate.x, this->cursorCoordinate.y, 0.0),
this->viewMatrix,
this->projectionMatrix,
this->viewPort);
glm::vec3 pointFinal = glm::unProject(
glm::vec3(this->cursorCoordinate.x, this->cursorCoordinate.y, 1.0),
this->viewMatrix,
this->projectionMatrix,
this->viewPort);
In any case this->modelMatrix * this->viewMatrix is incorrect. If you eant to intersect the ray with an object in model space, then it has to be this->viewMatrix * this->modelMatrix. Matrix multiplication is not Commutative.
How do i create a camera with a rotation on the z axis that can move in every direction? I have tried many things, this is my code:
glm::mat4 view = glm::mat4(1.0f);
glm::vec3 camRotInRadians = glm::vec3(toRadians(rot.x), toRadians(rot.y), toRadians(rot.z));
glm::vec3 direction = glm::vec3(
cos(camRotInRadians.x) * cos(camRotInRadians.y),
sin(camRotInRadians.x),
cos(camRotInRadians.x) * sin(camRotInRadians.y)
);
// Right vector
glm::vec3 right = glm::vec3(
cos(toRadians(rot.z)) * cos(toRadians(rot.y + 270.0f)),
sin(toRadians(rot.z)),
cos(toRadians(rot.z)) * sin(toRadians(rot.y + 270.0f))
);
glm::vec3 up = glm::cross(right, direction);
view = glm::lookAt(
pos - direction * glm::vec3(40.0f),
pos + direction,
up
);
I'm trying to calculate a per tile frustum by getting the screen space coordinates and then using a cross product to get the view frustum planes. However when I check which tiles are affected by a light they are in the opposite direction, as in, they're moving in the opposite direction of the camera as well as being behind it. I've tried changing the cross product order but it doesn't appear to be working either way. Here's the code that generates the frustums and checks if a light intersects it:
//Start by getting the corners in screen space
uint minX = MAX_WORK_GROUP_SIZE * gl_WorkGroupID.x;
uint minY = MAX_WORK_GROUP_SIZE * gl_WorkGroupID.y;
uint maxX = MAX_WORK_GROUP_SIZE * (gl_WorkGroupID.x + 1);
uint maxY = MAX_WORK_GROUP_SIZE * (gl_WorkGroupID.y + 1);
//Convert these corners into NDC and then convert them to view space
vec4 tileCorners[4];
tileCorners[0] = unProject(vec4( (float(minX)/SCREEN_WIDTH) * 2.0f - 1.0f, (float(minY)/SCREEN_HEIGHT) * 2.0f - 1.0f, 1.0f, 1.0f));
tileCorners[1] = unProject(vec4( (float(maxX)/SCREEN_WIDTH) * 2.0f - 1.0f, (float(minY)/SCREEN_HEIGHT) * 2.0f - 1.0f, 1.0f, 1.0f));
tileCorners[2] = unProject(vec4( (float(maxX)/SCREEN_WIDTH) * 2.0f - 1.0f, (float(maxY)/SCREEN_HEIGHT) * 2.0f - 1.0f, 1.0f, 1.0f));
tileCorners[3] = unProject(vec4( (float(minX)/SCREEN_WIDTH) * 2.0f - 1.0f, (float(maxY)/SCREEN_HEIGHT) * 2.0f - 1.0f, 1.0f, 1.0f));
//Create the frustum planes by using the cross product between these points
frustum[0] = CreatePlane(tileCorners[0], tileCorners[1]); //bot
frustum[1] = CreatePlane(tileCorners[1], tileCorners[2]); //right
frustum[2] = CreatePlane(tileCorners[2], tileCorners[3]); //top
frustum[3] = CreatePlane(tileCorners[3], tileCorners[0]); //left
and the functions:
vec4 unProject(vec4 v)
{
v = inverseProjectionMatrix * v;
v /= v.w;
return v;
}
vec4 CreatePlane( vec4 b, vec4 c )
{
vec4 normal;
normal.xyz = normalize(cross( b.xyz, c.xyz ));
normal.w = 0;
return normal;
}
float GetSignedDistanceFromPlane( vec4 p, vec4 eqn )
{
return dot( eqn.xyz, p.xyz );
}
And how I check for lights
int threadsPerTile = MAX_WORK_GROUP_SIZE*MAX_WORK_GROUP_SIZE;
for (uint i = 0; i < NUM_OF_LIGHTS; i+= threadsPerTile)
{
uint il = gl_LocalInvocationIndex + i;
if (il < NUM_OF_LIGHTS)
{
PointLight p = pointLights[il];
vec4 viewPos = viewMatrix * vec4(p.position.xyz, 1.0f);
float r = p.radius;
// if (viewPos.z + minDepthZ < r && viewPos.z - maxDepthZ < r)
// {
if( ( GetSignedDistanceFromPlane( viewPos, frustum[0] ) < r ) &&
( GetSignedDistanceFromPlane( viewPos, frustum[1] ) < r ) &&
( GetSignedDistanceFromPlane( viewPos, frustum[2] ) < r ) &&
( GetSignedDistanceFromPlane( viewPos, frustum[3] ) < r) )
{
uint id = atomicAdd(pointLightCount, 1);
pointLightIndex[id] = il;
}
// }
}
}
I've commented out the z part just for debugging. The frustums are completely reversed or I'm doing something very wrong, in this picture I'm looking behind me and up, so tiles are affected which are in the complete opposite direction of the scene, and when I move the camera the tiles move in the opposite directions as well
Apparently the frustum was calculated correctly but something about the ARB extensions (which I thought was unrelated) made everything explode. I used
#extension GL_ARB_compute_variable_group_size : enable
layout( local_size_variable ) in;
Which didn't work at all so I just changed it to
layout(local_size_x = MAX_WORK_GROUP_SIZE, local_size_y = MAX_WORK_GROUP_SIZE) in;
And on the CPU:
glDispatchCompute((1280 / 16), (720 / 16), 1);
//glDispatchComputeGroupSizeARB((1280 / 16), (720 / 16), 1, 16, 16, 1);
Which works fine, so I guess there's something about the ARB method that doesn't initialize the amount of work threads properly
I've been trying to emulate gluLookAt functionality, but with Quaternions. Each of my game object have a TranslationComponent. This component stores the object's position (glm::vec3), rotation (glm::quat) and scale (glm::vec3). The camera calculates its position each tick doing the following:
// UP = glm::vec3(0,1,0);
//FORWARD = glm::vec3(0,0,1);
cameraPosition = playerPosition - (UP * distanceUP) - (FORWARD * distanceAway);
This position code works as expexted, the camera is place 3 metres behind the player and 1 metre up. Now, the camera's Quaternion is set to the follow:
//Looking at the player's feet
cameraRotation = quatFromToRotation(FORWARD, playerPosition);
The rendering engine now takes these values and generates the ViewMatrix (camera) and the ModelMatrix (player) and then renders the scene. The code looks like this:
glm::mat4 viewTranslationMatrix =
glm::translate(glm::mat4(1.0f), cameraTransform->getPosition());
glm::mat4 viewScaleMatrix =
glm::scale(glm::mat4(1.0f), cameraTransform->getScale());
glm::mat4 viewRotationMatrix =
glm::mat4_cast(cameraTransform->getRotation());
viewMatrix = viewTranslationMatrix * viewRotationMatrix * viewScaleMatrix;
quatFromToRotation(glm::vec3 from, glm::vec3 to) is defined as the following:
glm::quat quatFromToRotation(glm::vec3 from, glm::vec3 to)
{
from = glm::normalize(from); to = glm::normalize(to);
float cosTheta = glm::dot(from, to);
glm::vec3 rotationAxis;
if (cosTheta < -1 + 0.001f)
{
rotationAxis = glm::cross(glm::vec3(0.0f, 0.0f, 1.0f), from);
if (glm::length2(rotationAxis) < 0.01f)
rotationAxis = glm::cross(glm::vec3(1.0f, 0.0f, 0.0f), from);
rotationAxis = glm::normalize(rotationAxis);
return glm::angleAxis(180.0f, rotationAxis);
}
rotationAxis = glm::cross(from, to);
float s = sqrt((1.0f + cosTheta) * 2.0f);
float invis = 1.0f / s;
return glm::quat(
s * 0.5f,
rotationAxis.x * invis,
rotationAxis.y * invis,
rotationAxis.z * invis
);
}
What I'm having troubles with is the fact the cameraRotation isn't being set correctly. No matter where the player is, the camera's forward is always (0,0,-1)
Your problem is in the line
//Looking at the player's feet
cameraRotation = quatToFromRotation(FORWARD, playerPosition);
You need to look from the camera position to the player's feet - not from "one meter above the player" (assuming the player is at (0,0,0) when you initially do this). Replace FORWARD with cameraPosition:
cameraRotation = quatToFromRotation(cameraPosition, playerPosition);
EDIT I believe you have an error in your quatToFromRotation function as well. See https://stackoverflow.com/a/11741520/1967396 for a very nice explanation (and some pseudo code) of quaternion rotation.