Advertisement
Guest User

3dmax

a guest
Jun 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.21 KB | None | 0 0
  1. #include <GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <ctime>
  9. #include <malloc.h>
  10. #include <math.h>
  11.  
  12.  
  13.  
  14. int screenWidth = 600;
  15. int screenHeight = 600;
  16. int fps = 60;
  17.  
  18. GLdouble cameraPos[] = {0.0, 0.0, 25.0};
  19. double rotation = 0.0;
  20.  
  21. GLfloat lightAmb[] = {1.0, 0.0, 0.0, 1.0};
  22. GLfloat lightDif[] = {0.7, 0.7, 0.7, 1.0};
  23. GLfloat lightPos[] = {10, 10, 10, 1.0};
  24. GLfloat lightSpec[] = {1, 1, 1, 1};
  25.  
  26. GLuint tex[1];
  27.  
  28. typedef struct {
  29.     GLuint bpp;     // ilość bitów na piksel
  30.     GLuint width;   // rozmiary w pikselach
  31.     GLuint height;
  32. } TARGAINFO;
  33.  
  34.  
  35.  
  36. typedef struct {
  37.     unsigned short vertices;
  38.     unsigned short normals;
  39.     unsigned short indices;
  40.     unsigned short texCoords;
  41.  
  42.     float verticesArray[10000];
  43.     float normalsArray[10000];
  44.     unsigned short indicesArray[10000];
  45.     float texCoordsArray[10000];
  46. } _3DSOBJ;
  47.  
  48. _3DSOBJ *ship;
  49.  
  50.  
  51.  
  52.  
  53.  
  54. GLubyte *LoadTGAImage(const char *filename, TARGAINFO *info)
  55. {
  56.     GLubyte TGAHeader[12]={0,0,2,0,0,0,0,0,0,0,0,0};    // Nagłówek TGA bez kompresji
  57.     GLubyte TGACompare[12];         // Tu się załaduje dane z pliku
  58.     GLubyte Header[6];          // Pierwsze potrzebne 6 bajtów z pliku
  59.     GLubyte *Bits = NULL;   // Wskaźnik na bufor z danymi pikseli
  60.  
  61.     FILE *plik = fopen(filename, "rb"); // Próba otwarcia do odczytu
  62.     if(plik)
  63.     {
  64.         fread(TGACompare, 1, sizeof(TGACompare), plik); // Odczytanie nagłówka pliku
  65.         if(memcmp(TGAHeader, TGACompare, sizeof(TGAHeader)) == 0)   // Nagłówek identyczny?
  66.         {
  67.             fread(Header, 1, sizeof(Header), plik); // Odczyt użytecznych danych
  68.  
  69.             // Wyłuskanie informacji o rozmiarach
  70.             info->width  = Header[1] * 256 + Header[0];
  71.             info->height = Header[3] * 256 + Header[2];
  72.             info->bpp = Header[4];
  73.  
  74.             // Sprawdzenie czy rozmiary > 0 oraz czy bitmapa 24 lub 32-bitowa
  75.             if(info->width>0 && info->height>0 && (info->bpp==24 || info->bpp==32))
  76.             {
  77.                 long ImageSize = info->height * info->width * info->bpp / 8;    // Obliczenie ilości danych
  78.                 Bits = (GLubyte*)malloc(ImageSize); // Alokacja pamięci na dane
  79.  
  80.                 if(Bits)
  81.                 {
  82.                     fread(Bits, 1, ImageSize, plik);    // Odczyt właściwych danych pikseli z pliku
  83.  
  84.                     // Konwersja BGR -> RGB
  85.                     int i;
  86.                     GLubyte tmp;    // Miejsce przechowania jednej wartości koloru
  87.  
  88.                     for(i=0;i < ImageSize;i += info->bpp / 8)   // Wszystkie wartości RGB lub RGBA
  89.                     {
  90.                         tmp = Bits[i];
  91.                         Bits[i] = Bits[i+2];
  92.                         Bits[i+2] = tmp;
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.  
  98.         fclose(plik);
  99.     }
  100.  
  101.     return(Bits);
  102. }
  103.  
  104.  
  105. bool LoadTGATexture(const char *filename)
  106. {
  107.     TARGAINFO info; // Dane o bitmapie
  108.     GLubyte *bits;  // Dane o pikselach
  109.  
  110.     // ładowanie pliku
  111.     bits = LoadTGAImage(filename, &info);   // Próba wczytania tekstury
  112.     if(bits == NULL)    return(FALSE);  // ERROR
  113.  
  114.     // Ustawienie parametrów tekstury
  115.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  116.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  117.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  118.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  119.  
  120.     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  121.  
  122.     if(info.bpp == 24)  // Bitmapa z danymi RGB
  123.         glTexImage2D(GL_TEXTURE_2D, 0, 3, info.width, info.height, 0, GL_RGB, GL_UNSIGNED_BYTE, bits);
  124.     else    // Bitmapa z danymi RGBA
  125.         glTexImage2D(GL_TEXTURE_2D, 0, 4, info.width, info.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);
  126.  
  127.     free(bits);
  128.     return(TRUE);
  129. }
  130.  
  131. bool LoadTGAMipmap(const char *filename)
  132. {
  133.     TARGAINFO info; // Dane o bitmapie
  134.     GLubyte *bits;  // Dane o pikselach
  135.  
  136.     // ładowanie pliku
  137.     bits = LoadTGAImage(filename, &info);   // Próba wczytania tekstury
  138.     if(bits == NULL)    return(FALSE);  // ERROR
  139.  
  140.     // Ustawienie parametrów tekstury
  141.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  142.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  143.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  144.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
  145.  
  146.     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  147.  
  148.     if(info.bpp == 24)  // Bitmapa z danymi RGB
  149.         gluBuild2DMipmaps(GL_TEXTURE_2D, 3, info.width, info.height, GL_RGB, GL_UNSIGNED_BYTE, bits);
  150.     else    // Bitmapa z danymi RGBA
  151.         gluBuild2DMipmaps(GL_TEXTURE_2D, 4, info.width, info.height, GL_RGBA, GL_UNSIGNED_BYTE, bits);
  152.  
  153.     free(bits);
  154.     return(TRUE);
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. _3DSOBJ *load3DSObject(const char *filename){
  163.     FILE *f = fopen(filename, "rb");
  164.     _3DSOBJ *object = (_3DSOBJ*)malloc(sizeof(_3DSOBJ));
  165.  
  166.     int size;
  167.     unsigned short id = 0;
  168.     unsigned int next = 0;
  169.  
  170.     if(f != NULL){
  171.         fseek(f, 0, SEEK_END);
  172.         size = ftell(f);
  173.         fseek(f, 0, SEEK_SET);
  174.  
  175.         while(ftell(f) < size){
  176.             id = 0;
  177.             next = 0;
  178.  
  179.             fread(&id, 2, 1, f);
  180.             fread(&next, 4, 1, f);
  181.  
  182.             switch(id){
  183.                 case 0x4D4D:
  184.                     printf("MAIN3DS\n");
  185.                     break;
  186.                 case 0x3D3D:
  187.                     printf("EDIT3DS\n");
  188.                     break;
  189.                 case 0x4000:
  190.                     printf("EDIT_OBJECT\n");
  191.                     char c;
  192.                     printf("\tObject name: ");
  193.                     do {
  194.                         fread(&c, 1, 1, f);
  195.                         printf("%c", c);
  196.                     } while(c != '\0');
  197.                     printf("\n");
  198.                     break;
  199.                 case 0x4100:
  200.                     printf("OBJ_TRIMESH\n");
  201.                     break;
  202.                 case 0x4110:
  203.                     printf("TRI_VERTEXL\n");
  204.                     fread(&object->vertices, sizeof(unsigned short), 1, f);
  205.                     printf("\tVertices: %d\n", object->vertices);
  206.                     for(int i = 0; i < object->vertices * 3; i++){
  207.                         fread(&object->verticesArray[i], sizeof(float), 1, f);
  208.                         if((i + 1) % 3 == 0)
  209.                             printf("\t\tVertex %d: (%.3f, %.3f, %.3f)\n", (i + 1) / 3, object->verticesArray[i - 2], object->verticesArray[i - 1], object->verticesArray[i]);
  210.                     }
  211.                     break;
  212.                 case 0x4120:
  213.                     printf("TRI_POLYGONL\n");
  214.                     fread(&object->indices, sizeof(unsigned short), 1, f);
  215.                     printf("\tIndices: %d\n", object->indices);
  216.                     GLushort tmp;
  217.                     int i2;
  218.                     i2 = 0;
  219.                     for(int i = 0; i < object->indices * 4; i++){
  220.                         if((i + 1) % 4 == 0) {
  221.                             fread(&tmp, sizeof(unsigned short), 1, f);
  222.                             printf("\t\tPolygon %d: (%d, %d, %d)\n", i2 / 3, object->indicesArray[i2 - 3], object->indicesArray[i2 - 2], object->indicesArray[i2 - 1]);
  223.                         } else {
  224.                             fread(&object->indicesArray[i2], sizeof(unsigned short), 1, f);
  225.                             i2++;
  226.                         }
  227.                     }
  228.                     break;
  229.                 case 0x4140:
  230.                     printf("TRI_MAPPINGCOORS\n");
  231.                     fread(&object->texCoords, sizeof(unsigned short), 1, f);
  232.                     printf("\tTex indices: %d\n", object->texCoords);
  233.                     for(int i = 0; i < object->texCoords * 2; i++){
  234.                         fread(&object->texCoordsArray[i], sizeof(float), 1, f);
  235.                         if((i + 1) % 2 == 0)
  236.                             printf("\t\tTex index %d: (%.3f, %.3f)\n", (i + 1) / 2, object->texCoordsArray[i - 1], object->texCoordsArray[i]);
  237.                     }
  238.                     break;
  239.                 default:
  240.                     fseek(f, next - 6, SEEK_CUR);
  241.             }
  242.         }
  243.  
  244.         for(int i = 0; i < object->indices; i++){
  245.                 GLfloat p1x = object->verticesArray[object->indicesArray[i * 3] * 3];
  246.                 GLfloat p1y = object->verticesArray[object->indicesArray[i * 3] * 3 + 1];
  247.                 GLfloat p1z = object->verticesArray[object->indicesArray[i * 3] * 3 + 2];
  248.  
  249.                 GLfloat p2x = object->verticesArray[object->indicesArray[i * 3 + 1] * 3];
  250.                 GLfloat p2y = object->verticesArray[object->indicesArray[i * 3 + 1] * 3 + 1];
  251.                 GLfloat p2z = object->verticesArray[object->indicesArray[i * 3 + 1] * 3 + 2];
  252.  
  253.                 GLfloat p3x = object->verticesArray[object->indicesArray[i * 3 + 2] * 3];
  254.                 GLfloat p3y = object->verticesArray[object->indicesArray[i * 3 + 2] * 3 + 1];
  255.                 GLfloat p3z = object->verticesArray[object->indicesArray[i * 3 + 2] * 3 + 2];
  256.  
  257.                 GLfloat nx1 = (p2y - p1y) * (p3z - p1z) - (p2z - p1z) * (p3y - p1y);
  258.                 GLfloat ny1 = (p2z - p1z) * (p3x - p1x) - (p2x - p1x) * (p3z - p1z);
  259.                 GLfloat nz1 = (p2x - p1x) * (p3y - p1y) - (p2y - p1y) * (p3x - p1x);
  260.  
  261.                 GLfloat l1 = sqrtf(powf(nx1, 2) + powf(ny1, 2) + powf(ny1, 2));
  262.  
  263.                 nx1 /= l1;
  264.                 ny1 /= l1;
  265.                 nz1 /= l1;
  266.  
  267.                 GLfloat nx2 = (p1y - p2y) * (p3z - p2z) - (p1z - p2z) * (p3y - p2y);
  268.                 GLfloat ny2 = (p1z - p2z) * (p3x - p2x) - (p1x - p2x) * (p3z - p2z);
  269.                 GLfloat nz2 = (p1x - p2x) * (p3y - p2y) - (p1y - p2y) * (p3x - p2x);
  270.  
  271.                 GLfloat l2 = sqrtf(pow(nx2, 2) + pow(ny2, 2) + pow(ny2, 2));
  272.  
  273.                 nx2 /= l2;
  274.                 ny2 /= l2;
  275.                 nz2 /= l2;
  276.  
  277.                 GLfloat nx3 = (p2y - p3y) * (p1z - p3z) - (p2z - p3z) * (p1y - p3y);
  278.                 GLfloat ny3 = (p2z - p3z) * (p1x - p3x) - (p2x - p3x) * (p1z - p3z);
  279.                 GLfloat nz3 = (p2x - p3x) * (p1y - p3y) - (p2y - p3y) * (p1x - p3x);
  280.  
  281.                 GLfloat l3 = sqrtf(pow(nx3, 2) + pow(ny3, 2) + pow(ny3, 2));
  282.  
  283.                 nx3 /= l3;
  284.                 ny3 /= l3;
  285.                 nz3 /= l3;
  286.  
  287.                 object->normals += 9;
  288.                 object->normalsArray[i * 9] = nx1;
  289.                 object->normalsArray[i * 9 + 1] = ny1;
  290.                 object->normalsArray[i * 9 + 2] = nz1;
  291.                 object->normalsArray[i * 9 + 3] = nx2;
  292.                 object->normalsArray[i * 9 + 4] = ny2;
  293.                 object->normalsArray[i * 9 + 5] = nz2;
  294.                 object->normalsArray[i * 9 + 6] = nx3;
  295.                 object->normalsArray[i * 9 + 7] = ny3;
  296.                 object->normalsArray[i * 9 + 8] = nz3;
  297.             }
  298.  
  299.         fclose(f);
  300.  
  301.         return object;
  302.     } else {
  303.         printf("Can't open file\n");
  304.         return NULL;
  305.     }
  306. }
  307.  
  308. void init(){
  309.     glEnable(GL_DEPTH_TEST);
  310.     glEnable(GL_TEXTURE_2D);
  311.     glEnable(GL_NORMALIZE);
  312.     glEnable(GL_LIGHTING);
  313.     glEnable(GL_COLOR_MATERIAL);
  314.     glEnable(GL_AUTO_NORMAL);
  315.     glEnable(GL_NORMALIZE);
  316.     glEnable(GL_LIGHT0);
  317.  
  318.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmb);
  319.     glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
  320.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDif);
  321.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  322.     glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec);
  323.  
  324.     glMaterialfv(GL_FRONT, GL_SPECULAR, lightSpec);
  325.     glMateriali(GL_FRONT, GL_SHININESS, 100);
  326.  
  327.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  328.  
  329.     glGenTextures(1, tex);
  330.     glBindTexture(GL_TEXTURE_2D, tex[0]);
  331.     LoadTGATexture("tekstura.tga");
  332.  
  333.     ship = load3DSObject("obiekt.3DS");
  334.     if(ship != NULL) {
  335.         printf("Success!!\n");
  336.     }
  337. }
  338.  
  339. void display() {
  340.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  341.     glClearColor(0.5, 0.5, 0.5, 1.0);
  342.     glLoadIdentity ();
  343.  
  344.     gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  345.  
  346.     glPointSize(3);
  347.     glScalef(0.05, 0.05, 0.05);
  348.     glRotatef(rotation, 1, 1, 1);
  349.  
  350.     glBegin(GL_TRIANGLES);
  351.         for(int i = 0; i < ship->indices * 3; i++){
  352.             glTexCoord2fv(&ship->texCoordsArray[ship->indicesArray[i] * 2]);
  353.             glNormal3fv(&ship->normalsArray[ship->indicesArray[i] * 3]);
  354.             glVertex3fv(&ship->verticesArray[ship->indicesArray[i] * 3]);
  355.         }
  356.     glEnd();
  357.  
  358.     glFlush();
  359.     glutPostRedisplay();
  360.     glutSwapBuffers();
  361. }
  362.  
  363. void reshape(int w, int h) {
  364.     glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  365.     glMatrixMode(GL_PROJECTION);
  366.     glLoadIdentity();
  367.  
  368.     gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.5, 50.0);
  369.  
  370.     glMatrixMode(GL_MODELVIEW);
  371. }
  372.  
  373. void keyboard(unsigned char key, int x, int y){
  374.     switch(key){
  375.         case 27 :
  376.         case 'q':
  377.             free(ship);
  378.             exit(0);
  379.             break;
  380.     }
  381. }
  382.  
  383. void timerfunc(int p){
  384.     rotation +=  1;
  385.  
  386.     glutPostRedisplay();
  387.     glutTimerFunc(1000/fps, &timerfunc, 0);
  388. }
  389.  
  390. void menu(int value) {
  391.     switch(value) {
  392.         case 0:
  393.             exit(0);
  394.             break;
  395.     }
  396. }
  397.  
  398. int main(int argc, char *argv[]) {
  399.     srand(time(NULL));
  400.     glutInit(&argc, argv);
  401.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  402.     glutInitWindowSize(screenWidth, screenHeight);
  403.     glutCreateWindow(argv[0]);
  404.     glutDisplayFunc(display);
  405.     glutReshapeFunc(reshape);
  406.     glutKeyboardFunc(keyboard);
  407.     glutTimerFunc(1000/fps, &timerfunc, 0);
  408.  
  409.     glutCreateMenu(menu);
  410.     glutAddMenuEntry("Wyjscie", 0);
  411.  
  412.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  413.  
  414.     init();
  415.  
  416.     glutMainLoop();
  417.  
  418.     return 0;
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement