string3d.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "string3d.h"
  2. #include <GL/glut.h>
  3. #include "vec3.h"
  4. #include <stdio.h>
  5. #include <vector>
  6. #include "loadModel.h"
  7. //Declare static variable
  8. std::vector<vec3> string3d::letter[26];
  9. std::vector<vec3> string3d::number[10];
  10. //Constructor - Front color starts as white and back at black
  11. string3d::string3d():colorFront(1.0f, 1.0f, 1.0f)
  12. {
  13. align = 0;
  14. }
  15. //Set front color
  16. void string3d::setColorFront(float r, float g, float b)
  17. {
  18. colorFront.x = r;
  19. colorFront.y = g;
  20. colorFront.z = b;
  21. }
  22. //Set back color
  23. void string3d::setColorBack(float r, float g, float b)
  24. {
  25. colorBack.x = r;
  26. colorBack.y = g;
  27. colorBack.z = b;
  28. }
  29. //Align
  30. void string3d::setAlign(unsigned char gAlign) {align = gAlign;}
  31. //Draw text
  32. void string3d::draw(const char* text)
  33. {
  34. //New matrix
  35. glPushMatrix();
  36. //Calculate length
  37. int length = -1;
  38. while (text[++length] != '\0');
  39. //Align (left is 0, so it's not needed)
  40. switch (align)
  41. {
  42. //Center
  43. case 1:
  44. glTranslatef(-(0.5f *length /2), 0.0f, 0.0f);
  45. break;
  46. //Right
  47. case 2:
  48. glTranslatef(-(0.5f *length), 0.0f, 0.0f);
  49. break;
  50. }
  51. //For each letter in given text
  52. int i;
  53. for (i = 0; text[i] != '\0'; i++)
  54. {
  55. //Start drawing
  56. glBegin(GL_TRIANGLES);
  57. //Reference to current letter or number (to type less)
  58. std::vector<vec3> &l = text[i] >= '0' && text[i] <= '9' ?
  59. number[text[i] -'0']:
  60. letter[text[i] -'a'];
  61. //For each vertex in letter
  62. unsigned int j;
  63. for (j = 0; j < l.size(); j++)
  64. {
  65. //Vertex is on the back - give it back color
  66. if (l[j].z < 0)
  67. glColor3f(colorBack.x, colorBack.y, colorBack.z);
  68. //Vertex is on front - give it front color
  69. else
  70. glColor3f(colorFront.x, colorFront.y, colorFront.z);
  71. //Draw vertex
  72. glVertex3f(l[j].x, l[j].y, l[j].z);
  73. }
  74. //Stop drawing
  75. glEnd();
  76. //Move one letter rightwards (for now I set them as 0.5 width each)
  77. glTranslatef(0.5f, 0, 0);
  78. }
  79. //Remove local matrix
  80. glPopMatrix();
  81. }
  82. //Draw dynamic number
  83. void string3d::drawInt(int num)
  84. {
  85. /* Calculate length */
  86. //At least 1 digit
  87. int length = 1;
  88. int diviner = 10;
  89. //Find extra digits
  90. while (num /diviner > 0)
  91. {
  92. //Add digit
  93. length++;
  94. //Next digit
  95. diviner *= 10;
  96. }
  97. //Move to last known digit
  98. diviner /= 10;
  99. /* Calculate text */
  100. //Create pointer array of length+1 (for \0)
  101. char *text = (char*) malloc( sizeof(char) *(length+1) );
  102. //For each digit
  103. int i;
  104. for (i = 0; i < length; i++)
  105. {
  106. //Convert digit to char
  107. text[i] = '0' +(num /diviner %10);
  108. //Previous digit
  109. diviner /= 10;
  110. }
  111. //Add ending character
  112. text[length] = '\0';
  113. //Draw number
  114. draw(text);
  115. free(text);
  116. }
  117. //Static initialization
  118. void string3d::init()
  119. {
  120. //Path of font (where `?` is the letter)
  121. char text[] = "obj/font/letter_?.obj";
  122. //For each letter
  123. char i;
  124. for (i = 'a'; i <= 'z'; i++)
  125. {
  126. //Change `?` in string with letter and load it
  127. text[16] = i;
  128. loadOBJ(text, letter[i -'a'], NULL, 0);
  129. }
  130. //For each number
  131. for (i = '0'; i <= '9'; i++)
  132. {
  133. //Change `?` in string with number and load it
  134. text[16] = i;
  135. loadOBJ(text, number[i -'0'], NULL, 0);
  136. }
  137. }