#include #include #include #include #include #include #include "nexus.h" static GLubyte *font_data; static GLubyte *letter_data; static unsigned int bytes_per_line = 4*760; GLfloat font_mat_amb_diff[4], font_mat_amb_diff_blue[4]; static void FontMakeLetter(unsigned char letter) { unsigned int cnt, iter, offset; for (cnt = 0, offset = (letter-32)*8*4; cnt < 4*8*16; cnt += 8*4) { for (iter = 0; iter < 32; iter++) *(letter_data + cnt + iter) = *(font_data + offset + iter); offset += bytes_per_line; } GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Linear = smooth glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Nearest = crisp //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 8, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, letter_data); glBindTexture(GL_TEXTURE_2D, 0); } void FontInit(void) { if (verbose) printf("Initializing text font\n"); font_data = ImageFromPNGFile(760, 16, "images/font-760x16a.png"); letter_data = malloc(4*8*16); unsigned char c; unsigned int cnt; for (cnt = 0, c = ' '; cnt <= 94; cnt++,c++) FontMakeLetter(c); free(font_data); free(letter_data); font_mat_amb_diff[0] = 0.8; font_mat_amb_diff[1] = 0.8; font_mat_amb_diff[2] = 0.8; font_mat_amb_diff[3] = 1.0; font_mat_amb_diff_blue[0] = 0.4; font_mat_amb_diff_blue[1] = 0.5; font_mat_amb_diff_blue[2] = 0.8; font_mat_amb_diff_blue[3] = 1.0; } void FontRender(int bgcolor, int fgcolor, GLfloat x, GLfloat y, GLfloat z, char *text) { glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); glEnable(GL_BLEND); // glEnable(GL_LIGHTING); // glEnable(GL_LIGHT0); // glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, // font_mat_amb_diff); /*if (fgcolor == FG_NONE) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, font_mat_amb_diff); else if (fgcolor == FG_BLUE) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, font_mat_amb_diff_blue);*/ if (bgcolor) { glPushMatrix(); glTranslatef(x, y, z); if (bgcolor == BG_BLACK) glColor4f(0.0, 0.0, 0.0, 1.0); else if (bgcolor == BG_GREY) glColor4f(0.3, 0.3, 0.3, 1.0); glBegin(GL_POLYGON); glVertex3f(0.0, 0.0, -0.01); glVertex3f(0.0, 0.2, -0.01); glVertex3f(0.08*strlen(text), 0.2, -0.01); glVertex3f(0.08*strlen(text), 0.0, -0.01); glEnd(); glPopMatrix(); } char *c = text; unsigned int cnt;if (fgcolor == FG_NONE) glColor3f(1.0, 1.0, 1.0); else if (fgcolor == FG_BLUE) glColor3f(0.3, 0.4, 1.0); else if (fgcolor == FG_GREEN) glColor3f(0.3, 1.0, 0.4); for (cnt = 0; cnt < strlen(text); cnt++,c++) { glPushMatrix(); glTranslatef(x + cnt*0.08, y, z); glBindTexture(GL_TEXTURE_2D, (*c)-31); glBegin(GL_POLYGON); glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(0.08, 0.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(0.08, 0.16, 0.0); glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.16, 0.0); glEnd(); glBindTexture(GL_TEXTURE_2D, 0); glPopMatrix(); } glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, font_mat_amb_diff); } void FontRender2D(int bgcolor, int x, int y, char *text) { glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); glEnable(GL_BLEND); if (bgcolor) { glPushMatrix(); glTranslatef(x, y, 0.5); if (bgcolor == BG_BLACK) glColor4f(0.0, 0.0, 0.0, 1.0); else if (bgcolor == BG_GREY) glColor4f(0.3, 0.3, 0.3, 1.0); glBegin(GL_POLYGON); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 20.0, 0.0); glVertex3f(8.0*strlen(text), 20.0, 0.0); glVertex3f(8.0*strlen(text), 0.0, 0.0); glEnd(); glPopMatrix(); } char *c = text; unsigned int cnt; glColor4f(1.0, 1.0, 1.0, 1.0); for (cnt = 0; cnt < strlen(text); cnt++,c++) { glPushMatrix(); glBindTexture(GL_TEXTURE_2D, (*c)-31); //glRasterPos2i(x + cnt*9, y); glTranslatef((GLfloat)(x + cnt*8), (GLfloat)y, 1.0); glBegin(GL_POLYGON); glTexCoord2i(0.0, 1.0); glVertex2i(0, 0); glTexCoord2i(1.0, 1.0); glVertex2i(8, 0); glTexCoord2i(1.0, 0.0); glVertex2i(8, 16); glTexCoord2i(0.0, 0.0); glVertex2i(0, 16); glEnd(); glPopMatrix(); } glBindTexture(GL_TEXTURE_2D, 0); }