Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <gl/glut.h>
- #include <math.h>
- #include <string.h>
- int vert = 1;
- int fac = 0;
- float vertice[5000][3];
- int face[5000][3];
- int altura = 420;
- int largura = 680;
- double angulo = 10*M_PI/180.0; // 10 graus
- FILE *arquivo;
- void myDisplay(void) {
- glClear(GL_COLOR_BUFFER_BIT);
- glFlush();
- }
- /*
- criar função de rotação (parte basica)
- void rotacionarz(double value)
- {
- x = round(point[0] * cos(value) - point[1] * sin(value));
- y = round(point[0] * sin(value) + point[1] * cos(value));
- }
- void rotacionarx(double value)
- {
- y = round(point[1] * cos(value) - point[2] * sin(value));
- z = round(point[1] * sin(value) + point[2] * cos(value));
- }
- void rotacionary(double value)
- {
- x = round(point[2] * sin(value) + point[0] * cos(value));
- z = round(point[2] * cos(value) - point[0] * sin(value));
- }
- */
- void lerArquivo(char *nomeArquivo){
- char linha[255];
- char *p;
- char *col1, *col2, *col3, *col4;
- if (( arquivo = fopen (nomeArquivo, "r")) == NULL ) {
- printf("Erro ao abrir o arquivo");
- }else{
- while (!feof(arquivo)) {
- fgets(linha, 255, arquivo);
- switch(linha[0]){
- case '#':
- printf("Comentario\n");
- break;
- case 'v':
- col1 = strtok(linha+1, " \n");
- col2 = strtok(NULL, " \n");
- col3 = strtok(NULL, " \n");
- printf ("vertice: %s %s %s", col1, col2, col3);
- vertice[vert][0] = atof(col1);
- vertice[vert][1] = atof(col2);
- vertice[vert][2] = atof(col3);
- vert ++;
- //while(p) {
- // vert = atof(p);
- // printf ("%s ", p);
- // p = strtok(NULL, " \n");
- // }
- printf ("\n");
- break;
- case 'f':
- col1 = strtok(linha+1, " \n");
- col2 = strtok(NULL, " \n");
- col3 = strtok(NULL, " \n");
- printf ("face: %s %s %s", col1, col2, col3);
- face[fac][0] = atof(col1);
- face[fac][1] = atof(col2);
- face[fac][2] = atof(col3);
- fac ++;
- printf ("\n");
- //p = strtok(linha+1, " \n");
- // printf ("Face: ");
- // while(p) {
- // vert = atof(p);
- // printf ("%s ", p);
- // p = strtok(NULL," \n");
- // }
- // printf ("\n");
- }
- }
- fclose(arquivo);
- }
- }
- void myInit(void) {
- glClearColor(1.0, 1.0, 1.0, 0.0);
- glColor3f(0.0f, 0.0f, 0.0f);
- glPointSize(2.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-largura/2.0, largura/2.0, -altura/2.0, altura/2.0, -150.0, 150.0);
- }
- void myMouse(int button, int state, int x, int y) {
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
- lerArquivo("Cat.obj");
- } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
- glClear(GL_COLOR_BUFFER_BIT);
- for(int i = 0; i < fac; i++){
- glBegin(GL_LINE_LOOP);
- glVertex3d(vertice[face[i][0]][0],vertice[face[i][0]][1], vertice[face[i][0]][2]);
- glVertex3d(vertice[face[i][1]][0],vertice[face[i][1]][1], vertice[face[i][1]][2]);
- glVertex3d(vertice[face[i][2]][0],vertice[face[i][2]][1], vertice[face[i][2]][2]);
- glEnd();
- glFlush();
- }
- printf("Desenhou?");
- }
- }
- void myKey(unsigned char key, int x, int y) {
- switch(key){
- case 'z':
- //rotacionarz(angulo);
- break;
- case 'x':
- //rotacionarx(angulo);
- break;
- case 'y':
- //rotacionary(angulo);
- break;
- }
- }
- int main(int argc, char *argv[]) {
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(largura,altura);
- glutInitWindowPosition(100, 150);
- glutCreateWindow("Ler Obj");
- glutDisplayFunc(myDisplay);
- glutMouseFunc(myMouse);
- glutKeyboardFunc(myKey);
- myInit();
- glutMainLoop();
- system("PAUSE");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement