Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <exception>
- #include <iostream>
- #include <string>
- #include <glew.h>
- #include <GL/glfw.h>
- #include <iterator>
- #include "../include/TextRenderer.h"
- #include <ft2build.h>
- #include FT_FREETYPE_H
- #include <stdexcept>
- #include <freetype/ftglyph.h>
- using std::runtime_error;
- using std::cout;
- TextRenderer::TextRenderer(float x,float y, FT_Face Face,std::string s)
- {
- FT_Set_Char_Size(
- Face, /* handle to face object */
- 0, /* char_width in 1/64th of points */
- 16*64, /* char_height in 1/64th of points */
- 0, /* horizontal device resolution */
- 0 ); /* vertical device resolution */
- slot= Face->glyph;
- text=s;
- setsx(x);
- setsy(y);
- penX=x;
- penY=y;
- face=Face;
- //shaders
- GLuint v = glCreateShader(GL_VERTEX_SHADER) ;
- const char* vs = "void main(){ gl_Position = ftransform();gl_TexCoord[0]=gl_MultiTexCoord0;}";
- glShaderSource(v,1,&vs,NULL);
- glCompileShader(v);
- GLuint f = glCreateShader(GL_FRAGMENT_SHADER) ;
- const char* fs = "uniform sampler2D texture1; void main() { gl_FragColor = texture2D(texture1, gl_TexCoord[0].st); }";
- glShaderSource(f,1,&fs,NULL);
- glCompileShader(f);
- Program= glCreateProgram();
- glAttachShader(Program,v);
- glAttachShader(Program,f);
- glLinkProgram(Program);
- }
- void TextRenderer::render()
- {
- glUseProgram(Program);
- FT_UInt glyph_index;
- for ( int n = 0; n < text.size(); n++ )
- {
- /* retrieve glyph index from character code */
- glyph_index = FT_Get_Char_Index( face, text[n] );
- /* load glyph image into the slot (erase previous one) */
- error = FT_Load_Glyph( face, glyph_index, FT_LOAD_RENDER );
- draw(&face->glyph->bitmap,penX + slot->bitmap_left,penY - slot->bitmap_top );
- penX += *(&face->glyph->bitmap.width)+3;
- penY += slot->advance.y >> 6; /* not useful for now */
- }
- }
- void TextRenderer::draw(FT_Bitmap * bitmap,float x,float y)
- {
- GLuint texture [1] ;
- //crashes on next line
- glGenTextures(1,texture);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->width, bitmap->rows, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap->buffer);//luminance vs gl_RED?
- // int loc = glGetUniformLocation(Program, "texture1");
- // glUniform1i(loc, 0);
- glEnable(GL_BLEND);
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glEnable(GL_TEXTURE_2D);
- int height=bitmap->rows;
- int width=bitmap->width;
- glLoadIdentity();
- glScalef(0.01f, 0.01f, 1.0f);
- glBegin(GL_QUADS);
- glTexCoord2f (0.0, 0.0);
- glVertex2f(x,y);
- glTexCoord2f (1.0, 0.0);
- glVertex2f(x+width,y);
- glTexCoord2f (1.0, 1.0);
- glVertex2f(x+width,y+height);
- glTexCoord2f (0.0, 1.0);
- glVertex2f(x,y+height);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- }
Advertisement
Add Comment
Please, Sign In to add comment