prepare to re-enable Gwen user interface (OpenGL_DebugFont) for OpenGL2.x

prepare to add improved btGeneric6DofSpringConstraint
This commit is contained in:
Erwin Coumans 2014-06-30 12:09:59 -07:00
parent 063a0344ac
commit 791b5149f6
7 changed files with 458 additions and 7 deletions

View File

@ -318,7 +318,7 @@ void GwenUserInterface::draw(int width, int height)
{
m_data->pCanvas->SetSize(width,height);
m_data->m_primRenderer->setScreenSize(width,height);
m_data->pRenderer->resize(width,height);
m_data->pRenderer->Resize(width,height);
m_data->pCanvas->RenderCanvas();
//restoreOpenGLState();
}

View File

@ -74,6 +74,7 @@ namespace Gwen
virtual Gwen::Point MeasureText( Gwen::Font* pFont, const Gwen::String& text );
virtual void RenderText( Gwen::Font* pFont, Gwen::Point pos, const Gwen::String& text );
virtual void Resize(int width, int height)=0;
public:
//

View File

@ -0,0 +1,405 @@
#include "OpenGL_DebugFont.h"
#include "Gwen/Utility.h"
#include "Gwen/Font.h"
#include "Gwen/Texture.h"
#include <math.h>
#include "OpenGLWindow/GlewWindows/GL/glew.h"
#include "FontData.h"
//saved OpenGL settings
GLfloat m_PrevLineWidth;
GLint m_PrevTexEnv;
GLint m_PrevPolygonMode[2];
GLint m_MaxClipPlanes;
GLint m_PrevTexture;
GLint m_PrevArrayBufferARB;
GLint m_PrevElementArrayBufferARB;
GLboolean m_PrevVertexProgramARB;
GLboolean m_PrevFragmentProgramARB;
GLuint m_PrevProgramObjectARB;
GLboolean m_PrevTexture3D;
GLboolean m_PrevActiveTexture1D[32];
GLboolean m_PrevActiveTexture2D[32];
GLboolean m_PrevActiveTexture3D[32];
GLint m_PrevActiveTextureARB;
bool m_SupportTexRect;
GLboolean m_PrevTexRectARB;
GLint m_PrevBlendEquation;
GLint m_PrevBlendEquationRGB;
GLint m_PrevBlendEquationAlpha;
GLint m_PrevBlendSrcRGB;
GLint m_PrevBlendDstRGB;
GLint m_PrevBlendSrcAlpha;
GLint m_PrevBlendDstAlpha;
GLint m_ViewportInit[4];
GLfloat m_ProjMatrixInit[16];
GLboolean m_texGenS;
GLboolean m_texGenT;
GLboolean m_texGenR;
void restoreOpenGLState()
{
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, m_PrevTexEnv);
glLineWidth(m_PrevLineWidth);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_TEXTURE);
glPopMatrix();
glPopClientAttrib();
glPopAttrib();
if (m_texGenS)
glEnable(GL_TEXTURE_GEN_S);
else
glDisable(GL_TEXTURE_GEN_S);
if (m_texGenT)
glEnable(GL_TEXTURE_GEN_T);
else
glDisable(GL_TEXTURE_GEN_T);
if (m_texGenR)
glEnable(GL_TEXTURE_GEN_R);
else
glDisable(GL_TEXTURE_GEN_R);
}
void saveOpenGLState(int screenWidth, int screenHeight)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
GLint Vp[4];
glGetIntegerv(GL_VIEWPORT, Vp);
if (screenWidth>0 && screenHeight>0)
{
Vp[0] = 0;
Vp[1] = 0;
Vp[2] = screenWidth-1;
Vp[3] = screenHeight-1;
glViewport(Vp[0], Vp[1], Vp[2], Vp[3]);
}
glLoadIdentity();
glOrtho(Vp[0], Vp[0]+Vp[2], Vp[1]+Vp[3], Vp[1], -1, 1);
glGetIntegerv(GL_VIEWPORT, m_ViewportInit);
glGetFloatv(GL_PROJECTION_MATRIX, m_ProjMatrixInit);
glGetFloatv(GL_LINE_WIDTH, &m_PrevLineWidth);
// glDisable(GL_POLYGON_STIPPLE);
glLineWidth(1);
glGetBooleanv(GL_TEXTURE_GEN_S,&m_texGenS);
glGetBooleanv(GL_TEXTURE_GEN_T,&m_texGenT);
glGetBooleanv(GL_TEXTURE_GEN_R,&m_texGenR);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_LINE_SMOOTH);
// glDisable(GL_LINE_STIPPLE);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &m_PrevTexEnv);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDisable(GL_TEXTURE_2D);
}
namespace Gwen
{
namespace Renderer
{
OpenGL_DebugFont::OpenGL_DebugFont()
{
m_iVertNum = 0;
for ( int i=0; i<MaxVerts; i++ )
{
m_Vertices[ i ].z = 0.5f;
}
m_fLetterSpacing = 1.0f/16.0f;
m_fFontScale[0] = 1.5f;
m_fFontScale[1] = 1.5f;
m_pFontTexture = new Gwen::Texture();
// Create a little texture pointer..
GLuint* pglTexture = new GLuint;
// Sort out our GWEN texture
m_pFontTexture->data = pglTexture;
m_pFontTexture->width = 256;
m_pFontTexture->height = 256;
// Create the opengl texture
glGenTextures( 1, pglTexture );
glBindTexture( GL_TEXTURE_2D, *pglTexture );
//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLenum format = GL_RGB;
unsigned char* texdata = new unsigned char[256*256*4];
for (int i=0;i<256*256;i++)
{
texdata[i*4] = sGwenFontData[i];
texdata[i*4+1] = sGwenFontData[i];
texdata[i*4+2] = sGwenFontData[i];
texdata[i*4+3] = sGwenFontData[i];
}
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, m_pFontTexture->width, m_pFontTexture->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)texdata );
delete[]texdata;
}
OpenGL_DebugFont::~OpenGL_DebugFont()
{
FreeTexture( m_pFontTexture );
delete m_pFontTexture;
}
void OpenGL_DebugFont::Begin()
{
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glAlphaFunc( GL_GREATER, 1.0f );
glEnable ( GL_BLEND );
}
void OpenGL_DebugFont::End()
{
if ( m_iVertNum == 0 ) return;
glVertexPointer( 3, GL_FLOAT, sizeof(Vertex), (void*) &m_Vertices[0].x );
glEnableClientState( GL_VERTEX_ARRAY );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Vertex), (void*)&m_Vertices[0].r );
glEnableClientState( GL_COLOR_ARRAY );
glTexCoordPointer( 2, GL_FLOAT, sizeof(Vertex), (void*) &m_Vertices[0].u );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glDrawArrays( GL_TRIANGLES, 0, (GLsizei) m_iVertNum );
m_iVertNum = 0;
glFlush();
}
void OpenGL_DebugFont::Flush()
{
if ( m_iVertNum == 0 ) return;
glVertexPointer( 3, GL_FLOAT, sizeof(Vertex), (void*) &m_Vertices[0].x );
glEnableClientState( GL_VERTEX_ARRAY );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Vertex), (void*)&m_Vertices[0].r );
glEnableClientState( GL_COLOR_ARRAY );
glTexCoordPointer( 2, GL_FLOAT, sizeof(Vertex), (void*) &m_Vertices[0].u );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glDrawArrays( GL_TRIANGLES, 0, (GLsizei) m_iVertNum );
m_iVertNum = 0;
glFlush();
}
void OpenGL_DebugFont::AddVert( int x, int y, float u, float v )
{
if ( m_iVertNum >= MaxVerts-1 )
{
Flush();
}
m_Vertices[ m_iVertNum ].x = (float)x;
m_Vertices[ m_iVertNum ].y = (float)y;
m_Vertices[ m_iVertNum ].u = u;
m_Vertices[ m_iVertNum ].v = v;
m_Vertices[ m_iVertNum ].r = m_Color.r;
m_Vertices[ m_iVertNum ].g = m_Color.g;
m_Vertices[ m_iVertNum ].b = m_Color.b;
m_Vertices[ m_iVertNum ].a = m_Color.a;
m_iVertNum++;
}
void OpenGL_DebugFont::DrawFilledRect( Gwen::Rect rect )
{
GLboolean texturesOn;
glGetBooleanv(GL_TEXTURE_2D, &texturesOn);
if ( texturesOn )
{
Flush();
glDisable(GL_TEXTURE_2D);
}
Translate( rect );
AddVert( rect.x, rect.y );
AddVert( rect.x+rect.w, rect.y );
AddVert( rect.x, rect.y + rect.h );
AddVert( rect.x+rect.w, rect.y );
AddVert( rect.x+rect.w, rect.y+rect.h );
AddVert( rect.x, rect.y + rect.h );
}
void OpenGL_DebugFont::SetDrawColor(Gwen::Color color)
{
glColor4ubv( (GLubyte*)&color );
m_Color = color;
}
void OpenGL_DebugFont::StartClip()
{
Flush();
Gwen::Rect rect = ClipRegion();
// OpenGL's coords are from the bottom left
// so we need to translate them here.
{
GLint view[4];
glGetIntegerv( GL_VIEWPORT, &view[0] );
rect.y = view[3] - (rect.y + rect.h);
}
glScissor( rect.x * Scale(), rect.y * Scale(), rect.w * Scale(), rect.h * Scale() );
glEnable( GL_SCISSOR_TEST );
};
void OpenGL_DebugFont::EndClip()
{
Flush();
glDisable( GL_SCISSOR_TEST );
};
void OpenGL_DebugFont::RenderText( Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString& text )
{
float fSize = pFont->size * Scale();
if ( !text.length() )
return;
Gwen::String converted_string = Gwen::Utility::UnicodeToString( text );
float yOffset=0.0f;
for ( int i=0; i<text.length(); i++ )
{
wchar_t chr = text[i];
char ch = converted_string[i];
float curSpacing = sGwenDebugFontSpacing[ch] * m_fLetterSpacing * fSize * m_fFontScale[0];
Gwen::Rect r( pos.x + yOffset, pos.y-fSize*0.2f, (fSize * m_fFontScale[0]), fSize * m_fFontScale[1] );
if ( m_pFontTexture )
{
float uv_texcoords[8]={0.,0.,1.,1.};
if ( ch >= 0 )
{
float cx= (ch%16)/16.0;
float cy= (ch/16)/16.0;
uv_texcoords[0] = cx;
uv_texcoords[1] = cy;
uv_texcoords[4] = float(cx+1.0f/16.0f);
uv_texcoords[5] = float(cy+1.0f/16.0f);
}
DrawTexturedRect( m_pFontTexture, r, uv_texcoords[0], uv_texcoords[5], uv_texcoords[4], uv_texcoords[1] );
yOffset+=curSpacing;
}
else
{
DrawFilledRect( r );
yOffset+=curSpacing;
}
}
}
void OpenGL_DebugFont::DrawTexturedRect( Gwen::Texture* pTexture, Gwen::Rect rect, float u1, float v1, float u2, float v2 )
{
GLuint* tex = (GLuint*)pTexture->data;
// Missing image, not loaded properly?
if ( !tex )
{
return DrawMissingImage( rect );
}
Translate( rect );
GLuint boundtex;
GLboolean texturesOn;
glGetBooleanv(GL_TEXTURE_2D, &texturesOn);
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *)&boundtex);
if ( !texturesOn || *tex != boundtex )
{
Flush();
glBindTexture( GL_TEXTURE_2D, *tex );
glEnable(GL_TEXTURE_2D);
}
AddVert( rect.x, rect.y, u1, v1 );
AddVert( rect.x+rect.w, rect.y, u2, v1 );
AddVert( rect.x, rect.y + rect.h, u1, v2 );
AddVert( rect.x+rect.w, rect.y, u2, v1 );
AddVert( rect.x+rect.w, rect.y+rect.h, u2, v2 );
AddVert( rect.x, rect.y + rect.h, u1, v2 );
}
Gwen::Point OpenGL_DebugFont::MeasureText( Gwen::Font* pFont, const Gwen::UnicodeString& text )
{
Gwen::Point p;
float fSize = pFont->size * Scale();
Gwen::String converted_string = Gwen::Utility::UnicodeToString( text );
float spacing = 0.0f;
for ( int i=0; i<text.length(); i++ )
{
char ch = converted_string[i];
spacing += sGwenDebugFontSpacing[ch];
}
p.x = spacing*m_fLetterSpacing*fSize * m_fFontScale[0];
p.y = pFont->size * Scale() * m_fFontScale[1];
return p;
}
}
}

View File

@ -53,6 +53,8 @@ namespace Gwen
void Flush();
void AddVert( int x, int y, float u = 0.0f , float v = 0.0f );
virtual void Resize(int width, int height) {}
protected:

View File

@ -7,6 +7,7 @@
extern char OpenSansData[];
#include "Gwen/Renderers/OpenGL_DebugFont.h"
#ifdef __APPLE__
#include "OpenGLWindow/MacOpenGLWindow.h"
#else
@ -27,6 +28,7 @@ extern char OpenSansData[];
#include <assert.h>
Gwen::Controls::Canvas* pCanvas = NULL;
Gwen::Skin::Simple skin;
void MyMouseMoveCallback( float x, float y)
{
@ -69,7 +71,9 @@ void MyMouseButtonCallback(int button, int state, float x, float y)
int sWidth = 1050;
int sHeight = 768;
GLPrimitiveRenderer* primRenderer=0;
GwenOpenGL3CoreRenderer* gwenRenderer=0;
//GwenOpenGL3CoreRenderer* gwenRenderer=0;
Gwen::Renderer::Base* gwenRenderer =0;
static void MyResizeCallback( float width, float height)
{
sWidth = width;
@ -81,7 +85,7 @@ static void MyResizeCallback( float width, float height)
}
if (gwenRenderer)
{
gwenRenderer->resize(width,height);
gwenRenderer->Resize(width,height);
}
if (pCanvas)
{
@ -301,6 +305,42 @@ extern int avoidUpdate;
int main()
{
//#define TEST_OPENGL2_GWEN
#ifdef TEST_OPENGL2_GWEN
b3gDefaultOpenGLWindow* window = new b3gDefaultOpenGLWindow();
window->setKeyboardCallback(keyCallback);
b3gWindowConstructionInfo wci;
wci.m_width = sWidth;
wci.m_height = sHeight;
// wci.m_resizeCallback = MyResizeCallback;
window->createWindow(wci);
window->setResizeCallback(MyResizeCallback);
window->setWindowTitle("render test");
// Gwen::Renderer::OpenGL_DebugFont* pRenderer = new Gwen::Renderer::OpenGL_DebugFont();
gwenRenderer = new Gwen::Renderer::OpenGL_DebugFont();
skin.SetRender( gwenRenderer );
pCanvas = new Gwen::Controls::Canvas( &skin );
pCanvas->SetSize( sWidth, sHeight);
pCanvas->SetDrawBackground( true );
pCanvas->SetBackgroundColor( Gwen::Color( 150, 170, 170, 255 ) );
glClearColor(1,0,0,1);
/* while( !window->requestedExit() )
{
window->startRendering();
// Main OpenGL Render Loop
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
window->endRendering();
}
*/
//exit(0);
#else
float retinaScale = 1.f;
b3gDefaultOpenGLWindow* window = new b3gDefaultOpenGLWindow();
@ -325,7 +365,7 @@ int main()
gwenRenderer = new GwenOpenGL3CoreRenderer(primRenderer,font,sWidth,sHeight,retinaScale);
#endif
//
@ -343,7 +383,6 @@ int main()
skin.SetRender( pRenderer );
skin.Init("DefaultSkin.png");
#else
Gwen::Skin::Simple skin;
skin.SetRender( gwenRenderer );
#endif
@ -379,6 +418,9 @@ int main()
// MSG msg;
while( !window->requestedExit() )
{
saveOpenGLState(sWidth,sHeight);
// Skip out if the window is closed
//if ( !IsWindowVisible( g_pHWND ) )
//break;
@ -466,7 +508,7 @@ int main()
// SwapBuffers( GetDC( g_pHWND ) );
}
window->endRendering();
restoreOpenGLState();
}
window->closeWindow();

View File

@ -83,7 +83,7 @@ public:
{
TwDeleteDefaultFonts();
}
void resize(int width, int height)
virtual void Resize(int width, int height)
{
m_screenWidth = width;
m_screenHeight = height;

View File

@ -44,6 +44,7 @@ enum btTypedConstraintType
D6_SPRING_CONSTRAINT_TYPE,
GEAR_CONSTRAINT_TYPE,
FIXED_CONSTRAINT_TYPE,
D6_SPRING_2_CONSTRAINT_TYPE,
MAX_CONSTRAINT_TYPE
};