latest view frustum work

This commit is contained in:
ZappoMan 2013-04-12 13:36:44 -07:00
parent 76d22f1b2b
commit d01d1164ca
10 changed files with 192 additions and 32 deletions

View file

@ -1,5 +1,7 @@
#include "Orientation.h"
#include "Util.h"
#include "glmUtils.h"
Orientation::Orientation() {
right = glm::vec3( 1.0, 0.0, 0.0 );

View file

@ -168,30 +168,6 @@ void drawvec3(int x, int y, float scale, float rotate, float thick, int mono, gl
}
// XXXBHG - These handy operators should probably go somewhere else, I'm surprised they don't
// already exist somewhere in OpenGL. Maybe someone can point me to them if they do exist!
glm::vec3 operator* (float lhs, const glm::vec3& rhs)
{
glm::vec3 result = rhs;
result.x *= lhs;
result.y *= lhs;
result.z *= lhs;
return result;
}
// XXXBHG - These handy operators should probably go somewhere else, I'm surprised they don't
// already exist somewhere in OpenGL. Maybe someone can point me to them if they do exist!
glm::vec3 operator* (const glm::vec3& lhs, float rhs)
{
glm::vec3 result = lhs;
result.x *= rhs;
result.y *= rhs;
result.z *= rhs;
return result;
}
void drawGroundPlaneGrid( float size, int resolution )
{

View file

@ -46,9 +46,6 @@ void drawvec3(int x, int y, float scale, float rotate, float thick, int mono, gl
double diffclock(timeval *clock1,timeval *clock2);
glm::vec3 operator* (float lhs, const glm::vec3& rhs);
glm::vec3 operator* (const glm::vec3& lhs, float rhs);
void drawGroundPlaneGrid( float size, int resolution );
#endif

View file

@ -75,6 +75,9 @@
#include <PerfStat.h>
#include <SharedUtil.h>
#include <PacketHeaders.h>
#include <glmUtils.h>
#include "ViewFrustum.h"
using namespace std;
@ -159,6 +162,7 @@ bool viewFrustumFromOffset=false; // Wether or not to offset the view of the
float viewFrustumOffsetYaw = -90.0;
float viewFrustumOffsetPitch = 7.5;
float viewFrustumOffsetRoll = 0.0;
float viewFrustumOffsetDistance = 0.0;
int noiseOn = 0; // Whether to add random noise
float noise = 1.0; // Overall magnitude scaling for random noise levels
@ -562,10 +566,13 @@ void render_view_frustum() {
right = headRight;
}
float nearDist = 0.1;
float farDist = 1.0;
ViewFrustum vf(viewFrustumPosition,viewFrustumDirection);
vf.dump();
float fovHalfAngle = 0.7854f*1.7; // 45 deg for half, so fov = 90 deg
float nearDist = 0.1;
float farDist = 10.0;
float fovHalfAngle = 0.7854f*1.5; // 45 deg for half, so fov = 90 deg
float screenWidth = ::WIDTH; // These values come from reshape()
float screenHeight = ::HEIGHT;
@ -588,6 +595,9 @@ void render_view_frustum() {
glm::vec3 nearBottomLeft = nearCenter - (up*nearHeight*0.5) - (right*nearWidth*0.5);
glm::vec3 nearBottomRight = nearCenter - (up*nearHeight*0.5) + (right*nearWidth*0.5);
//printf("farCenter.x=%f, farCenter.y=%f, farCenter.z=%f\n",farCenter.x,farCenter.y,farCenter.z);
//printf("farTopLeft.x=%f, farTopLeft.y=%f, farTopLeft.z=%f\n",farTopLeft.x,farTopLeft.y,farTopLeft.z);
// At this point we have all the corners for our frustum... we could use these to
// calculate various things...
@ -770,9 +780,9 @@ void display(void)
//----------------------------------------------------
viewFrustumOffsetCamera.setYaw ( 180.0 - myAvatar.getBodyYaw() + ::viewFrustumOffsetYaw );
viewFrustumOffsetCamera.setPitch ( 0.0 + ::viewFrustumOffsetPitch );
viewFrustumOffsetCamera.setRoll ( 0.0 + ::viewFrustumOffsetRoll );
viewFrustumOffsetCamera.setRoll ( 0.0 + ::viewFrustumOffsetRoll );
viewFrustumOffsetCamera.setUp ( 0.2 + 0.2 );
viewFrustumOffsetCamera.setDistance( 0.5 + 0.2 );
viewFrustumOffsetCamera.setDistance ( 0.5 + ::viewFrustumOffsetDistance );
viewFrustumOffsetCamera.update();
whichCamera = viewFrustumOffsetCamera;
@ -1138,6 +1148,9 @@ void key(unsigned char k, int x, int y)
if (k == '(') ::viewFrustumOffsetRoll -= 0.5;
if (k == ')') ::viewFrustumOffsetRoll += 0.5;
if (k == '<') ::viewFrustumOffsetDistance -= 0.5;
if (k == '>') ::viewFrustumOffsetDistance += 0.5;
if (k == '&') {
::paintOn = !::paintOn; // toggle paint
::setupPaintingVoxel(); // also randomizes colors

View file

@ -351,3 +351,4 @@ void printVoxelCode(unsigned char* voxelCode) {
} while( (time2 - time1) < waitTime);
}
#endif

View file

@ -54,4 +54,5 @@ bool createVoxelEditMessage(unsigned char command, short int sequence,
void usleep(int waitTime);
#endif
#endif /* defined(__hifi__SharedUtil__) */

View file

@ -0,0 +1,69 @@
//
// ViewFrustum.cpp
// hifi
//
// Created by Brad Hefta-Gaub on 04/11/13.
//
// Simple view frustum class.
//
//
#include "ViewFrustum.h"
#include "glmUtils.h"
ViewFrustum::ViewFrustum(glm::vec3 position, glm::vec3 direction) {
this->calculateViewFrustum(position, direction);
}
/////////////////////////////////////////////////////////////////////////////////////
// ViewFrustum::calculateViewFrustum()
//
// Description: this will calculate the view frustum bounds for a given position
// and direction
void ViewFrustum::calculateViewFrustum(glm::vec3 position, glm::vec3 direction) {
float nearDist = 0.1;
float farDist = 1.0;
glm::vec3 front = direction;
glm::vec3 up = glm::vec3(0,direction.y,0); // up is always this way
glm::vec3 right = glm::vec3(direction.z,direction.y,direction.x); // up is
float fovHalfAngle = 0.7854f; // 45 deg for half, so fov = 90 deg
float screenWidth = 800;//::WIDTH; // These values come from reshape()
float screenHeight = 600; //::HEIGHT;
float ratio = screenWidth/screenHeight;
float nearHeight = 2 * tan(fovHalfAngle) * nearDist;
float nearWidth = nearHeight * ratio;
float farHeight = 2 * tan(fovHalfAngle) * farDist;
float farWidth = farHeight * ratio;
this->_farCenter = position+front*farDist;
this->_farTopLeft = this->_farCenter + (up*farHeight*0.5) - (right*farWidth*0.5);
this->_farTopRight = this->_farCenter + (up*farHeight*0.5) + (right*farWidth*0.5);
this->_farBottomLeft = this->_farCenter - (up*farHeight*0.5) - (right*farWidth*0.5);
this->_farBottomRight = this->_farCenter - (up*farHeight*0.5) + (right*farWidth*0.5);
this->_nearCenter = position+front*nearDist;
this->_nearTopLeft = this->_nearCenter + (up*nearHeight*0.5) - (right*nearWidth*0.5);
this->_nearTopRight = this->_nearCenter + (up*nearHeight*0.5) + (right*nearWidth*0.5);
this->_nearBottomLeft = this->_nearCenter - (up*nearHeight*0.5) - (right*nearWidth*0.5);
this->_nearBottomRight = this->_nearCenter - (up*nearHeight*0.5) + (right*nearWidth*0.5);
}
void ViewFrustum::dump() {
printf("farCenter.x=%f, farCenter.y=%f, farCenter.z=%f\n",this->_farCenter.x,this->_farCenter.y,this->_farCenter.z);
printf("farTopLeft.x=%f, farTopLeft.y=%f, farTopLeft.z=%f\n",this->_farTopLeft.x,this->_farTopLeft.y,this->_farTopLeft.z);
printf("farTopRight.x=%f, farTopRight.y=%f, farTopRight.z=%f\n",this->_farTopRight.x,this->_farTopRight.y,this->_farTopRight.z);
printf("farBottomLeft.x=%f, farBottomLeft.y=%f, farBottomLeft.z=%f\n",this->_farBottomLeft.x,this->_farBottomLeft.y,this->_farBottomLeft.z);
printf("farBottomRight.x=%f, farBottomRight.y=%f, farBottomRight.z=%f\n",this->_farBottomRight.x,this->_farBottomRight.y,this->_farBottomRight.z);
printf("nearCenter.x=%f, nearCenter.y=%f, nearCenter.z=%f\n",this->_nearCenter.x,this->_nearCenter.y,this->_nearCenter.z);
printf("nearTopLeft.x=%f, nearTopLeft.y=%f, nearTopLeft.z=%f\n",this->_nearTopLeft.x,this->_nearTopLeft.y,this->_nearTopLeft.z);
printf("nearTopRight.x=%f, nearTopRight.y=%f, nearTopRight.z=%f\n",this->_nearTopRight.x,this->_nearTopRight.y,this->_nearTopRight.z);
printf("nearBottomLeft.x=%f, nearBottomLeft.y=%f, nearBottomLeft.z=%f\n",this->_nearBottomLeft.x,this->_nearBottomLeft.y,this->_nearBottomLeft.z);
printf("nearBottomRight.x=%f, nearBottomRight.y=%f, nearBottomRight.z=%f\n",this->_nearBottomRight.x,this->_nearBottomRight.y,this->_nearBottomRight.z);
}

View file

@ -0,0 +1,50 @@
//
// ViewFrustum.h
// hifi
//
// Created by Brad Hefta-Gaub on 04/11/13.
//
// Simple view frustum class.
//
//
#ifndef __hifi__ViewFrustum__
#define __hifi__ViewFrustum__
#include <glm/glm.hpp>
class ViewFrustum {
private:
glm::vec3 _farCenter;
glm::vec3 _farTopLeft;
glm::vec3 _farTopRight;
glm::vec3 _farBottomLeft;
glm::vec3 _farBottomRight;
glm::vec3 _nearCenter;
glm::vec3 _nearTopLeft;
glm::vec3 _nearTopRight;
glm::vec3 _nearBottomLeft;
glm::vec3 _nearBottomRight;
public:
glm::vec3 getFarCenter() const { return _farCenter; };
glm::vec3 getFarTopLeft() const { return _farTopLeft; };
glm::vec3 getFarTopRight() const { return _farTopRight; };
glm::vec3 getFarBottomLeft() const { return _farBottomLeft; };
glm::vec3 getFarBottomRight() const { return _farBottomRight; };
glm::vec3 getNearCenter() const { return _nearCenter; };
glm::vec3 getNearTopLeft() const { return _nearTopLeft; };
glm::vec3 getNearTopRight() const { return _nearTopRight; };
glm::vec3 getNearBottomLeft() const { return _nearBottomLeft; };
glm::vec3 getNearBottomRight() const { return _nearBottomRight; };
void calculateViewFrustum(glm::vec3 position, glm::vec3 direction);
ViewFrustum(glm::vec3 position, glm::vec3 direction);
void dump();
};
#endif /* defined(__hifi__ViewFrustum__) */

33
voxellib/src/glmUtils.cpp Normal file
View file

@ -0,0 +1,33 @@
//
// glmUtils.cpp
// hifi
//
// Created by Brad Hefta-Gaub on 4/12/13.
//
//
#include <glm/glm.hpp>
// XXXBHG - These handy operators should probably go somewhere else, I'm surprised they don't
// already exist somewhere in OpenGL. Maybe someone can point me to them if they do exist!
glm::vec3 operator* (float lhs, const glm::vec3& rhs)
{
glm::vec3 result = rhs;
result.x *= lhs;
result.y *= lhs;
result.z *= lhs;
return result;
}
// XXXBHG - These handy operators should probably go somewhere else, I'm surprised they don't
// already exist somewhere in OpenGL. Maybe someone can point me to them if they do exist!
glm::vec3 operator* (const glm::vec3& lhs, float rhs)
{
glm::vec3 result = lhs;
result.x *= rhs;
result.y *= rhs;
result.z *= rhs;
return result;
}

18
voxellib/src/glmUtils.h Normal file
View file

@ -0,0 +1,18 @@
//
// glmUtils.h
// hifi
//
// Created by Brad Hefta-Gaub on 4/12/13.
//
//
#ifndef __hifi__glmUtils__
#define __hifi__glmUtils__
#include <glm/glm.hpp>
glm::vec3 operator* (float lhs, const glm::vec3& rhs);
glm::vec3 operator* (const glm::vec3& lhs, float rhs);
#endif // __hifi__glmUtils__