123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include "string3d.h"
- #include <GL/glut.h>
- #include "vec3.h"
- #include <stdio.h>
- #include <vector>
- #include "loadModel.h"
- //Declare static variable
- std::vector<vec3> string3d::letter[26];
- std::vector<vec3> string3d::number[10];
- //Constructor - Front color starts as white and back at black
- string3d::string3d():colorFront(1.0f, 1.0f, 1.0f)
- {
- align = 0;
- }
- //Set front color
- void string3d::setColorFront(float r, float g, float b)
- {
- colorFront.x = r;
- colorFront.y = g;
- colorFront.z = b;
- }
- //Set back color
- void string3d::setColorBack(float r, float g, float b)
- {
- colorBack.x = r;
- colorBack.y = g;
- colorBack.z = b;
- }
- //Align
- void string3d::setAlign(unsigned char gAlign) {align = gAlign;}
- //Draw text
- void string3d::draw(const char* text)
- {
- //New matrix
- glPushMatrix();
- //Calculate length
- int length = -1;
- while (text[++length] != '\0');
- //Align (left is 0, so it's not needed)
- switch (align)
- {
- //Center
- case 1:
- glTranslatef(-(0.5f *length /2), 0.0f, 0.0f);
- break;
- //Right
- case 2:
- glTranslatef(-(0.5f *length), 0.0f, 0.0f);
- break;
- }
- //For each letter in given text
- int i;
- for (i = 0; text[i] != '\0'; i++)
- {
- //Start drawing
- glBegin(GL_TRIANGLES);
- //Reference to current letter or number (to type less)
- std::vector<vec3> &l = text[i] >= '0' && text[i] <= '9' ?
- number[text[i] -'0']:
- letter[text[i] -'a'];
- //For each vertex in letter
- unsigned int j;
- for (j = 0; j < l.size(); j++)
- {
- //Vertex is on the back - give it back color
- if (l[j].z < 0)
- glColor3f(colorBack.x, colorBack.y, colorBack.z);
- //Vertex is on front - give it front color
- else
- glColor3f(colorFront.x, colorFront.y, colorFront.z);
- //Draw vertex
- glVertex3f(l[j].x, l[j].y, l[j].z);
- }
- //Stop drawing
- glEnd();
- //Move one letter rightwards (for now I set them as 0.5 width each)
- glTranslatef(0.5f, 0, 0);
- }
- //Remove local matrix
- glPopMatrix();
- }
- //Draw dynamic number
- void string3d::drawInt(int num)
- {
- /* Calculate length */
- //At least 1 digit
- int length = 1;
- int diviner = 10;
- //Find extra digits
- while (num /diviner > 0)
- {
- //Add digit
- length++;
- //Next digit
- diviner *= 10;
- }
- //Move to last known digit
- diviner /= 10;
- /* Calculate text */
- //Create pointer array of length+1 (for \0)
- char *text = (char*) malloc( sizeof(char) *(length+1) );
- //For each digit
- int i;
- for (i = 0; i < length; i++)
- {
- //Convert digit to char
- text[i] = '0' +(num /diviner %10);
- //Previous digit
- diviner /= 10;
- }
- //Add ending character
- text[length] = '\0';
- //Draw number
- draw(text);
- free(text);
- }
- //Static initialization
- void string3d::init()
- {
- //Path of font (where `?` is the letter)
- char text[] = "obj/font/letter_?.obj";
- //For each letter
- char i;
- for (i = 'a'; i <= 'z'; i++)
- {
- //Change `?` in string with letter and load it
- text[16] = i;
- loadOBJ(text, letter[i -'a'], NULL, 0);
- }
- //For each number
- for (i = '0'; i <= '9'; i++)
- {
- //Change `?` in string with number and load it
- text[16] = i;
- loadOBJ(text, number[i -'0'], NULL, 0);
- }
- }
|