Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <gl/glut.h>
  4. #include <math.h>
  5. #include <string.h>
  6. int vert = 1;
  7. int fac = 0;
  8. float vertice[5000][3];
  9. int face[5000][3];
  10. int altura = 420;
  11. int largura = 680;
  12. double angulo = 10*M_PI/180.0; // 10 graus
  13. FILE *arquivo;
  14. void myDisplay(void) {
  15. glClear(GL_COLOR_BUFFER_BIT);
  16.  
  17. glFlush();
  18. }
  19. /*
  20. criar função de rotação (parte basica)
  21.  
  22.  
  23. void rotacionarz(double value)
  24. {
  25.  
  26. x = round(point[0] * cos(value) - point[1] * sin(value));
  27. y = round(point[0] * sin(value) + point[1] * cos(value));
  28.  
  29.  
  30.  
  31. }
  32. void rotacionarx(double value)
  33. {
  34.  
  35. y = round(point[1] * cos(value) - point[2] * sin(value));
  36. z = round(point[1] * sin(value) + point[2] * cos(value));
  37.  
  38.  
  39.  
  40. }
  41.  
  42. void rotacionary(double value)
  43. {
  44.  
  45. x = round(point[2] * sin(value) + point[0] * cos(value));
  46. z = round(point[2] * cos(value) - point[0] * sin(value));
  47.  
  48. }
  49. */
  50. void lerArquivo(char *nomeArquivo){
  51. char linha[255];
  52. char *p;
  53. char *col1, *col2, *col3, *col4;
  54. if (( arquivo = fopen (nomeArquivo, "r")) == NULL ) {
  55. printf("Erro ao abrir o arquivo");
  56. }else{
  57. while (!feof(arquivo)) {
  58. fgets(linha, 255, arquivo);
  59. switch(linha[0]){
  60. case '#':
  61. printf("Comentario\n");
  62. break;
  63. case 'v':
  64. col1 = strtok(linha+1, " \n");
  65. col2 = strtok(NULL, " \n");
  66. col3 = strtok(NULL, " \n");
  67. printf ("vertice: %s %s %s", col1, col2, col3);
  68. vertice[vert][0] = atof(col1);
  69. vertice[vert][1] = atof(col2);
  70. vertice[vert][2] = atof(col3);
  71. vert ++;
  72. //while(p) {
  73. // vert = atof(p);
  74. // printf ("%s ", p);
  75. // p = strtok(NULL, " \n");
  76. // }
  77. printf ("\n");
  78. break;
  79. case 'f':
  80. col1 = strtok(linha+1, " \n");
  81. col2 = strtok(NULL, " \n");
  82. col3 = strtok(NULL, " \n");
  83. printf ("face: %s %s %s", col1, col2, col3);
  84. face[fac][0] = atof(col1);
  85. face[fac][1] = atof(col2);
  86. face[fac][2] = atof(col3);
  87. fac ++;
  88. printf ("\n");
  89. //p = strtok(linha+1, " \n");
  90. // printf ("Face: ");
  91. // while(p) {
  92. // vert = atof(p);
  93. // printf ("%s ", p);
  94. // p = strtok(NULL," \n");
  95. // }
  96. // printf ("\n");
  97. }
  98.  
  99.  
  100.  
  101. }
  102. fclose(arquivo);
  103. }
  104.  
  105. }
  106.  
  107. void myInit(void) {
  108. glClearColor(1.0, 1.0, 1.0, 0.0);
  109. glColor3f(0.0f, 0.0f, 0.0f);
  110. glPointSize(2.0);
  111. glMatrixMode(GL_PROJECTION);
  112. glLoadIdentity();
  113. glOrtho(-largura/2.0, largura/2.0, -altura/2.0, altura/2.0, -150.0, 150.0);
  114. }
  115.  
  116.  
  117.  
  118.  
  119. void myMouse(int button, int state, int x, int y) {
  120.  
  121. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  122. lerArquivo("Cat.obj");
  123.  
  124. } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
  125. glClear(GL_COLOR_BUFFER_BIT);
  126. for(int i = 0; i < fac; i++){
  127. glBegin(GL_LINE_LOOP);
  128. glVertex3d(vertice[face[i][0]][0],vertice[face[i][0]][1], vertice[face[i][0]][2]);
  129. glVertex3d(vertice[face[i][1]][0],vertice[face[i][1]][1], vertice[face[i][1]][2]);
  130. glVertex3d(vertice[face[i][2]][0],vertice[face[i][2]][1], vertice[face[i][2]][2]);
  131. glEnd();
  132. glFlush();
  133. }
  134. printf("Desenhou?");
  135. }
  136. }
  137. void myKey(unsigned char key, int x, int y) {
  138. switch(key){
  139. case 'z':
  140. //rotacionarz(angulo);
  141. break;
  142. case 'x':
  143. //rotacionarx(angulo);
  144. break;
  145. case 'y':
  146. //rotacionary(angulo);
  147. break;
  148. }
  149.  
  150. }
  151. int main(int argc, char *argv[]) {
  152. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  153. glutInitWindowSize(largura,altura);
  154. glutInitWindowPosition(100, 150);
  155. glutCreateWindow("Ler Obj");
  156. glutDisplayFunc(myDisplay);
  157. glutMouseFunc(myMouse);
  158. glutKeyboardFunc(myKey);
  159. myInit();
  160. glutMainLoop();
  161. system("PAUSE");
  162. return EXIT_SUCCESS;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement