Commit 2c11e009 by Kai Westerkamp

Camera

parent 9c55c1f0
#include "Camera.h"
#include <iostream>
#include "glm/gtx/string_cast.hpp"
#include "quaternion_utils.h"
Camera::Camera()
{
// rotation = glm::gtx::quaternion::angleAxis(degrees(45), RotationAxis);
position = glm::vec3(20, 10, 0);
//direction = normalize(-position);
rotate(0, 0); //sets direction
up = glm::vec3(0.0f, 1.0f, 0.0f);
}
Camera::~Camera()
{
}
void Camera::move(glm::vec3 relative)
{
glm::vec3 right = cross(direction, up);
vec3 uplocal = cross(right, direction);
position += (relative.x*speed* direction)+ (relative.y*speed*uplocal)+ (relative.z*speed* right);
}
void Camera::rotate(int dx, int dy)
{
horizontalAngle += mouseSpeed * float(dx);
verticalAngle += mouseSpeed * float(dy);
direction = glm::vec3(
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);
}
void Camera::applyMatrix()
{
glm::vec3 p = position + direction;
gluLookAt(position.x, position.y, position.z,
p.x, p.y, p.z, up.x, up.y, up.z);
}
#pragma once
#define _USE_MATH_DEFINES
#include <glm/gtx/quaternion.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/norm.hpp>
#include "glut.h"
#include <math.h>
class Camera
{
public:
glm::vec3 direction;
glm::vec3 position;
glm::vec3 up;
float speed = 0.3f;
float mouseSpeed = 0.005f;
float horizontalAngle = -M_PI/2;
float verticalAngle = -M_PI/8;
Camera();
~Camera();
void move(glm::vec3 relative);
void rotate(int dx, int dy);
void applyMatrix();;
};
...@@ -87,17 +87,24 @@ ...@@ -87,17 +87,24 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Camera.cpp" />
<ClCompile Include="glut.cpp" /> <ClCompile Include="glut.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="quaternion_utils.cpp" />
<ClCompile Include="SteamTracking.cpp" /> <ClCompile Include="SteamTracking.cpp" />
<ClCompile Include="SteamVrTrackingServer.cpp" /> <ClCompile Include="SteamVrTrackingServer.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Camera.h" />
<ClInclude Include="glut.h" /> <ClInclude Include="glut.h" />
<ClInclude Include="quaternion_utils.h" />
<ClInclude Include="SteamTracking.h" /> <ClInclude Include="SteamTracking.h" />
<ClInclude Include="SteamVrTrackingServer.h" /> <ClInclude Include="SteamVrTrackingServer.h" />
<ClInclude Include="main.h" /> <ClInclude Include="main.h" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ResourceCompile Include="KinectTutorial3.rc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>
......
...@@ -27,6 +27,12 @@ ...@@ -27,6 +27,12 @@
<ClCompile Include="SteamTracking.cpp"> <ClCompile Include="SteamTracking.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="quaternion_utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="glut.h"> <ClInclude Include="glut.h">
...@@ -41,5 +47,16 @@ ...@@ -41,5 +47,16 @@
<ClInclude Include="SteamTracking.h"> <ClInclude Include="SteamTracking.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Camera.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="quaternion_utils.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="KinectTutorial3.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
...@@ -63,8 +63,8 @@ SteamTracking::~SteamTracking() ...@@ -63,8 +63,8 @@ SteamTracking::~SteamTracking()
void SteamTracking::printVRDevices() { void SteamTracking::printVRDevices() {
std::cout << "List of available VR devices:" << std::endl; std::cout << "List of available VR devices:" << std::endl;
if (!vr_system_) { if (!vr_system_) {
std::cout << "\tNot connected to VR System, no devices"; std::cout << "\tNot connected to VR System, no devices"<< std::endl;
return;
} }
for (int nDevice = 0; nDevice < vr::k_unMaxTrackedDeviceCount; ++nDevice) for (int nDevice = 0; nDevice < vr::k_unMaxTrackedDeviceCount; ++nDevice)
{ {
...@@ -86,6 +86,8 @@ void SteamTracking::printVRDevices() { ...@@ -86,6 +86,8 @@ void SteamTracking::printVRDevices() {
void SteamTracking::processEvents() void SteamTracking::processEvents()
{ {
if (!vr_system_) return;
vr::VREvent_t event; vr::VREvent_t event;
while (vr_system_->PollNextEvent(&event, sizeof(event))) while (vr_system_->PollNextEvent(&event, sizeof(event)))
{ {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "main.h" #include "main.h"
void draw() { void draw() {
drawKinectData(); mainRenderLoop();
glutSwapBuffers(); glutSwapBuffers();
} }
...@@ -16,6 +16,8 @@ bool init(int argc, char* argv[]) { ...@@ -16,6 +16,8 @@ bool init(int argc, char* argv[]) {
glutInitWindowSize(width,height); glutInitWindowSize(width,height);
glutCreateWindow("Kinect VR Point Cloud"); glutCreateWindow("Kinect VR Point Cloud");
glutKeyboardFunc(keyPressed); glutKeyboardFunc(keyPressed);
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMove);
glutDisplayFunc(draw); glutDisplayFunc(draw);
glutIdleFunc(draw); glutIdleFunc(draw);
glewInit(); glewInit();
......
#pragma once #pragma once
#include <openvr.h> #include <openvr.h>
const int width = 512; const int width = 512;
const int height = 424; const int height = 424;
const int colorwidth = 1920; const int colorwidth = 1920;
const int colorheight = 1080; const int colorheight = 1080;
void drawKinectData(); void mainRenderLoop();
void keyPressed(unsigned char key, int x, int y); void keyPressed(unsigned char key, int x, int y);
void mouseButton(int button, int state, int x, int y);
void mouseMove(int x, int y);
void captureNextFrame(vr::VREvent_t event); void captureNextFrame(vr::VREvent_t event);
void writePointCloud(); void writePointCloud();
void close(); void close();
void drawGround();
void setupGrid();
void setupFrameBuffer();
struct pntsHeader { struct pntsHeader {
......
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtx/norm.hpp>
#include "quaternion_utils.h"
// Returns a quaternion such that q*start = dest
quat RotationBetweenVectors(vec3 start, vec3 dest) {
start = normalize(start);
dest = normalize(dest);
float cosTheta = dot(start, dest);
vec3 rotationAxis;
if (cosTheta < -1 + 0.001f) {
// special case when vectors in opposite directions :
// there is no "ideal" rotation axis
// So guess one; any will do as long as it's perpendicular to start
// This implementation favors a rotation around the Up axis,
// since it's often what you want to do.
rotationAxis = cross(vec3(0.0f, 0.0f, 1.0f), start);
if (length2(rotationAxis) < 0.01) // bad luck, they were parallel, try again!
rotationAxis = cross(vec3(1.0f, 0.0f, 0.0f), start);
rotationAxis = normalize(rotationAxis);
return angleAxis(180.0f, rotationAxis);
}
// Implementation from Stan Melax's Game Programming Gems 1 article
rotationAxis = cross(start, dest);
float s = sqrt((1 + cosTheta) * 2);
float invs = 1 / s;
return quat(
s * 0.5f,
rotationAxis.x * invs,
rotationAxis.y * invs,
rotationAxis.z * invs
);
}
// Returns a quaternion that will make your object looking towards 'direction'.
// Similar to RotationBetweenVectors, but also controls the vertical orientation.
// This assumes that at rest, the object faces +Z.
// Beware, the first parameter is a direction, not the target point !
quat LookAt(vec3 direction, vec3 desiredUp) {
if (length2(direction) < 0.0001f)
return quat();
// Recompute desiredUp so that it's perpendicular to the direction
// You can skip that part if you really want to force desiredUp
vec3 right = cross(direction, desiredUp);
desiredUp = cross(right, direction);
// Find the rotation between the front of the object (that we assume towards +Z,
// but this depends on your model) and the desired direction
quat rot1 = RotationBetweenVectors(vec3(0.0f, 0.0f, 1.0f), direction);
// Because of the 1rst rotation, the up is probably completely screwed up.
// Find the rotation between the "up" of the rotated object, and the desired up
vec3 newUp = rot1 * vec3(0.0f, 1.0f, 0.0f);
quat rot2 = RotationBetweenVectors(newUp, desiredUp);
// Apply them
return rot2 * rot1; // remember, in reverse order.
}
// Like SLERP, but forbids rotation greater than maxAngle (in radians)
// In conjunction to LookAt, can make your characters
quat RotateTowards(quat q1, quat q2, float maxAngle) {
if (maxAngle < 0.001f) {
// No rotation allowed. Prevent dividing by 0 later.
return q1;
}
float cosTheta = dot(q1, q2);
// q1 and q2 are already equal.
// Force q2 just to be sure
if (cosTheta > 0.9999f) {
return q2;
}
// Avoid taking the long path around the sphere
if (cosTheta < 0) {
q1 = q1*-1.0f;
cosTheta *= -1.0f;
}
float angle = acos(cosTheta);
// If there is only a 2� difference, and we are allowed 5�,
// then we arrived.
if (angle < maxAngle) {
return q2;
}
// This is just like slerp(), but with a custom t
float t = maxAngle / angle;
angle = maxAngle;
quat res = (sin((1.0f - t) * angle) * q1 + sin(t * angle) * q2) / sin(angle);
res = normalize(res);
return res;
}
void tests() {
glm::vec3 Xpos(+1.0f, 0.0f, 0.0f);
glm::vec3 Ypos(0.0f, +1.0f, 0.0f);
glm::vec3 Zpos(0.0f, 0.0f, +1.0f);
glm::vec3 Xneg(-1.0f, 0.0f, 0.0f);
glm::vec3 Yneg(0.0f, -1.0f, 0.0f);
glm::vec3 Zneg(0.0f, 0.0f, -1.0f);
// Testing standard, easy case
// Must be 90� rotation on X : 0.7 0 0 0.7
quat X90rot = RotationBetweenVectors(Ypos, Zpos);
// Testing with v1 = v2
// Must be identity : 0 0 0 1
quat id = RotationBetweenVectors(Xpos, Xpos);
// Testing with v1 = -v2
// Must be 180� on +/-Y axis : 0 +/-1 0 0
quat Y180rot = RotationBetweenVectors(Xpos, Xneg);
// Testing with v1 = -v2, but with a "bad first guess"
// Must be 180� on +/-Y axis : 0 +/-1 0 0
quat X180rot = RotationBetweenVectors(Zpos, Zneg);
}
\ No newline at end of file
#pragma once
//https://github.com/opengl-tutorials/ogl/blob/master/common/quaternion_utils.hpp
#include <glm/gtx/quaternion.hpp>
using namespace glm;
quat RotationBetweenVectors(vec3 start, vec3 dest);
quat LookAt(vec3 direction, vec3 desiredUp);
quat RotateTowards(quat q1, quat q2, float maxAngle);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment