Sign in to follow this  
Yours3!f

OpenGL font rendering in OGL 3.3

Recommended Posts

Yours3!f    1532
Hi,

I'm trying to render text in OGL 3.3 using VBOs and VAOs, I can load the font files, however, when I write out the texture IDs I always get this number 4294967295, and I don't get any OpenGL errors...
When I try to display the text in OGL 2.1 using immediate mode, it works as expected.

here's how I render:
[code]
//-----------------------------------------

modelview_stack.push_matrix();

modelview_stack.translate_matrix ( mymath::vec3f ( 1.0f, 0.0f, -1.0f ) );
modelview_stack.rotate_matrix( 90.0f, mymath::vec3f ( 1.0f, 0.0f, 0.0f ) );

the_pipeline.get_model_view_matrix().copy_m4x4_data ( mv );
the_pipeline.get_projection_matrix().copy_m4x4_data ( proj );

font_shader.use_program();

glUniformMatrix4fv ( glGetUniformLocation ( font_shader.shader_program, font_shader.resources[0].name.c_str() ), 1, false, proj );
glUniformMatrix4fv ( glGetUniformLocation ( font_shader.shader_program, font_shader.resources[1].name.c_str() ), 1, false, mv );

//something is not right...
glBindTexture ( GL_TEXTURE_2D, character_textures[96].tex_data );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glUniform1i ( glGetUniformLocation ( font_shader.shader_program, font_shader.resources[2].name.c_str() ), 0 );

glEnable(GL_BLEND); //change
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //change

draw_meshes_vbo ( plane );

glDisable(GL_BLEND); //change

font_shader.return_to_fixed_program();

modelview_stack.pop_matrix();

//-----------------------------------------
[/code]

here's how I load the font texture:
[code]
void font::font_loadfontmap ()
{
font_setpointsize ( pointsize );

font_map fontmap;

fontmap.pointsize = pointsize; //change

for ( int c = 0;c < 256;c++ )
{
glGenTextures ( 1, &fontmap.charlist[c].texture );

//FT_Load_Char(ftface, c, FT_LOAD_RENDER);

if ( FT_Load_Char ( ftface, c, FT_LOAD_RENDER ) )
{
std::cerr << "error loading glyph....\n";
}

fontmap.charlist[c].charwidth = ftslot -> bitmap.width;
fontmap.charlist[c].charheight = ftslot -> bitmap.rows;

fontmap.charlist[c].width = font_next_powa2 ( ftslot -> bitmap.width );
fontmap.charlist[c].height = font_next_powa2 ( ftslot -> bitmap.rows );
fontmap.charlist[c].advancex = ftslot -> advance.x >> 6;

fontmap.charlist[c].offsetx = ftslot -> bitmap_left;
fontmap.charlist[c].offsety = ftslot -> bitmap_top;

GLubyte * data = new GLubyte[4 * fontmap.charlist[c].width * fontmap.charlist[c].height];

for ( int j = 0;j < fontmap.charlist[c].height; j++ )
{
for ( int i = 0;i < fontmap.charlist[c].width; i++ )
{
data[2 * ( i + j * fontmap.charlist[c].width ) ] = 255;
data[2 * ( i + j * fontmap.charlist[c].width ) + 1] = 255;
data[2 * ( i + j * fontmap.charlist[c].width ) + 2] = 255;
data[2 * ( i + j * fontmap.charlist[c].width ) + 3] = ( i >= ftslot -> bitmap.width || j >= ftslot -> bitmap.rows ) ? 0 : ftslot -> bitmap.buffer[i + ftslot -> bitmap.width * j];
}
}

glBindTexture ( GL_TEXTURE_2D, fontmap.charlist[c].texture );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, fontmap.charlist[c].width, fontmap.charlist[c].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );

delete [] data;
}

fontmaps.push_back ( fontmap );
}
[/code]

here's the shader code:
[code]
/*
* font.vs
*
*/
#version 330

//modelviewprojection, modelview matrices
uniform mat4 m4_p, m4_mv;

//the vertex position
in vec4 v4_vertex;
//the texture coordinates
in vec2 v2_texture;

smooth out vec2 v2_texture_coords;

void main()
{
v2_texture_coords = v2_texture;
mat4 m4_mvp = m4_p * m4_mv;
gl_Position = m4_mvp * v4_vertex; //simple mvp transformation, and pass the texture coords
}

/*
* font.ps
*
*/
#version 330

uniform sampler2D texture_map;

smooth in vec2 v2_texture_coords;

out vec4 v4_frag_color;

void main()
{
//simply display the texture
v4_frag_color = texture(texture_map, v2_texture_coords.st); //no texture?
}
[/code]

here's the texture loading:
[code]
texture font::get_char_texture ( int char_num )
{
if ( index < 0 )
{
for ( int ind = 0; ind < fontmaps.size(); ind++ )
{
if ( fontmaps[ind].pointsize == pointsize )
{
index = ind;
break;
}
}
}

texture out;

if ( index < 0 || index > fontmaps.size() - 1 )
{
//font_loadfontmap(); we don't need it
index = fontmaps.size() - 1;

if ( index < 0 )
{
std::cerr << "Cannot set index for character: " << char_num << "\n";
return out;
}
}

if ( char_num < 0 || char_num > 255 )
{
std::cerr << "Invalid charnum: " << char_num << "\n";
return out;
}

//if we have the index and the char_num right, then lets pass the texture's id, width, and height
out.tex_data = fontmaps[index].charlist[char_num].texture;
out.tex_width = fontmaps[index].charlist[char_num].width;
out.tex_height = fontmaps[index].charlist[char_num].height;

return out;
}
[/code]

and here's the mesh loading:
[code]
mesh font::get_char_mesh ( int char_num )
{
if ( index < 0 )
{
for ( int ind = 0; ind < fontmaps.size(); ind++ )
{
if ( fontmaps[ind].pointsize == pointsize )
{
index = ind;
break;
}
}
}

mesh out;

if ( index < 0 || index > fontmaps.size() - 1 )
{
//font_loadfontmap(); we don't need it
index = fontmaps.size() - 1;

if ( index < 0 )
{
std::cerr << "Cannot set index for character: " << char_num << "\n";
return out;
}
}

if ( char_num < 0 || char_num > 255 )
{
std::cerr << "Invalid charnum: " << char_num << "\n";
return out;
}

//if we got the index and the char_num right then let's set the offset for the char (the padding)
int offsetx = fontmaps[index].charlist[char_num].offsetx;
int offsety = -fontmaps[index].charlist[char_num].offsety;

//texture coordinates
float tx = ( float ) fontmaps[index].charlist[char_num].charwidth / ( float ) fontmaps[index].charlist[char_num].width;
float ty = ( float ) fontmaps[index].charlist[char_num].charheight / ( float ) fontmaps[index].charlist[char_num].height;

//make some space for four vertices and two faces
out.vertices.resize ( 4 );
out.tex_coords.resize ( 4 );
out.faces.resize ( 2 );

for ( int c = 0; c < 4; c++ )
{
out.vertices[c].resize ( 3 );
out.tex_coords[c].resize ( 2 );
}
out.faces[0].resize ( 9 );
out.faces[1].resize ( 9 );

//first vertex
out.vertices[0][0] = offsetx;
out.vertices[0][1] = offsety;
out.vertices[0][2] = 0.0f;
out.tex_coords[0][0] = 0.0f;
out.tex_coords[0][1] = 0.0f;

//second vertex
out.vertices[1][0] = offsetx;
out.vertices[1][1] = fontmaps[index].charlist[char_num].charheight + offsety;
out.vertices[1][2] = 0.0f;
out.tex_coords[1][0] = 0.0f;
out.tex_coords[1][1] = ty;

//third vertex
out.vertices[2][0] = fontmaps[index].charlist[char_num].charwidth + offsetx;
out.vertices[2][1] = fontmaps[index].charlist[char_num].charheight + offsety;
out.vertices[2][2] = 0.0f;
out.tex_coords[2][0] = tx;
out.tex_coords[2][1] = ty;

//fourth vertex
out.vertices[3][0] = fontmaps[index].charlist[char_num].charwidth + offsetx;
out.vertices[3][1] = offsety;
out.vertices[3][2] = 0.0f;
out.tex_coords[3][0] = tx;
out.tex_coords[3][1] = 0.0f;

//first poly, we use 1 for vertex index, since it should look like it was loaded from an obj file, so that our fill_obj() function will work
out.faces[0][0] = 1;
out.faces[0][1] = 1;
out.faces[0][2] = 0;

out.faces[0][3] = 3;
out.faces[0][4] = 3;
out.faces[0][5] = 0;

out.faces[0][6] = 2;
out.faces[0][7] = 2;
out.faces[0][8] = 0;

//second poly
out.faces[1][0] = 1;
out.faces[1][1] = 1;
out.faces[1][2] = 0;

out.faces[1][3] = 4;
out.faces[1][4] = 4;
out.faces[1][5] = 0;

out.faces[1][6] = 3;
out.faces[1][7] = 3;
out.faces[1][8] = 0;

return out;
}
[/code]

any ideas?

Best regards,
Yours3!f

Share this post


Link to post
Share on other sites
Yours3!f    1532
[quote name='zacaj' timestamp='1307294238' post='4819803']
What do you mean, when you write out the texture IDs? What variable are you writing out where?
[/quote]

I do:

std::cout << character_textures[96].tex_data << "\n";

where tex_data is a GLuint containing the texture's id.

Share this post


Link to post
Share on other sites
JackTheRapper    150
I see character_textures on your rendering snippet but not in your font loading snippet. Where does character_textures get it's tex IDs generated? 4294967295 looks like an uninitialized variable. Have you stepped through with a debugger?

Share this post


Link to post
Share on other sites
Yours3!f    1532
ok so I checked the texture generation and I just didn't set the pointsize right, so it loaded the data into the -1 th element of the array XD

now I did some modifications and now the the rectangles are all white, so I guess the RGB channels are loaded fine (all 255), but the alpha channel is just not right.
I tried to see what might be the error, so I wrote out the alpha values, here's the alpha values for "2":

R ? ? ? ? ? k 
 ? ? ? ? ? ? ? ? ? '
? ? ? ? ? ? ? ? ? ? ?
X ? ? ? ? T  \ ? ? ? ? |
? ? ? ? ? ? ? ?
? ? ? 9 P ? ? ?
 ? ? ?
c ? ? ?
? ? ? ]
+ ? ? ?
 ? ? ? $
 ? ? ? L
 ? ? ? X
 ? ? ? O
F ? ? ? B
] ? ? ? &
C ? ? ? 
 ? ? ? 
a ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ?

looks nice (despite some lines have slipped here, the output is ok) isn't it?

I've modified the code above so you can see the changes. Any ideas why the alpha values are wrong?

Best regards,
Yours3lf

Share this post


Link to post
Share on other sites
JackTheRapper    150
Just skim reading...

[code]
for ( int j = 0;j < fontmap.charlist[c].height; j++ )
{
for ( int i = 0;i < fontmap.charlist[c].width; i++ )
{
data[2 * ( i + j * fontmap.charlist[c].width ) ] = 255;
data[2 * ( i + j * fontmap.charlist[c].width ) + 1] = 255;
data[2 * ( i + j * fontmap.charlist[c].width ) + 2] = 255;
data[2 * ( i + j * fontmap.charlist[c].width ) + 3] = ( i >= ftslot -> bitmap.width || j >= ftslot -> bitmap.rows ) ? 0 : ftslot -> bitmap.buffer[i + ftslot -> bitmap.width * j];
}
}

[/code]
Why is your channel coefficient 2? You are working with a 4 channel image, should that not be 4? In my experience, if an image looks skewed and/or channels are corrupt, this is usually the reason.

Share this post


Link to post
Share on other sites
Yours3!f    1532
[quote name='JackTheRapper' timestamp='1307379173' post='4820159']
Why is your channel coefficient 2? You are working with a 4 channel image, should that not be 4? In my experience, if an image looks skewed and/or channels are corrupt, this is usually the reason.
[/quote]

ooops I left it in, in the OGL 2.1 version I used to work with 2 channels and GL_LUMINANCE_ALPHA as the format. However, I changed it but it's the same, except it is now translucent.

Share this post


Link to post
Share on other sites
JackTheRapper    150
Turn off blending and post a screenshot. It's difficult to visualise the problem based on your description and ASCII diagram. What do you mean by "It's the same but translucent"?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now