瀏覽代碼

added RenderQueue

master
cirdan 12 年之前
父節點
當前提交
7a3d5f6eb5
共有 17 個文件被更改,包括 596 次插入365 次删除
  1. +90
    -68
      engine/Camera.cpp
  2. +16
    -13
      engine/Camera.h
  3. +3
    -3
      engine/FontManager.cpp
  4. +2
    -2
      engine/MeshManager.cpp
  5. +2
    -2
      engine/ModelManager.cpp
  6. +3
    -2
      engine/ModelManager.h
  7. +82
    -225
      engine/RenderDevice.cpp
  8. +28
    -30
      engine/RenderDevice.h
  9. +51
    -0
      engine/RenderQueue.cpp
  10. +52
    -0
      engine/RenderQueue.h
  11. +0
    -6
      engine/RenderWindow.cpp
  12. +1
    -2
      engine/RenderWindow.h
  13. +1
    -1
      engine/ScriptSystem_Image.cpp
  14. +1
    -1
      engine/Utilities/Kernel.cpp
  15. +14
    -2
      engine/Utilities/Referenced.h
  16. +10
    -7
      engine/main.cpp
  17. +240
    -1
      engine/todo.txt

+ 90
- 68
engine/Camera.cpp 查看文件

@@ -7,116 +7,138 @@
namespace BlueCore
{

//--------------------------------------------------------------------------
Camera::Camera()
{
}

//--------------------------------------------------------------------------
Camera::~Camera()
{
}

//--------------------------------------------------------------------------
void Camera::setFoV(Scalar fov)
{
_FoV = fov;
_FoV = fov;
}

//--------------------------------------------------------------------------
void Camera::setAspectRatio(Scalar aspect)
{
_AspectRatio = aspect;
_AspectRatio = aspect;
}

//--------------------------------------------------------------------------
void Camera::setNearPlane(Scalar near)
{
_NearPlane = near;
_NearPlane = near;
}

//--------------------------------------------------------------------------
void Camera::setFarPlane(Scalar far)
{
_FarPlane = far;
_FarPlane = far;
}

//--------------------------------------------------------------------------
void Camera::setPosition(const Vector3& position)
{
_Position = position;
_Position = position;
}

const Vector3& Camera::getPosition()
{
return _Position;
return _Position;
}

//--------------------------------------------------------------------------
void Camera::setRotation(const Quaternion& rotation)
{
_Rotation = rotation;
_Rotation = rotation;
}

const Quaternion& Camera::getRotation()
{
return _Rotation;
}

//--------------------------------------------------------------------------
#if 0

void Camera::setupProjectionMatrix()
{
Scalar fW, fH;
fH = tan ( (_FoV / 2) / 180* Pi ) * _NearPlane;
fW = fH * _AspectRatio;
// setup projectiom matrix
glMatrixMode (GL_PROJECTION );
glLoadIdentity();
glFrustum ( -fW, fW, -fH, fH, _NearPlane, _FarPlane );
// save variables for frustum culling
/*
_near = nearZ;
_far = farZ;
_hNear = tan ( ( fov / 2 ) / 180 * Pi ) * nearZ;
_wNear = _hNear * aspect;
_hFar = tan ( ( fov / 2 ) / 180 * Pi ) * farZ;
_wFar = _hFar * aspect;
*/
Scalar fW, fH;
fH = tan ( (_FoV / 2) / 180* Pi ) * _NearPlane;
fW = fH * _AspectRatio;
// setup projectiom matrix
glMatrixMode (GL_PROJECTION );
glLoadIdentity();
glFrustum ( -fW, fW, -fH, fH, _NearPlane, _FarPlane );
// save variables for frustum culling
/*
_near = nearZ;
_far = farZ;
_hNear = tan ( ( fov / 2 ) / 180 * Pi ) * nearZ;
_wNear = _hNear * aspect;
_hFar = tan ( ( fov / 2 ) / 180 * Pi ) * farZ;
_wFar = _hFar * aspect;
*/
}

//--------------------------------------------------------------------------
void Camera::setupViewMatrix()
{
// set the view matrix
glMatrixMode (GL_MODELVIEW );
glLoadIdentity();
Matrix4x4 m(_Rotation, _Rotation.applyInversed(_Position * -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 );
*/
// set the view matrix
glMatrixMode (GL_MODELVIEW );
glLoadIdentity();
Matrix4x4 m(_Rotation, _Rotation.applyInversed(_Position * -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 );
*/
}
#endif

Scalar Camera::getNearPlane() const
{
return _NearPlane;
}

Scalar Camera::getFarPlane() const
{
return _FarPlane;
}

Scalar Camera::getFov() const
{
return _FoV;
}

} // namespace BlueCore


+ 16
- 13
engine/Camera.h 查看文件

@@ -8,7 +8,8 @@
#include "Math/Plane.h"
//#include "geometry/frustum.h"

namespace BlueCore {
namespace BlueCore
{

class Camera : public Referenced
{
@@ -23,28 +24,30 @@ private:

Vector3 _Position;
Quaternion _Rotation;
public:

Camera();
~Camera();

void lookAt( const Vector3 &point );
void lookAt( const Vector3 &point, const Vector3 &up );
void lookAt(const Vector3 &point);
void lookAt(const Vector3 &point, const Vector3 &up);
void lookStraight();

void setFoV( Scalar fov );
void setAspectRatio( Scalar aspect );
void setNearPlane( Scalar near );
void setFarPlane( Scalar far );
void setFoV(Scalar fov);
void setAspectRatio(Scalar aspect);
void setNearPlane(Scalar near);
void setFarPlane(Scalar far);

void setPosition (const Vector3& position);
const Vector3& getPosition ();
void setPosition(const Vector3& position);
const Vector3& getPosition();

void setRotation (const Quaternion& rotation);
void setRotation(const Quaternion& rotation);
const Quaternion& getRotation();

void setupViewMatrix ();
void setupProjectionMatrix ();
Scalar getNearPlane() const;
Scalar getFarPlane() const;
Scalar getFov() const;
//const Frustum &getFrustum() const;
};


+ 3
- 3
engine/FontManager.cpp 查看文件

@@ -78,7 +78,7 @@ Font *FontManager::loadFont(const std::string &filename, int size, bool hinting)
if (result != _fonts.end() && result->second.valid())
{
return result->second.get();
return result->second.pointer();
}
Font *font;
@@ -99,7 +99,7 @@ Font *FontManager::loadFont(const std::string &filename, int size, bool hinting)
if (ftfont->Error() == 0)
{
font = new Font( ftfont, buffer, _Device.get() );
font = new Font( ftfont, buffer, _Device );
clog << ">>> Font '"<< filename << "' ("<< size << ") loaded"
<< endline;
}
@@ -125,7 +125,7 @@ Font *FontManager::loadFont(const std::string &filename, int size, bool hinting)
//------------------------------------------------------------------------------
Font *FontManager::getDefaultFont()
{
return _DefaultFont.get();
return _DefaultFont.pointer();
}
//------------------------------------------------------------------------------


+ 2
- 2
engine/MeshManager.cpp 查看文件

@@ -208,10 +208,10 @@ Mesh *MeshManager::loadMesh(const string &filename)
if (result != _Meshes.end() && result->second.valid())
{
return result->second.get();
return result->second.pointer();
}
Mesh *mesh = new Mesh (_Device.get());
Mesh *mesh = new Mesh (_Device);
// check cache
PHYSFS_sint64 mod_file = PHYSFS_getLastModTime(filename.c_str());


+ 2
- 2
engine/ModelManager.cpp 查看文件

@@ -17,7 +17,7 @@ namespace BlueCore
{
//------------------------------------------------------------------------------
void Model::render()
void Model::render(RenderDevice *device) const
{
ModelMesh->render();
//glEnable(GL_LIGHTING);
@@ -208,7 +208,7 @@ Model *ModelManager::loadModel(const string &name)
if (result != _Models.end() )
{
return result->second.get();
return result->second.pointer();
}
string filename = name + ".model.xml";


+ 3
- 2
engine/ModelManager.h 查看文件

@@ -12,6 +12,7 @@
#include "TextureManager.h"
#include "ShaderManager.h"
#include "MeshManager.h"
#include "RenderQueue.h"

// forward declaration
class TiXmlElement;
@@ -19,7 +20,7 @@ class TiXmlElement;
namespace BlueCore
{

class Model : public Referenced, public Named
class Model : public Named, public RenderItem
{

public:
@@ -46,7 +47,7 @@ public:
RenderPassDefinition LitPass;
RenderPassDefinition DefaultPass;

void render();
void render(RenderDevice *) const;
};

class ModelManager : public Referenced


+ 82
- 225
engine/RenderDevice.cpp 查看文件

@@ -1,6 +1,7 @@
#include "RenderDevice.h"

#include "Utilities/Log.h"
#include "Math/Matrix.h"

#include "GL/gl.h"
#include "GL/glu.h"
@@ -8,7 +9,6 @@
namespace BlueCore
{

//------------------------------------------------------------------------------
void RenderDevice::WindowResizeSlot(int width, int height)
{
glViewport( 0, 0, width, height);
@@ -16,7 +16,6 @@ void RenderDevice::WindowResizeSlot(int width, int height)
_ViewportHeight = height;
}

//------------------------------------------------------------------------------
RenderDevice::RenderDevice(RenderWindow* renderWindow) :
_RenderWindow(renderWindow)
{
@@ -36,31 +35,26 @@ RenderDevice::RenderDevice(RenderWindow* renderWindow) :
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
@@ -81,7 +75,6 @@ void RenderDevice::begin2D()
glLoadIdentity();
}

//------------------------------------------------------------------------------
void RenderDevice::end2D()
{
// restore projection matrix
@@ -97,13 +90,16 @@ void RenderDevice::end2D()
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[] =
@@ -111,219 +107,80 @@ void RenderDevice::setAmbientLight(float r, float g, float b)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient);
}

//------------------------------------------------------------------------------
/*
void RenderDevice::renderShadowVolume(Mesh *mesh,
const Vector3Float &direction, const Vector3Float &extrude)
{
// Calculate visibility
unsigned int i, frontface_count = 0;
for (i=0; i < mesh->shadowfaces.count(); i++)
{
if (mesh->shadowfaces[i].plane.distance(extrude) < 0)
{
shadow_faces[i].backFace = false;
frontface_count++;
}
else
{
shadow_faces[i].backFace = true;
}
}

unsigned int max_edges = (frontface_count) * 3;
if (_extrudeVertexBuffer.count() < (max_edges * 4))
{
clog << ">>> increase shadow buffers to "<< max_edges << " Edges"<< endline;
_extrudeVertexBuffer.create( (max_edges + 100) * 4);
}

if (_extrudeIndexBuffer.count() < (max_edges * 6))
_extrudeIndexBuffer.create( (max_edges + 100) * 6);

// fill the buffer
unsigned int j, k;
extrude_quad_count = 0;
Vertex *face_vertices[3];
for (i=0; i<shadow_faces.count(); i++)
{
if (shadow_faces[i].backFace == false)
{
for (j=0; j<3; j++)
{
k = shadow_faces[i].neighbours[j];
if ( (k == 0) || (shadow_faces[k-1].backFace == true))
{
unsigned int src_index_offset = i * 3;
face_vertices[0]
= &mesh->vertex_buffer[ mesh->index_buffer[ src_index_offset ] ];
face_vertices[1]
= &mesh->vertex_buffer[ mesh->index_buffer[ src_index_offset + 1] ];
face_vertices[2]
= &mesh->vertex_buffer[ mesh->index_buffer[ src_index_offset + 2] ];

#ifdef DEBUG_SILHOUETTE
glBegin( GL_LINES );
glVertex3f( face_vertices[j]->point.x, face_vertices[j]->point.y, face_vertices[j]->point.z );
glVertex3f( face_vertices[(j+1)%3]->point.x, face_vertices[(j+1)%3]->point.y, face_vertices[(j+1)%3]->point.z );
glEnd();
#endif

unsigned int vertex_offset = extrude_quad_count*4;
_extrudeVertexBuffer[vertex_offset] = face_vertices[j]->point;
_extrudeVertexBuffer[vertex_offset+1] = face_vertices[(j+1)%3]->point;
_extrudeVertexBuffer[vertex_offset+2] = face_vertices[(j+1)%3]->point + extrude_vector;
_extrudeVertexBuffer[vertex_offset+3] = face_vertices[j]->point
+ extrude_vector;

unsigned int index_offset = extrude_quad_count*6;
_extrudeIndexBuffer[index_offset] = vertex_offset;
_extrudeIndexBuffer[index_offset+1] = vertex_offset + 3;
_extrudeIndexBuffer[index_offset+2] = vertex_offset + 1;
_extrudeIndexBuffer[index_offset+3] = vertex_offset + 1;
_extrudeIndexBuffer[index_offset+4] = vertex_offset + 3;
_extrudeIndexBuffer[index_offset+5] = vertex_offset + 2;

extrude_quad_count++;
}
}
}

}

if (_capIndexBuffer.count() < mesh->index_buffer.count() )
_capIndexBuffer.create(mesh->index_buffer.count() );

bf = 0;
ff = 0;

for (i=0; i<shadow_faces.count(); i++)
{
unsigned int dst_offset, src_offset = i*3;

if (shadow_faces[i].backFace == false)
{
dst_offset = ff*3;
ff++;
}
else
{
dst_offset = _capIndexBuffer.count() - (bf + 1)*3;
bf++;
}

_capIndexBuffer[dst_offset] = mesh->index_buffer[src_offset];
_capIndexBuffer[dst_offset+1] = mesh->index_buffer[src_offset+1];
_capIndexBuffer[dst_offset+2] = mesh->index_buffer[src_offset+2];
}

_lastOrientation = _AbsoluteRotation;
}

if (_extrudeVertexBuffer.count() > 0)
{
// draw the volume
glPushAttrib( GL_ALL_ATTRIB_BITS);

glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);

glEnable( GL_DEPTH_TEST);
glDepthMask(GL_FALSE);

#if defined(DEBUG_SILHOUETTE) || defined(DEBUG_CAPS) || defined(DEBUG_VOLUME)
glColorMask( 1, 1, 1, 1 );
glDepthFunc(GL_LEQUAL);
#else
glColorMask(0, 0, 0, 0);
glDepthFunc(GL_LESS);
#endif

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0, ~0);
glStencilMask( ~0);

glEnable(GL_CULL_FACE);

glMatrixMode ( GL_MODELVIEW);
glPushMatrix();
Matrix4x4 m(_AbsoluteRotation, _AbsoluteTranslation);
glMultMatrixd ( ( GLdouble * ) &m.m );

glActiveTextureARB ( GL_TEXTURE2_ARB);
glDisable ( GL_TEXTURE_2D);
glActiveTextureARB ( GL_TEXTURE1_ARB);
glDisable ( GL_TEXTURE_2D);
glActiveTextureARB ( GL_TEXTURE0_ARB);
glDisable ( GL_TEXTURE_2D);

ShaderManager::getSingleton()->useShaderProgram( 0);

#ifndef DEBUG_SILHOUETTE

glEnableClientState( GL_VERTEX_ARRAY);
glDisableClientState( GL_TEXTURE_COORD_ARRAY);
glDisableClientState( GL_NORMAL_ARRAY);
glDisableClientState( GL_ARRAY_BUFFER_ARB);
glDisableClientState( GL_ELEMENT_ARRAY_BUFFER_ARB);

for (int p=0; p<2; p++)
{
if (p==0)
{
glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
glCullFace(GL_FRONT);
//glColorMask(0, 1, 0, 0);
}
else
{
glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
glCullFace(GL_BACK);
//glColorMask(1, 0, 0, 0);
}

if (GLEW_ARB_vertex_buffer_object )
{
glBindBufferARB ( GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB ( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
// #if !defined(DEBUG_CAPS) || defined(DEBUG_VOLUME)
glVertexPointer ( 3, GL_FLOAT, 0, _extrudeVertexBuffer.data() );
glDrawElements (
GL_TRIANGLES,
extrude_quad_count * 6,
GL_UNSIGNED_SHORT,
_extrudeIndexBuffer.data());
// #endif

// #if !defined(DEBUG_VOLUME) || defined(DEBUG_CAPS)
// draw caps
mesh->vertex_buffer.bind();
//glVertexPointer ( 3, GL_FLOAT, sizeof(Vertex), mesh->vertex_buffer.data() );
glDrawElements (
GL_TRIANGLES,
ff*3,
GL_UNSIGNED_SHORT,
_capIndexBuffer.data());
//&_capIndexBuffer[_capIndexBuffer.count() - bf*3]);

glPushMatrix();
glTranslatef(extrude_vector.x, extrude_vector.y, extrude_vector.z);
glDrawElements (
GL_TRIANGLES,
bf*3,
GL_UNSIGNED_SHORT,
&_capIndexBuffer[_capIndexBuffer.count() - bf*3]);
//_capIndexBuffer.data() );
glPopMatrix();
// #endif
}

#endif

glPopAttrib();

glPopMatrix();
}
*/
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
}

+ 28
- 30
engine/RenderDevice.h 查看文件

@@ -2,7 +2,8 @@
#define BLUECORE_RENDERDEVICE_H

#include "RenderWindow.h"

#include "Camera.h"
#include "Material.h"
#include "Utilities/Buffer.h"
#include "Utilities/Referenced.h"
#include "Utilities/sigslot.h"
@@ -12,45 +13,42 @@ namespace BlueCore

class RenderDevice : public Referenced, public sigslot::has_slots<>
{
public:

class RenderItem
{

public:
//virtual void render ( RenderPass pass ) = 0;
virtual ~RenderItem()
{};
};
private:
int _ViewportWidth, _ViewportHeight;

void WindowResizeSlot(int width, int height);
void WindowCloseSlot();
private:
int _ViewportWidth, _ViewportHeight;

ref_ptr<RenderWindow> _RenderWindow;
void WindowResizeSlot(int width, int height);
void WindowCloseSlot();

public:
ref_ptr<RenderWindow> _RenderWindow;
ref_ptr<Camera> _Camera;
public:

RenderDevice(RenderWindow* renderWindow);
~RenderDevice();
RenderDevice(RenderWindow* renderWindow);
~RenderDevice();

int getViewportWidth();
int getViewportHeight();
int getViewportWidth();
int getViewportHeight();

void begin2D();
void end2D();
void begin2D();
void end2D();

void clear();
void clear();
void swap();

void setAmbientLight(float r, float g, float b);
void setAmbientLight(float r, float g, float b);
void setMaterial(Material *);

void setTexture(unsigned int unit, unsigned int texture);
void setTexture(unsigned int unit, unsigned int texture);
void begin3D(Camera *);
void setTransformation (const Vector3&, const Quaternion&);
void setupProjectionMatrix();
void setupViewMatrix();

sigslot::signal0<> DeviceShutdownSignal;
sigslot::signal0<> DeviceShutdownSignal;

//Buffer<ShadowFace> &_ShadowFaces;
//Buffer<ShadowFace> &_ShadowFaces;
};

} // namespaced BlueCore


+ 51
- 0
engine/RenderQueue.cpp 查看文件

@@ -0,0 +1,51 @@
#include "RenderQueue.h"
namespace BlueCore
{
void RenderQueue::addOpaqueItem(RenderItem *item, const Vector3& position,
const Quaternion& orientation)
{
_OpaqueItems.push_back(QueueItem(item, position, orientation));
}
void RenderQueue::addTransparentItem(RenderItem *item, const Vector3& position,
const Quaternion& orientation)
{
_TransparentItems.push_back(QueueItem(item, position, orientation));
}
const std::list<RenderQueue::QueueItem>& RenderQueue::getOpaqueItems()
{
return _OpaqueItems;
}
const std::list<RenderQueue::QueueItem>& RenderQueue::getTransparentItems()
{
return _TransparentItems;
}
void RenderQueue::clear()
{
_OpaqueItems.clear();
_TransparentItems.clear();
}
void RenderQueue::render(RenderDevice *device) const
{
std::list<QueueItem>::const_iterator i;
for (i = _OpaqueItems.begin(); i != _OpaqueItems.end(); i++)
{
device->setTransformation((*i).position, (*i).orientation);
(*i).item->render(device);
}
for (i = _TransparentItems.begin(); i != _TransparentItems.end(); i++)
{
device->setTransformation((*i).position, (*i).orientation);
(*i).item->render(device);
}
}
}

+ 52
- 0
engine/RenderQueue.h 查看文件

@@ -0,0 +1,52 @@
#ifndef BLUECORE_RENDER_QUEUE_H
#define BLUECORE_RENDER_QUEUE_H
#include "RenderDevice.h"
namespace BlueCore
{
class RenderItem : public Referenced
{
public:
virtual void render(RenderDevice *) const = 0;
};
class RenderQueue : public Referenced
{
struct QueueItem
{
QueueItem(RenderItem *renderitem, const Vector3& vector,
const Quaternion& quat) :
item(renderitem), position(vector), orientation(quat)
{
}
ref_ptr<RenderItem> item;
Vector3 position;
Quaternion orientation;
};
std::list<QueueItem> _OpaqueItems;
std::list<QueueItem> _TransparentItems;
public:
void addOpaqueItem(RenderItem *item, const Vector3& position,
const Quaternion& orientation);
void addTransparentItem(RenderItem *item, const Vector3& position,
const Quaternion& orientation);
const std::list<QueueItem>& getOpaqueItems();
const std::list<QueueItem>& getTransparentItems();
void clear();
void render(RenderDevice *device) const;
};
} // namespace BluewCore
#endif // BLUECORE_RENDER_QUEUE_H

+ 0
- 6
engine/RenderWindow.cpp 查看文件

@@ -115,12 +115,6 @@ bool RenderWindow::create(int width, int height, int pixelbits, int depthbits,
}
//------------------------------------------------------------------------------
void RenderWindow::swap()
{
glfwSwapBuffers();
}
//------------------------------------------------------------------------------
void RenderWindow::destroy()
{
if (_Initialized)


+ 1
- 2
engine/RenderWindow.h 查看文件

@@ -5,6 +5,7 @@
#include <vector>
// library includes
#include "GL/glew.h"
#include "GL/glfw.h"
// project includes
@@ -63,8 +64,6 @@ class RenderWindow : public Referenced
int getHeight();
bool isOpen();
void swap();
};
}


+ 1
- 1
engine/ScriptSystem_Image.cpp 查看文件

@@ -40,7 +40,7 @@ static SQInteger _image_constructor(HSQUIRRELVM vm)
sq_getfloat(vm, 6, &by );

Texture *texture = gTextureManager->loadTexture(texturename, 0, 0);
image = new TextureImage( gRenderDevice.get(), texture, ax, ay, bx, by );
image = new TextureImage( gRenderDevice, texture, ax, ay, bx, by );
image->addReference();
sq_setinstanceup(vm, 1, (void *)image );


+ 1
- 1
engine/Utilities/Kernel.cpp 查看文件

@@ -30,7 +30,7 @@ void Kernel::setTaskPriority(KernelTask* task, int priority)
std::list<KernelTaskContainer>::iterator i;
for (i = _Tasks.begin(); i != _Tasks.end(); i++)
{
if ((*i)._Task.get() == task)
if ((*i)._Task == task)
{
(*i)._Priority = priority;
_Changed = true;


+ 14
- 2
engine/Utilities/Referenced.h 查看文件

@@ -137,6 +137,14 @@ public:
return false;
}
bool operator == (T* ref)
{
if (_Referenced == ref)
return true;
else
return false;
}
bool valid() const
{
return (_Referenced != 0);
@@ -147,7 +155,7 @@ public:
return _Referenced;
}
T* get() const
T* pointer() const
{
return _Referenced;
}
@@ -215,11 +223,15 @@ public:
}
T* get() const
T* pointer() const
{
return _Referenced;
}
operator T*() const
{
return _Referenced;
}
void onDestruction(Referenced *referenced)
{


+ 10
- 7
engine/main.cpp 查看文件

@@ -87,6 +87,7 @@ int main(int argc, char **argv)
ref_ptr<RigidBodySimulation> simulation = new RigidBodySimulation(scriptsystem);
ref_ptr<ModelManager> modelmanager = new ModelManager (texturemanager, shadermanager, meshmanager);
ref_ptr<Camera> camera = new Camera();
ref_ptr<RenderQueue> queue = new RenderQueue();

/*
ShaderProgram prog =
@@ -130,15 +131,16 @@ int main(int argc, char **argv)

while (window->isOpen())
{
device->clear();
double time = glfwGetTime();
_DeltaTime = time - _LastTime;
_LastTime = time;

// device->setViewMatrix();
// device->setProjectionMatrix();
camera->setupProjectionMatrix();
camera->setupViewMatrix();
device->clear();
device->begin3D(camera);

queue->clear();
queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion(Vector3(0.0,1.0,0.0), fmod(time, 3)));
queue->render(device);

// device->useShader (program);
// device->setTexture (stage, name, texture)
@@ -148,7 +150,8 @@ int main(int argc, char **argv)
//glBindTexture (GL_TEXTURE_2D, texture->getId() );

// device->
model->render();
//device->
//model->render();

simulation->saveStates();
simulation->updateSteps(_DeltaTime);
@@ -156,7 +159,7 @@ int main(int argc, char **argv)
;
scriptsystem->callFunction("OnFrame", _DeltaTime);

window->swap();
device->swap();
}

scriptsystem->callFunction("Shutdown");


+ 240
- 1
engine/todo.txt 查看文件

@@ -10,4 +10,243 @@ Scripting
* make ShaderProgram a class
* create RenderSet AND/OR RenderQueue
#if 0
/*
void RenderDevice::renderShadowVolume(Mesh *mesh,
const Vector3Float &direction, const Vector3Float &extrude)
{
// Calculate visibility
unsigned int i, frontface_count = 0;
for (i=0; i < mesh->shadowfaces.count(); i++)
{
if (mesh->shadowfaces[i].plane.distance(extrude) < 0)
{
shadow_faces[i].backFace = false;
frontface_count++;
}
else
{
shadow_faces[i].backFace = true;
}
}
unsigned int max_edges = (frontface_count) * 3;
if (_extrudeVertexBuffer.count() < (max_edges * 4))
{
clog << ">>> increase shadow buffers to "<< max_edges << " Edges"<< endline;
_extrudeVertexBuffer.create( (max_edges + 100) * 4);
}
if (_extrudeIndexBuffer.count() < (max_edges * 6))
_extrudeIndexBuffer.create( (max_edges + 100) * 6);
// fill the buffer
unsigned int j, k;
extrude_quad_count = 0;
Vertex *face_vertices[3];
for (i=0; i<shadow_faces.count(); i++)
{
if (shadow_faces[i].backFace == false)
{
for (j=0; j<3; j++)
{
k = shadow_faces[i].neighbours[j];
if ( (k == 0) || (shadow_faces[k-1].backFace == true))
{
unsigned int src_index_offset = i * 3;
face_vertices[0]
= &mesh->vertex_buffer[ mesh->index_buffer[ src_index_offset ] ];
face_vertices[1]
= &mesh->vertex_buffer[ mesh->index_buffer[ src_index_offset + 1] ];
face_vertices[2]
= &mesh->vertex_buffer[ mesh->index_buffer[ src_index_offset + 2] ];
#ifdef DEBUG_SILHOUETTE
glBegin( GL_LINES );
glVertex3f( face_vertices[j]->point.x, face_vertices[j]->point.y, face_vertices[j]->point.z );
glVertex3f( face_vertices[(j+1)%3]->point.x, face_vertices[(j+1)%3]->point.y, face_vertices[(j+1)%3]->point.z );
glEnd();
#endif
unsigned int vertex_offset = extrude_quad_count*4;
_extrudeVertexBuffer[vertex_offset] = face_vertices[j]->point;
_extrudeVertexBuffer[vertex_offset+1] = face_vertices[(j+1)%3]->point;
_extrudeVertexBuffer[vertex_offset+2] = face_vertices[(j+1)%3]->point + extrude_vector;
_extrudeVertexBuffer[vertex_offset+3] = face_vertices[j]->point
+ extrude_vector;
unsigned int index_offset = extrude_quad_count*6;
_extrudeIndexBuffer[index_offset] = vertex_offset;
_extrudeIndexBuffer[index_offset+1] = vertex_offset + 3;
_extrudeIndexBuffer[index_offset+2] = vertex_offset + 1;
_extrudeIndexBuffer[index_offset+3] = vertex_offset + 1;
_extrudeIndexBuffer[index_offset+4] = vertex_offset + 3;
_extrudeIndexBuffer[index_offset+5] = vertex_offset + 2;
extrude_quad_count++;
}
}
}
}
if (_capIndexBuffer.count() < mesh->index_buffer.count() )
_capIndexBuffer.create(mesh->index_buffer.count() );
bf = 0;
ff = 0;
for (i=0; i<shadow_faces.count(); i++)
{
unsigned int dst_offset, src_offset = i*3;
if (shadow_faces[i].backFace == false)
{
dst_offset = ff*3;
ff++;
}
else
{
dst_offset = _capIndexBuffer.count() - (bf + 1)*3;
bf++;
}
_capIndexBuffer[dst_offset] = mesh->index_buffer[src_offset];
_capIndexBuffer[dst_offset+1] = mesh->index_buffer[src_offset+1];
_capIndexBuffer[dst_offset+2] = mesh->index_buffer[src_offset+2];
}
_lastOrientation = _AbsoluteRotation;
}
if (_extrudeVertexBuffer.count() > 0)
{
// draw the volume
glPushAttrib( GL_ALL_ATTRIB_BITS);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glEnable( GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
#if defined(DEBUG_SILHOUETTE) || defined(DEBUG_CAPS) || defined(DEBUG_VOLUME)
glColorMask( 1, 1, 1, 1 );
glDepthFunc(GL_LEQUAL);
#else
glColorMask(0, 0, 0, 0);
glDepthFunc(GL_LESS);
#endif
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0, ~0);
glStencilMask( ~0);
glEnable(GL_CULL_FACE);
glMatrixMode ( GL_MODELVIEW);
glPushMatrix();
Matrix4x4 m(_AbsoluteRotation, _AbsoluteTranslation);
glMultMatrixd ( ( GLdouble * ) &m.m );
glActiveTextureARB ( GL_TEXTURE2_ARB);
glDisable ( GL_TEXTURE_2D);
glActiveTextureARB ( GL_TEXTURE1_ARB);
glDisable ( GL_TEXTURE_2D);
glActiveTextureARB ( GL_TEXTURE0_ARB);
glDisable ( GL_TEXTURE_2D);
ShaderManager::getSingleton()->useShaderProgram( 0);
#ifndef DEBUG_SILHOUETTE
glEnableClientState( GL_VERTEX_ARRAY);
glDisableClientState( GL_TEXTURE_COORD_ARRAY);
glDisableClientState( GL_NORMAL_ARRAY);
glDisableClientState( GL_ARRAY_BUFFER_ARB);
glDisableClientState( GL_ELEMENT_ARRAY_BUFFER_ARB);
for (int p=0; p<2; p++)
{
if (p==0)
{
glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
glCullFace(GL_FRONT);
//glColorMask(0, 1, 0, 0);
}
else
{
glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
glCullFace(GL_BACK);
//glColorMask(1, 0, 0, 0);
}
if (GLEW_ARB_vertex_buffer_object )
{
glBindBufferARB ( GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB ( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
// #if !defined(DEBUG_CAPS) || defined(DEBUG_VOLUME)
glVertexPointer ( 3, GL_FLOAT, 0, _extrudeVertexBuffer.data() );
glDrawElements (
GL_TRIANGLES,
extrude_quad_count * 6,
GL_UNSIGNED_SHORT,
_extrudeIndexBuffer.data());
// #endif
// #if !defined(DEBUG_VOLUME) || defined(DEBUG_CAPS)
// draw caps
mesh->vertex_buffer.bind();
//glVertexPointer ( 3, GL_FLOAT, sizeof(Vertex), mesh->vertex_buffer.data() );
glDrawElements (
GL_TRIANGLES,
ff*3,
GL_UNSIGNED_SHORT,
_capIndexBuffer.data());
//&_capIndexBuffer[_capIndexBuffer.count() - bf*3]);
glPushMatrix();
glTranslatef(extrude_vector.x, extrude_vector.y, extrude_vector.z);
glDrawElements (
GL_TRIANGLES,
bf*3,
GL_UNSIGNED_SHORT,
&_capIndexBuffer[_capIndexBuffer.count() - bf*3]);
//_capIndexBuffer.data() );
glPopMatrix();
// #endif
}
#endif
glPopAttrib();
glPopMatrix();
}
*/
Scalar fW, fH;
fH = tan ( (_FoV / 2) / 180* Pi ) * _NearPlane;
fW = fH * _AspectRatio;
// setup projectiom matrix
glMatrixMode (GL_PROJECTION );
glLoadIdentity();
glFrustum ( -fW, fW, -fH, fH, _NearPlane, _FarPlane );
// save variables for frustum culling
/*
_near = nearZ;
_far = farZ;
_hNear = tan ( ( fov / 2 ) / 180 * Pi ) * nearZ;
_wNear = _hNear * aspect;
_hFar = tan ( ( fov / 2 ) / 180 * Pi ) * farZ;
_wFar = _hFar * aspect;
*/
}
#endif

Loading…
取消
儲存