PDA

View Full Version : Help rendering text



CroftxD
03-06-2013, 01:33 PM
Hi, I'm very new to OpenGL and still coming to grasps with how it works. Basically I've created a 3d cube that has movement by keyboard control etc... basic stuff.

What I'm trying to achieve initially to begin with is for text to render over the top (from which I plan to do more technical things with) of the window, I used a tutorial and tried to implement into my code. But the text doesn't show up. Due to not actually getting any errors messages from VS am struggling to see where it is I am going wrong or obvious clashes in code from the tutorial and mine.

Any help and pointers would be greatly appreciated.


#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <GL/glut.h>
#include <math.h>

GLfloat g_xeye;
GLfloat g_yeye;


bool rollOn;

GLfloat g_light_position[] = {0.0, 10.0, 10.0, 0.0};

void *font = GLUT_BITMAP_TIMES_ROMAN_24;
void *fonts[] =
{
GLUT_BITMAP_9_BY_15,
GLUT_BITMAP_TIMES_ROMAN_10,
GLUT_BITMAP_TIMES_ROMAN_24
};

char defaultMessage[] = "GLUT means OpenGL.";
char *message = defaultMessage;

void selectFont(int newfont)
{
font = fonts[newfont];
glutPostRedisplay();
}

void selectMessage(int msg)
{
switch(msg) {
case 1:
message = "abcdefghijklmnop";
break;
case 2:
message = "ABCDEFGHIJKLMNOP";
break;
}
}

void selectColor(int color)
{
switch (color) {
case 1:
glColor3f(0.0, 1.0, 0.0);
break;
case 2:
glColor3f(1.0, 0.0, 0.0);
break;
case 3:
glColor3f(1.0, 1.0, 1.0);
break;
}
glutPostRedisplay();
}

void tick(void)
{
glutPostRedisplay();
}

void output(int x, int y, char *string)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i< len; i++) {
glutBitmapCharacter(font, string[i]);
}
}


void roll(void)
{

if(rollOn){
Sleep(1);
g_xeye = g_xeye + 000000.1;
glutPostRedisplay();
g_yeye = g_yeye + 000000.1;
glutPostRedisplay();
}
}
void display(void)
{
GLfloat redDiffuseMaterial[] = {0.50, 0.0, 0.50, 0.0};

glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
gluLookAt(0,0, 6 , 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, redDiffuseMaterial);
glRotatef(g_xeye, 1, 0, 0);
glRotatef(g_yeye, 0, 1, 0);
glutSolidCube(2.0);

output(0, 24, "This is written in a GLUT bitmap font.");
output(100, 100, message);
output(50, 145, "(positioned in pixels with upper-left origin)");


//glColor3f(0.250, 0.0, 0.200);

glutSwapBuffers ();
}

void keyboard(unsigned char key, int x, int y)
{
if (key==27)
exit(0);
if (key == ',')
{

g_light_position[0] = g_light_position[0] - 1;
glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);

}
if (key == '.')
{
g_light_position[0] = g_light_position[0] + 1;
glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);
glutPostRedisplay();
}
if (key==32)
{
if(rollOn==false)
rollOn=true;
else
rollOn=false;
}
}


void special(int key, int x, int y)
{
if (key==27)
exit(0);
if (key == GLUT_KEY_UP)
{
g_xeye = g_xeye + 2;
glutPostRedisplay();
}
if (key == GLUT_KEY_DOWN)
{
g_xeye = g_xeye - 2;
glutPostRedisplay();
}
if (key == GLUT_KEY_RIGHT)
{
g_yeye = g_yeye + 2;
glutPostRedisplay();
}
if (key == GLUT_KEY_LEFT)
{
g_yeye = g_yeye - 2;
glutPostRedisplay();
}
}

void init(void)
{
glEnable(GL_DEPTH_TEST);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);

rollOn = false;

g_xeye = 0;
g_yeye = 0;
}

void reshape (int width, int height)
{
/* Called when window is created, moved or resized */
GLfloat aspectRatio;

/* Set the viewport to be the whole window */
glViewport(0, 0, (GLsizei) width, (GLsizei) height);

/* Prepare to set up the Projection matrix */
glMatrixMode(GL_PROJECTION);

glLoadIdentity();
//glColor3f(0.250, 0.0, 0.200);




/* Set a prespective projection */
aspectRatio = (GLfloat)width / (GLfloat) height;
gluPerspective(60, aspectRatio, 1.0, 100.0);

/* Prepare to set up the Model view matrix */
glMatrixMode(GL_MODELVIEW);
glClearColor (0.0, 0.0, 0.0, 0.0);
}

int main(int argc, char** argv)
{
int i, msg_submenu, color_submenu;

glutInit(&argc, argv);
for (i=1; i< argc; i++) {
if (strcmp(argv[i], "-mono")) {
font = GLUT_BITMAP_9_BY_15;
}
}




glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100, 100);
glutCreateWindow ("Project");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutReshapeFunc(reshape);
glutIdleFunc(roll);
glutIdleFunc(tick);
msg_submenu = glutCreateMenu(selectMessage);
glutAddMenuEntry("abc", 1);
glutAddMenuEntry("ABC", 2);
color_submenu = glutCreateMenu(selectColor);
glutAddMenuEntry("Green", 1);
glutAddMenuEntry("Red", 2);
glutAddMenuEntry("White", 3);
glutCreateMenu(selectFont);
glutAddMenuEntry("9 by 15", 0);
glutAddMenuEntry("Times Roman 10", 1);
glutAddMenuEntry("Times Roman 24", 2);
glutAddSubMenu("Messages", msg_submenu);
glutAddSubMenu("Color", color_submenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}