Reputation: 47
I am using the glc library to render font in the context of glfw window. However, I am not able to set co-ordinate to the font. It is always rendering the font at (0,0) position (i.e. at the below-left corner).
Below is the code to render sample string using GLC library,
int main(){
GLint ctx, myFont;
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
GLFWwindow* window = glfwCreateWindow(500, 500, "font rendering", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glfwSwapInterval(1);
if ( GLEW_OK != glewInit( ) )
{
std::cout << "Failed to initialize GLEW" << std::endl;
}
ctx = glcGenContext();
glcContext(ctx);
myFont = glcGenFontID();
glcNewFontFromFamily(myFont, "Palatino");
glcFontFace(myFont, "Normal");
glcFont(myFont);
glViewport(0, 0, 500, 500);
glMatrixMode(GL_PROJECTION); // make the projection matrix the current matrix
glLoadIdentity(); // init the projection matrix by the identity matrix
glOrtho(0.0, 500.0, 500.0, 0.0, -1.0, 1.0); // top-left (0, 0); bottom-right (500, 500)
glcScale(100,100);
glcRenderString("Sample Text");
glMatrixMode(GL_MODELVIEW); // make the modelview matrix the current matrix
glLoadIdentity(); // init the modelview matrix by the identity matrix
glfwSwapBuffers(window);
while(!glfwWindowShouldClose(window)){
}
Here, glTranslatef(100,100, 0);
does not work and if I use glRasterPos3f(x,y,z);
then nothing will get render onto the screen.
Did I miss something here?
Upvotes: 1
Views: 232
Reputation: 211057
The projection matrix describes how the objects are projected onto the viewport. If there is no projection matrix, the objects have to be setup in normalized device space where all coordinates are int the range [-1.0, 1.0].
Since you want to draw in 2D, I recommend to setup an orthographic projection, which maps the viewspace 1:1 to window coordinates.
Use glOrtho
for this and use glMatrixMode
to switch between the projection matrix stack and the modelview matrix stack:
glViewport(0, 0, 500, 500);
glMatrixMode(GL_PROJECTION); // make the projection matrix the current matrix
glLoadIdentity(); // init the projection matrix by the identity matrix
glOrtho(0.0, 500.0, 500.0, 0.0, -1.0, 1.0); // top-left (0, 0); bottom-right (500, 500)
glMatrixMode(GL_MODELVIEW); // make the modelview matrix the current matrix
glLoadIdentity(); // init the modelview matrix by the identity matrix
glcScale(100,100);
.....
glcRenderString
renders a string by using glBitmap
respectively glDrawPixels
. Becaus of that the positon has to be set by glRasterPos
.
If you want to render the string in red, then you have to skip the red an green color component. This can be done by glPixelTransfer
.
e.g.
glPixelTransferf(GL_RED_SCALE, 1.0f);
glPixelTransferf(GL_GREE_SCALE, 0.0f);
glPixelTransferf(GL_BLUE_SCALE, 0.0f);
Upvotes: 1