Guest User

Untitled

a guest
Aug 13th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1.  
  2. #include <exception>
  3. #include <iostream>
  4. #include <string>
  5. #include <glew.h>
  6. #include <GL/glfw.h>
  7. #include <iterator>
  8. #include "../include/TextRenderer.h"
  9. #include <ft2build.h>
  10. #include FT_FREETYPE_H
  11. #include <stdexcept>
  12. #include <freetype/ftglyph.h>
  13.  
  14.  
  15. using std::runtime_error;
  16. using std::cout;
  17. TextRenderer::TextRenderer(float x,float y, FT_Face Face,std::string s)
  18. {
  19.  
  20.  
  21. FT_Set_Char_Size(
  22. Face, /* handle to face object */
  23. 0, /* char_width in 1/64th of points */
  24. 16*64, /* char_height in 1/64th of points */
  25. 0, /* horizontal device resolution */
  26. 0 ); /* vertical device resolution */
  27. slot= Face->glyph;
  28.  
  29. text=s;
  30. setsx(x);
  31. setsy(y);
  32. penX=x;
  33. penY=y;
  34. face=Face;
  35. //shaders
  36. GLuint v = glCreateShader(GL_VERTEX_SHADER) ;
  37. const char* vs = "void main(){ gl_Position = ftransform();gl_TexCoord[0]=gl_MultiTexCoord0;}";
  38. glShaderSource(v,1,&vs,NULL);
  39. glCompileShader(v);
  40. GLuint f = glCreateShader(GL_FRAGMENT_SHADER) ;
  41. const char* fs = "uniform sampler2D texture1; void main() { gl_FragColor = texture2D(texture1, gl_TexCoord[0].st); }";
  42. glShaderSource(f,1,&fs,NULL);
  43. glCompileShader(f);
  44. Program= glCreateProgram();
  45. glAttachShader(Program,v);
  46. glAttachShader(Program,f);
  47. glLinkProgram(Program);
  48.  
  49.  
  50. }
  51. void TextRenderer::render()
  52. {
  53. glUseProgram(Program);
  54.  
  55.  
  56. FT_UInt glyph_index;
  57. for ( int n = 0; n < text.size(); n++ )
  58. {
  59.  
  60.  
  61.  
  62. /* retrieve glyph index from character code */
  63.  
  64. glyph_index = FT_Get_Char_Index( face, text[n] );
  65.  
  66. /* load glyph image into the slot (erase previous one) */
  67. error = FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER );
  68.  
  69.  
  70. draw(&face->glyph->bitmap,penX + slot->bitmap_left,penY - slot->bitmap_top );
  71.  
  72.  
  73. penX += *(&face->glyph->bitmap.width)+3;
  74. penY += slot->advance.y >> 6; /* not useful for now */
  75. }
  76.  
  77.  
  78.  
  79. }
  80. void TextRenderer::draw(FT_Bitmap * bitmap,float x,float y)
  81. {
  82.  
  83.  
  84.  
  85. GLuint texture [1] ;
  86. //crashes on next line
  87. glGenTextures(1,texture);
  88. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  90. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  91. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  92. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  93. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  94. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->width, bitmap->rows, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap->buffer);//luminance vs gl_RED?
  95.  
  96.  
  97. // int loc = glGetUniformLocation(Program, "texture1");
  98. // glUniform1i(loc, 0);
  99. glEnable(GL_BLEND);
  100. glBindTexture(GL_TEXTURE_2D, texture[0]);
  101. glEnable(GL_TEXTURE_2D);
  102. int height=bitmap->rows;
  103. int width=bitmap->width;
  104. glLoadIdentity();
  105. glScalef(0.01f, 0.01f, 1.0f);
  106. glBegin(GL_QUADS);
  107. glTexCoord2f (0.0, 0.0);
  108. glVertex2f(x,y);
  109. glTexCoord2f (1.0, 0.0);
  110. glVertex2f(x+width,y);
  111. glTexCoord2f (1.0, 1.0);
  112. glVertex2f(x+width,y+height);
  113. glTexCoord2f (0.0, 1.0);
  114. glVertex2f(x,y+height);
  115. glEnd();
  116. glDisable(GL_TEXTURE_2D);
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment