This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Render System Framework
  Submitted by



This material is intended to provide a sample framework for beginners that would start to develop a render system capable of supporting various 3DApi (Direct X, OpenGL are included). This sample render system should give a starting point to those who are still wondering how to design this, and also provide the base to add other features that are not yet implemented here (as you can see, the goal was not to provide a complete render system but to present how it should be layed out according to me). I would be glad to ear from experienced people about this approach as this is the first time I give a try to designing such a renderer. Of course, as "educational" material, I am sure that a lot of improvements/optimisations can be implemented, starting with adding decent math library, using vertex array range in the OpenGL implementation of static vertex buffers, ... Feel free to suggest me other ideas or report any bug (about the render system, not about the sample app, I already know this one is crappy :)

More information about the framework and how it was designed can be found in the readme.txt file. Vincent

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/OpenGLRenderDevice.cpp] - (6,818 bytes)

#ifdef COMPILE_OPENGL_IMPLEMENTATION

#include "OpenGLRenderDevice.h" #include "OpenGLVertexBuffer.h"

OpenGLRenderDevice::OpenGLRenderDevice( HWND hwnd, DWORD width, DWORD height ) : RenderDeviceImplementor() { // Init some member values //-- m_baseApi = RSAPI_OPENGL; m_hWnd = hwnd; // Fill in the pixel format descriptor. // PIXELFORMATDESCRIPTOR pfd ; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW ; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE;

HDC hdc = ::GetDC( m_hWnd );

// Choose pixel format //-- int nPixelFormat = ChoosePixelFormat( hdc, &pfd ); if (nPixelFormat == 0) { ::OutputDebugString( "ChoosePixelFormat Failed\n" ) ; } else { // Set pixel format //-- BOOL bResult = SetPixelFormat ( hdc, nPixelFormat, &pfd ); if (!bResult) { ::OutputDebugString( "SetPixelFormat Failed\n" ) ; } else { // Create a rendering context. //-- m_hrc = wglCreateContext( hdc ); if ( !m_hrc ) { ::OutputDebugString( "wglCreateContext Failed\n" ) ; } else { // Set it as the current context //-- if ( !wglMakeCurrent( hdc, m_hrc ) ) { ::OutputDebugString( "wglMakeCurrent Failed\n" ) ; } } } }

// Set a default viewport //-- glEnable( GL_SCISSOR_TEST ); SetViewport( 0, 0, width, height );

SetBackgroundColor( 1, 0, 0, 0 ); SetDepthClearValue( 1.0f ); SetStencilClearValue( 0 ); }

OpenGLRenderDevice::~OpenGLRenderDevice() { Release(); }



void OpenGLRenderDevice::Release() { wglDeleteContext( m_hrc ); }

//------------------------------------------------------------------ // Tranformation functions //-- void OpenGLRenderDevice::MatrixModeSet(RS_MATRIXMODE mode) { m_matrixMode = mode;

if ( m_matrixMode==RSMM_PROJECTION ) glMatrixMode( GL_PROJECTION ); else glMatrixMode( GL_MODELVIEW ); }

const RS_MATRIXMODE OpenGLRenderDevice::MatrixModeGet() const { return m_matrixMode; }

void OpenGLRenderDevice::MatrixLoadIdentity() { glLoadIdentity(); }

void OpenGLRenderDevice::MatrixLoad( const Matrix4x4& mat ) { Matrix4x4 m = mat.GetTranspose(); glLoadMatrixf( (float*)(m.m_Mat) ); }

void OpenGLRenderDevice::MatrixPush() { glPushMatrix(); }

void OpenGLRenderDevice::MatrixPop() { glPopMatrix(); }

void OpenGLRenderDevice::MatrixScale( float x, float y, float z ) { glScalef( x, y, z ); }

void OpenGLRenderDevice::MatrixTranslation( float x, float y, float z ) { glTranslatef( x, y, z ); }

void OpenGLRenderDevice::MatrixRotation( float angle, float x, float y, float z ) { glRotatef( angle, x, y, z ); }

void OpenGLRenderDevice::MatrixMultiply(const Matrix4x4& m) { Matrix4x4 mat=m.GetTranspose(); glMultMatrixf( (float*)(m.m_Mat) ); }

Matrix4x4 OpenGLRenderDevice::MatrixGet() { Matrix4x4 rv;

if ( m_matrixMode==RSMM_PROJECTION ) glGetFloatv(GL_PROJECTION_MATRIX, (float *) rv.m_Mat); else glGetFloatv(GL_MODELVIEW_MATRIX, (float *) rv.m_Mat); return rv.GetTranspose(); }

//------------------------------------------------------------------ // Rendering functions //-- void OpenGLRenderDevice::BeginRendering() { }

void OpenGLRenderDevice::EndRendering() { }

void OpenGLRenderDevice::SwapBuffers() { HDC hdc = ::GetDC( m_hWnd ); ::SwapBuffers( hdc ); }

bool OpenGLRenderDevice::DrawPrimitives( RS_PRIMITIVETYPE primitiveType, VertexBuffer* pVB, DWORD baseVertex, DWORD primitiveCount )

{ if ( pVB==NULL ) return 0;

// Convert primitive type //-- GLenum PT; DWORD count; if ( !GetOpenGLPrimitiveType( primitiveType, primitiveCount, &PT, &count ) ) return 0;

// Say that the VB will be the source for our draw primitive calls //-- if ( !pVB->PrepareForDrawing() ) return 0;

// Draw primitives //-- glDrawArrays( PT, baseVertex, count ); return (GL_NO_ERROR == glGetError()); }

void OpenGLRenderDevice::UseTexture( Texture* pTex ) { pTex->Use(); }

//------------------------------------------------------------------ // Render state switches functions //-- bool OpenGLRenderDevice::SetRenderState( RS_RENDERSTATETYPE state, DWORD value ) { switch (state) { case RSRS_ZENABLE: if (value==1) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); break;

case RSRS_LIGHTINGENABLE: if (value==1) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); break;

case RSRS_CULLINGENABLE: switch (value) { case 0: glDisable(GL_CULL_FACE); break;

case 1: glEnable(GL_CULL_FACE); break;

default: return false; }; break;

case RSRS_FILLMODE: switch (value) { case RSFILL_WIREFRAME: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); break;

case RSFILL_SOLID: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break;

default: return false; }; break;

default: return false; } return false; }

DWORD OpenGLRenderDevice::GetRenderState( RS_RENDERSTATETYPE state ) { GLint rv= 0;

switch (state) { case RSRS_ZENABLE: glGetIntegerv(GL_DEPTH_TEST, &rv); return rv;

case RSRS_LIGHTINGENABLE: glGetIntegerv(GL_LIGHTING, &rv); return rv;

case RSRS_CULLINGENABLE: glGetIntegerv(GL_CULL_FACE, &rv); return rv;

case RSRS_FILLMODE: glGetIntegerv(GL_POLYGON_MODE, &rv); if ( rv == GL_LINE ) return RSFILL_WIREFRAME; else return RSFILL_SOLID; } return 0; }

//------------------------------------------------------------------ // Other functions //-- void OpenGLRenderDevice::SetBackgroundColor( float a, float r, float g, float b ) { glClearColor( r, g, b, a ); }

void OpenGLRenderDevice::SetDepthClearValue( float z ) { glClearDepth( z ); }

void OpenGLRenderDevice::SetStencilClearValue( DWORD s ) { glClearStencil( s ); }

void OpenGLRenderDevice::Clear( DWORD flags ) { GLbitfield glFlags = 0;

if ( flags & RSCLR_COLOR ) glFlags |= GL_COLOR_BUFFER_BIT;

if ( flags & RSCLR_ZBUFFER ) glFlags |= GL_DEPTH_BUFFER_BIT;

if ( flags & RSCLR_STENCIL ) glFlags |= GL_STENCIL_BUFFER_BIT;

glClear( glFlags ); }

bool OpenGLRenderDevice::SetViewport( int x, int y, int w, int h ) { if ( (w==0) || (h==0) ) return false;

glViewport( x, h-y, w, h ); if (GL_NO_ERROR != glGetError()) return false;

glScissor( x, h-y, w, h ); if (GL_NO_ERROR != glGetError()) return false;

return true; }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/Matrix4x4.cpp] - (2,854 bytes)

#include <math.h>
#include <memory.h>
#include "Matrix4x4.h"

Matrix4x4::Matrix4x4() { SetIdentity(); }

Matrix4x4::Matrix4x4(const Matrix4x4& mat) { memcpy( m_Mat, mat.m_Mat, 16*sizeof(float) ); }

Matrix4x4& Matrix4x4::operator=(const Matrix4x4& mat) { memcpy( m_Mat, mat.m_Mat, 16*sizeof(float) ); return *this; }

Matrix4x4::~Matrix4x4() { }

void Matrix4x4::SetIdentity(void) { m_Mat[0][0]= 1; m_Mat[1][0]= 0; m_Mat[2][0]= 0; m_Mat[3][0]= 0; m_Mat[0][1]= 0; m_Mat[1][1]= 1; m_Mat[2][1]= 0; m_Mat[3][1]= 0; m_Mat[0][2]= 0; m_Mat[1][2]= 0; m_Mat[2][2]= 1; m_Mat[3][2]= 0; m_Mat[0][3]= 0; m_Mat[1][3]= 0; m_Mat[2][3]= 0; m_Mat[3][3]= 1; }

void Matrix4x4::SetPerspective(float ncp, float fcp, float fov, float aspect) { float c,s,Q; c= (float) cos( 0.5f*RS_DEGTORAD(fov) ); s= (float) sin( 0.5f*RS_DEGTORAD(fov) ); Q= s/(1.0f-ncp/fcp); m_Mat[0][0]= c/(aspect*Q*ncp); m_Mat[1][0]= 0; m_Mat[2][0]= 0; m_Mat[3][0]= 0; m_Mat[0][1]= 0; m_Mat[1][1]= c/(Q*ncp); m_Mat[2][1]= 0; m_Mat[3][1]= 0; m_Mat[0][2]= 0; m_Mat[1][2]= 0; m_Mat[2][2]= -1/ncp; m_Mat[3][2]= -s/(Q*ncp); m_Mat[0][3]= 0; m_Mat[1][3]= 0; m_Mat[2][3]= -1; m_Mat[3][3]= 0; }

void Matrix4x4::SetRotation(float angle, float x, float y, float z) { float length; float c,s,t; float theta = RS_DEGTORAD(angle); // normalize //-- length = sqrtf(x*x + y*y + z*z); // too close to 0, can't make a normalized vector //-- if (length < 0.000001f) return; x /= length; y /= length; z /= length; // do the trig //-- c = cosf(theta); s = sinf(theta); t = 1-c; // build the rotation matrix //-- m_Mat[0][0] = t*x*x + c; m_Mat[0][1] = t*x*y - s*z; m_Mat[0][2] = t*x*z + s*y; m_Mat[0][3] = 0; m_Mat[1][0] = t*x*y + s*z; m_Mat[1][1] = t*y*y + c; m_Mat[1][2] = t*y*z - s*x; m_Mat[1][3] = 0; m_Mat[2][0] = t*x*z - s*y; m_Mat[2][1] = t*y*z + s*x; m_Mat[2][2] = t*z*z + c; m_Mat[2][3] = 0; m_Mat[3][0] = 0; m_Mat[3][1] = 0; m_Mat[3][2] = 0; m_Mat[3][3] = 1; }

void Matrix4x4::SetTranslation(float x, float y, float z) { m_Mat[0][3] = x; m_Mat[1][3] = y; m_Mat[2][3] = z; }

void Matrix4x4::SetScale(float x, float y, float z) { m_Mat[0][0] = x; m_Mat[1][1] = y; m_Mat[2][2] = z; }

void Matrix4x4::Multiply(const Matrix4x4& mat) { Matrix4x4 res; int i,j,k; for (i=0; i<4; i++) { for (j=0; j<4; j++) { res.m_Mat[i][j] = 0.0; for (k=0; k<4; k++) { res.m_Mat[i][j] += m_Mat[i][k]*mat.m_Mat[k][j]; } } }

*this = res; }

Matrix4x4 Matrix4x4::GetTranspose(void) const { Matrix4x4 rv; int i,j; for (i=0; i<4; i++) { for (j=0; j<4; j++) { rv.m_Mat[i][j]= m_Mat[j][i]; } }

return rv; }

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/RenderTypeDefs.h] - (3,100 bytes)

#ifndef _RENDERTYPEDEFS_H_INCLUDED_
#define _RENDERTYPEDEFS_H_INCLUDED_

#include <windows.h> #include <windef.h>

/** * Base Api used to render primitives */ typedef enum _BASE_API { RSAPI_DIRECT3D, /** Direct3D is used as the base API */ RSAPI_OPENGL, /** OpenGL is used as the base API */ } RS_BASEAPI;

/** * Formats to use for surfaces */ typedef enum _PIXELFORMAT { RSFMT_A8R8G8B8, /** 32 bits per pixel (ARGB) */ } RS_FORMAT;

/** * Current matrix mode in the render device (indicate which * matrix is modified by matrix function calls). */ typedef enum _MATRIX_MODE { RSMM_PROJECTION = 0, /** Projection matrix mode */ RSMM_MODELVIEW = 1, /** Model-view matrix mode */ } RS_MATRIXMODE;

/** * Fill mode used to render primitives on screen */ typedef enum _FILLMODE { RSFILL_WIREFRAME = 0x0000, /** Render primitives using wireframe mode */ RSFILL_SOLID = 0x0001, /** Render filled primitives */ } RS_FILLMODE;

/** * List of the render states that can be set on a device. */ typedef enum _RENDERSTATETYPE { RSRS_FILLMODE, /** Set to one of the _FILLMODE values */ RSRS_ZENABLE, /** Enable z-buffer testing. Set to 1 (True) or 0 (False) */ RSRS_CULLINGENABLE, /** Enable CCW backface culling. Set to 1 (True) or 0 (False) */ RSRS_LIGHTINGENABLE, /** Enable lighting. Set to 1 (True) or 0 (False) */ } RS_RENDERSTATETYPE;

/** * Flags used to describe the format of a vertex in a vertex buffer */ typedef enum __VERTEXFORMATFLAGS { RSVF_XYZ = 0x0001, /** Untransformed XYZ value set. */ RSVF_XYZRHW = 0x0002, /** transformed XYZRHW value set. */ RSVF_NORMAL = 0x0004, /** XYZ normal value set */ RSVF_DIFFUSE = 0x0008, /** RGBA diffuse color value set */ RSVF_TEXCOORD = 0x0010, /** Texture vertex flag. */ } RS_VERTEXFORMATFLAGS;

/** * Supported primitive types */ typedef enum __PRIMITIVETYPE { RSPT_POINTLIST, /** Specifies a point list */ RSPT_LINELIST, /** Specifies a line list */ RSPT_LINESTRIP, /** Specifies a line strip */ RSPT_TRIANGLELIST, /** Specifies a triangle list */ RSPT_TRIANGLESTRIP, /** Specifies a triangle strip */ RSPT_TRIANGLEFAN, /** Specifies a triangle fan */ } RS_PRIMITIVETYPE;

/** * Valid clear flags */ typedef enum _CLEAR_FLAG { RSCLR_COLOR = 0x0001, /** Clear color buffer */ RSCLR_ZBUFFER = 0x0002, /** Clear z-buffer */ RSCLR_STENCIL = 0x0004, /** Clear stencil buffer */ } RS_CLEAR_FLAG;

/** * A handy macro to release a pointer */ #ifndef SAFE_RELEASE #define SAFE_RELEASE( ptr ) { if (ptr!=NULL) { ptr->Release(); ptr = NULL; } } #endif

/** * A handy macro to delete a pointer */ #ifndef SAFE_DELETE #define SAFE_DELETE( ptr ) { if (ptr!=NULL) { delete ptr; ptr = NULL; } } #endif

/** * A handy macro to delete a pointer to an array */ #ifndef SAFE_DELETEARRAY #define SAFE_DELETEARRAY( ptr ) { if (ptr!=NULL) { delete [] ptr; ptr = NULL; } } #endif

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/Matrix4x4.h] - (4,188 bytes)

#ifndef _Matrix4x4_h_
#define _Matrix4x4_h_

#define RS_DEGTORAD(X) ((X)*3.1415926f/180.0f) #define RS_RADTODEG(X) ((X)*180.0f/3.1415926f)

class Matrix4x4 { public: Matrix4x4(); Matrix4x4(const Matrix4x4& mat); Matrix4x4& operator=(const Matrix4x4& mat); ~Matrix4x4();

void SetIdentity(); void SetTranslation(float x, float y, float z); void SetRotation(float angle, float x, float y, float z); void SetScale(float x, float y, float z); void SetPerspective(float ncp, float fcp, float fov, float aspect); void Multiply(const Matrix4x4& mat); Matrix4x4 GetTranspose (void) const;

public: float m_Mat[4][4]; };

#endif





























////////////////////////////////////////////////////////////////////////////////// #if 0 class Matrix4x4 { public: Matrix4x4 (); ~Matrix(); Matrix4x4 (Double a11, Double a12, Double a13, Double a14, Double a21, Double a22, Double a23, Double a24, Double a31, Double a32, Double a33, Double a34, Double a41, Double a42, Double a43, Double a44 ); Matrix4x4 (const Matrix4x4 &m );

void setValue ( const Matrix4x4 &m ); void getValue ( Matrix4x4 &m ) const; const Matrix4x4& getValue () const;

void makeIdentity (); static Matrix4x4 identity ();

void setRotate (const RotationEuler &q ); void setRotate (const Quaternion &q ); void setScale ( Double s ); void setScale ( const Vec3f &s ); void setTranslate ( const Vec3f &t ); Matrix4x4 inverse (void) const; Matrix4x4 transpose (void) const; Matrix4x4& multRight ( const Matrix4x4 &m ); Matrix4x4& multLeft ( const Matrix4x4 &m ); void multMatrixVec( const Vec3f &src, Vec3f &dst ) const; void multVecMatrix( const Vec3f &src, Vec3f &dst ) const; void multDirMatrix( const Vec3f &src, Vec3f &dst ) const; void multLineMatrix( const Line &src, Line &dst ) const;

void print(void) const;

operator Double*(); operator Matrix4x4&();

Double* operator [] ( int i ); const Double* operator[] ( int i ) const;

Matrix4x4& operator = ( const Matrix4x4 &m ); Matrix4x4& operator = ( const Matrix4x4 &m ); Matrix4x4& operator = ( const RotationXYZ &q ); Matrix4x4& operator = ( const RotationQuat &q );

Matrix4x4& operator *= ( const Matrix4x4 &m ); friend Matrix4x4 operator *( const Matrix4x4 &m1, const Matrix4x4 &m2 ); friend bool operator == ( const Matrix4x4 & m1, const Matrix4x4 &m2 ); friend bool operator != ( const Matrix4x4 & m1, const Matrix4x4 &m2 ); bool equals( const Matrix4x4 &m, Double tolerance ) const;

Double _mat[4][4]; }; void Matrix4x4::setRotation(const RotationEuler &euler ) { float cosroll,sinroll,cospitch,sinpitch,cosyaw,sinyaw; float sinyawcosroll,sinyawsinroll;

/* Convert to radians */ float roll,pitch,yaw; euler.getRPY(roll,pitch,yaw); roll*=_PI_OVER_180; pitch*=_PI_OVER_180; yaw*=_PI_OVER_180;

/* Calculate trig functions */ cosroll=cos(euler.roll()); sinroll=sin(euler.roll()); cospitch=cos(euler.pitch()); sinpitch=sin(euler.pitch()); cosyaw=cos(euler.yaw()); sinyaw=sin(euler.yaw()); sinyawcosroll=sinyaw*cosroll; sinyawsinroll=sinyaw*sinroll;

/* Form the matrix */

_mat[0][0] = cosyaw*cosroll; //XX _mat[0][1] = sinpitch*sinyawcosroll - cospitch*sinroll; //YX _mat[0][2] = cospitch*sinyawcosroll + sinpitch*sinroll; //ZX _mat[1][0] = cosyaw*sinroll; //XY _mat[1][1] = sinpitch*sinyawsinroll + cospitch*cosroll; //YY _mat[1][2] = cospitch*sinyawsinroll - sinpitch*cosroll; //ZY _mat[2][0] = -sinyaw; //XZ _mat[2][1] = cosyaw*sinpitch; //YZ _mat[2][2] = cosyaw*cospitch; //ZZ } void Matrix4x4::setRotate(const Quaternion &q ) { float sqw = q._angle*q._angle; float sqx = q._x*q._x; float sqy = q._y*q._y; float sqz = q._z*q._z;

float heading = atan(2.0* ((q._x*q._y + q._z*q._angle)/(sqx-sqy-sqz+sqw)));

float bank = atan(2.0* ((q._y*q._z + q._x*q._angle)/(-sqx-sqy+sqz+sqw)));

float attitude = asin(-2.0*(q._x*q._z + q._y*q._angle));

RotationEuler rot(heading,bank,attitude); setRotation(rot); }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/MatrixStack.h] - (4,240 bytes)

#ifndef _MatrixStack_h_
#define _MatrixStack_h_

#include "Matrix4x4.h" #include <list>

class MatrixStack { private: // The stack, represented by a STL container: list //-- std::list< Matrix4x4 > stack;

public: // Constructor //-- MatrixStack() { // Empty the stack //-- stack.clear(); }

// Destruction //-- ~MatrixStack() { stack.clear(); }

// Push currentMatrix on the stack //-- void push( const Matrix4x4& m ) { stack.push_back( m ); }

// Remove the top-most matrix from the stack //-- void pop() { stack.pop_back(); }

// Get top most matrix //-- const Matrix4x4& getCurrent() const { return stack.back(); } };

#endif





























////////////////////////////////////////////////////////////////////////////////// #if 0 class Matrix4x4 { public: Matrix4x4 (); ~Matrix(); Matrix4x4 (Double a11, Double a12, Double a13, Double a14, Double a21, Double a22, Double a23, Double a24, Double a31, Double a32, Double a33, Double a34, Double a41, Double a42, Double a43, Double a44 ); Matrix4x4 (const Matrix4x4 &m );

void setValue ( const Matrix4x4 &m ); void getValue ( Matrix4x4 &m ) const; const Matrix4x4& getValue () const;

void makeIdentity (); static Matrix4x4 identity ();

void setRotate (const RotationEuler &q ); void setRotate (const Quaternion &q ); void setScale ( Double s ); void setScale ( const Vec3f &s ); void setTranslate ( const Vec3f &t ); Matrix4x4 inverse (void) const; Matrix4x4 transpose (void) const; Matrix4x4& multRight ( const Matrix4x4 &m ); Matrix4x4& multLeft ( const Matrix4x4 &m ); void multMatrixVec( const Vec3f &src, Vec3f &dst ) const; void multVecMatrix( const Vec3f &src, Vec3f &dst ) const; void multDirMatrix( const Vec3f &src, Vec3f &dst ) const; void multLineMatrix( const Line &src, Line &dst ) const;

void print(void) const;

operator Double*(); operator Matrix4x4&();

Double* operator [] ( int i ); const Double* operator[] ( int i ) const;

Matrix4x4& operator = ( const Matrix4x4 &m ); Matrix4x4& operator = ( const Matrix4x4 &m ); Matrix4x4& operator = ( const RotationXYZ &q ); Matrix4x4& operator = ( const RotationQuat &q );

Matrix4x4& operator *= ( const Matrix4x4 &m ); friend Matrix4x4 operator *( const Matrix4x4 &m1, const Matrix4x4 &m2 ); friend bool operator == ( const Matrix4x4 & m1, const Matrix4x4 &m2 ); friend bool operator != ( const Matrix4x4 & m1, const Matrix4x4 &m2 ); bool equals( const Matrix4x4 &m, Double tolerance ) const;

Double _mat[4][4]; }; void Matrix4x4::setRotation(const RotationEuler &euler ) { float cosroll,sinroll,cospitch,sinpitch,cosyaw,sinyaw; float sinyawcosroll,sinyawsinroll;

/* Convert to radians */ float roll,pitch,yaw; euler.getRPY(roll,pitch,yaw); roll*=_PI_OVER_180; pitch*=_PI_OVER_180; yaw*=_PI_OVER_180;

/* Calculate trig functions */ cosroll=cos(euler.roll()); sinroll=sin(euler.roll()); cospitch=cos(euler.pitch()); sinpitch=sin(euler.pitch()); cosyaw=cos(euler.yaw()); sinyaw=sin(euler.yaw()); sinyawcosroll=sinyaw*cosroll; sinyawsinroll=sinyaw*sinroll;

/* Form the matrix */

_mat[0][0] = cosyaw*cosroll; //XX _mat[0][1] = sinpitch*sinyawcosroll - cospitch*sinroll; //YX _mat[0][2] = cospitch*sinyawcosroll + sinpitch*sinroll; //ZX _mat[1][0] = cosyaw*sinroll; //XY _mat[1][1] = sinpitch*sinyawsinroll + cospitch*cosroll; //YY _mat[1][2] = cospitch*sinyawsinroll - sinpitch*cosroll; //ZY _mat[2][0] = -sinyaw; //XZ _mat[2][1] = cosyaw*sinpitch; //YZ _mat[2][2] = cosyaw*cospitch; //ZZ } void Matrix4x4::setRotate(const Quaternion &q ) { float sqw = q._angle*q._angle; float sqx = q._x*q._x; float sqy = q._y*q._y; float sqz = q._z*q._z;

float heading = atan(2.0* ((q._x*q._y + q._z*q._angle)/(sqx-sqy-sqz+sqw)));

float bank = atan(2.0* ((q._y*q._z + q._x*q._angle)/(-sqx-sqy+sqz+sqw)));

float attitude = asin(-2.0*(q._x*q._z + q._y*q._angle));

RotationEuler rot(heading,bank,attitude); setRotation(rot); }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/D3DTexture.h] - (4,140 bytes)

#ifndef _D3DTEXTURE_H_INCLUDED_
#define _D3DTEXTURE_H_INCLUDED_

#include "Texture.h" #include <d3d9.h>

/** * This is Direct3D 9 implementation of Texture. * * Inspired by the work of Tobin Schwaiger-Hastanan */ class D3DTexture : public Texture { protected: /// disable default constructor //-- D3DTexture(); /// disable copy constructor //-- D3DTexture( const D3DTexture& ); /// disable assignment operator //-- D3DTexture& operator =( const D3DTexture& );

public: /** * Constructor used to create vertex buffer. * @param pD3DDevice Direct 3D Device * @param Width width of the texture * @param Height height of the texture * @param format format of the texture * @param isDynamic specifies that texture is dynamic. [default: false] * @param dwMipMaLevels Number of mipmap levels to generate. Value of * 0 will generate all the levels till 1x1. [default: 0] */ D3DTexture( LPDIRECT3DDEVICE9 pD3DDevice, DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic = false, DWORD dwMipMapLevelNum = 0 );

/// destructor //-- ~D3DTexture(); /// for the following methods, see Texture //-- bool Use();

bool Lock(); bool Unlock(); const bool IsLocked() const { return m_bIsLocked; }

void* GetData(); const DWORD GetWidth() const { return m_dwWidth; } const DWORD GetHeight() const { return m_dwHeight; } const DWORD GetPitch() const { return m_dwPitch; } const RS_FORMAT GetFormat() const { return m_Format; } const DWORD GetMipMapLevelsNum() const { return m_dwMipMapLevelNum; }

inline void SetPixel4f( float a, float r, float g, float b ); inline void SetPixel4uc( unsigned char a, unsigned char r, unsigned char g, unsigned char b );

//---------------------------------------------------------------------- // D3D related methods //-- /** * Returns the D3D Vertex buffer created by this class. * This is a D3DTexture specific method. * * @return pointer to a Direct 3D Vertex Buffer. */ const LPDIRECT3DTEXTURE9 GetD3DTexture() const { return m_pD3DTexture; }

/** * Returns the D3D texture format. * This is a D3DTexture specific method. * * @return The format of the texture in D3D type */ const D3DFORMAT GetD3DFormat() const { return m_FMT; }

//---------------------------------------------------------------------- // Private members and accessors //-- private: RS_FORMAT m_Format; /// format of the texture DWORD m_dwMipMapLevelNum; /// number of mipmap levels DWORD m_dwWidth; /// texture width DWORD m_dwHeight; /// texture height DWORD m_dwPitch; /// pitch of the texture bool m_bIsLocked; /// flag to specify if buffer is locked bool m_bIsDynamic; /// flag to specify if buffer is holding dynamic data void* m_pBuffer; /// pointer to vertex data unsigned char* m_pCurrentPixel;

//---------------------------------------------------------------------- // D3D related members //-- LPDIRECT3DDEVICE9 m_pD3DDevice; /// d3d device LPDIRECT3DTEXTURE9 m_pD3DTexture; /// d3d texture LPDIRECT3DTEXTURE9 m_pD3DRamTexture; /// d3d texture used when locking static texture D3DFORMAT m_FMT; /// d3d format };

inline void D3DTexture::SetPixel4f( float a, float r, float g, float b ) { if ( m_Format==RSFMT_A8R8G8B8 ) { m_pCurrentPixel[0] = (unsigned char)(255.0f*b); m_pCurrentPixel[1] = (unsigned char)(255.0f*g); m_pCurrentPixel[2] = (unsigned char)(255.0f*r); m_pCurrentPixel[3] = (unsigned char)(255.0f*a); m_pCurrentPixel+=4; } }

inline void D3DTexture::SetPixel4uc( unsigned char a, unsigned char r, unsigned char g, unsigned char b ) { if ( m_Format==RSFMT_A8R8G8B8 ) { m_pCurrentPixel[0] = b; m_pCurrentPixel[1] = g; m_pCurrentPixel[2] = r; m_pCurrentPixel[3] = a; m_pCurrentPixel+=4; } }





#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/Texture.h] - (2,281 bytes)

#ifndef _TEXTURE_H_INCLUDED_
#define _TEXTURE_H_INCLUDED_

#include "RenderTypeDefs.h"

/** * This is an abstract class that provides an interface on top of various * implementations of textures. * */ class Texture { public: virtual ~Texture() {} /** * Locks the texture (usually for writing)... * @return True if the lock request succeeded. False else. */ virtual bool Lock() = 0;

/** * Unlocks the texture * @return True if the unlock request succeeded. False else. */ virtual bool Unlock() = 0;

/** * Determines if the texture is locked or not. * @return boolean value specifying if texture is locked or not. */ virtual const bool IsLocked() const = 0;

/** * Returns a pointer to the raw texture data. * This should only be called when texture is locked. * @return pointer to raw vertex data. */ virtual void* GetData() = 0; /** * Get width of the texture */ virtual const DWORD GetWidth() const = 0;

/** * Get height of the texture */ virtual const DWORD GetHeight() const = 0; /** * Get pitch of the texture (length of a row of pixels) * This might differ from the width of the texture. For instance, a * non-power of two texture might have a width of 120 and a pitch of * 128. */ virtual const DWORD GetPitch() const = 0;

/** * returns the format of the texture. (see RS_PIXELFORMAT type) * @return Pixel format of the texture. */ virtual const RS_FORMAT GetFormat() const = 0;

/** * This method is called to activate the texture before rendering * textured primitives. */ virtual bool Use() = 0;

/** * Use this to set current pixel in the texture (helps to solve * Api dependant ordering of rgba components) */ virtual void SetPixel4f( float a, float r, float g, float b ) = 0;

/** * Use this to set current pixel in the texture (helps to solve * Api dependant ordering of rgba components) */ virtual void SetPixel4uc( unsigned char a, unsigned char r, unsigned char g, unsigned char b ) = 0; };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/RenderObjectFactory.h] - (1,907 bytes)

#ifndef _RENDEROBJECTFACTORY_H_INCLUDED_
#define _RENDEROBJECTFACTORY_H_INCLUDED_

#include "VertexBuffer.h" #include "Texture.h"

/** * This is an abstract class that provides an interface on top of various * implementations of render object (textures, vertex buffers, ...) * factories. * * @author Vincent Prat */ class RenderObjectFactory { //---------------------------------------------------------------------- // Public methods //-- public:

/** * Default object constructor */ RenderObjectFactory() {}

/** * Object destructor */ virtual ~RenderObjectFactory() {}

//------------------------------------------------------------------ // Object creation functions //-- /** * This function should be used to allocate memory for a new vertex * buffer. * @param count Number of vertex to be created * @param format Format of vertex buffer ( See format flags in VertexBuffer) * @param isDynamic Specifies that vertex buffer is dynamic. [default: true] * @return A pointer to the new vertex buffer or NULL if it could not be created. */ virtual VertexBuffer* CreateVertexBuffer( int count, DWORD format, bool isDynamic = true ) = 0;

/** * This function should be used to allocate memory for a new texture * @param Width width of the texture * @param Height height of the texture * @param format format of the texture * @param isDynamic specifies that texture is dynamic. [default: false] * @param dwMipMaLevels Number of mipmap levels to generate. Value of * 0 will generate all the levels till 1x1. [default: 0] * @return A pointer to the new texture or NULL if it could not be created. */ virtual Texture* CreateTexture( DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic = false, DWORD dwMipMapLevelNum = 0 ) = 0; };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/Sphere.h] - (555 bytes)

#ifndef _Sphere_h_
#define _Sphere_h_

#include "RenderDevice.h" #include "VertexBuffer.h"

/// This class is only a helper to fill a vertex buffer. In a real engine, /// your sphere class could have many other responsabilities such as to render /// by itself, its own transformation matrix, ... class Sphere { public: Sphere( RenderDevice* pRenderDevice, float radius, DWORD slices ); ~Sphere(); void Draw( RenderDevice* pRenderDevice );

public: VertexBuffer* m_pVertexBuffer; float m_fRadius; DWORD m_dwSlices; };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/OpenGLVertexBuffer.cpp] - (4,186 bytes)

#ifdef COMPILE_OPENGL_IMPLEMENTATION

#include "OpenGLVertexBuffer.h"

OpenGLVertexBuffer::OpenGLVertexBuffer( int count, DWORD format, bool isDynamic ) : VertexBuffer() { DWORD size = 0; m_dwFormat = format; m_dwVertexCount = count;

if( format & RSVF_XYZ ) { m_dwVertexCoordNum = 3; size += sizeof( float ) * 3; } else if( format & RSVF_XYZRHW ) { m_dwVertexCoordNum = 4; size += sizeof( float ) * 4; }

if( format & RSVF_NORMAL ) { size += sizeof( float ) * 3; }

if( format & RSVF_DIFFUSE ) { size += sizeof( DWORD ); }

if( format & RSVF_TEXCOORD ) { size += sizeof( float ) * 2; } m_dwStrideVertex = size; m_bIsDynamic = isDynamic; m_bIsLocked = false;

m_pVertex = NULL; m_pColor = NULL; m_pNormal = NULL; m_pTexCoord = NULL;

m_pGLVertices = NULL; m_pGLNormals = NULL; m_pGLColors = NULL; m_pGLTexCoords = NULL;

// Allocate memory for vertex data //-- if ( (m_dwFormat & RSVF_XYZ) || (m_dwFormat & RSVF_XYZRHW) ) m_pGLVertices = new float[ m_dwVertexCoordNum*m_dwVertexCount ];

if ( m_dwFormat & RSVF_NORMAL ) m_pGLNormals = new float[ 3*m_dwVertexCount ];

if ( m_dwFormat & RSVF_DIFFUSE ) m_pGLColors = new float[ 4*m_dwVertexCount ];

if ( m_dwFormat & RSVF_TEXCOORD ) m_pGLTexCoords= new float[ 2*m_dwVertexCount ]; }

OpenGLVertexBuffer::~OpenGLVertexBuffer() { if ( m_pGLVertices != NULL ) { delete [] m_pGLVertices; m_pGLVertices = NULL; }

if ( m_pGLNormals != NULL ) { delete [] m_pGLNormals; m_pGLNormals = NULL; }

if ( m_pGLColors != NULL ) { delete [] m_pGLColors; m_pGLColors = NULL; }

if ( m_pGLTexCoords != NULL ) { delete [] m_pGLTexCoords; m_pGLTexCoords = NULL; } }

bool OpenGLVertexBuffer::Lock() { m_bIsLocked = true;

m_pVertex = m_pGLVertices; m_pColor = m_pGLColors; m_pNormal = m_pGLNormals; m_pTexCoord = m_pGLTexCoords;

return true; }

bool OpenGLVertexBuffer::Unlock() { m_bIsLocked = false;

m_pVertex = NULL; m_pColor = NULL; m_pNormal = NULL; m_pTexCoord = NULL;

return true; }

void* OpenGLVertexBuffer::GetVertexData() { return NULL; }

void OpenGLVertexBuffer::Vertex( float x, float y, float z ) { m_pVertex[ 0 ] = x; m_pVertex[ 1 ] = y; m_pVertex[ 2 ] = z;

m_pVertex += 3; }

void OpenGLVertexBuffer::Vertex( float x, float y, float z, float w ) { m_pVertex[ 0 ] = x; m_pVertex[ 1 ] = y; m_pVertex[ 2 ] = z; m_pVertex[ 3 ] = w;

m_pVertex += 4; }

void OpenGLVertexBuffer::Normal( float x, float y, float z ) { m_pNormal[ 0 ] = x; m_pNormal[ 1 ] = y; m_pNormal[ 2 ] = z;

m_pNormal += 3; }

void OpenGLVertexBuffer::Diffuse( float r, float g, float b, float a ) { m_pColor[ 0 ] = r; m_pColor[ 1 ] = g; m_pColor[ 2 ] = b; m_pColor[ 3 ] = a;

m_pColor += 4; }

void OpenGLVertexBuffer::TexVertex( float u, float v ) { m_pTexCoord[ 0 ] = u; m_pTexCoord[ 1 ] = v;

m_pTexCoord += 2; }

bool OpenGLVertexBuffer::PrepareForDrawing() { // Set pointers to arrays //-- if ( m_pGLVertices != NULL ) { glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer( m_dwVertexCoordNum, GL_FLOAT, 0, m_pGLVertices ); } else { glDisableClientState(GL_VERTEX_ARRAY); }

if ( m_pGLNormals != NULL ) { glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer( GL_FLOAT, 0, m_pGLNormals ); } else { glDisableClientState(GL_NORMAL_ARRAY); }

if ( m_pGLColors != NULL ) { glEnableClientState(GL_COLOR_ARRAY); glColorPointer( 4, GL_FLOAT, 0, m_pGLColors ); } else { glDisableClientState(GL_COLOR_ARRAY); }

if ( m_pGLTexCoords != NULL ) { glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer( 2, GL_FLOAT, 0, m_pGLTexCoords ); } else { glDisableClientState(GL_TEXTURE_COORD_ARRAY); }

return ( GL_NO_ERROR == glGetError() ); }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/Sphere.cpp] - (2,227 bytes)

#include <math.h>
#include "Sphere.h"

Sphere::Sphere( RenderDevice* pRenderDevice, float radius, DWORD slices ) { m_dwSlices = slices; m_fRadius = radius;

// Create VB //-- m_pVertexBuffer = pRenderDevice->CreateVertexBuffer( 2*(m_dwSlices+1)*m_dwSlices, RSVF_XYZ | RSVF_TEXCOORD | RSVF_NORMAL, false );

// Fill VB (build triangle stripes) // I know we could use fans at the poles to reduce // vertex count but I don't care for this sample. //-- DWORD i=0, j=0; float Phi=0, Theta=2*3.1415926f; float PhiInc = 3.1415926f/m_dwSlices; float ThetaInc = -2*3.1415926f/m_dwSlices; float Pos[3]; // Position of current point float Norm; // Norm of position vector (used to compute normal) m_pVertexBuffer->Lock();

for ( i=0; i<m_dwSlices; i++ ) { Theta = 0;

for ( j=0; j<=m_dwSlices; j++ ) { // Position and normal computation for 1st point of strip //-- Pos[0] = radius*cosf(Theta)*sinf(Phi); Pos[1] = radius*cosf(Phi); Pos[2] = radius*sinf(Theta)*sinf(Phi); Norm = sqrtf( Pos[0]*Pos[0] + Pos[1]*Pos[1] + Pos[2]*Pos[2] );

m_pVertexBuffer->Normal( Pos[0]/Norm, Pos[1]/Norm, Pos[2]/Norm ); m_pVertexBuffer->TexVertex( Theta/(2*3.1415926f), Phi/3.1415926f ); m_pVertexBuffer->Vertex( Pos[0], Pos[1], Pos[2] );

// Position and normal computation for 2nd point of strip //-- Pos[0] = radius*cosf(Theta)*sinf(Phi+PhiInc); Pos[1] = radius*cosf(Phi+PhiInc); Pos[2] = radius*sinf(Theta)*sinf(Phi+PhiInc); Norm = sqrtf( Pos[0]*Pos[0] + Pos[1]*Pos[1] + Pos[2]*Pos[2] );

m_pVertexBuffer->Normal( Pos[0]/Norm, Pos[1]/Norm, Pos[2]/Norm ); m_pVertexBuffer->TexVertex( Theta/(2*3.1415926f), (Phi+PhiInc)/3.1415926f ); m_pVertexBuffer->Vertex( Pos[0], Pos[1], Pos[2] );

Theta += ThetaInc; }

Phi += PhiInc; }

m_pVertexBuffer->Unlock(); }

Sphere::~Sphere() { // Release VB memory //-- SAFE_DELETE( m_pVertexBuffer ); }

void Sphere::Draw( RenderDevice* pRenderDevice ) { DWORD i=0; for ( i=0; i<m_dwSlices; i++ ) { pRenderDevice->DrawPrimitives( RSPT_TRIANGLESTRIP, m_pVertexBuffer, i*2*(m_dwSlices+1), 2*m_dwSlices ); }

}

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/VertexBuffer.h] - (3,935 bytes)

#ifndef _VERTEXBUFFER_H_INCLUDED_
#define _VERTEXBUFFER_H_INCLUDED_

#include "RenderTypeDefs.h"

/** * This is an abstract class that provides an interface on top of various * implementations of vertex buffers or similar structures. * * Inspired by the work of Tobin Schwaiger-Hastanan */ class VertexBuffer { public: virtual ~VertexBuffer() {} /** * Locks the vertex buffer (usually for writing)... * @return True if the lock request succeeded. False else. */ virtual bool Lock() = 0;

/** * Unlocks the vertex buffer. * @return True if the unlock request succeeded. False else. */ virtual bool Unlock() = 0;

/** * Determines if the vertex buffer is locked or not. * @return boolean value specifying if vertex buffer is locked or not. */ virtual const bool IsLocked() const = 0;

/** * Returns a pointer to the raw vertex data. * This should only be called when vertex buffer is locked. * @return pointer to raw vertex data. */ virtual void* GetVertexData() = 0; /** * returns the number of vertex currently in the vertex buffer * @return number of vertex in vertex buffer. */ virtual const DWORD GetVertexCount() const = 0;

/** * returns the vertex format of the vertex buffer. (see _VERTEXFORMATFLAGS type) * @return vertex format of the vertex buffer. */ virtual const DWORD GetVertexFormat() const = 0;

/** * returns the width in bytes of a single vertex described by the format. * @return width in bytes of a single vertex described by the format. */ virtual const DWORD GetVertexStride() const = 0;

/** * Specifies untransformed vertex data. Should be last call * for each vertex. That is after the normal, color, and * texture coordinate calls are made for the vertex. * Note: should be used when format includes FORMAT_XYZ * * @param x x coordinate. * @param y y coordinate. * @param z z coordinate. */ virtual void Vertex( float x, float y, float z ) = 0;

/** * Specifies transformed vertex data. Should be last call * for each vertex. That is after the normal, color, and * texture coordinate calls are made for the vertex. * Note: should be used when format includes FORMAT_XYZRHW * * @param x x coordinate. * @param y y coordinate. * @param z z coordinate. * @param w w coordinate. */ virtual void Vertex( float x, float y, float z, float w ) = 0;

/** * Specifies vertex normal data. * Note: should be used when format includes FORMAT_NORMAL * * @param x Normal x value. * @param y Normal y value. * @param z Normal z value. */ virtual void Normal( float x, float y, float z ) = 0;

/** * Specifies diffuse color data. * Note: should be used when format includes FORMAT_DIFFUSE * * @param r Red color value. * @param g Green color value. * @param b Blue color value. * @param a Alpha color value. */ virtual void Diffuse( float r, float g, float b, float a = 1.0f ) = 0;

/** * Specifies texture vertex. Successive calls should be made to this * method if more than one set of texture coordinates need to be * specified. * Note: should be used when format includes FORMAT_TEXTUREFLAG * * @param u U texture coordinate. * @param v V texture coordinate. */ virtual void TexVertex( float u, float v ) = 0;

/** * This method is called to setup the vertex buffer just before a call * to render primitives */ virtual bool PrepareForDrawing() = 0; };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/RenderDevice.h] - (7,909 bytes)

#ifndef _RENDERDEVICE_H_INCLUDED_
#define _RENDERDEVICE_H_INCLUDED_

#include "RenderTypeDefs.h" #include "Matrix4x4.h"

#include "RenderDeviceImplementor.h" #include "RenderObjectFactory.h"

/** * This is a class that is the base of the bridge pattern used to * design our renderer system. * * @author Vincent Prat * @remark Instead of the two functions "UseD3D" and "UseOpenGL", * we could define two derived classes to extend this base class * and that would initialise the factory and the renderDevice * with the relevant implementations. */ class RenderDevice { //---------------------------------------------------------------------- // Public methods //-- public:

/** * Default object constructor * @param hWnd Handle of the window associated to this device */ RenderDevice();

/** * Object destructor */ virtual ~RenderDevice();

/** * Release the implementors */ void Release();

/** * Use D3D implementation */ #ifdef COMPILE_DIRECT3D_IMPLEMENTATION void UseD3D( HWND hWnd, DWORD width, DWORD height ); #endif

/** * Use OpenGL implementation */ #ifdef COMPILE_OPENGL_IMPLEMENTATION void UseOpenGL( HWND hWnd, DWORD width, DWORD height ); #endif

//------------------------------------------------------------------ // Object creation functions //-- /** * This function should be used to allocate memory for a new vertex * buffer. * @param count Number of vertex to be created * @param format Format of vertex buffer ( See format flags in VertexBuffer) * @param isDynamic Specifies that vertex buffer is dynamic. [default: true] * @return A pointer to the new vertex buffer or NULL if it could not be created. */ VertexBuffer* CreateVertexBuffer( int count, DWORD format, bool isDynamic = true ) { return m_pObjectFactory->CreateVertexBuffer( count, format, isDynamic ); }

/** * This function should be used to allocate memory for a new texture * @param Width width of the texture * @param Height height of the texture * @param format format of the texture * @param isDynamic specifies that texture is dynamic. [default: false] * @param dwMipMaLevels Number of mipmap levels to generate. Value of * 0 will generate all the levels till 1x1. [default: 0] * @return A pointer to the new texture or NULL if it could not be created. */ Texture* CreateTexture( DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic = false, DWORD dwMipMapLevelNum = 0 ) { return m_pObjectFactory->CreateTexture( Width, Height, format, isDynamic, dwMipMapLevelNum ); }

//------------------------------------------------------------------ // Render state switches functions //-- /** * Set a given render state to some value. */ bool SetRenderState( _RENDERSTATETYPE state, DWORD value ) { return m_pRenderDevice->SetRenderState( state, value ); }

/** * Get a render state value */ DWORD GetRenderState( _RENDERSTATETYPE state ) { return m_pRenderDevice->GetRenderState( state ); }

//------------------------------------------------------------------ // Rendering functions //-- /** * Indicate that we are about to begin rendering our scene. */ void BeginRendering() { m_pRenderDevice->BeginRendering(); }

/** * Indicate we have finished to describe what we want to render */ void EndRendering() { m_pRenderDevice->EndRendering(); }

/** * This function should be used to allocate memory for a new vertex * buffer. * @param primitiveType Type of primitives to render * @param pVB Pointer to the buffer holding vertex data * @param baseVertex Index of the vertex from which to begin primitive reading * @param primitiveCount Number of primitives to draw * @return true if it succeeded, false else. */ bool DrawPrimitives( RS_PRIMITIVETYPE primitiveType, VertexBuffer* pVB, DWORD baseVertex, DWORD primitiveCount ) { return m_pRenderDevice->DrawPrimitives( primitiveType, pVB, baseVertex, primitiveCount ); } /** * Display the content of the active backbuffer on screen */ void SwapBuffers() { m_pRenderDevice->SwapBuffers(); }

/** * Say that we will use this texture for following draw primitive * calls that need a texture. */ void UseTexture( Texture* pTex ) { m_pRenderDevice->UseTexture( pTex ); }

//------------------------------------------------------------------ // Tranformation functions //-- /** * Set current matrix mode */ void MatrixModeSet( RS_MATRIXMODE mode ) { m_pRenderDevice->MatrixModeSet(mode); }

/** * Get current matrix mode */ const RS_MATRIXMODE MatrixModeGet() const { return m_pRenderDevice->MatrixModeGet(); }

/** * Set current matrix to identity matrix */ void MatrixLoadIdentity() { m_pRenderDevice->MatrixLoadIdentity(); }

/** * Set current matrix to given matrix */ void MatrixLoad(const Matrix4x4& m) { m_pRenderDevice->MatrixLoad(m); }

/** * Push current matrix on the stack */ void MatrixPush() { m_pRenderDevice->MatrixPush(); }

/** * Remove last matrix from the stack */ void MatrixPop() { m_pRenderDevice->MatrixPop(); }

/** * Multiply current matrix by the matrix corresponding to * provided scale values. */ void MatrixScale( float x, float y, float z ) { m_pRenderDevice->MatrixScale( x, y, z ); }

/** * Multiply current matrix by the matrix corresponding to * provided translation values. */ void MatrixTranslation( float x, float y, float z ) { m_pRenderDevice->MatrixTranslation( x, y, z ); }

/** * Multiply current matrix by the matrix corresponding to * provided rotation angle (in degrees) and axis. */ void MatrixRotation( float angle, float x, float y, float z ) { m_pRenderDevice->MatrixRotation( angle, x, y, z ); }

/** * Multiply current transformation matric by given matrix */ void MatrixMultiply(const Matrix4x4& m) { m_pRenderDevice->MatrixMultiply(m); }

/** * Get current tranformation matrix */ Matrix4x4 MatrixGet() { return m_pRenderDevice->MatrixGet(); }

//------------------------------------------------------------------ // Other functions //-- /** * Set clear values */ void SetBackgroundColor( float a, float r, float g, float b ) { m_pRenderDevice->SetBackgroundColor( a, r, g, b ); }

void SetDepthClearValue( float z ) { m_pRenderDevice->SetDepthClearValue(z); }

void SetStencilClearValue( DWORD s ) { m_pRenderDevice->SetStencilClearValue(s); }

/** * Clears the backbuffer, Z-Buffer and stencil buffer to given * values if relevant flags are set. * @param flags A combination of any of the CLEARFLAG */ void Clear( DWORD flags ) { m_pRenderDevice->Clear(flags); }

/** * Set viewport area */ bool SetViewport( int x, int y, int w, int h ) { return m_pRenderDevice->SetViewport( x, y, w, h ); }

//---------------------------------------------------------------------- // Typedefs, public members and accessors //-- public: const RS_BASEAPI GetBaseApi() const { return m_pRenderDevice->GetBaseApi(); }

//---------------------------------------------------------------------- // Protected member functions //-- protected:

//---------------------------------------------------------------------- // Protected members and accessors //-- protected: /** * Pointer to an implementation of render device */ RenderDeviceImplementor* m_pRenderDevice;

/** * Pointer to an implementation of factory */ RenderObjectFactory* m_pObjectFactory; };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/RenderDevice.cpp] - (1,207 bytes)

#include "RenderDevice.h"

#ifdef COMPILE_DIRECT3D_IMPLEMENTATION #include "D3DRenderDevice.h" #include "D3DRenderObjectFactory.h" #endif

#ifdef COMPILE_OPENGL_IMPLEMENTATION #include "OpenGLRenderDevice.h" #include "OpenGLRenderObjectFactory.h" #endif

RenderDevice::RenderDevice() { m_pRenderDevice = NULL; m_pObjectFactory = NULL; }

RenderDevice::~RenderDevice() { Release(); }

void RenderDevice::Release() { if ( m_pRenderDevice!=NULL ) { delete m_pRenderDevice; m_pRenderDevice = NULL; }

if ( m_pObjectFactory!=NULL ) { delete m_pObjectFactory; m_pObjectFactory = NULL; } }

#ifdef COMPILE_OPENGL_IMPLEMENTATION void RenderDevice::UseOpenGL( HWND hWnd, DWORD width, DWORD height ) { m_pRenderDevice = new OpenGLRenderDevice( hWnd, width, height ); m_pObjectFactory = new OpenGLRenderObjectFactory(); } #endif

#ifdef COMPILE_DIRECT3D_IMPLEMENTATION void RenderDevice::UseD3D( HWND hWnd, DWORD width, DWORD height ) { D3DRenderDevice* pRenderDevice = new D3DRenderDevice( hWnd, width, height ); m_pObjectFactory = new D3DRenderObjectFactory( pRenderDevice->GetD3DDevice() ); m_pRenderDevice = pRenderDevice; } #endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/D3DRenderDevice.h] - (6,319 bytes)

#ifndef _D3DRENDERDEVICE_H_INCLUDED_
#define _D3DRENDERDEVICE_H_INCLUDED_

#include "RenderDeviceImplementor.h" #include "MatrixStack.h"

#include <d3d9.h> #include <d3dx9.h>

/** * This is the implementation of the D3D version of render devices * * @author Vincent Prat */ class D3DRenderDevice : public RenderDeviceImplementor { //---------------------------------------------------------------------- // Public methods //-- public:

/** * Default object constructor * @param hWnd Handle of the window associated to this device */ D3DRenderDevice( HWND hWnd, DWORD width, DWORD height );

/** * Object destructor */ ~D3DRenderDevice();

//------------------------------------------------------------------ // Rendering functions (see base class documentation for this) //-- void SwapBuffers(); void BeginRendering(); void EndRendering(); bool DrawPrimitives( RS_PRIMITIVETYPE primitiveType, VertexBuffer* pVB, DWORD baseVertex, DWORD primitiveCount ); void UseTexture( Texture* pTex );

//------------------------------------------------------------------ // Render state switches functions (see base class documentation // for this) //-- bool SetRenderState( RS_RENDERSTATETYPE state, DWORD value ); DWORD GetRenderState( RS_RENDERSTATETYPE state );

//------------------------------------------------------------------ // Tranformation functions (see base class documentation for this) //-- void MatrixModeSet(RS_MATRIXMODE mode); const RS_MATRIXMODE MatrixModeGet() const; void MatrixLoadIdentity(); void MatrixLoad(const Matrix4x4& m); void MatrixPush(); void MatrixPop(); void MatrixMultiply(const Matrix4x4& m); void MatrixScale( float x, float y, float z ); void MatrixTranslation( float x, float y, float z ); void MatrixRotation( float angle, float x, float y, float z ); Matrix4x4 MatrixGet(); //------------------------------------------------------------------ // Other functions (see base class documentation for this) //-- void SetBackgroundColor( float a, float r, float g, float b ); void SetDepthClearValue( float z ); void SetStencilClearValue( DWORD s ); void Clear( DWORD flags ); bool SetViewport( int x, int y, int w, int h );

//------------------------------------------------------------------ // D3D specific //-- LPDIRECT3DDEVICE9 GetD3DDevice() { return m_pD3DDevice; }

//---------------------------------------------------------------------- // Protected member functions //-- protected: /** * Open the Direct3D DLL to retrieve function pointers * @return True if functions could be retrieved. */ bool OpenD3DDLL();

/** * Close the Direct3D DLL and free function pointers */ void CloseD3DDLL();

/** * Create the D3D device object * @return True if device could be created. */ bool CreateD3DDevice( HWND hWnd, DWORD width, DWORD height );

// Overloaded functions //-- void Release();

//---------------------------------------------------------------------- // Protected members and accessors //-- protected: /** * Pointer to the function to create the Direct3D object. Because * we are using dynamic linking to Direct X */ HMODULE D3D9DLL; HMODULE D3DX9DLL; typedef IDirect3D9* LPDIRECT3D9; typedef LPDIRECT3D9 (__stdcall *LPDIRECT3DCREATE9)(UINT); LPDIRECT3DCREATE9 myDirect3DCreate9;

/** * Pointer to the D3D Object used to create the D3D device */ IDirect3D9* m_pD3D;

/** * Pointer to the D3D device */ IDirect3DDevice9* m_pD3DDevice;

/** * Caps of selected device */ D3DCAPS9 m_D3DCaps;

/** * Return corresponding D3D primitive type */ inline D3DPRIMITIVETYPE GetD3DPrimitiveType( RS_PRIMITIVETYPE pt );

/** * Useful stuff for matrices */

// Our matrix stack to emulate OpenGL internal one MatrixStack m_matrixStack; // The projection and the modelview matrices Matrix4x4 m_projectionMatrix; Matrix4x4 m_modelviewMatrix;

inline void MatrixToD3D( const Matrix4x4* Mat, D3DMATRIX *d3dMat ); inline void MatrixD3DTo( const D3DMATRIX *d3dMat, Matrix4x4* Mat );

/** * Clear values */ D3DCOLOR m_backgroundColor; float m_depthClearValue; DWORD m_stencilClearValue; };



inline D3DPRIMITIVETYPE D3DRenderDevice::GetD3DPrimitiveType( RS_PRIMITIVETYPE pt ) { switch (pt) { case RSPT_POINTLIST: return D3DPT_POINTLIST;

case RSPT_LINELIST: return D3DPT_LINELIST;

case RSPT_LINESTRIP: return D3DPT_LINESTRIP;

case RSPT_TRIANGLELIST: return D3DPT_TRIANGLELIST;

case RSPT_TRIANGLESTRIP: return D3DPT_TRIANGLESTRIP;

case RSPT_TRIANGLEFAN: return D3DPT_TRIANGLEFAN;

default: return D3DPT_POINTLIST; } }

inline void D3DRenderDevice::MatrixToD3D( const Matrix4x4* Mat, D3DMATRIX *d3dMat ) { // Unrolled version for speed d3dMat->m[0][0] = Mat->m_Mat[0][0]; d3dMat->m[0][1] = Mat->m_Mat[1][0]; d3dMat->m[0][2] = Mat->m_Mat[2][0]; d3dMat->m[0][3] = Mat->m_Mat[3][0]; d3dMat->m[1][0] = Mat->m_Mat[0][1]; d3dMat->m[1][1] = Mat->m_Mat[1][1]; d3dMat->m[1][2] = Mat->m_Mat[2][1]; d3dMat->m[1][3] = Mat->m_Mat[3][1]; d3dMat->m[2][0] = Mat->m_Mat[0][2]; d3dMat->m[2][1] = Mat->m_Mat[1][2]; d3dMat->m[2][2] = Mat->m_Mat[2][2]; d3dMat->m[2][3] = Mat->m_Mat[3][2]; d3dMat->m[3][0] = Mat->m_Mat[0][3]; d3dMat->m[3][1] = Mat->m_Mat[1][3]; d3dMat->m[3][2] = Mat->m_Mat[2][3]; d3dMat->m[3][3] = Mat->m_Mat[3][3]; }

inline void D3DRenderDevice::MatrixD3DTo( const D3DMATRIX *d3dMat, Matrix4x4* Mat ) { // Unrolled version for speed Mat->m_Mat[0][0] = d3dMat->m[0][0]; Mat->m_Mat[1][0] = d3dMat->m[0][1]; Mat->m_Mat[2][0] = d3dMat->m[0][2]; Mat->m_Mat[3][0] = d3dMat->m[0][3]; Mat->m_Mat[0][1] = d3dMat->m[1][0]; Mat->m_Mat[1][1] = d3dMat->m[1][1]; Mat->m_Mat[2][1] = d3dMat->m[1][2]; Mat->m_Mat[3][1] = d3dMat->m[1][3]; Mat->m_Mat[0][2] = d3dMat->m[2][0]; Mat->m_Mat[1][2] = d3dMat->m[2][1]; Mat->m_Mat[2][2] = d3dMat->m[2][2]; Mat->m_Mat[3][2] = d3dMat->m[2][3]; Mat->m_Mat[0][3] = d3dMat->m[3][0]; Mat->m_Mat[1][3] = d3dMat->m[3][1]; Mat->m_Mat[2][3] = d3dMat->m[3][2]; Mat->m_Mat[3][3] = d3dMat->m[3][3]; }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/RenderDeviceImplementor.h] - (5,198 bytes)

#ifndef _RENDERDEVICEIMPLEMENTOR_H_INCLUDED_
#define _RENDERDEVICEIMPLEMENTOR_H_INCLUDED_

#include "VertexBuffer.h" #include "Texture.h" #include "RenderTypeDefs.h" #include "Matrix4x4.h"

/** * This is an abstract class that provides an interface on top of various * implementations of render devices (capable of rendering primitives to * screen. * * @author Vincent Prat */ class RenderDeviceImplementor { //---------------------------------------------------------------------- // Public methods //-- public:

/** * Default object constructor */ RenderDeviceImplementor() { }

/** * Object destructor */ virtual ~RenderDeviceImplementor() {}

//------------------------------------------------------------------ // Render state switches functions //-- /** * Set a given render state to some value. */ virtual bool SetRenderState( RS_RENDERSTATETYPE state, DWORD value ) = 0;

/** * Get a render state value */ virtual DWORD GetRenderState( RS_RENDERSTATETYPE state ) = 0;

//------------------------------------------------------------------ // Rendering functions //-- /** * Indicate that we are about to begin rendering our scene. */ virtual void BeginRendering() = 0;

/** * Indicate we have finished to describe what we want to render */ virtual void EndRendering() = 0;

/** * This function should be used to allocate memory for a new vertex * buffer. * @param primitiveType Type of primitives to render * @param pVB Pointer to the buffer holding vertex data * @param baseVertex Index of the vertex from which to begin primitive reading * @param primitiveCount Number of primitives to draw * @return true if it succeeded, false else. */ virtual bool DrawPrimitives( RS_PRIMITIVETYPE primitiveType, VertexBuffer* pVB, DWORD baseVertex, DWORD primitiveCount ) = 0; /** * Display the content of the active backbuffer on screen */ virtual void SwapBuffers() = 0;

/** * Say that we will use this texture for following draw primitive * calls that need a texture. */ virtual void UseTexture( Texture* pTex ) = 0;

//------------------------------------------------------------------ // Tranformation functions //-- /** * Set current matrix mode */ virtual void MatrixModeSet(RS_MATRIXMODE mode) = 0;

/** * Get current matrix mode */ virtual const RS_MATRIXMODE MatrixModeGet() const = 0;

/** * Set current matrix to identity matrix */ virtual void MatrixLoadIdentity() = 0;

/** * Set current matrix to given matrix */ virtual void MatrixLoad(const Matrix4x4& m) = 0;

/** * Push current matrix on the stack */ virtual void MatrixPush() = 0;

/** * Remove last matrix from the stack */ virtual void MatrixPop() = 0;

/** * Multiply current matrix by the matrix corresponding to * provided scale values. */ virtual void MatrixScale( float x, float y, float z ) = 0;

/** * Multiply current matrix by the matrix corresponding to * provided translation values. */ virtual void MatrixTranslation( float x, float y, float z ) = 0;

/** * Multiply current matrix by the matrix corresponding to * provided rotation angle (in degrees) and axis. */ virtual void MatrixRotation( float angle, float x, float y, float z ) = 0;

/** * Multiply current transformation matric by given matrix */ virtual void MatrixMultiply(const Matrix4x4& m) = 0;

/** * Get current tranformation matrix */ virtual Matrix4x4 MatrixGet() = 0;

//------------------------------------------------------------------ // Other functions //-- /** * Set clear values */ virtual void SetBackgroundColor( float a, float r, float g, float b ) = 0; virtual void SetDepthClearValue( float z ) = 0; virtual void SetStencilClearValue( DWORD s ) = 0;

/** * Clears the backbuffer, Z-Buffer and stencil buffer to given * values if relevant flags are set. * @param flags A combination of any of the CLEARFLAG */ virtual void Clear( DWORD flags ) = 0;

/** * Set viewport area */ virtual bool SetViewport( int x, int y, int w, int h ) = 0;

//---------------------------------------------------------------------- // Typedefs, public members and accessors //-- public: const RS_BASEAPI GetBaseApi() const { return m_baseApi; }

//---------------------------------------------------------------------- // Protected member functions //-- protected: /** * This function will release all the memory that was dynamically * allocated for the device's internal members. */ virtual void Release() = 0;

//---------------------------------------------------------------------- // Protected members and accessors //-- protected: /** * Base 3D API that is currently used to render primitives */ RS_BASEAPI m_baseApi;

/** * Matrix affected by the matrix function calls (projection or * model-view) */ RS_MATRIXMODE m_matrixMode; };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/OpenGLRenderObjectFactory.h] - (876 bytes)

#ifndef _OPENGLRENDEROBJECTFACTORY_H_INCLUDED_
#define _OPENGLRENDEROBJECTFACTORY_H_INCLUDED_

#include "RenderObjectFactory.h"

/** * This is the implementation of the OpenGL version of render object * factory * * @author Vincent Prat */ class OpenGLRenderObjectFactory : public RenderObjectFactory { //---------------------------------------------------------------------- // Public methods //-- public:

OpenGLRenderObjectFactory(); ~OpenGLRenderObjectFactory();

//------------------------------------------------------------------ // Object creation functions (see base class documentation for this) //-- VertexBuffer* CreateVertexBuffer( int count, DWORD format, bool isDynamic = true ); Texture* CreateTexture( DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic = false, DWORD dwMipMapLevelNum = 0 ); };



#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/D3DVertexBuffer.cpp] - (4,156 bytes)

#ifdef COMPILE_DIRECT3D_IMPLEMENTATION

#include "D3DVertexBuffer.h" #include <d3d9.h>

D3DVertexBuffer::D3DVertexBuffer( LPDIRECT3DDEVICE9 pD3DDevice, int count, DWORD format, bool isDynamic ) : VertexBuffer() { DWORD size = 0; m_pD3DDevice = pD3DDevice; m_pD3DVertexBuffer = NULL;

m_FVF = 0; m_dwFormat = format; m_dwVertexCount = count;

if( format & RSVF_XYZ ) { m_FVF |= D3DFVF_XYZ; size += sizeof( float ) * 3; m_dwOffsetVertex = 0; } else if( format & RSVF_XYZRHW ) { m_FVF |= D3DFVF_XYZRHW; size += sizeof( float ) * 4; m_dwOffsetVertex = 0; }

if( format & RSVF_NORMAL ) { m_FVF |= D3DFVF_NORMAL; m_dwOffsetNormal = size; size += sizeof( float ) * 3; }

if( format & RSVF_DIFFUSE ) { m_FVF |= D3DFVF_DIFFUSE; m_dwOffsetDiffuse = size; size += sizeof( DWORD ); }

if( format & RSVF_TEXCOORD ) { m_FVF |= D3DFVF_TEX1; m_dwOffsetTexCoord = size; size += sizeof( float ) * 2; } m_dwStrideVertex = size; m_bIsDynamic = isDynamic; m_bIsLocked = false; m_pVertex = NULL; m_pBuffer = NULL;

HRESULT hRes;

if (m_bIsDynamic) { hRes = m_pD3DDevice->CreateVertexBuffer( m_dwStrideVertex * count, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, m_FVF, D3DPOOL_DEFAULT, &m_pD3DVertexBuffer, NULL ); } else { hRes = m_pD3DDevice->CreateVertexBuffer( m_dwStrideVertex * count, D3DUSAGE_WRITEONLY, m_FVF, D3DPOOL_MANAGED, &m_pD3DVertexBuffer, NULL ); }

if( FAILED(hRes) ) ::OutputDebugString( "D3DVertexBuffer::D3DVertexBuffer() --> creation of D3D vertex buffer failed !"); }

D3DVertexBuffer::~D3DVertexBuffer() { if( m_pD3DVertexBuffer != NULL ) { m_pD3DVertexBuffer->Release(); m_pD3DVertexBuffer = NULL; } }

bool D3DVertexBuffer::Lock() { HRESULT hRes = m_pD3DVertexBuffer->Lock( 0, 0, &m_pBuffer, (m_bIsDynamic?D3DLOCK_DISCARD:0) );

if ( FAILED(hRes) ) ::OutputDebugString( "D3DVertexBuffer::lock() --> Unable to lock D3D Vertex Buffer." );

m_bIsLocked = true; m_pVertex = (BYTE*)m_pBuffer;

return (!FAILED(hRes)); }

bool D3DVertexBuffer::Unlock() { HRESULT hRes = m_pD3DVertexBuffer->Unlock();

if ( FAILED(hRes) ) ::OutputDebugString( "D3DVertexBuffer::unlock() --> Unable to unlock D3D Vertex Buffer." );

m_bIsLocked = false; m_pVertex = NULL; m_pBuffer = NULL;

return (!FAILED(hRes)); }

void* D3DVertexBuffer::GetVertexData() { return m_pBuffer; }

void D3DVertexBuffer::Vertex( float x, float y, float z ) { float* vertex = (float*)(m_pVertex + m_dwOffsetVertex);

vertex[ 0 ] = x; vertex[ 1 ] = y; vertex[ 2 ] = z;

m_pVertex += m_dwStrideVertex; }

void D3DVertexBuffer::Vertex( float x, float y, float z, float w ) { float* vertex = (float*)(m_pVertex + m_dwOffsetVertex);

vertex[ 0 ] = x; vertex[ 1 ] = y; vertex[ 2 ] = z; vertex[ 3 ] = w;

m_pVertex += m_dwStrideVertex; }

void D3DVertexBuffer::Normal( float x, float y, float z ) { float* normal = (float*)( m_pVertex + m_dwOffsetNormal );

normal[ 0 ] = x; normal[ 1 ] = y; normal[ 2 ] = z; }

void D3DVertexBuffer::Diffuse( float r, float g, float b, float a ) { DWORD* diffuse = (DWORD*)( m_pVertex + m_dwOffsetDiffuse ); *diffuse = D3DCOLOR_COLORVALUE( r, g, b, a ); }

void D3DVertexBuffer::TexVertex( float u, float v ) { float* texcoord = (float*)( m_pVertex + m_dwOffsetTexCoord );

texcoord[ 0 ] = u; texcoord[ 1 ] = v; }

bool D3DVertexBuffer::PrepareForDrawing() { HRESULT hRes;

hRes = m_pD3DDevice->SetStreamSource( 0, m_pD3DVertexBuffer, 0, m_dwStrideVertex ); if (FAILED(hRes)) return 0; hRes = m_pD3DDevice->SetFVF( m_FVF ); if (FAILED(hRes)) return 0; return true; }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/D3DTexture.cpp] - (4,767 bytes)

#ifdef COMPILE_DIRECT3D_IMPLEMENTATION

#include "D3DTexture.h" #include <d3d9.h>

D3DTexture::D3DTexture( LPDIRECT3DDEVICE9 pD3DDevice, DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic, DWORD dwMipMapLevelNum ) : Texture() { m_pD3DDevice = pD3DDevice; m_pD3DTexture = NULL;

m_dwWidth = Width; m_dwHeight = Height; m_Format = format; m_dwMipMapLevelNum = dwMipMapLevelNum;

if( format == RSFMT_A8R8G8B8 ) { m_FMT = D3DFMT_A8R8G8B8; } else { m_FMT = D3DFMT_A8R8G8B8; ::OutputDebugString( "D3DTexture::D3DTexture() --> Invalid texture format (reverting to default 32 BPP) !\n"); } m_bIsDynamic = isDynamic; m_bIsLocked = false; m_pBuffer = NULL; m_pCurrentPixel= NULL;

HRESULT hRes;

if (m_bIsDynamic) { hRes = m_pD3DDevice->CreateTexture( m_dwWidth, m_dwHeight, m_dwMipMapLevelNum, D3DUSAGE_DYNAMIC, m_FMT, D3DPOOL_DEFAULT, &m_pD3DTexture, NULL ); } else { hRes = m_pD3DDevice->CreateTexture( m_dwWidth, m_dwHeight, m_dwMipMapLevelNum, 0, m_FMT, D3DPOOL_DEFAULT, &m_pD3DTexture, NULL ); }

if( FAILED(hRes) ) ::OutputDebugString( "D3DTexture::D3DTexture() --> creation of D3D texture failed !\n"); }

D3DTexture::~D3DTexture() { if( m_pD3DTexture != NULL ) { m_pD3DTexture->Release(); m_pD3DTexture = NULL; } }

bool D3DTexture::Lock() { HRESULT hRes; D3DLOCKED_RECT rect;

if ( m_bIsDynamic ) { // Dynamic textures can be locked directly //-- if ( FAILED( m_pD3DTexture->LockRect( 0, &rect, NULL, D3DLOCK_DISCARD ) ) ) { ::OutputDebugString( "D3DTexture::Lock() --> Unable to lock D3D Texture.\n" ); m_pBuffer = NULL; m_pCurrentPixel= NULL; m_bIsLocked = false; return false; } } else { // Static textures require us to first create a dynamic texture //-- hRes = m_pD3DDevice->CreateTexture( m_dwWidth, m_dwHeight, m_dwMipMapLevelNum, D3DUSAGE_DYNAMIC, m_FMT, D3DPOOL_SYSTEMMEM, &m_pD3DRamTexture, NULL );

if ( FAILED(hRes) ) { ::OutputDebugString( "D3DTexture::Lock() --> Failed to create temp texture.\n" ); m_pBuffer = NULL; m_pCurrentPixel= NULL; m_pD3DRamTexture=NULL; m_bIsLocked = false; return false; }

// Then lock this one and work with it //-- hRes = m_pD3DRamTexture->LockRect( 0, &rect, NULL, D3DLOCK_DISCARD ); if ( FAILED(hRes) ) { ::OutputDebugString( "D3DTexture::Lock() --> Failed to lock temp texture.\n" );

// Don't forget to release temp texture ! //-- m_pD3DRamTexture->Release(); m_pD3DRamTexture=NULL;

m_pBuffer = NULL; m_pCurrentPixel= NULL; m_bIsLocked = false;

return false; } }

m_dwPitch = rect.Pitch; m_pBuffer = rect.pBits; m_pCurrentPixel= (unsigned char*)m_pBuffer; m_bIsLocked = true;

return true; }

bool D3DTexture::Unlock() { if ( m_bIsLocked ) { if (m_bIsDynamic ) { // Dynamic textures are handled by d3d directly //-- if ( FAILED( m_pD3DTexture->UnlockRect(0) ) ) { ::OutputDebugString( "D3DTexture::unlock() --> Unable to unlock D3D Vertex Buffer." ); return false; } } else { // Static texture are a bit more annoying //-- if (m_pD3DRamTexture==NULL) { ::OutputDebugString( "D3DTexture::unlock() --> Unable to unlock D3D Vertex Buffer." ); return false; }

// Unlock temp texture //-- if ( FAILED( m_pD3DRamTexture->UnlockRect(0) ) ) { ::OutputDebugString( "D3DTexture::unlock() --> Unable to unlock D3D Vertex Buffer." );

// Don't forget to release temp texture ! //-- m_pD3DRamTexture->Release(); m_pD3DRamTexture=NULL; return false; }

// Copy the temp texture to the static texture //-- if( FAILED( m_pD3DDevice->UpdateTexture( m_pD3DRamTexture, m_pD3DTexture ) )) { ::OutputDebugString( "D3DTexture::unlock() --> Unable to unlock D3D Vertex Buffer." );

// Don't forget to release temp texture ! //-- m_pD3DRamTexture->Release(); m_pD3DRamTexture=NULL; return false; }

// Don't forget to release temp texture ! //-- m_pD3DRamTexture->Release(); m_pD3DRamTexture=NULL; }

m_bIsLocked = false; m_pBuffer = NULL; m_pCurrentPixel= NULL; } else { return false; }

return true; }

void* D3DTexture::GetData() { return m_pBuffer; }

bool D3DTexture::Use() { HRESULT hRes;

hRes = m_pD3DDevice->SetTexture( 0, m_pD3DTexture ); if (FAILED(hRes)) return false; return true; }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/D3DRenderDevice.cpp] - (10,513 bytes)

#ifdef COMPILE_DIRECT3D_IMPLEMENTATION

#include "D3DRenderDevice.h" #include "D3DVertexBuffer.h"

D3DRenderDevice::D3DRenderDevice( HWND hWnd, DWORD width, DWORD height ) : RenderDeviceImplementor() { // Init some member values //-- m_baseApi = RSAPI_DIRECT3D; m_pD3D = NULL; m_pD3DDevice = NULL; D3D9DLL = 0; D3DX9DLL = 0; myDirect3DCreate9 = 0;

// Retrieve d3d dll functions //-- if ( !OpenD3DDLL() ) { ::OutputDebugString( "D3DRenderDevice::D3DRenderDevice() --> Failed to open D3D Dlls\n" ); return; }

// First, create D3D //-- if( NULL == ( m_pD3D = (myDirect3DCreate9)( D3D_SDK_VERSION ) ) ) { ::OutputDebugString( "D3DRenderDevice::D3DRenderDevice() --> Failed to create D3D object\n" ); Release(); return; }

// Retrieve hardware capabilities and some useful stuff //-- m_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_D3DCaps );

// Create D3D device //-- if ( !CreateD3DDevice( hWnd, width, height ) ) { ::OutputDebugString( "D3DRenderDevice::D3DRenderDevice() --> Failed to create D3D device\n" ); Release(); return; }

// Set a default viewport //-- SetViewport( 0, 0, width, height );

SetBackgroundColor( 1, 0, 0, 0 ); SetDepthClearValue( 1.0f ); SetStencilClearValue( 0 ); }

D3DRenderDevice::~D3DRenderDevice() { Release(); }

bool D3DRenderDevice::CreateD3DDevice( HWND hWnd, DWORD width, DWORD height ) { // Build the creation parameters //-- D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(D3DPRESENT_PARAMETERS) ); d3dpp.Windowed = true; d3dpp.hDeviceWindow = hWnd; d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = width; d3dpp.BackBufferHeight = height; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = true; d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

// Try to require hardware T&L (Best case) //-- if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice ) ) ) { ::OutputDebugString( " -- D3D -- Failed to create D3D Device with D3DCREATE_HARDWARE_VERTEXPROCESSING\n" );

// Failed ! try to get software T&L (Not that bad after all) //-- if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice ) ) ) { ::OutputDebugString( " -- D3D -- Failed to create D3D Device with D3DCREATE_SOFTWARE_VERTEXPROCESSING\n" );

// Failed ! try to get software T&L AND reference device (Worst case) //-- if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice ) ) ) { ::OutputDebugString( " -- D3D -- Failed to create D3D Device with D3DCREATE_SOFTWARE_VERTEXPROCESSING and REF rasterizer. Buy another grphics card !!\n" ); return false; } } }

return true; }

bool D3DRenderDevice::OpenD3DDLL() { D3D9DLL = LoadLibrary("d3d9.dll"); if(!D3D9DLL) { CloseD3DDLL(); return false; } D3DX9DLL = LoadLibrary("d3dx9.dll"); if(!D3DX9DLL) { D3DX9DLL = LoadLibrary("d3dx9d.dll"); if(!D3DX9DLL) { CloseD3DDLL(); return false; } }

myDirect3DCreate9 = (LPDIRECT3DCREATE9)GetProcAddress(D3D9DLL,"Direct3DCreate9"); if( !myDirect3DCreate9 ) { CloseD3DDLL(); return false; }

return true; }

void D3DRenderDevice::CloseD3DDLL() { if( D3D9DLL ) FreeLibrary(D3D9DLL); if( D3DX9DLL ) FreeLibrary(D3DX9DLL); D3D9DLL = 0; D3DX9DLL = 0; myDirect3DCreate9 = NULL; }

void D3DRenderDevice::Release() { SAFE_RELEASE( m_pD3DDevice ); SAFE_RELEASE( m_pD3D ); CloseD3DDLL(); }

//------------------------------------------------------------------ // Tranformation functions //-- void D3DRenderDevice::MatrixModeSet(RS_MATRIXMODE mode) { m_matrixMode = mode; }

const RS_MATRIXMODE D3DRenderDevice::MatrixModeGet() const { return m_matrixMode; }

void D3DRenderDevice::MatrixLoadIdentity() { Matrix4x4 identity; MatrixLoad(identity); }

void D3DRenderDevice::MatrixLoad(const Matrix4x4& m) { D3DMATRIX d3dMat; MatrixToD3D( &m, &d3dMat );

if ( m_matrixMode==RSMM_PROJECTION ) { m_projectionMatrix = m; m_pD3DDevice->SetTransform( D3DTS_PROJECTION, &d3dMat ); } else { m_modelviewMatrix = m; m_pD3DDevice->SetTransform( D3DTS_WORLD, &d3dMat ); }

// Reset D3D view matrix //-- Matrix4x4 identity; MatrixToD3D( &identity, &d3dMat ); m_pD3DDevice->SetTransform( D3DTS_VIEW, &d3dMat ); }

void D3DRenderDevice::MatrixPush() { if ( m_matrixMode==RSMM_PROJECTION ) m_matrixStack.push( m_projectionMatrix ); else m_matrixStack.push( m_modelviewMatrix ); }

void D3DRenderDevice::MatrixPop() { D3DMATRIX d3dMat;

if ( m_matrixMode==RSMM_PROJECTION ) { m_projectionMatrix = m_matrixStack.getCurrent(); MatrixToD3D( &m_projectionMatrix, &d3dMat ); m_pD3DDevice->SetTransform( D3DTS_PROJECTION, &d3dMat ); } else { m_modelviewMatrix = m_matrixStack.getCurrent(); MatrixToD3D( &m_modelviewMatrix, &d3dMat ); m_pD3DDevice->SetTransform( D3DTS_WORLD, &d3dMat ); }

m_matrixStack.pop(); }

void D3DRenderDevice::MatrixScale( float x, float y, float z ) { Matrix4x4 mat; mat.SetScale( x, y, z ); MatrixMultiply( mat ); }

void D3DRenderDevice::MatrixTranslation( float x, float y, float z ) { Matrix4x4 mat; mat.SetTranslation( x, y, z ); MatrixMultiply( mat ); }

void D3DRenderDevice::MatrixRotation( float angle, float x, float y, float z ) { Matrix4x4 mat; mat.SetRotation( angle, x, y, z ); MatrixMultiply( mat ); }

void D3DRenderDevice::MatrixMultiply(const Matrix4x4& mat) { D3DMATRIX d3dMat;

if ( m_matrixMode==RSMM_PROJECTION ) { m_projectionMatrix.Multiply( mat ); MatrixToD3D( &m_projectionMatrix, &d3dMat ); m_pD3DDevice->SetTransform( D3DTS_PROJECTION, &d3dMat ); } else { m_modelviewMatrix.Multiply( mat ); MatrixToD3D( &m_modelviewMatrix, &d3dMat ); m_pD3DDevice->SetTransform( D3DTS_WORLD, &d3dMat ); } }

Matrix4x4 D3DRenderDevice::MatrixGet() { D3DMATRIX d3dMat; Matrix4x4 retMat;

if ( m_matrixMode==RSMM_PROJECTION ) { m_pD3DDevice->GetTransform( D3DTS_PROJECTION, &d3dMat ); } else { m_pD3DDevice->GetTransform( D3DTS_WORLD, &d3dMat ); }

MatrixD3DTo( &d3dMat, &retMat ); return retMat; }

//------------------------------------------------------------------ // Rendering functions //-- bool D3DRenderDevice::DrawPrimitives( RS_PRIMITIVETYPE primitiveType, VertexBuffer* pVB, DWORD baseVertex, DWORD primitiveCount )

{ if ( pVB==NULL ) return 0;

// Convert primitive type //-- D3DPRIMITIVETYPE d3dPT = GetD3DPrimitiveType( primitiveType );

// Say that the VB will be the source for our draw primitive calls //-- if ( !pVB->PrepareForDrawing() ) return 0;

// Draw primitives //-- if ( FAILED( m_pD3DDevice->DrawPrimitive( d3dPT, baseVertex, primitiveCount ) ) ) return 0;

return true; }

void D3DRenderDevice::SwapBuffers() { m_pD3DDevice->Present( NULL, NULL, NULL, NULL ); }

void D3DRenderDevice::BeginRendering() { m_pD3DDevice->BeginScene(); }

void D3DRenderDevice::EndRendering() { m_pD3DDevice->EndScene(); }

void D3DRenderDevice::UseTexture( Texture* pTex ) { pTex->Use(); }

//------------------------------------------------------------------ // Render state switches functions //-- bool D3DRenderDevice::SetRenderState( RS_RENDERSTATETYPE state, DWORD value ) { DWORD d3dValue; D3DRENDERSTATETYPE d3dRS;

switch (state) { case RSRS_CULLINGENABLE: d3dRS = D3DRS_CULLMODE; switch (value) { case 0: d3dValue = D3DCULL_NONE; break;

case 1: d3dValue = D3DCULL_CW; break;

default: return false; }; break;

case RSRS_ZENABLE: d3dRS = D3DRS_ZENABLE; d3dValue = value; break;

case RSRS_LIGHTINGENABLE: d3dRS = D3DRS_LIGHTING; d3dValue = value; break;

case RSRS_FILLMODE: d3dRS = D3DRS_FILLMODE; switch (value) { case RSFILL_WIREFRAME: d3dValue = D3DFILL_WIREFRAME; break;

case RSFILL_SOLID: d3dValue = D3DFILL_SOLID; break;

default: return false; }; break;

default: return false; } return SUCCEEDED( m_pD3DDevice->SetRenderState( d3dRS, d3dValue ) ); }

DWORD D3DRenderDevice::GetRenderState( _RENDERSTATETYPE state ) { D3DRENDERSTATETYPE d3dRS; DWORD returnValue = 0;

switch (state) { case RSRS_ZENABLE: d3dRS = D3DRS_ZENABLE; m_pD3DDevice->GetRenderState( d3dRS, &returnValue ); return returnValue;

case RSRS_LIGHTINGENABLE: d3dRS = D3DRS_LIGHTING; m_pD3DDevice->GetRenderState( d3dRS, &returnValue ); return returnValue;

case RSRS_CULLINGENABLE: d3dRS = D3DRS_CULLMODE; m_pD3DDevice->GetRenderState( d3dRS, &returnValue ); return returnValue;

case RSRS_FILLMODE: d3dRS = D3DRS_FILLMODE; m_pD3DDevice->GetRenderState( d3dRS, &returnValue );

if ( returnValue == D3DFILL_WIREFRAME ) return RSFILL_WIREFRAME; else return RSFILL_SOLID; } return 0; }

//------------------------------------------------------------------ // Other functions //-- void D3DRenderDevice::SetBackgroundColor( float a, float r, float g, float b ) { m_backgroundColor = D3DCOLOR_COLORVALUE( r, g, b, a ); }

void D3DRenderDevice::SetDepthClearValue( float z ) { m_depthClearValue = z; }

void D3DRenderDevice::SetStencilClearValue( DWORD s ) { m_stencilClearValue = s; }

void D3DRenderDevice::Clear( DWORD flags ) { DWORD d3dFlags=0;

if ( flags & RSCLR_COLOR ) d3dFlags |= D3DCLEAR_TARGET;

if ( flags & RSCLR_ZBUFFER ) d3dFlags |= D3DCLEAR_ZBUFFER;

if ( flags & RSCLR_STENCIL ) d3dFlags |= D3DCLEAR_STENCIL;

m_pD3DDevice->Clear( 0, NULL, d3dFlags, m_backgroundColor, m_depthClearValue, m_stencilClearValue ); }

bool D3DRenderDevice::SetViewport( int x, int y, int w, int h ) { if ( (w==0) || (h==0) ) return false;

D3DVIEWPORT9 vp; vp.X = x; vp.Y = y; vp.Width = w; vp.Height = h; vp.MinZ = 0.0f; vp.MaxZ = 1.0f;

return SUCCEEDED( m_pD3DDevice->SetViewport( &vp ) ); }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/D3DRenderObjectFactory.cpp] - (973 bytes)

#ifdef COMPILE_DIRECT3D_IMPLEMENTATION

#include "D3DRenderObjectFactory.h" #include "D3DVertexBuffer.h" #include "D3DTexture.h"

D3DRenderObjectFactory::D3DRenderObjectFactory( LPDIRECT3DDEVICE9 pD3DDevice ) : RenderObjectFactory() { m_pD3DDevice = pD3DDevice; }

D3DRenderObjectFactory::~D3DRenderObjectFactory() { }

//------------------------------------------------------------------ // Object creation functions //-- VertexBuffer* D3DRenderObjectFactory::CreateVertexBuffer( int count, DWORD format, bool isDynamic )

{ D3DVertexBuffer* pVB = new D3DVertexBuffer( m_pD3DDevice, count, format, isDynamic ); return pVB; }

Texture* D3DRenderObjectFactory::CreateTexture( DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic, DWORD dwMipMapLevelNum ) { D3DTexture* pTex = new D3DTexture( m_pD3DDevice, Width, Height, format, isDynamic, dwMipMapLevelNum );

return pTex; }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/D3DRenderObjectFactory.h] - (1,091 bytes)

#ifndef _D3DRENDEROBJECTFACTORY_H_INCLUDED_
#define _D3DRENDEROBJECTFACTORY_H_INCLUDED_

#include "RenderObjectFactory.h" #include <d3d9.h>

/** * This is the implementation of the D3D version of render object * factory. * * @author Vincent Prat */ class D3DRenderObjectFactory : public RenderObjectFactory { //---------------------------------------------------------------------- // Public methods //-- public:

/** * Default object constructor */ D3DRenderObjectFactory( LPDIRECT3DDEVICE9 pD3DDevice );

/** * Object destructor */ ~D3DRenderObjectFactory();

//------------------------------------------------------------------ // Object creation functions (see base class documentation for this) //-- VertexBuffer* CreateVertexBuffer( int count, DWORD format, bool isDynamic = true ); Texture* CreateTexture( DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic = false, DWORD dwMipMapLevelNum = 0 );

protected: /** * Pointer to the d3d device object */ LPDIRECT3DDEVICE9 m_pD3DDevice; };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/OpenGLRenderDevice.h] - (3,857 bytes)

#ifndef _OPENGLRENDERDEVICE_H_INCLUDED_
#define _OPENGLRENDERDEVICE_H_INCLUDED_

#include <windows.h> #include <GL/gl.h> #include "RenderDevice.h"

/** * This is the implementation of the OpenGL version of render devices * * @author Vincent Prat */ class OpenGLRenderDevice : public RenderDeviceImplementor { //---------------------------------------------------------------------- // Public methods //-- public:

OpenGLRenderDevice( HWND hwnd, DWORD width, DWORD height ); ~OpenGLRenderDevice();

//------------------------------------------------------------------ // Rendering functions (see base class documentation for this) //-- void BeginRendering(); void EndRendering(); void SwapBuffers(); bool DrawPrimitives( RS_PRIMITIVETYPE primitiveType, VertexBuffer* pVB, DWORD baseVertex, DWORD primitiveCount ); void UseTexture( Texture* pTex );

//------------------------------------------------------------------ // Render state switches functions (see base class documentation // for this) //-- bool SetRenderState( RS_RENDERSTATETYPE state, DWORD value ); DWORD GetRenderState( RS_RENDERSTATETYPE state );

//------------------------------------------------------------------ // Tranformation functions (see base class documentation for this) //-- void MatrixModeSet(RS_MATRIXMODE mode); const RS_MATRIXMODE MatrixModeGet() const; void MatrixLoadIdentity(); void MatrixLoad(const Matrix4x4& m); void MatrixPush(); void MatrixPop(); void MatrixMultiply(const Matrix4x4& m); void MatrixScale( float x, float y, float z ); void MatrixTranslation( float x, float y, float z ); void MatrixRotation( float angle, float x, float y, float z ); Matrix4x4 MatrixGet(); //------------------------------------------------------------------ // Other functions (see base class documentation for this) //-- void SetBackgroundColor( float a, float r, float g, float b ); void SetDepthClearValue( float z ); void SetStencilClearValue( DWORD s ); void Clear( DWORD flags ); bool SetViewport( int x, int y, int w, int h );

//---------------------------------------------------------------------- // Protected member functions //-- protected:

// Overloaded functions //-- void Release();

//---------------------------------------------------------------------- // Protected members and accessors //-- protected: /** * The device context */ HGLRC m_hrc; HWND m_hWnd;

/** * Return corresponding OpenGL primitive type and primitive count */ inline bool GetOpenGLPrimitiveType( const RS_PRIMITIVETYPE pt, const DWORD nInitialPrimitiveCount, GLenum* GLPrimitiveType, DWORD* nGLPrimitiveCount );

};



inline bool OpenGLRenderDevice::GetOpenGLPrimitiveType( const RS_PRIMITIVETYPE pt, const DWORD nInitialPrimitiveCount, GLenum* GLPrimitiveType, DWORD* nGLPrimitiveCount ) { switch (pt) { case RSPT_POINTLIST: *GLPrimitiveType = GL_POINTS; *nGLPrimitiveCount = nInitialPrimitiveCount; return true;

case RSPT_LINELIST: *GLPrimitiveType = GL_LINES; *nGLPrimitiveCount = 2*nInitialPrimitiveCount; return true;

case RSPT_LINESTRIP: *GLPrimitiveType = GL_LINE_STRIP; *nGLPrimitiveCount = nInitialPrimitiveCount+1; return true;

case RSPT_TRIANGLELIST: *GLPrimitiveType = GL_TRIANGLES; *nGLPrimitiveCount = 3*nInitialPrimitiveCount; return true;

case RSPT_TRIANGLESTRIP: *GLPrimitiveType = GL_TRIANGLE_STRIP; *nGLPrimitiveCount = nInitialPrimitiveCount+2; return true;

case RSPT_TRIANGLEFAN: *GLPrimitiveType = GL_TRIANGLE_FAN; *nGLPrimitiveCount = nInitialPrimitiveCount+2; return true;

default: *GLPrimitiveType = GL_POINTS; *nGLPrimitiveCount = nInitialPrimitiveCount; return false; } }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/D3DVertexBuffer.h] - (3,647 bytes)

#ifndef _D3DVERTEXBUFFER_H_INCLUDED_
#define _D3DVERTEXBUFFER_H_INCLUDED_

#include "VertexBuffer.h" #include <d3d9.h>

/** * This is Direct3D 9 implementation of VertexBuffer. * * Inspired by the work of Tobin Schwaiger-Hastanan */ class D3DVertexBuffer : public VertexBuffer { protected: // disable default constructor //-- D3DVertexBuffer(); // disable copy constructor //-- D3DVertexBuffer( const D3DVertexBuffer& ); // disable assignment operator //-- D3DVertexBuffer& operator =( const D3DVertexBuffer& );

public: /** * Constructor used to create vertex buffer. * @param pD3DDevice Direct 3D Device * @param count number of vertex to be created * @param format format of vertex buffer ( See format flags in VertexBuffer) * @param isDynamic specifies that vertex buffer is dynamic. [default: true] */ D3DVertexBuffer( LPDIRECT3DDEVICE9 pD3DDevice, int count, DWORD format, bool isDynamic = true );

// destructor //-- ~D3DVertexBuffer(); // for the following methods, see VertexBuffer //-- bool PrepareForDrawing(); bool Lock(); bool Unlock(); const bool IsLocked() const { return m_bIsLocked; }

void* GetVertexData(); const DWORD GetVertexCount() const { return m_dwVertexCount; } const DWORD GetVertexFormat() const { return m_dwFormat; } const DWORD GetVertexStride() const { return m_dwStrideVertex; }

void Vertex( float x, float y, float z ); void Vertex( float x, float y, float z, float w ); void Normal( float x, float y, float z ); void Diffuse( float r, float g, float b, float a = 1.0f ); void TexVertex( float u, float v );

//---------------------------------------------------------------------- // D3D related methods //-- /** * Returns the D3D Vertex buffer created by this class. * This is a D3DVertexBuffer specific method. * * @return pointer to a Direct 3D Vertex Buffer. */ const LPDIRECT3DVERTEXBUFFER9 GetD3DVertexBuffer() const { return m_pD3DVertexBuffer; }

/** * Returns the D3D Vertex buffer format. * This is a D3DVertexBuffer specific method. * * @return pointer to a Direct 3D Vertex Buffer Format. */ const DWORD GetD3DVertexFormat() const { return m_FVF; }

//---------------------------------------------------------------------- // Private members and accessors //-- private: bool m_bIsLocked; // flag to specify if buffer is locked bool m_bIsDynamic; // flag to specify if buffer is holding dynamic data void* m_pBuffer; // pointer to vertex data DWORD m_dwVertexCount;// number of vertex in buffer DWORD m_dwFormat; // vertex format BYTE* m_pVertex; // pointer to head of current vertex DWORD m_dwStrideVertex; // stride of entire vertex data DWORD m_dwOffsetVertex; // offset of vertex data start of vertex DWORD m_dwOffsetNormal; // offset of normal data from start of vertex DWORD m_dwOffsetDiffuse; // offset of diffuse data from start of vertex DWORD m_dwOffsetTexCoord; // offset of TexVertex data from start of vertex //---------------------------------------------------------------------- // D3D related members //-- LPDIRECT3DVERTEXBUFFER9 m_pD3DVertexBuffer; // d3d vertex buffer LPDIRECT3DDEVICE9 m_pD3DDevice; // d3d device DWORD m_FVF; // d3d vertex format };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/OpenGLVertexBuffer.h] - (2,979 bytes)

#ifndef _OPENGLVERTEXBUFFER_H_INCLUDED_
#define _OPENGLVERTEXBUFFER_H_INCLUDED_

#include "VertexBuffer.h" #include "GL/gl.h"

/** * This is OpenGL implementation of vertex buffers. * * Inspired by the work of Tobin Schwaiger-Hastanan */ class OpenGLVertexBuffer : public VertexBuffer { protected: // disable default constructor //-- OpenGLVertexBuffer(); // disable copy constructor //-- OpenGLVertexBuffer( const OpenGLVertexBuffer& ); // disable assignment operator //-- OpenGLVertexBuffer& operator =( const OpenGLVertexBuffer& );

public: /** * Constructor used to create vertex buffer. * @param count number of vertex to be created * @param format format of vertex buffer ( See format flags in VertexBuffer) * @param isDynamic specifies that vertex buffer is dynamic. [default: true] */ OpenGLVertexBuffer( int count, DWORD format, bool isDynamic = true );

// destructor //-- ~OpenGLVertexBuffer(); // for the following methods, see VertexBuffer //-- bool PrepareForDrawing(); bool Lock(); bool Unlock(); const bool IsLocked() const { return m_bIsLocked; }

void* GetVertexData(); const DWORD GetVertexCount() const { return m_dwVertexCount; } const DWORD GetVertexFormat() const { return m_dwFormat; } const DWORD GetVertexStride() const { return m_dwStrideVertex; }

void Vertex( float x, float y, float z ); void Vertex( float x, float y, float z, float w ); void Normal( float x, float y, float z ); void Diffuse( float r, float g, float b, float a = 1.0f ); void TexVertex( float u, float v ); //---------------------------------------------------------------------- // OpenGL related methods //-- //---------------------------------------------------------------------- // Private members and accessors //-- private: bool m_bIsLocked; // flag to specify if buffer is locked bool m_bIsDynamic; // flag to specify if buffer is holding dynamic data DWORD m_dwVertexCount; // number of vertex in buffer DWORD m_dwFormat; // vertex format DWORD m_dwStrideVertex; // stride of entire vertex data DWORD m_dwVertexCoordNum; // Number of coordinates per vertex float* m_pVertex; // pointer to head of current vertex float* m_pColor; // pointer to head of current color float* m_pNormal; // pointer to head of current normal float* m_pTexCoord; // pointer to head of current texture coordinate //---------------------------------------------------------------------- // OpenGL related members //-- float* m_pGLVertices; // Buffer containing vertex data float* m_pGLColors; // Buffer containing vertex data float* m_pGLNormals; // Buffer containing vertex data float* m_pGLTexCoords; // Buffer containing vertex data };

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/OpenGLTexture.h] - (3,458 bytes)

#ifndef _OPENGLTexture_H_INCLUDED_
#define _OPENGLTexture_H_INCLUDED_

#include "Texture.h" #include "GL/gl.h"

/** * This is OpenGL implementation of Texture. * */ class OpenGLTexture : public Texture { protected: /// disable default constructor //-- OpenGLTexture(); /// disable copy constructor //-- OpenGLTexture( const OpenGLTexture& ); /// disable assignment operator //-- OpenGLTexture& operator =( const OpenGLTexture& );

public: /** * Constructor used to create vertex buffer. * @param Width width of the texture * @param Height height of the texture * @param format format of the texture * @param isDynamic specifies that texture is dynamic. [default: false] * @param dwMipMaLevels Number of mipmap levels to generate. Value of * 0 will generate all the levels till 1x1. [default: 0] */ OpenGLTexture( DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic = false, DWORD dwMipMapLevelNum = 0 );

/// destructor //-- ~OpenGLTexture(); // for the following methods, see Texture //-- bool Use();

bool Lock(); bool Unlock(); const bool IsLocked() const { return m_bIsLocked; }

void* GetData(); const DWORD GetWidth() const { return m_dwWidth; } const DWORD GetHeight() const { return m_dwHeight; } const DWORD GetPitch() const { return m_dwWidth*m_dwPixelStride; } const RS_FORMAT GetFormat() const { return m_Format; } const DWORD GetMipMapLevelsNum() const { return m_dwMipMapLevelNum; } inline void SetPixel4f( float a, float r, float g, float b ); inline void SetPixel4uc( unsigned char a, unsigned char r, unsigned char g, unsigned char b );

//---------------------------------------------------------------------- // OpenGL related methods //-- //---------------------------------------------------------------------- // Private members and accessors //-- private: DWORD m_dwMipMapLevelNum; /// number of mipmap levels RS_FORMAT m_Format; /// format of the texture DWORD m_dwWidth; /// texture width DWORD m_dwHeight; /// texture height bool m_bIsLocked; /// flag to specify if buffer is locked bool m_bIsDynamic; /// flag to specify if buffer is holding dynamic data void* m_pBuffer; /// pointer to vertex data unsigned char* m_pCurrentPixel; //---------------------------------------------------------------------- // OpenGL related members //-- GLuint m_GLTextureID; /// OpenGL ID of the texture GLenum m_GLFormat; /// Format of the textue for OpenGL GLuint m_GLInternalFormat; DWORD m_dwPixelStride; /// Size of a pixel in bytes };

inline void OpenGLTexture::SetPixel4f( float a, float r, float g, float b ) { if ( m_Format==RSFMT_A8R8G8B8 ) { m_pCurrentPixel[0] = (unsigned char)(255.0f*r); m_pCurrentPixel[1] = (unsigned char)(255.0f*g); m_pCurrentPixel[2] = (unsigned char)(255.0f*b); m_pCurrentPixel[3] = (unsigned char)(255.0f*a); m_pCurrentPixel+=4; } }

inline void OpenGLTexture::SetPixel4uc( unsigned char a, unsigned char r, unsigned char g, unsigned char b ) { if ( m_Format==RSFMT_A8R8G8B8 ) { m_pCurrentPixel[0] = r; m_pCurrentPixel[1] = g; m_pCurrentPixel[2] = b; m_pCurrentPixel[3] = a; m_pCurrentPixel+=4; } }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/OpenGLRenderObjectFactory.cpp] - (932 bytes)

#ifdef COMPILE_OPENGL_IMPLEMENTATION

#include "OpenGLRenderObjectFactory.h" #include "OpenGLVertexBuffer.h" #include "OpenGLTexture.h"

OpenGLRenderObjectFactory::OpenGLRenderObjectFactory() : RenderObjectFactory() { }

OpenGLRenderObjectFactory::~OpenGLRenderObjectFactory() { }

//------------------------------------------------------------------ // Object creation functions //-- VertexBuffer* OpenGLRenderObjectFactory::CreateVertexBuffer( int count, DWORD format, bool isDynamic )

{ OpenGLVertexBuffer* pVB = new OpenGLVertexBuffer( count, format, isDynamic );

return pVB; }

Texture* OpenGLRenderObjectFactory::CreateTexture( DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic, DWORD dwMipMapLevelNum ) { OpenGLTexture* pTex = new OpenGLTexture( Width, Height, format, isDynamic, dwMipMapLevelNum );

return pTex; }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/RenderSystem/OpenGLTexture.cpp] - (2,563 bytes)

#ifdef COMPILE_OPENGL_IMPLEMENTATION

#include "OpenGLTexture.h" #include <GL\glu.h>

OpenGLTexture::OpenGLTexture( DWORD Width, DWORD Height, RS_FORMAT format, bool isDynamic, DWORD dwMipMapLevelNum ) : Texture() { m_dwWidth = Width; m_dwHeight = Height; m_Format = format; m_dwMipMapLevelNum = dwMipMapLevelNum; if( format == RSFMT_A8R8G8B8 ) { m_GLInternalFormat = GL_RGBA8; m_GLFormat = GL_RGBA; m_dwPixelStride = 4; } else { m_GLInternalFormat = GL_RGBA8; m_GLFormat = GL_RGBA; m_dwPixelStride = 4; ::OutputDebugString( "OpenGLTexture::OpenGLTexture() --> Invalid texture format (reverting to default 32 BPP) !"); }

// Build GL texture //-- glGenTextures( 1, &m_GLTextureID ); if ( GL_NO_ERROR != glGetError() ) { ::OutputDebugString( "OpenGLTexture::OpenGLTexture() --> glGenTextures failed"); }

m_bIsDynamic = isDynamic; m_bIsLocked = false; m_pBuffer = NULL; m_pCurrentPixel = (unsigned char*)m_pBuffer; }

OpenGLTexture::~OpenGLTexture() { // Free the GL texture //-- glDeleteTextures( 1, &m_GLTextureID ); }

bool OpenGLTexture::Lock() { // Allocate memory for pixels //-- m_pBuffer = (void*)(new unsigned char[ m_dwPixelStride*m_dwWidth*m_dwHeight ]); ZeroMemory( m_pBuffer, m_dwPixelStride*m_dwWidth*m_dwHeight );

m_pCurrentPixel= (unsigned char*)m_pBuffer; m_bIsLocked = true;

return true; }

bool OpenGLTexture::Unlock() { m_bIsLocked = false;

if ( m_pBuffer!=NULL ) { // Build GL texture //-- glBindTexture( GL_TEXTURE_2D, m_GLTextureID ); if ( GL_NO_ERROR != glGetError() ) { ::OutputDebugString( "OpenGLTexture::OpenGLTexture() --> glBindTexture failed"); }

// Carefull, when adding support for new pixel formats, // not sure about following lines. //-- GLuint components = (m_GLInternalFormat==GL_RGB5?3:4); int res = gluBuild2DMipmaps( GL_TEXTURE_2D, components, m_dwWidth, m_dwHeight, m_GLFormat, GL_UNSIGNED_BYTE, m_pBuffer );

if ( res!=0 ) { ::OutputDebugString( "OpenGLTexture::OpenGLTexture() --> glTexImage2D failed"); }

// Free system mem that we allocated //-- delete [] m_pBuffer; m_pBuffer = NULL; m_pCurrentPixel= (unsigned char*)m_pBuffer; }

return true; }

void* OpenGLTexture::GetData() { return m_pBuffer; }

bool OpenGLTexture::Use() { glEnable( GL_TEXTURE_2D ); glBindTexture( GL_TEXTURE_2D, m_GLTextureID ); return true; }

#endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/ChildFrm.h] - (1,397 bytes)

// ChildFrm.h : interface of the CChildFrame class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_CHILDFRM_H__462B01B3_11D2_41EC_B5DD_B6BBE36C9552__INCLUDED_)
#define AFX_CHILDFRM_H__462B01B3_11D2_41EC_B5DD_B6BBE36C9552__INCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000

class CChildFrame : public CMDIChildWnd { DECLARE_DYNCREATE(CChildFrame) public: CChildFrame();

// Attributes public:

// Operations public:

// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CChildFrame) virtual BOOL PreCreateWindow(CREATESTRUCT& cs); //}}AFX_VIRTUAL // Implementation public: virtual ~CChildFrame(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif

// Generated message map functions protected: //{{AFX_MSG(CChildFrame) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG DECLARE_MESSAGE_MAP() };

///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_CHILDFRM_H__462B01B3_11D2_41EC_B5DD_B6BBE36C9552__INCLUDED_)

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/RenderSystemDoc.h] - (1,607 bytes)

// D3DRendererTestAppDoc.h : interface of the CD3DRendererTestAppDoc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_D3DRENDERERTESTAPPDOC_H__037EB3F3_A591_478F_937A_CFA4642DAC2D__INCLUDED_)
#define AFX_D3DRENDERERTESTAPPDOC_H__037EB3F3_A591_478F_937A_CFA4642DAC2D__INCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000

class CD3DRendererTestAppDoc : public CDocument { protected: // create from serialization only CD3DRendererTestAppDoc(); DECLARE_DYNCREATE(CD3DRendererTestAppDoc)

// Attributes public:

// Operations public:

// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CD3DRendererTestAppDoc) public: virtual BOOL OnNewDocument(); virtual void Serialize(CArchive& ar); //}}AFX_VIRTUAL // Implementation public: virtual ~CD3DRendererTestAppDoc(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif

protected:

// Generated message map functions protected: //{{AFX_MSG(CD3DRendererTestAppDoc) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() };

///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_D3DRENDERERTESTAPPDOC_H__037EB3F3_A591_478F_937A_CFA4642DAC2D__INCLUDED_)

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/Resource.h] - (976 bytes)

//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by RenderSystemApp.rc
//
#define IDD_ABOUTBOX                    100
#define ID_STATUS_TEXT                  101
#define IDR_MAINFRAME                   128
#define IDR_D3DRENTYPE                  129
#define ID_VIEW_DIRECTX                 32771
#define ID_VIEW_OPENGL                  32772
#define ID_SWITCH_TO_D3D                32773
#define ID_SWITCH_TO_OGL                32774
#define ID_TEXTURE_CHECKER              32775
#define ID_TEXTURE_PLASMA               32776
#define ID_TEXTURE_ANIMATED             32777

// Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_3D_CONTROLS 1 #define _APS_NEXT_RESOURCE_VALUE 130 #define _APS_NEXT_COMMAND_VALUE 32778 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 102 #endif #endif

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/RenderSystemView.cpp] - (14,464 bytes)

// D3DRendererTestAppView.cpp : implementation of the CD3DRendererTestAppView class
//

#include "RenderSystem.h"
#include "RenderSystemDoc.h"
#include "RenderSystemView.h"

#include "MainFrm.h" #include <cassert> #include <math.h>

#include "mmsystem.h" // timeGetTime() #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppView IMPLEMENT_DYNCREATE(CD3DRendererTestAppView, CView)

BEGIN_MESSAGE_MAP(CD3DRendererTestAppView, CView) //{{AFX_MSG_MAP(CD3DRendererTestAppView) ON_WM_CREATE() ON_WM_DESTROY() ON_COMMAND(ID_VIEW_DIRECTX, OnViewDirectx) ON_UPDATE_COMMAND_UI(ID_VIEW_DIRECTX, OnUpdateViewDirectx) ON_COMMAND(ID_VIEW_OPENGL, OnViewOpengl) ON_UPDATE_COMMAND_UI(ID_VIEW_OPENGL, OnUpdateViewOpengl) ON_WM_CHAR() ON_COMMAND(ID_SWITCH_TO_D3D, OnSwitchToD3d) ON_COMMAND(ID_SWITCH_TO_OGL, OnSwitchToOgl) ON_UPDATE_COMMAND_UI(ID_SWITCH_TO_D3D, OnUpdateSwitchToD3d) ON_UPDATE_COMMAND_UI(ID_SWITCH_TO_OGL, OnUpdateSwitchToOgl) ON_WM_TIMER() ON_WM_SIZE() ON_COMMAND(ID_TEXTURE_PLASMA, OnTexturePlasma) ON_UPDATE_COMMAND_UI(ID_TEXTURE_PLASMA, OnUpdateTexturePlasma) ON_COMMAND(ID_TEXTURE_CHECKER, OnTextureChecker) ON_UPDATE_COMMAND_UI(ID_TEXTURE_CHECKER, OnUpdateTextureChecker) //}}AFX_MSG_MAP END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppView construction/destruction CD3DRendererTestAppView::CD3DRendererTestAppView() { pRenderDevice = NULL; pSphere = NULL; pTexture = NULL;

m_bUseOpenGL = false;

#ifndef COMPILE_OPENGL_IMPLEMENTATION m_bUseOpenGL = true; #endif

#ifndef COMPILE_DIRECT3D_IMPLEMENTATION m_bUseOpenGL = false; #endif }

CD3DRendererTestAppView::~CD3DRendererTestAppView() { ReleaseRenderer(); }

bool CD3DRendererTestAppView::CreateRenderer() { assert( (pRenderDevice==NULL) && "CD3DRendererTestAppView::CreateRenderer() --> Attempting to create renderer while this one is not NULL." );

CClientDC dc(this);

RECT rc; GetClientRect( &rc );

// Build renderer //-- DWORD width = ( rc.right-rc.left<=0 ? 1024 : rc.right-rc.left ); DWORD height = ( rc.bottom-rc.top<=0 ? 1024 : rc.bottom-rc.top ); HWND hWnd = GetSafeHwnd();

pRenderDevice = new RenderDevice();

if ( !m_bUseOpenGL ) { #ifdef COMPILE_DIRECT3D_IMPLEMENTATION pRenderDevice->UseD3D( hWnd, width, height ); #else pRenderDevice->UseOpenGL( hWnd, width, height ); #endif } else { #ifdef COMPILE_OPENGL_IMPLEMENTATION pRenderDevice->UseOpenGL( hWnd, width, height ); #else pRenderDevice->UseD3D( hWnd, width, height ); #endif }

// Now put the renderer in some initial state //-- // Build a view and a projection matrix //-- Matrix4x4 proj; proj.SetPerspective(0.1f, 1000.0f, 45.0f, (float)(width)/(float)(height) ); pRenderDevice->MatrixModeSet( RSMM_PROJECTION ); pRenderDevice->MatrixLoad( proj ); pRenderDevice->MatrixModeSet( RSMM_MODELVIEW ); pRenderDevice->MatrixLoadIdentity();

pRenderDevice->SetRenderState( RSRS_LIGHTINGENABLE, 0 ); pRenderDevice->SetRenderState( RSRS_CULLINGENABLE, 1 ); pRenderDevice->SetRenderState( RSRS_FILLMODE, RSFILL_SOLID );

return true; }

bool CD3DRendererTestAppView::CreateVertexBuffer() { if (pRenderDevice!=NULL) { pSphere = new Sphere( pRenderDevice, 1.5f, 50 ); } else { return false; }

return true; }

bool CD3DRendererTestAppView::CreateTexture( DWORD dwGenMethod ) { // Create texture //-- pTexture = pRenderDevice->CreateTexture( 128, 128, RSFMT_A8R8G8B8, false, 1 );

// Fill it with some data //-- if ( pTexture->Lock() ) { DWORD i=0, j=0; DWORD *pPixels = (DWORD*)pTexture->GetData(); DWORD dwPitch = pTexture->GetPitch(); DWORD dwWidth = pTexture->GetWidth(); DWORD dwHeight = pTexture->GetHeight();

if ( dwGenMethod==1 ) { for ( j=0; j<dwHeight; j++ ) { for (i=0; i<dwWidth; i++) { BYTE r=0; BYTE g=0; BYTE b=0;

// Do a kind of plasma //-- r = (BYTE)( 0.9f*(127.0f + 127.0f*cosf( 5*j*3.1415f/dwWidth )*sinf( 2*i*3.1415f/dwWidth ) )); g = (BYTE)( 0.8f*(127.0f + 127.0f*sinf( 0.2f*j*3.1415f/dwWidth + i*3.1415f/dwWidth )*sinf( 2*j*i*3.1415f/(dwWidth*dwWidth) - 0.18f )*cosf( 2*(j+i)*3.1415f/dwWidth - 0.18f )) ); b = (BYTE)( 0.6f*(127.0f + 127.0f*sinf( 3*i*3.1415f/dwWidth )*sinf( 2*j*3.1415f/dwWidth - 0.18f )*sinf( 2*j*3.1415f/dwWidth - 0.18f ) ) );

pTexture->SetPixel4uc( 255, r, g, b ); } } } else if ( dwGenMethod==0 ) { for ( j=0; j<dwHeight; j++ ) { for (i=0; i<dwWidth; i++) { BYTE r=0; BYTE g=0; BYTE b=0;

// Do a 2x2 checker //-- if ( i < dwWidth/2 ) r=0; else r=255;

if ( j < dwHeight/2 ) g=255; else g=0;

pTexture->SetPixel4uc( 255, r, g, b ); }

pPixels += (dwPitch/4-dwWidth); } }

pTexture->Unlock(); } else { return false; }

return true; }

bool CD3DRendererTestAppView::SwapRenderers() { // First release renderer //-- ReleaseRenderer();

// Then Update flag //-- m_bUseOpenGL = !m_bUseOpenGL;

// Finally create new renderer //-- bool bSucceeded = false; bSucceeded = CreateRenderer(); bSucceeded = bSucceeded && CreateVertexBuffer(); bSucceeded = bSucceeded && CreateTexture();

// Update the status bar of main frame window and repaint the view //-- UpdateMainFrameStatusBar();

return bSucceeded; }

void CD3DRendererTestAppView::ReleaseRenderer() { SAFE_DELETE( pTexture ); SAFE_DELETE( pSphere ); SAFE_DELETE( pRenderDevice ); }

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppView diagnostics #ifdef _DEBUG void CD3DRendererTestAppView::AssertValid() const { CView::AssertValid(); }

void CD3DRendererTestAppView::Dump(CDumpContext& dc) const { CView::Dump(dc); }

CD3DRendererTestAppDoc* CD3DRendererTestAppView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CD3DRendererTestAppDoc))); return (CD3DRendererTestAppDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppView message handlers int CD3DRendererTestAppView::OnCreate(LPCREATESTRUCT lpCreateStruct) { lpCreateStruct->style |= (CS_OWNDC | CS_HREDRAW /*| CS_DRAW */ );

if (CView::OnCreate(lpCreateStruct) == -1) return -1;

// Create timer //-- pRefreshTimer = SetTimer( 69, 10, 0 ); // Create renderer //-- SwapRenderers();

return 0; }

void CD3DRendererTestAppView::OnDestroy() { KillTimer( 69 ); CView::OnDestroy(); }

void CD3DRendererTestAppView::OnDraw(CDC* pDC) { RECT clientRect; GetClientRect( &clientRect );

int width = clientRect.right - clientRect.left; int height = clientRect.bottom - clientRect.top;

if ( pRenderDevice==NULL ) { pDC->SetBkColor( 0xFFFF0000 ); } else { // Start drawing the scene //-- pRenderDevice->BeginRendering();

// Clear screen //-- pRenderDevice->SetViewport( 0, 0, width, height ); pRenderDevice->SetBackgroundColor( 1, 0, 0, 0 ); pRenderDevice->Clear( RSCLR_COLOR | RSCLR_ZBUFFER );

// Set view transformation //-- pRenderDevice->MatrixModeSet( RSMM_MODELVIEW ); pRenderDevice->MatrixLoadIdentity(); pRenderDevice->MatrixTranslation( 1, 0, -5.0f );

// Draw vertex buffer content 4 times //-- pRenderDevice->SetViewport( 0, 0, width/2, height/2 ); pRenderDevice->MatrixRotation( timeGetTime()/30.0f, 0, 0, 1 ); pRenderDevice->SetBackgroundColor( 1, 1, 0, 0 ); pRenderDevice->Clear( RSCLR_COLOR | RSCLR_ZBUFFER ); pRenderDevice->UseTexture( pTexture ); pSphere->Draw( pRenderDevice );

pRenderDevice->SetViewport( width/2, height/2, width/2, height/2 ); pRenderDevice->MatrixRotation( timeGetTime()/30.0f, 0, 0.5, 0.5 ); pRenderDevice->SetBackgroundColor( 1, 1, 1, 0 ); pRenderDevice->Clear( RSCLR_COLOR | RSCLR_ZBUFFER ); pRenderDevice->UseTexture( pTexture ); pSphere->Draw( pRenderDevice ); pRenderDevice->SetViewport( width/2, 0, width/2, height/2 ); pRenderDevice->MatrixRotation( timeGetTime()/30.0f, 1, 0, 0 ); pRenderDevice->SetBackgroundColor( 1, 1, 0, 1 ); pRenderDevice->Clear( RSCLR_COLOR | RSCLR_ZBUFFER ); pRenderDevice->UseTexture( pTexture ); pSphere->Draw( pRenderDevice );

pRenderDevice->SetViewport( 0, height/2, width/2, height/2 ); pRenderDevice->MatrixRotation( timeGetTime()/30.0f, 0, 1, 0 ); pRenderDevice->SetBackgroundColor( 1, 0, 1, 0 ); pRenderDevice->Clear( RSCLR_COLOR | RSCLR_ZBUFFER ); pRenderDevice->UseTexture( pTexture ); pSphere->Draw( pRenderDevice );

// Finished drawing the scene //-- pRenderDevice->EndRendering();

// Display backbuffer //-- pRenderDevice->SwapBuffers(); }

CView::OnDraw( pDC ); }

void CD3DRendererTestAppView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) { CView::CalcWindowRect(lpClientRect, nAdjustType); }

void CD3DRendererTestAppView::OnViewDirectx() { // Do something only if we are in OpenGL mode //-- if ( m_bUseOpenGL ) { SwapRenderers();

CWnd* pMain = AfxGetMainWnd(); pMain->SendMessage( WM_ERASEBKGND ); pMain->SendMessage( WM_PAINT ); PostMessage( WM_PAINT ); } }

void CD3DRendererTestAppView::OnUpdateViewDirectx(CCmdUI* pCmdUI) { // Update menu //-- CWnd* pMain = AfxGetMainWnd(); CMenu* pMenu = ( pMain==NULL ? NULL : pMain->GetMenu()->GetSubMenu(1) );

if (pMenu != NULL && pMenu->GetMenuItemCount() > 0) { if (!m_bUseOpenGL) { pMenu->CheckMenuItem( ID_VIEW_DIRECTX, MF_CHECKED | MF_BYCOMMAND ); pMenu->CheckMenuItem( ID_VIEW_OPENGL, MF_UNCHECKED | MF_BYCOMMAND ); } else { pMenu->CheckMenuItem( ID_VIEW_DIRECTX, MF_UNCHECKED | MF_BYCOMMAND ); pMenu->CheckMenuItem( ID_VIEW_OPENGL, MF_CHECKED | MF_BYCOMMAND ); } }

pMain->DrawMenuBar(); }

void CD3DRendererTestAppView::OnViewOpengl() { // Do something only if we are in OpenGL mode //-- if ( !m_bUseOpenGL ) { SwapRenderers();

CWnd* pMain = AfxGetMainWnd(); pMain->SendMessage( WM_ERASEBKGND ); pMain->SendMessage( WM_PAINT ); PostMessage( WM_PAINT ); } }

void CD3DRendererTestAppView::OnUpdateViewOpengl(CCmdUI* pCmdUI) { // Update menu //-- CWnd* pMain = AfxGetMainWnd(); CMenu* pMenu = ( pMain==NULL ? NULL : pMain->GetMenu()->GetSubMenu(1) );

if (pMenu != NULL && pMenu->GetMenuItemCount() > 0) { if (!m_bUseOpenGL) { pMenu->CheckMenuItem( ID_VIEW_DIRECTX, MF_CHECKED | MF_BYCOMMAND ); pMenu->CheckMenuItem( ID_VIEW_OPENGL, MF_UNCHECKED | MF_BYCOMMAND ); } else { pMenu->CheckMenuItem( ID_VIEW_DIRECTX, MF_UNCHECKED | MF_BYCOMMAND ); pMenu->CheckMenuItem( ID_VIEW_OPENGL, MF_CHECKED | MF_BYCOMMAND ); } }

pMain->DrawMenuBar(); }

void CD3DRendererTestAppView::UpdateMainFrameStatusBar() { CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd(); if ( m_bUseOpenGL ) { pMain->SetStatusBarText( (pRenderDevice==NULL ? "Error creating OpenGL render device" : "Using OpenGL" ) ); } else { pMain->SetStatusBarText( (pRenderDevice==NULL ? "Error creating Direct3D render device" : "Using Direct3D" ) ); }

pMain->PostMessage( WM_PAINT ); PostMessage( WM_PAINT ); }

void CD3DRendererTestAppView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { switch (nChar) { case 'w': pRenderDevice->SetRenderState( RSRS_FILLMODE, RSFILL_WIREFRAME ); break; case 'W': pRenderDevice->SetRenderState( RSRS_FILLMODE, RSFILL_SOLID ); break; case 'c': pRenderDevice->SetRenderState( RSRS_CULLINGENABLE, 0 ); break; case 'C': pRenderDevice->SetRenderState( RSRS_CULLINGENABLE, 1 ); break; case 'l': pRenderDevice->SetRenderState( RSRS_LIGHTINGENABLE, 0 ); break; case 'L': pRenderDevice->SetRenderState( RSRS_LIGHTINGENABLE, 1 ); break; } CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd(); pMain->PostMessage( WM_PAINT ); PostMessage( WM_PAINT );

CView::OnChar(nChar, nRepCnt, nFlags); }

void CD3DRendererTestAppView::OnSwitchToD3d() { OnViewDirectx(); }

void CD3DRendererTestAppView::OnSwitchToOgl() { OnViewOpengl(); }

void CD3DRendererTestAppView::OnUpdateSwitchToD3d(CCmdUI* pCmdUI) { OnUpdateViewDirectx( pCmdUI ); }

void CD3DRendererTestAppView::OnUpdateSwitchToOgl(CCmdUI* pCmdUI) { OnUpdateViewOpengl( pCmdUI ); }

void CD3DRendererTestAppView::OnTimer(UINT nIDEvent) { if ( nIDEvent==69 ) // Refresh timer { CMainFrame* pMain = (CMainFrame*)AfxGetMainWnd(); pMain->PostMessage( WM_PAINT ); PostMessage( WM_PAINT ); } CView::OnTimer(nIDEvent); }

void CD3DRendererTestAppView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy);

// Rebuild the renderer (without swapping Api -> m_bUseOpenGL = !m_bUseOpenGL) //-- m_bUseOpenGL = !m_bUseOpenGL; SwapRenderers(); }

void CD3DRendererTestAppView::OnTexturePlasma() { SAFE_DELETE( pTexture ); CreateTexture( 0 ); }

void CD3DRendererTestAppView::OnUpdateTexturePlasma(CCmdUI* pCmdUI) { // Update menu //-- CWnd* pMain = AfxGetMainWnd(); CMenu* pMenu = ( pMain==NULL ? NULL : pMain->GetMenu()->GetSubMenu(2) );

if (pMenu != NULL && pMenu->GetMenuItemCount() > 0) { pMenu->CheckMenuItem( ID_TEXTURE_PLASMA, MF_CHECKED | MF_BYCOMMAND ); pMenu->CheckMenuItem( ID_TEXTURE_CHECKER, MF_UNCHECKED | MF_BYCOMMAND ); }

pMain->DrawMenuBar(); }

void CD3DRendererTestAppView::OnTextureChecker() { SAFE_DELETE( pTexture ); CreateTexture( 1 ); }

void CD3DRendererTestAppView::OnUpdateTextureChecker(CCmdUI* pCmdUI) { // Update menu //-- CWnd* pMain = AfxGetMainWnd(); CMenu* pMenu = ( pMain==NULL ? NULL : pMain->GetMenu()->GetSubMenu(2) );

if (pMenu != NULL && pMenu->GetMenuItemCount() > 0) { pMenu->CheckMenuItem( ID_TEXTURE_PLASMA, MF_UNCHECKED | MF_BYCOMMAND ); pMenu->CheckMenuItem( ID_TEXTURE_CHECKER, MF_CHECKED | MF_BYCOMMAND ); }

pMain->DrawMenuBar(); }

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/StdAfx.cpp] - (220 bytes)

// stdafx.cpp : source file that includes just the standard includes
//	D3DRendererTestApp.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"




Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/RenderSystemView.h] - (2,998 bytes)

// D3DRendererTestAppView.h : interface of the CD3DRendererTestAppView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_D3DRENDERERTESTAPPVIEW_H__B8F4D2AC_CE4C_4510_A01D_92C011E353B4__INCLUDED_)
#define AFX_D3DRENDERERTESTAPPVIEW_H__B8F4D2AC_CE4C_4510_A01D_92C011E353B4__INCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <afxwin.h> // MFC core and standard components #include <windows.h>

#include "../RenderSystem/RenderDevice.h" #include "../RenderSystem/Sphere.h"

class CD3DRendererTestAppView : public CView { protected: UINT pRefreshTimer;

RenderDevice* pRenderDevice; Sphere* pSphere; Texture* pTexture;

bool m_bUseOpenGL;

bool CreateRenderer(); void ReleaseRenderer(); bool SwapRenderers();

bool CreateVertexBuffer(); bool CreateTexture( DWORD dwGenMethod=0 );

protected: // create from serialization only CD3DRendererTestAppView(); DECLARE_DYNCREATE(CD3DRendererTestAppView)

// Attributes public: CD3DRendererTestAppDoc* GetDocument();

// Operations public:

// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CD3DRendererTestAppView) protected: virtual void OnDraw(CDC* pDC); virtual void CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType = adjustBorder); //}}AFX_VIRTUAL // Implementation public: virtual ~CD3DRendererTestAppView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif

public:

// Generated message map functions protected: void UpdateMainFrameStatusBar(); //{{AFX_MSG(CD3DRendererTestAppView) afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDestroy(); afx_msg void OnViewDirectx(); afx_msg void OnUpdateViewDirectx(CCmdUI* pCmdUI); afx_msg void OnViewOpengl(); afx_msg void OnUpdateViewOpengl(CCmdUI* pCmdUI); afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); afx_msg void OnSwitchToD3d(); afx_msg void OnSwitchToOgl(); afx_msg void OnUpdateSwitchToD3d(CCmdUI* pCmdUI); afx_msg void OnUpdateSwitchToOgl(CCmdUI* pCmdUI); afx_msg void OnTimer(UINT nIDEvent); afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg void OnTexturePlasma(); afx_msg void OnUpdateTexturePlasma(CCmdUI* pCmdUI); afx_msg void OnTextureChecker(); afx_msg void OnUpdateTextureChecker(CCmdUI* pCmdUI); //}}AFX_MSG DECLARE_MESSAGE_MAP() };

#ifndef _DEBUG // debug version in D3DRendererTestAppView.cpp inline CD3DRendererTestAppDoc* CD3DRendererTestAppView::GetDocument() { return (CD3DRendererTestAppDoc*)m_pDocument; } #endif

///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_D3DRENDERERTESTAPPVIEW_H__B8F4D2AC_CE4C_4510_A01D_92C011E353B4__INCLUDED_)

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/StdAfx.h] - (1,019 bytes)

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__B6E3FDA3_0C9A_4092_8299_BACB2B309570__INCLUDED_)
#define AFX_STDAFX_H__B6E3FDA3_0C9A_4092_8299_BACB2B309570__INCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC extensions #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls #ifndef _AFX_NO_AFXCMN_SUPPORT #include <afxcmn.h> // MFC support for Windows Common Controls #endif // _AFX_NO_AFXCMN_SUPPORT #include <cassert>

//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STDAFX_H__B6E3FDA3_0C9A_4092_8299_BACB2B309570__INCLUDED_)

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/RenderSystem.h] - (1,496 bytes)

// D3DRendererTestApp.h : main header file for the D3DRENDERERTESTAPP application
//

#if !defined(AFX_D3DRENDERERTESTAPP_H__ED1E5A17_A0A0_4AEE_BC01_9DB1FED5886F__INCLUDED_)
#define AFX_D3DRENDERERTESTAPP_H__ED1E5A17_A0A0_4AEE_BC01_9DB1FED5886F__INCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <afxwin.h> // MFC core and standard components #include "resource.h" // main symbols ///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppApp: // See D3DRendererTestApp.cpp for the implementation of this class // class CD3DRendererTestAppApp : public CWinApp { public: CD3DRendererTestAppApp();

// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CD3DRendererTestAppApp) public: virtual BOOL InitInstance(); virtual BOOL OnIdle(LONG lCount); //}}AFX_VIRTUAL // Implementation //{{AFX_MSG(CD3DRendererTestAppApp) afx_msg void OnAppAbout(); // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() };

///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_D3DRENDERERTESTAPP_H__ED1E5A17_A0A0_4AEE_BC01_9DB1FED5886F__INCLUDED_)

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/ChildFrm.cpp] - (1,516 bytes)

// ChildFrm.cpp : implementation of the CChildFrame class
//

#include "RenderSystem.h"
#include "ChildFrm.h"

#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif

///////////////////////////////////////////////////////////////////////////// // CChildFrame IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)

BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd) //{{AFX_MSG_MAP(CChildFrame) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG_MAP END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////// // CChildFrame construction/destruction CChildFrame::CChildFrame() { // TODO: add member initialization code here }

CChildFrame::~CChildFrame() { }

BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs if( !CMDIChildWnd::PreCreateWindow(cs) ) return FALSE;

return TRUE; }



///////////////////////////////////////////////////////////////////////////// // CChildFrame diagnostics #ifdef _DEBUG void CChildFrame::AssertValid() const { CMDIChildWnd::AssertValid(); }

void CChildFrame::Dump(CDumpContext& dc) const { CMDIChildWnd::Dump(dc); }

#endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CChildFrame message handlers

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/MainFrm.cpp] - (1,922 bytes)

// MainFrm.cpp : implementation of the CMainFrame class
//

#include "RenderSystem.h"
#include "MainFrm.h"

#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif

///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP()

static UINT indicators[] = { ID_STATUS_TEXT, // status line indicator };

///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here }

CMainFrame::~CMainFrame() { }

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;

if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create }

return 0; }

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CMDIFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; }

///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CMDIFrameWnd::AssertValid(); }

void CMainFrame::Dump(CDumpContext& dc) const { CMDIFrameWnd::Dump(dc); }

#endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers

void CMainFrame::SetStatusBarText(const char *pText) { m_wndStatusBar.SetPaneText( 0, pText ); }

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/RenderSystem.cpp] - (4,603 bytes)

// D3DRendererTestApp.cpp : Defines the class behaviors for the application.
//

#include "RenderSystem.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "RenderSystemDoc.h"
#include "RenderSystemView.h"

#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppApp BEGIN_MESSAGE_MAP(CD3DRendererTestAppApp, CWinApp) //{{AFX_MSG_MAP(CD3DRendererTestAppApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP // Standard file based document commands ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppApp construction CD3DRendererTestAppApp::CD3DRendererTestAppApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance }

///////////////////////////////////////////////////////////////////////////// // The one and only CD3DRendererTestAppApp object CD3DRendererTestAppApp theApp;

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppApp initialization BOOL CD3DRendererTestAppApp::InitInstance() { // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif

// Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications"));

LoadStdProfileSettings(0); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_D3DRENTYPE, RUNTIME_CLASS(CD3DRendererTestAppDoc), RUNTIME_CLASS(CChildFrame), // custom MDI child frame RUNTIME_CLASS(CD3DRendererTestAppView)); AddDocTemplate(pDocTemplate);

// create main MDI Frame window CMainFrame* pMainFrame = new CMainFrame; if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; m_pMainWnd = pMainFrame;

// Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE;

// The main window has been initialized, so show and update it. pMainFrame->ShowWindow(m_nCmdShow); pMainFrame->UpdateWindow();

return TRUE; }

///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg();

// Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) // No message handlers //}}AFX_MSG DECLARE_MESSAGE_MAP() };

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT }

void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP }

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP()

// App command to run the dialog void CD3DRendererTestAppApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); }

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppApp message handlers

BOOL CD3DRendererTestAppApp::OnIdle(LONG lCount) { // TODO: Add your specialized code here and/or call the base class return CWinApp::OnIdle(lCount); }

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/RenderSystemDoc.cpp] - (1,947 bytes)

// D3DRendererTestAppDoc.cpp : implementation of the CD3DRendererTestAppDoc class
//

#include "RenderSystem.h"
#include "RenderSystemDoc.h"

#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppDoc IMPLEMENT_DYNCREATE(CD3DRendererTestAppDoc, CDocument)

BEGIN_MESSAGE_MAP(CD3DRendererTestAppDoc, CDocument) //{{AFX_MSG_MAP(CD3DRendererTestAppDoc) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppDoc construction/destruction CD3DRendererTestAppDoc::CD3DRendererTestAppDoc() { // TODO: add one-time construction code here }

CD3DRendererTestAppDoc::~CD3DRendererTestAppDoc() { }

BOOL CD3DRendererTestAppDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE;

// TODO: add reinitialization code here // (SDI documents will reuse this document) return TRUE; }



///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppDoc serialization void CD3DRendererTestAppDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here } }

///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppDoc diagnostics #ifdef _DEBUG void CD3DRendererTestAppDoc::AssertValid() const { CDocument::AssertValid(); }

void CD3DRendererTestAppDoc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CD3DRendererTestAppDoc commands

Currently browsing [RenderSystem.zip] (241,705 bytes) - [Sources/App/MainFrm.h] - (1,510 bytes)

// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_MAINFRM_H__24F2126A_FDB4_4032_B9D8_48809BB598FC__INCLUDED_)
#define AFX_MAINFRM_H__24F2126A_FDB4_4032_B9D8_48809BB598FC__INCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <afxwin.h> // MFC core and standard components #include <afxext.h>

class CMainFrame : public CMDIFrameWnd { DECLARE_DYNAMIC(CMainFrame) public: CMainFrame();

// Attributes public:

// Operations public:

// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMainFrame) virtual BOOL PreCreateWindow(CREATESTRUCT& cs); //}}AFX_VIRTUAL // Implementation public: void SetStatusBarText( const char* pText ); virtual ~CMainFrame(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif

protected: // control bar embedded members CStatusBar m_wndStatusBar;

// Generated message map functions protected: //{{AFX_MSG(CMainFrame) afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); //}}AFX_MSG DECLARE_MESSAGE_MAP() };

///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_MAINFRM_H__24F2126A_FDB4_4032_B9D8_48809BB598FC__INCLUDED_)

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.