Facebook
From Funky Tamarin, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 264
  1. #define GLUT_DISABLE_ATEXIT_HACK
  2. #include <GL/gl.h>
  3. #include <GL/glu.h>
  4. #include <GL/glut.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include <ctime>
  10. #include <malloc.h>
  11. #include <math.h>
  12.  
  13. #include "Targa.h"
  14.  
  15. int screenWidth = 600;
  16. int screenHeight = 600;
  17. int fps = 60;
  18.  
  19. GLdouble cameraPos[] = {0.0, 0.0, 25.0};
  20. double rotation = 0.0;
  21.  
  22. GLfloat lightAmb[] = {1.0, 0.0, 0.0, 1.0};
  23. GLfloat lightDif[] = {0.7, 0.7, 0.7, 1.0};
  24. GLfloat lightPos[] = {10, 10, 10, 1.0};
  25. GLfloat lightSpec[] = {1, 1, 1, 1};
  26.  
  27. GLuint tex[1];
  28.  
  29. typedef struct {
  30.     unsigned short vertices;
  31.     unsigned short normals;
  32.     unsigned short indices;
  33.     unsigned short texCoords;
  34.  
  35.     float verticesArray[10000];
  36.     float normalsArray[10000];
  37.     unsigned short indicesArray[10000];
  38.     float texCoordsArray[10000];
  39. } _3DSOBJ;
  40.  
  41. _3DSOBJ *ship;
  42.  
  43. _3DSOBJ *load3DSObject(const char *filename){
  44.     FILE *f = fopen(filename, "rb");
  45.     _3DSOBJ *object = (_3DSOBJ*)malloc(sizeof(_3DSOBJ));
  46.  
  47.     int size;
  48.     unsigned short id = 0;
  49.     unsigned int next = 0;
  50.  
  51.     if(f != NULL){
  52.         fseek(f, 0, SEEK_END);
  53.         size = ftell(f);
  54.         fseek(f, 0, SEEK_SET);
  55.  
  56.         while(ftell(f) < size){
  57.             id = 0;
  58.             next = 0;
  59.  
  60.             fread(&id, 2, 1, f);
  61.             fread(&next, 4, 1, f);
  62.  
  63.             switch(id){
  64.                 case 0x4D4D:
  65.                     printf("MAIN3DS\n");
  66.                     break;
  67.                 case 0x3D3D:
  68.                     printf("EDIT3DS\n");
  69.                     break;
  70.                 case 0x4000:
  71.                     printf("EDIT_OBJECT\n");
  72.                     char c;
  73.                     printf("\tObject name: ");
  74.                     do {
  75.                         fread(&c, 1, 1, f);
  76.                         printf("%c", c);
  77.                     } while(c != '\0');
  78.                     printf("\n");
  79.                     break;
  80.                 case 0x4100:
  81.                     printf("OBJ_TRIMESH\n");
  82.                     break;
  83.                 case 0x4110:
  84.                     printf("TRI_VERTEXL\n");
  85.                     fread(&object->vertices, sizeof(unsigned short), 1, f);
  86.                     printf("\tVertices: %d\n", object->vertices);
  87.                     for(int i = 0; i < object->vertices * 3; i++){
  88.                         fread(&object->verticesArray[i], sizeof(float), 1, f);
  89.                         if((i + 1) % 3 == 0)
  90.                             printf("\t\tVertex %d: (%.3f, %.3f, %.3f)\n", (i + 1) / 3, object->verticesArray[i - 2], object->verticesArray[i - 1], object->verticesArray[i]);
  91.                     }
  92.                     break;
  93.                 case 0x4120:
  94.                     printf("TRI_POLYGONL\n");
  95.                     fread(&object->indices, sizeof(unsigned short), 1, f);
  96.                     printf("\tIndices: %d\n", object->indices);
  97.                     GLushort tmp;
  98.                     int i2;
  99.                     i2 = 0;
  100.                     for(int i = 0; i < object->indices * 4; i++){
  101.                         if((i + 1) % 4 == 0) {
  102.                             fread(&tmp, sizeof(unsigned short), 1, f);
  103.                             printf("\t\tPolygon %d: (%d, %d, %d)\n", i2 / 3, object->indicesArray[i2 - 3], object->indicesArray[i2 - 2], object->indicesArray[i2 - 1]);
  104.                         } else {
  105.                             fread(&object->indicesArray[i2], sizeof(unsigned short), 1, f);
  106.                             i2++;
  107.                         }
  108.                     }
  109.                     break;
  110.                 case 0x4140:
  111.                     printf("TRI_MAPPINGCOORS\n");
  112.                     fread(&object->texCoords, sizeof(unsigned short), 1, f);
  113.                     printf("\tTex indices: %d\n", object->texCoords);
  114.                     for(int i = 0; i < object->texCoords * 2; i++){
  115.                         fread(&object->texCoordsArray[i], sizeof(float), 1, f);
  116.                         if((i + 1) % 2 == 0)
  117.                             printf("\t\tTex index %d: (%.3f, %.3f)\n", (i + 1) / 2, object->texCoordsArray[i - 1], object->texCoordsArray[i]);
  118.                     }
  119.                     break;
  120.                 default:
  121.                     fseek(f, next - 6, SEEK_CUR);
  122.             }
  123.         }
  124.  
  125.         for(int i = 0; i < object->indices; i++){
  126.                 GLfloat p1x = object->verticesArray[object->indicesArray[i * 3] * 3];
  127.                 GLfloat p1y = object->verticesArray[object->indicesArray[i * 3] * 3 + 1];
  128.                 GLfloat p1z = object->verticesArray[object->indicesArray[i * 3] * 3 + 2];
  129.  
  130.                 GLfloat p2x = object->verticesArray[object->indicesArray[i * 3 + 1] * 3];
  131.                 GLfloat p2y = object->verticesArray[object->indicesArray[i * 3 + 1] * 3 + 1];
  132.                 GLfloat p2z = object->verticesArray[object->indicesArray[i * 3 + 1] * 3 + 2];
  133.  
  134.                 GLfloat p3x = object->verticesArray[object->indicesArray[i * 3 + 2] * 3];
  135.                 GLfloat p3y = object->verticesArray[object->indicesArray[i * 3 + 2] * 3 + 1];
  136.                 GLfloat p3z = object->verticesArray[object->indicesArray[i * 3 + 2] * 3 + 2];
  137.  
  138.                 GLfloat nx1 = (p2y - p1y) * (p3z - p1z) - (p2z - p1z) * (p3y - p1y);
  139.                 GLfloat ny1 = (p2z - p1z) * (p3x - p1x) - (p2x - p1x) * (p3z - p1z);
  140.                 GLfloat nz1 = (p2x - p1x) * (p3y - p1y) - (p2y - p1y) * (p3x - p1x);
  141.  
  142.                 GLfloat l1 = sqrtf(powf(nx1, 2) + powf(ny1, 2) + powf(ny1, 2));
  143.  
  144.                 nx1 /= l1;
  145.                 ny1 /= l1;
  146.                 nz1 /= l1;
  147.  
  148.                 GLfloat nx2 = (p1y - p2y) * (p3z - p2z) - (p1z - p2z) * (p3y - p2y);
  149.                 GLfloat ny2 = (p1z - p2z) * (p3x - p2x) - (p1x - p2x) * (p3z - p2z);
  150.                 GLfloat nz2 = (p1x - p2x) * (p3y - p2y) - (p1y - p2y) * (p3x - p2x);
  151.  
  152.                 GLfloat l2 = sqrtf(pow(nx2, 2) + pow(ny2, 2) + pow(ny2, 2));
  153.  
  154.                 nx2 /= l2;
  155.                 ny2 /= l2;
  156.                 nz2 /= l2;
  157.  
  158.                 GLfloat nx3 = (p2y - p3y) * (p1z - p3z) - (p2z - p3z) * (p1y - p3y);
  159.                 GLfloat ny3 = (p2z - p3z) * (p1x - p3x) - (p2x - p3x) * (p1z - p3z);
  160.                 GLfloat nz3 = (p2x - p3x) * (p1y - p3y) - (p2y - p3y) * (p1x - p3x);
  161.  
  162.                 GLfloat l3 = sqrtf(pow(nx3, 2) + pow(ny3, 2) + pow(ny3, 2));
  163.  
  164.                 nx3 /= l3;
  165.                 ny3 /= l3;
  166.                 nz3 /= l3;
  167.  
  168.                 object->normals += 9;
  169.                 object->normalsArray[i * 9] = nx1;
  170.                 object->normalsArray[i * 9 + 1] = ny1;
  171.                 object->normalsArray[i * 9 + 2] = nz1;
  172.                 object->normalsArray[i * 9 + 3] = nx2;
  173.                 object->normalsArray[i * 9 + 4] = ny2;
  174.                 object->normalsArray[i * 9 + 5] = nz2;
  175.                 object->normalsArray[i * 9 + 6] = nx3;
  176.                 object->normalsArray[i * 9 + 7] = ny3;
  177.                 object->normalsArray[i * 9 + 8] = nz3;
  178.             }
  179.  
  180.         fclose(f);
  181.  
  182.         return object;
  183.     } else {
  184.         printf("Can't open file\n");
  185.         return NULL;
  186.     }
  187. }
  188.  
  189. void init(){
  190.     glEnable(GL_DEPTH_TEST);
  191.         glEnable(GL_TEXTURE_2D);
  192.     glEnable(GL_NORMALIZE);
  193.     glEnable(GL_LIGHTING);
  194.         glEnable(GL_COLOR_MATERIAL);
  195.     glEnable(GL_AUTO_NORMAL);
  196.     glEnable(GL_NORMALIZE);
  197.         glEnable(GL_LIGHT0);
  198.  
  199.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmb);
  200.     glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
  201.         glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDif);
  202.         glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  203.         glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec);
  204.  
  205.         glMaterialfv(GL_FRONT, GL_SPECULAR, lightSpec);
  206.         glMateriali(GL_FRONT, GL_SHININESS, 100);
  207.  
  208.         glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  209.  
  210.         glGenTextures(1, tex);
  211.         glBindTexture(GL_TEXTURE_2D, tex[0]);
  212.         LoadTGATexture("statek2.tga");
  213.  
  214.         ship = load3DSObject("obiekt.3DS");
  215.         if(ship != NULL) {
  216.         printf("Success!!\n");
  217.         }
  218. }
  219.  
  220. void display() {
  221.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  222.     glClearColor(0.5, 0.5, 0.5, 1.0);
  223.     glLoadIdentity ();
  224.  
  225.     gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  226.  
  227.     glPointSize(3);
  228.     glScalef(0.05, 0.05, 0.05);
  229.         glRotatef(rotation, 1, 1, 1);
  230.  
  231.     glBegin(GL_TRIANGLES);
  232.         for(int i = 0; i < ship->indices * 3; i++){
  233.             glTexCoord2fv(&ship->texCoordsArray[ship->indicesArray[i] * 2]);
  234.             glNormal3fv(&ship->normalsArray[ship->indicesArray[i] * 3]);
  235.             glVertex3fv(&ship->verticesArray[ship->indicesArray[i] * 3]);
  236.         }
  237.     glEnd();
  238.  
  239.     glFlush();
  240.     glutPostRedisplay();
  241.     glutSwapBuffers();
  242. }
  243.  
  244. void reshape(int w, int h) {
  245.     glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  246.     glMatrixMode(GL_PROJECTION);
  247.     glLoadIdentity();
  248.  
  249.     gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.5, 50.0);
  250.  
  251.     glMatrixMode(GL_MODELVIEW);
  252. }
  253.  
  254. void keyboard(unsigned char key, int x, int y){
  255.     switch(key){
  256.         case 27 :
  257.         case 'q':
  258.             free(ship);
  259.             exit(0);
  260.             break;
  261.     }
  262. }
  263.  
  264. void timerfunc(int p){
  265.     rotation +=  1;
  266.  
  267.     glutPostRedisplay();
  268.     glutTimerFunc(1000/fps, &timerfunc, 0);
  269. }
  270.  
  271. void menu(int value) {
  272.     switch(value) {
  273.         case 0:
  274.             exit(0);
  275.             break;
  276.     }
  277. }
  278.  
  279. int main(int argc, char *argv[]) {
  280.     srand(time(NULL));
  281.     glutInit(&argc, argv);
  282.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  283.     glutInitWindowSize(screenWidth, screenHeight);
  284.     glutCreateWindow(argv[0]);
  285.     glutDisplayFunc(display);
  286.     glutReshapeFunc(reshape);
  287.     glutKeyboardFunc(keyboard);
  288.     glutTimerFunc(1000/fps, &timerfunc, 0);
  289.  
  290.     glutCreateMenu(menu);
  291.     glutAddMenuEntry("Wyjscie", 0);
  292.  
  293.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  294.  
  295.     init();
  296.  
  297.     glutMainLoop();
  298.  
  299.     return 0;
  300. }
  301.