Posted By

Desoxena on 06/07/11


Tagged

text opengl texture


Versions (?)

Make a texture plate from a text file


 / Published in: C++
 

simple openGL script that makes a texture plate out of a text file.

  1. #include <string>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <Windows.h>
  5. #include <glut.h>
  6. #include <GL\GLU.h>
  7.  
  8. using namespace std;
  9.  
  10. const int dimensions = 14;
  11. int texture[10][dimensions*2][dimensions*2];
  12. char Ctexture[dimensions][dimensions];
  13. float R[] = {0, 1, 0, 1, 1, 0, .5, 1, .9, .5}; //black, white, blue, red, yellow, green, purple, pink, orange, gray
  14. float G[] = {0, 1, 0, 0, 1, 1, 0, 0, .5, .5}; // 0 1 2 3 4 5 6 7 8 9
  15. float B[] = {0, 1, 1, 0, 0, 0, .5, .8, 0, .5};
  16. float plateSize = 1;
  17.  
  18.  
  19. void load_plate(const char *_filename, int ID){
  20. ifstream myFile;
  21. myFile.open(_filename);
  22. string fileLine[dimensions];
  23. int i = 0;
  24. while(myFile.good()){
  25. getline(myFile, fileLine[i]);
  26. i++;
  27. }
  28. for (int a = 0; a < dimensions; a++) {
  29. int e = 0;
  30. for (int b = 0; b < dimensions*2-1; b+=2) {
  31. Ctexture[a][e] = fileLine[a].at(b);
  32. e++;
  33. }
  34. }
  35. for (int a = 0; a < dimensions; a++) {
  36. for (int b = 0; b < dimensions; b++) {
  37. if (Ctexture[a][b] == '0') { texture[ID][a][b] = 0;}
  38. else if (Ctexture[a][b] == '1') { texture[ID][a][b] = 1;}
  39. else if (Ctexture[a][b] == '2') { texture[ID][a][b] = 2;}
  40. else if (Ctexture[a][b] == '3') { texture[ID][a][b] = 3;}
  41. else if (Ctexture[a][b] == '4') { texture[ID][a][b] = 4;}
  42. else if (Ctexture[a][b] == '5') { texture[ID][a][b] = 5;}
  43. else if (Ctexture[a][b] == '6') { texture[ID][a][b] = 6;}
  44. else if (Ctexture[a][b] == '7') { texture[ID][a][b] = 7;}
  45. else if (Ctexture[a][b] == '8') { texture[ID][a][b] = 8;}
  46. else if (Ctexture[a][b] == '9') { texture[ID][a][b] = 9;}
  47. }
  48. }
  49. myFile.close();
  50. }
  51. void drawSquare(float x, float y, float z) {
  52. glPushMatrix();
  53. glBegin(GL_QUADS);
  54. glVertex3f(x, y, z);
  55. glVertex3f(x, y+plateSize/dimensions, z);
  56. glVertex3f(x+plateSize/dimensions, y+plateSize/dimensions, z);
  57. glVertex3f(x+plateSize/dimensions, y, z);
  58. glEnd();
  59. glPopMatrix();
  60. }
  61. void Reshape(int height, int width) {
  62. glClearColor(0, 0, 0, 1);
  63. glMatrixMode(GL_PROJECTION);
  64. glLoadIdentity();
  65. gluPerspective(60, (float)height/(float)width, 1, 100);
  66. glMatrixMode(GL_MODELVIEW);
  67. }
  68. void drawPlate(float x, float y, float z, int ID) {
  69. for (int i = 0; i < dimensions; i++) {
  70. for (int j = 0; j < dimensions; j++) {
  71. glColor3d(R[texture[ID][i][j]], G[texture[ID][i][j]], B[texture[ID][i][j]]);
  72. drawSquare(x+(j*(plateSize/dimensions)), y+(i*(plateSize/dimensions)), z);
  73. }
  74. }
  75. }
  76. void Display(void){
  77. glClear(GL_COLOR_BUFFER_BIT);
  78. load_plate("texture1.txt", 1);
  79. drawPlate(0, 0, -4, 1);
  80. glutSwapBuffers();
  81. }
  82. void Keyboard(unsigned char key, int x, int y) {
  83. switch(key){
  84. case'a':
  85. plateSize += .05;
  86. glutPostRedisplay();
  87. break;
  88. case's':
  89. plateSize -= .05;
  90. glutPostRedisplay();
  91. break;
  92. }
  93. }
  94. int main(int argc, char **argv){
  95. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  96. glutInit(&argc, argv);
  97. glutInitWindowSize(500, 500);
  98. glutInitWindowPosition(100, 100);
  99. glutCreateWindow("Texture plate tests");
  100. glutReshapeFunc(Reshape);
  101. glutDisplayFunc(Display);
  102. glutKeyboardFunc(Keyboard);
  103. glutMainLoop();
  104. }
  105.  
  106.  
  107.  
  108.  
  109. texture1.txt
  110. --------------------
  111. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  112. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  113. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  114. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  115. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  116. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  117. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  118. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  119. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  120. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  121. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  122. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  123. 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  124. 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Report this snippet  

You need to login to post a comment.