more example refactoring
This commit is contained in:
33
examples/CommonInterfaces/CommonCameraInterface.h
Normal file
33
examples/CommonInterfaces/CommonCameraInterface.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef COMMON_CAMERA_INTERFACE_H
|
||||
#define COMMON_CAMERA_INTERFACE_H
|
||||
|
||||
struct CommonCameraInterface
|
||||
{
|
||||
virtual void getCameraProjectionMatrix(float m[16])const = 0;
|
||||
virtual void getCameraViewMatrix(float m[16]) const = 0;
|
||||
|
||||
virtual void getCameraTargetPosition(float pos[3]) const = 0;
|
||||
virtual void getCameraPosition(float pos[3]) const = 0;
|
||||
|
||||
virtual void setCameraTargetPosition(float x,float y,float z) = 0;
|
||||
virtual void setCameraDistance(float dist) = 0;
|
||||
virtual float getCameraDistance() const = 0;
|
||||
|
||||
virtual void setCameraUpVector(float x,float y, float z) = 0;
|
||||
virtual void getCameraUpVector(float up[3]) const = 0;
|
||||
///the setCameraUpAxis will call the 'setCameraUpVector' and 'setCameraForwardVector'
|
||||
virtual void setCameraUpAxis(int axis) = 0;
|
||||
virtual int getCameraUpAxis() const = 0;
|
||||
|
||||
virtual void setCameraYaw(float yaw) = 0;
|
||||
virtual float getCameraYaw() const = 0;
|
||||
|
||||
virtual void setCameraPitch(float pitch) = 0;
|
||||
virtual float getCameraPitch() const = 0;
|
||||
|
||||
virtual void setAspectRatio(float ratio) = 0;
|
||||
virtual float getAspectRatio() const = 0;
|
||||
};
|
||||
|
||||
#endif //COMMON_CAMERA_INTERFACE_H
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
#ifndef COMMON_GRAPHICS_APP_H
|
||||
#define COMMON_GRAPHICS_APP_H
|
||||
|
||||
|
||||
|
||||
#include "Bullet3Common/b3Vector3.h"
|
||||
#include "CommonRenderInterface.h"
|
||||
#include "CommonWindowInterface.h"
|
||||
#include "CommonCameraInterface.h"
|
||||
|
||||
struct DrawGridData
|
||||
{
|
||||
int gridSize;
|
||||
@@ -22,21 +29,38 @@ struct DrawGridData
|
||||
|
||||
struct CommonGraphicsApp
|
||||
{
|
||||
class b3gWindowInterface* m_window;
|
||||
struct CommonRenderInterface* m_renderer;
|
||||
struct CommonParameterInterface* m_parameterInterface;
|
||||
struct Common2dCanvasInterface* m_2dCanvasInterface;
|
||||
|
||||
bool m_leftMouseButton;
|
||||
bool m_middleMouseButton;
|
||||
bool m_rightMouseButton;
|
||||
float m_wheelMultiplier;
|
||||
float m_mouseMoveMultiplier;
|
||||
float m_mouseXpos;
|
||||
float m_mouseYpos;
|
||||
bool m_mouseInitialized;
|
||||
|
||||
CommonGraphicsApp()
|
||||
:m_window(0),
|
||||
m_renderer(0),
|
||||
m_parameterInterface(0),
|
||||
m_2dCanvasInterface(0)
|
||||
m_2dCanvasInterface(0),
|
||||
m_leftMouseButton(false),
|
||||
m_middleMouseButton(false),
|
||||
m_rightMouseButton(false),
|
||||
m_wheelMultiplier(0.01f),
|
||||
m_mouseMoveMultiplier(0.4f),
|
||||
m_mouseXpos(0.f),
|
||||
m_mouseYpos(0.f),
|
||||
m_mouseInitialized(false)
|
||||
{
|
||||
}
|
||||
virtual ~CommonGraphicsApp()
|
||||
{
|
||||
}
|
||||
|
||||
class b3gWindowInterface* m_window;
|
||||
struct CommonRenderInterface* m_renderer;
|
||||
struct CommonParameterInterface* m_parameterInterface;
|
||||
struct Common2dCanvasInterface* m_2dCanvasInterface;
|
||||
|
||||
virtual void drawGrid(DrawGridData data=DrawGridData()) = 0;
|
||||
virtual void setUpAxis(int axis) = 0;
|
||||
@@ -48,6 +72,155 @@ struct CommonGraphicsApp
|
||||
virtual int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ)=0;
|
||||
virtual int registerGraphicsSphereShape(float radius, bool usePointSprites=true, int largeSphereThreshold=100, int mediumSphereThreshold=10)=0;
|
||||
virtual void registerGrid(int xres, int yres, float color0[4], float color1[4])=0;
|
||||
|
||||
void defaultMouseButtonCallback( int button, int state, float x, float y)
|
||||
{
|
||||
if (button==0)
|
||||
m_leftMouseButton=state;
|
||||
if (button==1)
|
||||
m_middleMouseButton=state;
|
||||
|
||||
if (button==2)
|
||||
m_rightMouseButton=state;
|
||||
|
||||
m_mouseXpos = x;
|
||||
m_mouseYpos = y;
|
||||
m_mouseInitialized = true;
|
||||
}
|
||||
void defaultMouseMoveCallback( float x, float y)
|
||||
{
|
||||
|
||||
if (m_window && m_renderer)
|
||||
{
|
||||
CommonCameraInterface* camera = m_renderer->getActiveCamera();
|
||||
|
||||
bool isAltPressed = m_window->isModifiedKeyPressed(B3G_ALT);
|
||||
bool isControlPressed = m_window->isModifiedKeyPressed(B3G_CONTROL);
|
||||
|
||||
|
||||
if (isAltPressed || isControlPressed)
|
||||
{
|
||||
float xDelta = x-m_mouseXpos;
|
||||
float yDelta = y-m_mouseYpos;
|
||||
float cameraDistance = camera->getCameraDistance();
|
||||
float pitch = camera->getCameraPitch();
|
||||
float yaw = camera->getCameraYaw();
|
||||
|
||||
float targPos[3];
|
||||
float camPos[3];
|
||||
|
||||
camera->getCameraTargetPosition(targPos);
|
||||
camera->getCameraPosition(camPos);
|
||||
|
||||
b3Vector3 cameraPosition = b3MakeVector3(b3Scalar(camPos[0]),
|
||||
b3Scalar(camPos[1]),
|
||||
b3Scalar(camPos[2]));
|
||||
|
||||
b3Vector3 cameraTargetPosition = b3MakeVector3( b3Scalar(targPos[0]),
|
||||
b3Scalar(targPos[1]),
|
||||
b3Scalar(targPos[2]));
|
||||
b3Vector3 cameraUp = b3MakeVector3(0,0,0);
|
||||
cameraUp[camera->getCameraUpAxis()] = 1.f;
|
||||
|
||||
if (m_leftMouseButton)
|
||||
{
|
||||
// if (b3Fabs(xDelta)>b3Fabs(yDelta))
|
||||
// {
|
||||
pitch -= xDelta*m_mouseMoveMultiplier;
|
||||
// } else
|
||||
// {
|
||||
yaw += yDelta*m_mouseMoveMultiplier;
|
||||
// }
|
||||
}
|
||||
|
||||
if (m_middleMouseButton)
|
||||
{
|
||||
cameraTargetPosition += cameraUp * yDelta*0.01;
|
||||
|
||||
|
||||
b3Vector3 fwd = cameraTargetPosition-cameraPosition;
|
||||
b3Vector3 side = cameraUp.cross(fwd);
|
||||
side.normalize();
|
||||
cameraTargetPosition += side * xDelta*0.01;
|
||||
|
||||
}
|
||||
if (m_rightMouseButton)
|
||||
{
|
||||
cameraDistance -= xDelta*0.01f;
|
||||
cameraDistance -= yDelta*0.01f;
|
||||
if (cameraDistance<1)
|
||||
cameraDistance=1;
|
||||
if (cameraDistance>1000)
|
||||
cameraDistance=1000;
|
||||
}
|
||||
camera->setCameraDistance(cameraDistance);
|
||||
camera->setCameraPitch(pitch);
|
||||
camera->setCameraYaw(yaw);
|
||||
camera->setCameraTargetPosition(cameraTargetPosition[0],cameraTargetPosition[1],cameraTargetPosition[2]);
|
||||
|
||||
}
|
||||
|
||||
}//m_window && m_renderer
|
||||
|
||||
m_mouseXpos = x;
|
||||
m_mouseYpos = y;
|
||||
m_mouseInitialized = true;
|
||||
}
|
||||
// void defaultKeyboardCallback(int key, int state)
|
||||
// {
|
||||
// }
|
||||
void defaultWheelCallback( float deltax, float deltay)
|
||||
{
|
||||
|
||||
if (m_renderer)
|
||||
{
|
||||
b3Vector3 cameraTargetPosition, cameraPosition, cameraUp = b3MakeVector3(0,0,0);
|
||||
cameraUp[getUpAxis()] = 1;
|
||||
CommonCameraInterface* camera = m_renderer->getActiveCamera();
|
||||
|
||||
camera->getCameraPosition(cameraPosition);
|
||||
camera->getCameraTargetPosition(cameraTargetPosition);
|
||||
|
||||
if (!m_leftMouseButton)
|
||||
{
|
||||
|
||||
float cameraDistance = camera->getCameraDistance();
|
||||
if (deltay<0 || cameraDistance>1)
|
||||
{
|
||||
cameraDistance -= deltay*0.1f;
|
||||
if (cameraDistance<1)
|
||||
cameraDistance=1;
|
||||
camera->setCameraDistance(cameraDistance);
|
||||
|
||||
} else
|
||||
{
|
||||
|
||||
b3Vector3 fwd = cameraTargetPosition-cameraPosition;
|
||||
fwd.normalize();
|
||||
cameraTargetPosition += fwd*deltay*m_wheelMultiplier;//todo: expose it in the GUI?
|
||||
}
|
||||
} else
|
||||
{
|
||||
if (b3Fabs(deltax)>b3Fabs(deltay))
|
||||
{
|
||||
b3Vector3 fwd = cameraTargetPosition-cameraPosition;
|
||||
b3Vector3 side = cameraUp.cross(fwd);
|
||||
side.normalize();
|
||||
cameraTargetPosition += side * deltax*m_wheelMultiplier;
|
||||
|
||||
} else
|
||||
{
|
||||
cameraTargetPosition -= cameraUp * deltay*m_wheelMultiplier;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
camera->setCameraTargetPosition(cameraTargetPosition[0],cameraTargetPosition[1],cameraTargetPosition[2]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "CommonRenderInterface.h"
|
||||
#include "CommonGraphicsAppInterface.h"
|
||||
#include "CommonWindowInterface.h"
|
||||
#include "CommonCameraInterface.h"
|
||||
|
||||
struct CommonMultiBodyBase : public ExampleInterface
|
||||
{
|
||||
@@ -181,8 +182,8 @@ struct CommonMultiBodyBase : public ExampleInterface
|
||||
float fov = btScalar(2.0) * btAtan(tanFov);
|
||||
|
||||
btVector3 camPos,camTarget;
|
||||
renderer->getCameraPosition(camPos);
|
||||
renderer->getCameraTargetPosition(camTarget);
|
||||
renderer->getActiveCamera()->getCameraPosition(camPos);
|
||||
renderer->getActiveCamera()->getCameraTargetPosition(camTarget);
|
||||
|
||||
btVector3 rayFrom = camPos;
|
||||
btVector3 rayForward = (camTarget-camPos);
|
||||
@@ -240,7 +241,7 @@ struct CommonMultiBodyBase : public ExampleInterface
|
||||
|
||||
btVector3 rayTo = getRayTo(int(x), int(y));
|
||||
btVector3 rayFrom;
|
||||
renderer->getCameraPosition(rayFrom);
|
||||
renderer->getActiveCamera()->getCameraPosition(rayFrom);
|
||||
movePickedBody(rayFrom,rayTo);
|
||||
|
||||
return false;
|
||||
@@ -264,7 +265,7 @@ struct CommonMultiBodyBase : public ExampleInterface
|
||||
if(button==0 && (!window->isModifiedKeyPressed(B3G_ALT) && !window->isModifiedKeyPressed(B3G_CONTROL) ))
|
||||
{
|
||||
btVector3 camPos;
|
||||
renderer->getCameraPosition(camPos);
|
||||
renderer->getActiveCamera()->getCameraPosition(camPos);
|
||||
|
||||
btVector3 rayFrom = camPos;
|
||||
btVector3 rayTo = getRayTo(int(x),int(y));
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef COMMON_RENDER_INTERFACE_H
|
||||
#define COMMON_RENDER_INTERFACE_H
|
||||
|
||||
struct CommonCameraInterface;
|
||||
|
||||
enum
|
||||
{
|
||||
B3_GL_TRIANGLES = 1,
|
||||
@@ -20,21 +22,11 @@ struct CommonRenderInterface
|
||||
virtual void init()=0;
|
||||
virtual void updateCamera(int upAxis)=0;
|
||||
virtual void removeAllInstances() = 0;
|
||||
virtual void setCameraDistance(float dist) = 0;
|
||||
virtual void setCameraPitch(float pitch) = 0;
|
||||
virtual void setCameraTargetPosition(float x, float y, float z)=0;
|
||||
|
||||
virtual const CommonCameraInterface* getActiveCamera() const =0;
|
||||
virtual CommonCameraInterface* getActiveCamera()=0;
|
||||
virtual void setActiveCamera(CommonCameraInterface* cam)=0;
|
||||
|
||||
|
||||
virtual void getCameraPosition(float cameraPos[4])=0;
|
||||
virtual void getCameraPosition(double cameraPos[4])=0;
|
||||
|
||||
virtual void setCameraTargetPosition(float cameraPos[4])=0;
|
||||
virtual void getCameraTargetPosition(float cameraPos[4]) const=0;
|
||||
virtual void getCameraTargetPosition(double cameraPos[4]) const=0;
|
||||
|
||||
virtual void getCameraViewMatrix(float viewMat[16]) const=0;
|
||||
virtual void getCameraProjectionMatrix(float projMat[16]) const=0;
|
||||
|
||||
virtual void renderScene()=0;
|
||||
|
||||
virtual int getScreenWidth() = 0;
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include "ExampleInterface.h"
|
||||
#include "CommonGUIHelperInterface.h"
|
||||
#include "CommonRenderInterface.h"
|
||||
#include "CommonCameraInterface.h"
|
||||
|
||||
#include "CommonGraphicsAppInterface.h"
|
||||
#include "CommonWindowInterface.h"
|
||||
|
||||
@@ -169,8 +171,9 @@ struct CommonRigidBodyBase : public ExampleInterface
|
||||
float fov = btScalar(2.0) * btAtan(tanFov);
|
||||
|
||||
btVector3 camPos,camTarget;
|
||||
renderer->getCameraPosition(camPos);
|
||||
renderer->getCameraTargetPosition(camTarget);
|
||||
|
||||
renderer->getActiveCamera()->getCameraPosition(camPos);
|
||||
renderer->getActiveCamera()->getCameraTargetPosition(camTarget);
|
||||
|
||||
btVector3 rayFrom = camPos;
|
||||
btVector3 rayForward = (camTarget-camPos);
|
||||
@@ -228,7 +231,7 @@ struct CommonRigidBodyBase : public ExampleInterface
|
||||
|
||||
btVector3 rayTo = getRayTo(int(x), int(y));
|
||||
btVector3 rayFrom;
|
||||
renderer->getCameraPosition(rayFrom);
|
||||
renderer->getActiveCamera()->getCameraPosition(rayFrom);
|
||||
movePickedBody(rayFrom,rayTo);
|
||||
|
||||
return false;
|
||||
@@ -252,7 +255,7 @@ struct CommonRigidBodyBase : public ExampleInterface
|
||||
if(button==0 && (!window->isModifiedKeyPressed(B3G_ALT) && !window->isModifiedKeyPressed(B3G_CONTROL) ))
|
||||
{
|
||||
btVector3 camPos;
|
||||
renderer->getCameraPosition(camPos);
|
||||
renderer->getActiveCamera()->getCameraPosition(camPos);
|
||||
|
||||
btVector3 rayFrom = camPos;
|
||||
btVector3 rayTo = getRayTo(int(x),int(y));
|
||||
|
||||
Reference in New Issue
Block a user