add simple 3d debug text for OpenGL2 in example browser

This commit is contained in:
erwin coumans 2015-05-06 16:11:03 -07:00
parent 5a0b744e73
commit 5b34cda617
2 changed files with 202 additions and 68 deletions

View File

@ -5,10 +5,10 @@
#include "Bullet3Common/b3Scalar.h"
#include "Bullet3Common/b3AlignedObjectArray.h"
#include "Bullet3Common/b3Vector3.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../OpenGLWindow/GLPrimitiveRenderer.h"
#include "stdlib.h"
#include "TwFonts.h"
#ifdef __APPLE__
#include "MacOpenGLWindow.h"
#else
@ -67,8 +67,31 @@ void Simple2WheelCallback( float deltax, float deltay)
struct SimpleOpenGL2AppInternalData
{
GLuint m_fontTextureId;
GLuint m_largeFontTextureId;
};
static GLuint BindFont2(const CTexFont *_Font)
{
GLuint TexID = 0;
glGenTextures(1, &TexID);
glBindTexture(GL_TEXTURE_2D, TexID);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE , _Font->m_TexWidth, _Font->m_TexHeight, 0, GL_LUMINANCE , GL_UNSIGNED_BYTE, _Font->m_TexBytes);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
return TexID;
}
SimpleOpenGL2App::SimpleOpenGL2App(const char* title, int width, int height)
{
@ -85,6 +108,7 @@ SimpleOpenGL2App::SimpleOpenGL2App(const char* title, int width, int height)
m_window->setWindowTitle(title);
#ifndef __APPLE__
#ifndef _WIN32
//some Linux implementations need the 'glewExperimental' to be true
@ -104,6 +128,13 @@ SimpleOpenGL2App::SimpleOpenGL2App(const char* title, int width, int height)
}
#endif
TwGenerateDefaultFonts();
m_data->m_fontTextureId = BindFont2(g_DefaultNormalFont);
m_data->m_largeFontTextureId = BindFont2(g_DefaultLargeFont);
glGetError();//don't remove this call, it is needed for Ubuntu
glClearColor(0.9,0.9,1,1);
@ -243,9 +274,174 @@ void SimpleOpenGL2App::drawText( const char* txt, int posX, int posY)
}
void SimpleOpenGL2App::drawText3D( const char* txt, float posX, float posZY, float posZ, float size)
{
static void restoreOpenGLState()
{
glPopClientAttrib();
glPopAttrib();
}
static void saveOpenGLState(int screenWidth, int screenHeight)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
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);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDisable(GL_TEXTURE_2D);
}
void SimpleOpenGL2App::drawText3D( const char* txt, float worldPosX, float worldPosY, float worldPosZ, float size1)
{
saveOpenGLState(gApp2->m_renderer->getScreenWidth(),gApp2->m_renderer->getScreenHeight());
float viewMat[16];
float projMat[16];
CommonCameraInterface* cam = gApp2->m_renderer->getActiveCamera();
cam->getCameraViewMatrix(viewMat);
cam->getCameraProjectionMatrix(projMat);
float camPos[4];
cam->getCameraPosition(camPos);
b3Vector3 cp= b3MakeVector3(camPos[0],camPos[2],camPos[1]);
b3Vector3 p = b3MakeVector3(worldPosX,worldPosY,worldPosZ);
float dx=0;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glAlphaFunc( GL_GREATER, 1.0f );
int viewport[4]={0,0,gApp2->m_renderer->getScreenWidth(),gApp2->m_renderer->getScreenHeight()};
float posX = 450.f;
float posY = 100.f;
float winx,winy, winz;
if (!projectWorldCoordToScreen(worldPosX, worldPosY, worldPosZ,viewMat,projMat,viewport,&winx, &winy, &winz))
{
return;
}
posX = winx;
posY = gApp2->m_renderer->getScreenHeight()/2+(gApp2->m_renderer->getScreenHeight()/2)-winy;
{
//float width = 0.f;
int pos=0;
//float color[]={0.2f,0.2,0.2f,1.f};
glActiveTexture(GL_TEXTURE0);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D,m_data->m_largeFontTextureId);
glEnable(GL_TEXTURE_2D);//BindTexture
//float width = r.x;
//float extraSpacing = 0.;
float startX = posX;
float startY = posY-g_DefaultLargeFont->m_CharHeight*size1;
glEnable(GL_COLOR_MATERIAL);
while (txt[pos])
{
int c = txt[pos];
//r.h = g_DefaultNormalFont->m_CharHeight;
//r.w = g_DefaultNormalFont->m_CharWidth[c]+extraSpacing;
float endX = startX+g_DefaultLargeFont->m_CharWidth[c]*size1;
float endY = posY;
float currentColor[]={1.f,0.2,0.2f,1.f};
float u0 = g_DefaultLargeFont->m_CharU0[c];
float u1 = g_DefaultLargeFont->m_CharU1[c];
float v0 = g_DefaultLargeFont->m_CharV0[c];
float v1 = g_DefaultLargeFont->m_CharV1[c];
float color[4] = {currentColor[0],currentColor[1],currentColor[2],currentColor[3]};
float x0 = startX;
float x1 = endX;
float y0 = startY;
float y1 = endY;
int screenWidth = gApp2->m_renderer->getScreenWidth();
int screenHeight = gApp2->m_renderer->getScreenHeight();
float z = 2.f*winz-1.f;//*(far
float identity[16]={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
PrimVertex vertexData[4] = {
{ PrimVec4(-1.f+2.f*x0/float(screenWidth), 1.f-2.f*y0/float(screenHeight), z, 1.f ), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u0,v0)},
{ PrimVec4(-1.f+2.f*x0/float(screenWidth), 1.f-2.f*y1/float(screenHeight), z, 1.f ), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u0,v1)},
{ PrimVec4( -1.f+2.f*x1/float(screenWidth), 1.f-2.f*y1/float(screenHeight), z, 1.f ), PrimVec4(color[0], color[1], color[2], color[3]) ,PrimVec2(u1,v1)},
{ PrimVec4( -1.f+2.f*x1/float(screenWidth), 1.f-2.f*y0/float(screenHeight), z, 1.f ), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u1,v0)}
};
glBegin(GL_TRIANGLES);
//use red colored text for now
glColor4f(1,0,0,1);
float scaling = 1;
glTexCoord2f(vertexData[0].uv.p[0],vertexData[0].uv.p[1]);
glVertex3d(vertexData[0].position.p[0]*scaling, vertexData[0].position.p[1]*scaling,vertexData[0].position.p[2]*scaling);
glTexCoord2f(vertexData[1].uv.p[0],vertexData[1].uv.p[1]);
glVertex3d(vertexData[1].position.p[0]*scaling, vertexData[1].position.p[1]*scaling,vertexData[1].position.p[2]*scaling);
glTexCoord2f(vertexData[2].uv.p[0],vertexData[2].uv.p[1]);
glVertex3d(vertexData[2].position.p[0]*scaling, vertexData[2].position.p[1]*scaling,vertexData[2].position.p[2]*scaling);
glTexCoord2f(vertexData[0].uv.p[0],vertexData[0].uv.p[1]);
glVertex3d(vertexData[0].position.p[0]*scaling, vertexData[0].position.p[1]*scaling,vertexData[0].position.p[2]*scaling);
glTexCoord2f(vertexData[2].uv.p[0],vertexData[2].uv.p[1]);
glVertex3d(vertexData[2].position.p[0]*scaling, vertexData[2].position.p[1]*scaling,vertexData[2].position.p[2]*scaling);
glTexCoord2f(vertexData[3].uv.p[0],vertexData[3].uv.p[1]);
glVertex3d(vertexData[3].position.p[0]*scaling, vertexData[3].position.p[1]*scaling,vertexData[3].position.p[2]*scaling);
glEnd();
startX = endX;
pos++;
}
}
glBindTexture(GL_TEXTURE_2D,0);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
restoreOpenGLState();
}
void SimpleOpenGL2App::registerGrid(int xres, int yres, float color0[4], float color1[4])

View File

@ -327,68 +327,6 @@ void SimpleOpenGL3App::drawText3D( const char* txt, float worldPosX, float world
glDisable(GL_BLEND);
#if 0
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
int pos=0;
//float color[]={0.2f,0.2,0.2f,1.f};
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,m_data->m_largeFontTextureId);
//float width = r.x;
//float extraSpacing = 0.;
float startX = posX;
float startY = posY;
while (txt[pos])
{
float scaling = 0.02;
int c = txt[pos];
//r.h = g_DefaultNormalFont->m_CharHeight;
//r.w = g_DefaultNormalFont->m_CharWidth[c]+extraSpacing;
float endX = startX-float(g_DefaultLargeFont->m_CharWidth[c])*scaling;
float endY = startY-float(g_DefaultLargeFont->m_CharHeight)*scaling;
float currentColor[]={0.2f,0.2,0.2f,1.f};
float u0 = g_DefaultLargeFont->m_CharU0[c];
float v0 = g_DefaultLargeFont->m_CharV0[c];
float u1 = g_DefaultLargeFont->m_CharU1[c];
float v1 = g_DefaultLargeFont->m_CharV1[c];
float color[4] = {0,0,0,1};
PrimVertex vertexData[4] = {
{ PrimVec4(startX, startY, 0, 1.f ), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u0,v0)},
{ PrimVec4(startX, endY, 0 , 1.f), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u0,v1)},
{ PrimVec4(endX, endY,0.f, 1.f ), PrimVec4(color[0], color[1], color[2], color[3]) ,PrimVec2(u1,v1)},
{ PrimVec4(endX,startY, 0.f, 1.f ), PrimVec4( color[0], color[1], color[2], color[3] ) ,PrimVec2(u1,v0)}
};
float viewMat[16];
float projMat[16];
m_instancingRenderer->getCameraViewMatrix(viewMat);
m_instancingRenderer->getCameraProjectionMatrix(projMat);
m_primRenderer->drawTexturedRect3D(vertexData[0],vertexData[1],vertexData[2],vertexData[3],viewMat,projMat,false);
//DrawTexturedRect(0,r,g_DefaultNormalFont->m_CharU0[c],g_DefaultNormalFont->m_CharV0[c],g_DefaultNormalFont->m_CharU1[c],g_DefaultNormalFont->m_CharV1[c]);
// DrawFilledRect(r);
startX = endX;
//startY = endY;
pos++;
}
glBindTexture(GL_TEXTURE_2D,0);
glDisable(GL_BLEND);
#endif
}