PDA

View Full Version : Text rendering



choco brownie
04-20-2016, 05:58 AM
First time posting in this forum.
I have been trying to render text into glut context, after initializing everything works except text rendering. I simplified the code so that you can only view the text rendering function.

// Include standard headers
#include "stdio.h"
#include <stdlib.h>
#include <string>
#include <sstream>
#include<Windows.h>
// Include GLEW
#include <glew.h>

#include <freeglut.h>
using namespace std;

void initFreeGlut(int ac, char *av[]) {
// A. init
glutInit(&ac, av); // 1. inits glut with arguments from the shell
glutInitDisplayString(""); // 2a. sets display parameters with a string (obsolete)
glutInitDisplayMode(GLUT_SINGLE); // 2b. sets display parameters with defines
glutInitWindowSize(600, 600); // 3. window size
glutInitContextVersion(3, 3); // 4. sets the version 3.3 as current version (so some functions of 1.x and 2.x could not work properly)
glutInitContextProfile(GLUT_CORE_PROFILE); // 5. sets the version 3.3 for the profile core
glutInitWindowPosition(100,100); // 6. distance from the top-left screen
glutCreateWindow("I love this forum"); // 7. message displayed on top bar window
}
void RenderString(GLdouble x, GLdouble y, const std::string &string)
{
glColor3d(1.0, 0.0, 0.0);
glRasterPos2d(x, y);
for (int n = 0; n<string.size(); ++n) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[n]);
}
}
int main(int argc, char** argv)
{
initFreeGlut(argc,argv); // inits freeglut
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
do {
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT);
RenderString(0,0,"hello world");
// Swap buffers
glutSwapBuffers();
glutMainLoopEvent();
} // Check if the ESC key was pressed or the window was closed
while (1);
// Close OpenGL window and terminate GLFW
//glfwTerminate();
return 0;
}
Background color shows perfectly, but I tried different coordinates for the text and it does not work.

Carmine
04-20-2016, 05:44 PM
It's good that you posted some code and described your problem.

It would have been better (i.e. more enthusiastic responses),
if your code had been enclosed in [ code] and [ /code] tags,
(without the spaces), with indenting to make it easier to read.

You should really have a display function designated inside of
which the graphics is done (instead of a 'do' loop in the main
function). This is done with glutDisplayFunc. Also, you never
set up a coordinate system inside the window? This is done using
glOrtho or gluPerspective commands.

Look at demo code below for a basic setup.

http://www.mitchr.me/SS/exampleCode/glut/helloWorld.c.html

choco brownie
04-22-2016, 09:18 AM
Thank you Carmine,

It is just wierd when I looked back on my code, it works perfectly without the following two lines:


glutInitContextVersion(3, 3); // 4. sets the version 3.3 as current version (so some functions of 1.x and 2.x could not work properly)
glutInitContextProfile(GLUT_CORE_PROFILE); // 5. sets the version 3.3 for the profile core


Also for you suggestion of the glutDisplayFunc, wouldn't it make the program less flexible when there are keyboard and much more to change during the running?

Thanks!

Spoops
04-22-2016, 02:27 PM
Freeglut uses legacy OpenGL, which is not available with any core profile. Either use modern rendering techniques or simply don't create a core context.

Carmine
04-22-2016, 03:36 PM
... wouldn't it make the program less flexible when there are keyboard and much more to change during the running

Interaction with the code is done with functions like glutKeyboardFunc, glutMouseFunc, etc.
The code below shows how to set up the keyboard to toggle or reset global variables.
Note that at the bottom of the Keybord routine, glutPostRedisplay is called.
This causes your display routine to be called which uses the global variables to change the graphics.




//--+----4----+----3----+----2----+----1----+----|----+----1----+----2----+----3----+----4----+----5
//----------------------------------------- Keybord --------------------------------------------

void Keybord (unsigned char ch, int x, int y)
{
// printf (" Keyboard -> '%c' pressed.\n", ch);

switch (ch) {
case 'a' : tog_triad = !tog_triad; break;
case 'b' : tog_unbox = !tog_unbox; break;
case 'c' : tog_clear = !tog_clear; break;
case 'p' : proj = !proj; break;
case 'r' : mouse_trn = 0;
mouse_rot = 1; break;
case 't' : mouse_trn = 1;
mouse_rot = 0; break;
case 'q' : rotiseri = !rotiseri; break;
case 'x' : rotax = 0; break;
case 'y' : rotax = 1; break;
case 'z' : rotax = 2; break;
case '0' : turn = 0.0; tipp = 0.0;
xtm = 0.0; ytm = 0.0;
scl = 1.0; break;
case '-' : scl *= 0.92; break;
case '+' : scl *= 1.06; break;

case 27 : exit (0); break;
}

if (rotiseri) glutIdleFunc (Animate);
else glutIdleFunc ( NULL );

glutPostRedisplay();
}

//--+----4----+----3----+----2----+----1----+----|----+----1----+----2----+----3----+----4----+----5
//---------------------------------------- Init_Glut -------------------------------------------

int Init_Glut (void)
{
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition (200, 200);

glutInitWindowSize (900, 600);
glutCreateWindow ("STL Model Disply - Carmine");

glutKeyboardFunc (Keybord);
glutDisplayFunc (Display);
glutMouseFunc (Mouse );
glutMotionFunc (Motion );
}