/********************************************************************* * * * FLY * * --E#Y#E-- * * ===MUSCLE=== * * * *********************************************************************/ #include "font.h" /* Class */ UNIFORM uniform5; TMFONT tmfont5; TARGA targa5; WIND wind5; FLY fly5; //--------------------------------------------------------------------------- /* Buffer */ GLuint vbos5[MAX_BUFFERS]; /* Shader Program */ unsigned int m_fontProgram; /* Texture */ GLubyte *font; //--------------------------------------------------------------------------- /* TMFont */ void TMFONT::TMFont() { /* Font Version */ static const GLchar font_vert[] = "GLSL/font.vert"; static const GLchar font_frag[] = "GLSL/font.frag"; /* Create GLSL Shaders / Compile Shaders */ uniform5.createShader(font_vert, font_frag); uniform5.createProgram(m_fontProgram); //-------------------------------------------------------------------- /* Targa Font */ const char fontTGA[] = "FONT/font.tga"; glEnable(GL_CULL_FACE); glDepthFunc(GL_LEQUAL); /* Viewport */ GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); //Viewport[2] stores the width of the viewport, vieport[3] stores the height //We pass these into our font so the ortho mode can set the resolution for the window m_Width = viewport[2]; m_Height = viewport[3]; m_fontSize = 25.0; /* Load TGA */ targa5.readTarga(fontTGA, &font); } //--------------------------------------------------------------------------- /* Font Initialize */ bool TMFONT::initFont() { int side5 = 0; const GLint countE = 8; /* Font */ fly5.MipmapsImage(countE, side5, font); //-------------------------------------------------------------------- /* Vertex Font */ float vertices[] = { 0.0f, 0.0f, m_fontSize, 0.0f, m_fontSize, m_fontSize, 0.0f, m_fontSize }; /* Vertex Buffer */ glGenBuffers(MAX_BUFFERS, &vbos5[0]); //Generate a buffer for the color glBindBuffer(GL_ARRAY_BUFFER, vbos5[VERTEX_BUFFER]); //Bind the vertex buffer glBufferData(GL_ARRAY_BUFFER, sizeof(float) * EV, &vertices[0], GL_STATIC_DRAW); //-------------------------------------------------------------------- //Just initialize with something for now, the tex coords are updated //for each character printed float texCoords[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; /* Texture Buffer */ glBindBuffer(GL_ARRAY_BUFFER, vbos5[NORMAL_BUFFER]); //Bind the vertex buffer glBufferData(GL_ARRAY_BUFFER, sizeof(float) * EV, &texCoords[0], GL_DYNAMIC_DRAW); //-------------------------------------------------------------------- /* Init */ uniform5.Location(m_fontProgram,0, "f_Vertex"); uniform5.Location(m_fontProgram,1, "f_TexCoord"); uniform5.linkProgram(m_fontProgram); uniform5.Uniform(m_fontProgram, "outColor3", 0); return true; } //--------------------------------------------------------------------------- /* Print Font */ void TMFONT::printString(const char* str, float x, float y) { /* Matrix */ static float modelview1[ID]; static float projection1[ID]; /* Font Program */ uniform5.bindShader(m_fontProgram); // Enable our shader /* Set Ortho */ wind5.setOrthoMode(); float texCoords[EV]; /* Disable Cull Face */ glDisable(GL_CULL_FACE); /* Bind Texture */ glBindTexture(GL_TEXTURE_2D, m_textureID); /* Enable Array */ glEnableVertexAttribArrayARB(0); glEnableVertexAttribArrayARB(1); /* Vertex Array */ //glEnableClientState(GL_VERTEX_ARRAY); //glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTranslatef(x, y, 0.0); //Position our text for(string::size_type i = 0; i < sizeof(str); ++i) { /* Variables */ const float oneOverSixteen = 1.0f / 16.0f; int ch = int(str[i]); float xPos = float(ch % 16) * oneOverSixteen; float yPos = float(ch / 16) * oneOverSixteen; /* 8 Array */ texCoords[0] = xPos; texCoords[1] = 1.0f - yPos - oneOverSixteen; texCoords[2] = xPos + oneOverSixteen; texCoords[3] = 1.0f - yPos - oneOverSixteen; texCoords[4] = xPos + oneOverSixteen; texCoords[5] = 1.0f - yPos - 0.001f; texCoords[6] = xPos; texCoords[7] = 1.0f - yPos - 0.001f; /* Bind Buffer */ glBindBuffer(GL_ARRAY_BUFFER, vbos5[NORMAL_BUFFER]); //Bind the vertex buffer glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * EV, &texCoords[0]); //------------------------------------------------------------- glBindBuffer(GL_ARRAY_BUFFER, vbos5[VERTEX_BUFFER]); /* Vertex */ const GLint vsize = 3; // ( X, Y, Z, W ) const GLenum vtype = GL_FLOAT; const GLboolean vnormalize = GL_FALSE; const GLsizei vstride = 0; const GLbyte voffset = 0; //glVertexPointer(vsize, vtype, vstride, &w_vertices5[voffset]); glVertexAttribPointerARB((GLuint)0, vsize, vtype, vnormalize, vstride, &w_vertices5[voffset]); //------------------------------------------------------------- glBindBuffer(GL_ARRAY_BUFFER, vbos5[NORMAL_BUFFER]); /* Texture */ const GLint tsize = 2; // ( S, T, P, Q ) const GLenum ttype = GL_FLOAT; const GLboolean tnormalize = GL_FALSE; const GLsizei tstride = 0; const GLbyte toffset = 0; //glTexCoordPointer(tsize, ttype, tstride, &w_texCoords5[toffset]); glVertexAttribPointerARB((GLuint)1, tsize, ttype, tnormalize, tstride, &w_texCoords5[toffset]); //------------------------------------------------------------- /* Passing Data */ glGetFloatv(GL_PROJECTION_MATRIX, projection1); glGetFloatv(GL_MODELVIEW_MATRIX, modelview1); /* Matrix */ uniform5.Uniform4x4(m_fontProgram, "projection_matrix", 0, projection1); uniform5.Uniform4x4(m_fontProgram, "modelview_matrix", 1, modelview1); /* Draw & Move */ glDrawArrays(GL_QUADS, 0, 4); glTranslatef(m_fontSize * 0.80f, 0, 0); //Move along a bit for the next character } /* Disable Client State */ //glDisableClientState(GL_TEXTURE_COORD_ARRAY); //glDisableClientState(GL_VERTEX_ARRAY); /* Disable Array */ glDisableVertexAttribArrayARB(0); glDisableVertexAttribArrayARB(1); /* Delete Shader */ uniform5.DeleteShader(m_fontProgram); /* Unset Ortho */ wind5.unsetOrthoMode(); } //---------------------------------------------------------------------------