bluecore/engine/RenderDevice.cpp

186 rindas
4.3 KiB
C++

#include "RenderDevice.h"
#include "Utilities/Log.h"
#include "Math/Matrix.h"
#include "GL/gl.h"
#include "GL/glu.h"
namespace BlueCore
{
void RenderDevice::WindowResizeSlot(int width, int height)
{
glViewport( 0, 0, width, height);
_ViewportWidth = width;
_ViewportHeight = height;
}
RenderDevice::RenderDevice(RenderWindow* renderWindow) :
_RenderWindow(renderWindow)
{
if (_RenderWindow.valid())
{
_RenderWindow->WindowResizeSignal.connect(this,
&RenderDevice::WindowResizeSlot);
_RenderWindow->WindowCloseSignal.connect(this,
&RenderDevice::WindowCloseSlot);
_ViewportWidth = _RenderWindow->getWidth();
_ViewportHeight = _RenderWindow->getHeight();
glViewport( 0, 0, _ViewportWidth, _ViewportHeight);
}
clog << ">>> RenderDevice constructed..."<< endlog;
}
RenderDevice::~RenderDevice()
{
clog << ">>> RenderDevice destructed..."<< endlog;
}
void RenderDevice::WindowCloseSlot()
{
DeviceShutdownSignal();
}
int RenderDevice::getViewportWidth()
{
return _ViewportWidth;
}
int RenderDevice::getViewportHeight()
{
return _ViewportHeight;
}
void RenderDevice::begin2D()
{
// prepare state
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glNormal3f( 0.0, 0.0, 1.0);
// set projection matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
//int newheight = (_width / 16.0) * 9.0;
gluOrtho2D( 0, _ViewportWidth-1, 0, _ViewportHeight-1);
// prepare model matrix
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}
void RenderDevice::end2D()
{
// restore projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// restore model matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
// restore old state
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
}
void RenderDevice::clear()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void RenderDevice::swap()
{
glfwSwapBuffers();
}
void RenderDevice::setAmbientLight(float r, float g, float b)
{
GLfloat lightAmbient[] =
{ r, g, b, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient);
}
void RenderDevice::begin3D(Camera *camera)
{
_Camera = camera;
setupProjectionMatrix();
setupViewMatrix();
}
void RenderDevice::setupProjectionMatrix()
{
if (_Camera.valid())
{
Scalar fH = tan( (_Camera->getFov() / 2) / 180 * Pi)
* _Camera->getNearPlane();
Scalar fW = fH * _ViewportWidth / _ViewportHeight;
// setup projectiom matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum( -fW, fW, -fH, fH, _Camera->getNearPlane(),
_Camera->getFarPlane());
}
}
void RenderDevice::setupViewMatrix()
{
// set the view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Matrix4x4 m(_Camera->getRotation(), _Camera->getRotation().applyInversed(_Camera->getPosition() * -1.0) );
glMultMatrixd( ( GLdouble * ) &m.m );
/*
// calculate frustum planes
Vector3 up = q.apply ( Vector3 ( 0.0, 1.0, 0.0 ) );
Vector3 right = q.apply ( Vector3 ( 1.0, 0.0, 0.0 ) );
Vector3 d = q.apply ( Vector3 ( 0.0, 0.0, -1.0 ) );
Vector3 fc = p + d * _far;
Vector3 ftl = fc + ( up * _hFar ) - ( right * _wFar );
Vector3 ftr = fc + ( up * _hFar ) + ( right * _wFar );
Vector3 fbl = fc - ( up * _hFar ) - ( right * _wFar );
Vector3 fbr = fc - ( up * _hFar ) + ( right * _wFar );
Vector3 nc = p + d * _near;
Vector3 ntl = nc + ( up * _hNear ) - ( right * _wNear );
Vector3 ntr = nc + ( up * _hNear ) + ( right * _wNear );
Vector3 nbl = nc - ( up * _hNear ) - ( right * _wNear );
Vector3 nbr = nc - ( up * _hNear ) + ( right * _wNear );
_frustumPlanes[RightPlane] = Plane ( nbr, fbr, ntr );
_frustumPlanes[LeftPlane] = Plane ( ntl, fbl, nbl );
_frustumPlanes[BottomPlane] = Plane ( nbl, fbr, nbr );
_frustumPlanes[TopPlane] = Plane ( ntr, ftl, ntl );
_frustumPlanes[FarPlane] = Plane ( ftl, ftr, fbl );
_frustumPlanes[NearPlane] = Plane ( ntl, nbl, ntr );
*/
}
void RenderDevice::setTransformation (const Vector3& position, const Quaternion& orientation)
{
glMatrixMode ( GL_MODELVIEW );
glPushMatrix();
Matrix4x4 m ( orientation, position );
glMultMatrixd ( ( GLdouble * ) &m.m );
}
#if 0
void RenderDevice::setu
// set the view matrix
glMatrixMode (GL_MODELVIEW );
glLoadIdentity();
Matrix4x4 m(_Rotation, _Rotation.applyInversed(_Position * -1.0) );
glMultMatrixd ( ( GLdouble * ) &m.m );
} // namespace BlueCore
#endif
}