PDA

View Full Version : Displaying freeglut strokefont text in opengl4



sueyllam
11-08-2014, 11:03 AM
I have the following code:



#include <iostream>
#include <GL/glew.h>
#include <GL\freeglut.h>
//#include <glm/glm.hpp>
//#include <glm/gtc/matrix_transform.hpp>
#include <stddef.h>
#include <stdio.h>

using namespace std;

GLint rx=-1, ry=-1;
GLint ww=500, wh=500;
int base;

//glm::mat4 pMat(1.0f);
//glm::mat4 vMat(1.0f);

void myinit(void) {
glClearColor(0.0,0.0,0.0,1.0);
}

void mouse(int btn, int state, int xx, int yy) {
if (btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
rx=xx; ry=wh-yy;
glutPostRedisplay();
}
}


void myReshape(int w, int h) {
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble) w , 0.0, (GLdouble) h , -1.0, 1.0);
//pMat = glm::ortho(0.0, (GLdouble) w, 0.0, (GLdouble) h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
ww=w; wh=h;
}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
if (rx>=0) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef((GLfloat) rx, (GLfloat) ry, 0.0);
//vMat = glm::translate(glm::mat4(1.0f), glm::vec3((GLfloat) rx, (GLfloat) ry, 0.0));
//glScalef(0.16,0.16,1.0);
glScalef(ww/3000.0,wh/3000.0,1.0);
//vMat = glm::scale(vMat, glm::vec3(ww/3000.0,wh/3000.0,1.0,1.0));

glColor4f(1.0, 1.0, 1.0, 1.0);
/* glCallList(base+'H');
glCallList(base+'i');
glCallList(base+'s');
glCallList(base+'h');
glCallList(base+'a');
glCallList(base+'m');
*/
glutStrokeString(GLUT_STROKE_MONO_ROMAN, (unsigned char *) "Hisham");
}
glFlush();
}

int
main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitContextVersion (3, 3);
glutInitContextFlags (GLUT_CORE_PROFILE | GLUT_DEBUG);
glutInitContextProfile(GLUT_FORWARD_COMPATIBLE);
glutInitWindowSize(ww,wh);
glutInitWindowPosition(100,100);
glutCreateWindow("Text Test");
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err){
cerr<<"Error: "<<glewGetErrorString(err)<<endl;
} else {
if (GLEW_VERSION_3_3)
{
cout<<"Driver supports OpenGL 3.3\nDetails:"<<endl;
}
}
cout<<"\tUsing glew "<<glewGetString(GLEW_VERSION)<<endl;
cout<<"\tVendor: "<<glGetString (GL_VENDOR)<<endl;
cout<<"\tRenderer: "<<glGetString (GL_RENDERER)<<endl;
cout<<"\tVersion: "<<glGetString (GL_VERSION)<<endl;
cout<<"\tGLSL: "<<glGetString(GL_SHADING_LANGUAGE_VERSION)<<endl;
myinit ();
glutDisplayFunc(display);
glutReshapeFunc (myReshape);
glutMouseFunc (mouse);
glutMainLoop();
return 0;
}


It works fine on my old laptop with opengl 2.
I need to rewrite it to work on the recent desktop with opengl 4.4 while avoiding the deprecated calls to set the projection and modelview matrices.
As you can see I started using glm to calculate these matrices myself, but then when I pass their product as a uniform mat4 how would I use it in the vertex shader? what would be the vertex vector?

carsten neumann
11-08-2014, 03:47 PM
I'm a bit confused, which vertex shader are you referring to?

In any case I doubt the glut text (stroke or bitmap) functions work in a core context - in fact since you are using freeglut you can look at the source for glutStrokeString (https://github.com/dcnieho/FreeGLUT/blob/git_master/freeglut/freeglut/src/fg_font.c#L293) and you'll see that it uses immediate mode rendering (i.e. glBegin/glEnd). You'll either have to use a compatibility profile context or if that is not an option implement a different text rendering solution.

sueyllam
11-08-2014, 10:37 PM
Well I was referring to the vertex shader that I intended to write in opengl4. But thanks I think I will use Compatibility profile. I looked at another option which comes with glsdk in glutil but it is way too complicated and it does not have stroke fonts only bitmap. Thanks anyway for the informative reply and I am willing to hear about other open source suggestions other than glsdk...