Added cShapeCircleSprite and cShapePolySprite ( sprites support for chipmunk wrapper ).

Fixed chipmunk wrapper default renderer.
This commit is contained in:
spartanj
2011-02-05 21:23:14 -03:00
parent 8b19225c22
commit c53f5e858c
15 changed files with 280 additions and 69 deletions

View File

@@ -197,5 +197,7 @@
#include "physics/constraints/cslidejoint.hpp"
#include "physics/moment.hpp"
#include "physics/area.hpp"
#include "physics/cshapepolysprite.hpp"
#include "physics/cshapecirclesprite.hpp"
using namespace EE::Physics;
#endif

View File

@@ -43,7 +43,7 @@ const GLchar * EEGL_SHADER_BASE_VS[] = {
#ifdef EE_GLES2
"uniform float dgl_PointSize = 1;\n",
#endif
"layout(location = 0) in vec2 dgl_Vertex;\n", // replaces deprecated gl_Vertex
"layout(location = 0) in vec4 dgl_Vertex;\n", // replaces deprecated gl_Vertex
"layout(location = 2) in vec4 dgl_Color;\n", // replaces deprecated gl_Color
"layout(location = 4) in vec2 dgl_TexCoord;\n", // replaces deprecated gl_TexCoord
"invariant out vec4 Color;\n", // to fragment shader
@@ -55,8 +55,7 @@ const GLchar * EEGL_SHADER_BASE_VS[] = {
#endif
" Color = dgl_Color;\n",
" TexCoord = dgl_TexCoord;\n",
" vec4 v4 = vec4( dgl_Vertex, 0.0, 1.0 );\n",
" vec4 vEye = dgl_ModelViewMatrix * v4;\n",
" vec4 vEye = dgl_ModelViewMatrix * dgl_Vertex;\n",
" gl_Position = dgl_ProjectionMatrix * vEye;\n",
" if ( 1 == dgl_ClippingEnabled ) {\n",
" for ( int i = 0; i < MAX_CLIP_PLANES; i++ ) {\n",
@@ -287,8 +286,8 @@ void cRendererGL3::Init() {
glGenBuffers( EEGL_ARRAY_STATES_COUNT, &mVBO[0] );
//"in vec2 dgl_Vertex;",
glBindBuffer(GL_ARRAY_BUFFER, mVBO[ EEGL_VERTEX_ARRAY ] );
glBufferData(GL_ARRAY_BUFFER, 131072, NULL, GL_STREAM_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, mVBO[ EEGL_VERTEX_ARRAY ] );
glBufferData( GL_ARRAY_BUFFER, 131072, NULL, GL_STREAM_DRAW );
//"in vec4 dgl_Color;",
glBindBuffer( GL_ARRAY_BUFFER, mVBO[ EEGL_COLOR_ARRAY ] );
@@ -299,6 +298,27 @@ void cRendererGL3::Init() {
glBufferData( GL_ARRAY_BUFFER, 131072, NULL, GL_STREAM_DRAW );
}
void cRendererGL3::UpdateMatrix() {
switch ( mCurrentMode ) {
case GL_PROJECTION:
{
if ( -1 != mProjectionMatrix_id ) {
mCurShader->SetUniformMatrix( mProjectionMatrix_id, &mProjectionMatrix.top()[0][0] );
}
break;
}
case GL_MODELVIEW:
{
if ( -1 != mModelViewMatrix_id ) {
mCurShader->SetUniformMatrix( mModelViewMatrix_id, &mModelViewMatrix.top()[0][0] );
}
break;
}
}
}
void cRendererGL3::PushMatrix() {
mCurMatrix->push( mCurMatrix->top() );
UpdateMatrix();
@@ -309,28 +329,16 @@ void cRendererGL3::PopMatrix() {
UpdateMatrix();
}
void cRendererGL3::UpdateMatrix() {
switch ( mCurrentMode ) {
case GL_PROJECTION:
{
if ( -1 != mProjectionMatrix_id )
mCurShader->SetUniformMatrix( mProjectionMatrix_id, &mProjectionMatrix.top()[0][0] );
break;
}
case GL_MODELVIEW:
{
if ( -1 != mModelViewMatrix_id )
mCurShader->SetUniformMatrix( mModelViewMatrix_id, &mModelViewMatrix.top()[0][0] );
break;
}
}
}
void cRendererGL3::LoadIdentity() {
mCurMatrix->top() = glm::mat4(1.0);
UpdateMatrix();
}
void cRendererGL3::MultMatrixf ( const GLfloat * m ) {
mCurMatrix->top() *= glm::mat4( m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], m[12], m[13], m[14], m[15] );
UpdateMatrix();
}
void cRendererGL3::Translatef( GLfloat x, GLfloat y, GLfloat z ) {
mCurMatrix->top() *= glm::translate( glm::vec3( x, y, z ) );
UpdateMatrix();
@@ -346,6 +354,21 @@ void cRendererGL3::Scalef( GLfloat x, GLfloat y, GLfloat z ) {
UpdateMatrix();
}
void cRendererGL3::Ortho( GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar ) {
mCurMatrix->top() *= glm::ortho( left, right, bottom, top , zNear, zFar );
UpdateMatrix();
}
void cRendererGL3::LookAt( GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat upX, GLfloat upY, GLfloat upZ ) {
mCurMatrix->top() *= glm::lookAt( glm::vec3(eyeX, eyeY, eyeZ), glm::vec3(centerX, centerY, centerZ), glm::vec3(upX, upY, upZ) );
UpdateMatrix();
}
void cRendererGL3::Perspective ( GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar ) {
mCurMatrix->top() *= glm::perspective( fovy, aspect, zNear, zFar );
UpdateMatrix();
}
void cRendererGL3::MatrixMode(GLenum mode) {
mCurrentMode = mode;
@@ -363,21 +386,6 @@ void cRendererGL3::MatrixMode(GLenum mode) {
}
}
void cRendererGL3::Ortho( GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar ) {
mCurMatrix->top() *= glm::ortho( left, right, bottom, top , zNear, zFar );
UpdateMatrix();
}
void cRendererGL3::LookAt( GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat upX, GLfloat upY, GLfloat upZ ) {
mCurMatrix->top() *= glm::lookAt( glm::vec3(eyeX, eyeY, eyeZ), glm::vec3(centerX, centerY, centerZ), glm::vec3(upX, upY, upZ) );
UpdateMatrix();
}
void cRendererGL3::Perspective ( GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar ) {
mCurMatrix->top() *= glm::perspective( fovy, aspect, zNear, zFar );
UpdateMatrix();
}
void cRendererGL3::EnableClientState( GLenum array ) {
glEnableVertexAttribArray( array - GL_VERTEX_ARRAY );
}
@@ -517,10 +525,6 @@ void cRendererGL3::ClipPlane( GLenum plane, const GLdouble * equation ) {
glUniform4f( nplane, (GLfloat)teq[0], (GLfloat)teq[1], (GLfloat)teq[2], (GLfloat)teq[3] );
}
void cRendererGL3::MultMatrixf ( const GLfloat * m ) {
mCurMatrix->top() *= glm::mat4( *m );
}
GLfloat cRendererGL3::PointSize() {
return mPointSize;
}

View File

@@ -55,7 +55,6 @@ void cDampedSpring::Damping( const cpFloat& damping ) {
}
void cDampedSpring::Draw() {
//! FIXME: cDampedSpring::Draw()
#ifdef PHYSICS_RENDERER_ENABLED
cpDampedSpring * spring = (cpDampedSpring*)mConstraint;
cpBody * body_a = mConstraint->a;
@@ -64,7 +63,7 @@ void cDampedSpring::Draw() {
cVect a = tovect( cpvadd(body_a->p, cpvrotate(spring->anchr1, body_a->rot)) );
cVect b = tovect( cpvadd(body_b->p, cpvrotate(spring->anchr2, body_b->rot)) );
GLi->PointSize(5.0f);
GLi->PointSize( 5.0f );
cBatchRenderer * BR = cGlobalBatchRenderer::instance();
BR->SetTexture( NULL );
@@ -75,7 +74,13 @@ void cDampedSpring::Draw() {
cVect delta = b - a;
GLi->Disable( GL_TEXTURE_2D );
GLi->DisableClientState( GL_TEXTURE_COORD_ARRAY );
std::vector<eeColorA> tcolors( springVAR_count * 4, eeColorA( 0, 255, 0, 255 ) );
GLi->ColorPointer( 4, GL_UNSIGNED_BYTE, 0, reinterpret_cast<const GLvoid*>( &tcolors[0] ), pillVAR_count * sizeof(GLfloat) * 4 );
GLi->VertexPointer( 2, GL_FLOAT, 0, springVAR, springVAR_count * sizeof(GLfloat) * 2 );
GLi->PushMatrix();
GLfloat x = a.x;
@@ -85,15 +90,14 @@ void cDampedSpring::Draw() {
GLfloat s = 1.0f / cpvlength( tocpv( delta ) );
const GLfloat matrix[] = {
cos, sin, 0.0f, 0.0f,
-sin*s, cos*s, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
x, y, 0.0f, 1.0f,
cos , sin , 0.0f, 0.0f,
-sin * s, cos * s , 0.0f, 0.0f,
0.0f , 0.0f , 1.0f, 0.0f,
x , y , 0.0f, 1.0f,
};
GLi->MultMatrixf( matrix );
GLi->MultMatrixf(matrix);
GLi->DrawArrays(GL_LINE_STRIP, 0, springVAR_count);
GLi->DrawArrays( GL_LINE_STRIP, 0, springVAR_count );
GLi->PopMatrix();
#endif

View File

@@ -47,6 +47,7 @@ void cGrooveJoint::Draw() {
cBatchRenderer * BR = cGlobalBatchRenderer::instance();
cpFloat ps = BR->GetPointSize();
BR->SetTexture( NULL );
BR->SetPointSize( 5.0f );
BR->PointsBegin();
BR->PointSetColor( eeColorA( 128, 255, 128, 255 ) );

View File

@@ -46,6 +46,7 @@ void cPinJoint::Draw() {
cBatchRenderer * BR = cGlobalBatchRenderer::instance();
cpFloat ps = BR->GetPointSize();
BR->SetTexture( NULL );
BR->SetPointSize( 5.0f );
BR->PointsBegin();
BR->PointSetColor( eeColorA( 128, 255, 128, 255 ) );

View File

@@ -43,6 +43,7 @@ void cPivotJoint::Draw() {
cBatchRenderer * BR = cGlobalBatchRenderer::instance();
cpFloat ps = BR->GetPointSize();
BR->SetTexture( NULL );
BR->SetPointSize( 10.f );
BR->PointsBegin();
BR->PointSetColor( eeColorA( 128, 255, 128, 255 ) );

View File

@@ -55,6 +55,7 @@ void cSlideJoint::Draw() {
cBatchRenderer * BR = cGlobalBatchRenderer::instance();
cpFloat ps = BR->GetPointSize();
BR->SetTexture( NULL );
BR->SetPointSize( 5.0f );
BR->PointsBegin();
BR->PointSetColor( eeColorA( 128, 255, 128, 255 ) );

View File

@@ -19,7 +19,7 @@ class CP_API cShape {
cpShape * Shape() const;
~cShape();
virtual ~cShape();
cBody * Body() const;

View File

@@ -13,11 +13,11 @@ class CP_API cShapeCircle : public cShape {
cVect Offset();
void Offset( const cVect& offset );
virtual void Offset( const cVect& offset );
cpFloat Radius();
void Radius( const cpFloat& radius );
virtual void Radius( const cpFloat& radius );
virtual void Draw( cSpace * space );
};

View File

@@ -0,0 +1,53 @@
#include "cshapecirclesprite.hpp"
#ifdef PHYSICS_RENDERER_ENABLED
CP_NAMESPACE_BEGIN
cShapeCircleSprite * cShapeCircleSprite::New( cBody * body, cpFloat radius, cVect offset, cSprite * Sprite, bool AutoDeleteSprite ) {
return cpNew( cShapeCircleSprite, ( body, radius, offset, Sprite, AutoDeleteSprite ) );
}
cShapeCircleSprite::cShapeCircleSprite( cBody * body, cpFloat radius, cVect offset, cSprite * Sprite, bool AutoDeleteSprite ) :
cShapeCircle( body, radius, offset ),
mSprite( Sprite ),
mSpriteAutoDelete( AutoDeleteSprite )
{
OffsetSet();
}
cShapeCircleSprite::~cShapeCircleSprite() {
if ( mSpriteAutoDelete )
eeSAFE_DELETE( mSprite );
}
void cShapeCircleSprite::Draw( cSpace * space ) {
cVect Pos = Body()->Pos();
mSprite->Update( Pos.x, Pos.y, 1.0f, Body()->AngleDeg() );
mSprite->Draw();
}
void cShapeCircleSprite::OffsetSet() {
mSprite->UpdateSize( cShapeCircle::Radius() * 2, cShapeCircle::Radius() * 2 );
mSprite->OffSetX( -cShapeCircle::Radius() + cShapeCircle::Offset().x );
mSprite->OffSetY( -cShapeCircle::Radius() + cShapeCircle::Offset().y );
}
cSprite * cShapeCircleSprite::GetSprite() const {
return mSprite;
}
void cShapeCircleSprite::Radius( const cpFloat& radius ) {
cShapeCircle::Radius( radius );
OffsetSet();
}
void cShapeCircleSprite::Offset( const cVect &offset ) {
cShapeCircle::Offset( offset );
OffsetSet();
}
CP_NAMESPACE_END
#endif

View File

@@ -0,0 +1,39 @@
#ifndef CSHAPECIRCLESPRITE_HPP
#define CSHAPECIRCLESPRITE_HPP
#include "cshapecircle.hpp"
#ifdef PHYSICS_RENDERER_ENABLED
#include "../graphics/csprite.hpp"
using namespace EE::Graphics;
CP_NAMESPACE_BEGIN
class CP_API cShapeCircleSprite : public cShapeCircle {
public:
static cShapeCircleSprite * New( cBody * body, cpFloat radius, cVect offset, cSprite * Sprite, bool AutoDeleteSprite = false );
cShapeCircleSprite( cBody * body, cpFloat radius, cVect offset, cSprite * Sprite, bool AutoDeleteSprite = false );
virtual ~cShapeCircleSprite();
virtual void Draw( cSpace * space );
virtual void Radius( const cpFloat& radius );
virtual void Offset( const cVect &offset );
cSprite * GetSprite() const;
protected:
cSprite * mSprite;
bool mSpriteAutoDelete;
void OffsetSet();
};
CP_NAMESPACE_END
#endif
#endif

View File

@@ -0,0 +1,57 @@
#include "cshapepolysprite.hpp"
#ifdef PHYSICS_RENDERER_ENABLED
CP_NAMESPACE_BEGIN
cShapePolySprite * cShapePolySprite::New( cBody * body, int numVerts, cVect *verts, cVect offset, cSprite * Sprite, bool AutoDeleteSprite ) {
return cpNew( cShapePolySprite, ( body, numVerts, verts, offset, Sprite, AutoDeleteSprite ) );
}
cShapePolySprite * cShapePolySprite::New( cBody * body, cpFloat width, cpFloat height, cSprite * Sprite, bool AutoDeleteSprite ) {
return cpNew( cShapePolySprite, ( body, width, height, Sprite, AutoDeleteSprite ) );
}
cShapePolySprite::cShapePolySprite( cBody * body, int numVerts, cVect *verts, cVect offset, cSprite * Sprite, bool AutoDeleteSprite ) :
cShapePoly( body, numVerts, verts, offset ),
mSprite( Sprite ),
mSpriteAutoDelete( AutoDeleteSprite )
{
OffsetSet( Centroid( numVerts, verts ) );
}
cShapePolySprite::cShapePolySprite( cBody * body, cpFloat width, cpFloat height, cSprite * Sprite, bool AutoDeleteSprite ) :
cShapePoly( body, width, height ),
mSprite( Sprite ),
mSpriteAutoDelete( AutoDeleteSprite )
{
mSprite->UpdateSize( width, height );
OffsetSet( cVectNew( width / 2, height / 2 ) );
}
cShapePolySprite::~cShapePolySprite() {
if ( mSpriteAutoDelete )
eeSAFE_DELETE( mSprite );
}
void cShapePolySprite::Draw( cSpace * space ) {
cVect Pos = Body()->Pos();
mSprite->Update( Pos.x, Pos.y, 1.0f, Body()->AngleDeg() );
mSprite->Draw();
}
void cShapePolySprite::OffsetSet( cVect center ) {
cVect myCenter = cVectNew( ( mSprite->Width() / 2 ), ( mSprite->Height() / 2 ) );
mSprite->OffSetX( -myCenter.x + ( center.x - myCenter.x ) );
mSprite->OffSetY( -myCenter.y + ( center.y - myCenter.y ) );
}
cSprite * cShapePolySprite::GetSprite() const {
return mSprite;
}
CP_NAMESPACE_END
#endif

View File

@@ -0,0 +1,39 @@
#ifndef EE_PHYSICS_CSHAPEPOLYSPRITE_HPP
#define EE_PHYSICS_CSHAPEPOLYSPRITE_HPP
#include "cshapepoly.hpp"
#ifdef PHYSICS_RENDERER_ENABLED
#include "../graphics/csprite.hpp"
using namespace EE::Graphics;
CP_NAMESPACE_BEGIN
class CP_API cShapePolySprite : public cShapePoly {
public:
static cShapePolySprite * New( cBody * body, int numVerts, cVect *verts, cVect offset, cSprite * Sprite, bool AutoDeleteSprite = false );
static cShapePolySprite * New( cBody * body, cpFloat width, cpFloat height, cSprite * Sprite, bool AutoDeleteSprite = false );
cShapePolySprite( cBody * body, int numVerts, cVect *verts, cVect offset, cSprite * Sprite, bool AutoDeleteSprite = false );
cShapePolySprite( cBody * body, cpFloat width, cpFloat height, cSprite * Sprite, bool AutoDeleteSprite = false );
virtual ~cShapePolySprite();
virtual void Draw( cSpace * space );
cSprite * GetSprite() const;
protected:
cSprite * mSprite;
bool mSpriteAutoDelete;
void OffsetSet( cVect center );
};
CP_NAMESPACE_END
#endif
#endif

View File

@@ -55,7 +55,6 @@ cpFloat cShapeSegment::QueryHitDist( const cVect start, const cVect end, const c
}
void cShapeSegment::Draw( cSpace * space ) {
//! FIXME: cShapeSegment::Draw
#ifdef PHYSICS_RENDERER_ENABLED
cpSegmentShape * seg = (cpSegmentShape *)mShape;
cVect a = tovect( seg->CP_PRIVATE(ta) );
@@ -64,9 +63,9 @@ void cShapeSegment::Draw( cSpace * space ) {
if ( seg->CP_PRIVATE(r) ) {
GLi->Disable( GL_TEXTURE_2D );
GLi->DisableClientState( GL_TEXTURE_COORD_ARRAY );
GLi->DisableClientState( GL_COLOR_ARRAY );
GLi->VertexPointer( 3, GL_FLOAT, 0, pillVAR, pillVAR_count * sizeof(GLfloat) * 3 );
std::vector<eeColorA> tcolors( pillVAR_count * 4 );
GLi->PushMatrix();
cVect d = b - a;
@@ -78,29 +77,30 @@ void cShapeSegment::Draw( cSpace * space ) {
d.x, d.y, 0.0f, 0.0f,
a.x, a.y, 0.0f, 1.0f,
};
GLi->MultMatrixf(matrix);
if( !seg->CP_PRIVATE(shape).sensor ){
GLi->MultMatrixf( matrix );
GLi->VertexPointer( 3, GL_FLOAT, 0, pillVAR, pillVAR_count * sizeof(GLfloat) * 3 );
if( !seg->CP_PRIVATE(shape).sensor ) {
eeColorA C = ColorForShape( mShape, space->Space() );
/// TODO: Implement this fine
#ifndef EE_GLES2
glColor3ub( C.R(), C.B(), C.B() );
#endif
tcolors.assign( tcolors.size(), C );
GLi->DrawArrays(GL_TRIANGLE_FAN, 0, pillVAR_count);
GLi->ColorPointer( 4, GL_UNSIGNED_BYTE, 0, reinterpret_cast<const GLvoid*>( &tcolors[0] ), pillVAR_count * sizeof(GLfloat) * 4 );
GLi->DrawArrays( GL_TRIANGLE_FAN, 0, pillVAR_count );
}
#ifndef EE_GLES2
glColor3f( 0.4f, 0.4f, 0.4f );
#endif
tcolors.assign( tcolors.size(), eeColorA( 102, 102, 102, 255 ) );
GLi->ColorPointer( 4, GL_UNSIGNED_BYTE, 0, reinterpret_cast<const GLvoid*>( &tcolors[0] ), pillVAR_count * sizeof(GLfloat) * 4 );
GLi->DrawArrays( GL_LINE_LOOP, 0, pillVAR_count );
GLi->PopMatrix();
GLi->EnableClientState( GL_TEXTURE_COORD_ARRAY );
GLi->EnableClientState( GL_COLOR_ARRAY );
GLi->Enable( GL_TEXTURE_2D );
} else {
cPrimitives p;

View File

@@ -268,6 +268,8 @@ class cEETest : private cThread {
std::vector<physicDemo> mDemo;
Int32 mCurDemo;
cSprite * mBoxSprite;
cSprite * mCircleSprite;
};
void cEETest::CreateAquaTextureAtlas() {
@@ -1043,6 +1045,11 @@ void cEETest::LoadTextures() {
CreateTiling(Wireframe);
cGlobalShapeGroup::instance()->Add( eeNew( Graphics::cShape, ( TF->Load( MyPath + "data/aqua/aqua_button_ok.png" ), "aqua_button_ok" ) ) );
mBoxSprite = eeNew( cSprite, () );
mBoxSprite->CreateStatic( cGlobalShapeGroup::instance()->Add( eeNew( Graphics::cShape, ( TN[3], "ilmare" ) ) ) );
mCircleSprite = eeNew( cSprite, () );
mCircleSprite->CreateStatic( cGlobalShapeGroup::instance()->Add( eeNew( Graphics::cShape, ( TN[1], "thecircle" ) ) ) );
}
void cEETest::RandomizeHeights() {
@@ -1670,6 +1677,8 @@ void cEETest::End() {
eeSAFE_DELETE( mFBO );
eeSAFE_DELETE( mVBO );
eeSAFE_DELETE( mBlindyPtr );
eeSAFE_DELETE( mBoxSprite );
eeSAFE_DELETE( mCircleSprite );
cLog::instance()->Save();
@@ -1745,7 +1754,7 @@ void cEETest::Demo1Create() {
body = mSpace->AddBody( cBody::New( 1.0f, Moment::ForBox( 1.0f, 30.0f, 30.0f ) ) );
body->Pos( cVectNew( hw + j * 32 - i * 16, 100 + i * 32 ) );
shape = mSpace->AddShape( cShapePoly::New( body, 30.f, 30.f ) );
shape = mSpace->AddShape( cShapePolySprite::New( body, 30.f, 30.f, mBoxSprite ) );
shape->e( 0.0f );
shape->u( 0.8f );
}
@@ -1756,7 +1765,7 @@ void cEETest::Demo1Create() {
body = mSpace->AddBody( cBody::New( 10.0f, Moment::ForCircle( 10.0f, 0.0f, radius, cVectZero ) ) );
body->Pos( cVectNew( hw, EE->GetHeight() - radius - 5 ) );
shape = mSpace->AddShape( cShapeCircle::New( body, radius, cVectZero ) );
shape = mSpace->AddShape( cShapeCircleSprite::New( body, radius, cVectZero, mCircleSprite ) );
shape->e( 0.0f );
shape->u( 0.9f );
}