Advertisement
Angeljruiz

Untitled

Dec 29th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. float xRot = 0.0f;
  2. float yRot = 0.0f;
  3. float zRot = 0.0f;
  4. float zTrans = -60.0f;
  5. int boxList, texList;
  6. unsigned char* Pixels;
  7. GLuint texture;
  8. GLuint width, height, imgSize;
  9.  
  10. static void display()
  11. {
  12.     static GLint s_v[4] = { 2, 0, 0, 0 };
  13.     static GLint t_v[4] = { 0, 0, 2, 0 };
  14.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  15.     glColor3f(0.0f, 0.0f, 1.0f);
  16.     glPushMatrix();
  17.     glTranslatef(0.0f, 0.0f, zTrans);
  18.     glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  19.     glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  20.     glRotatef(zRot, 0.0f, 0.0f, 1.0f);
  21.     glEnable(GL_TEXTURE_2D);
  22.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  23.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  24.     glTexGeniv(GL_S, GL_OBJECT_PLANE, s_v);
  25.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  26.     glTexGeniv(GL_T, GL_OBJECT_PLANE, t_v);
  27.     glCallList(texList);
  28.     glCallList(boxList);
  29.     glDisable(GL_TEXTURE_2D);
  30.     glPopMatrix();
  31.     glFlush();
  32.     glutSwapBuffers();
  33. }
  34. static void init()
  35. {
  36.     GLfloat box1[3][3] = {{ 500, 500, -500}, { 500, -500, -500}, { -500, -500, -500}};
  37.     GLfloat box2[3][3] = {{ -500, 500, -500}, { -500, -500, -500}, { -500, -500, 500}};
  38.     GLfloat box3[3][3] = {{ -500, 500, 500}, { -500, -500, 500}, { 500, -500, 500}};
  39.     GLfloat box4[3][3] = {{ 500, 500, 500}, { 500, -500, 500}, { 500, -500, -500}};
  40.     GLfloat normals[3];
  41.     glColor3f(0.0f, 0.0f, 1.0f);
  42.     glNewList(boxList = glGenLists(1), GL_COMPILE);
  43.         glBegin(GL_QUADS);
  44.             glFrontFace(GL_CW);
  45.             calcNormal(box1, normals);
  46.             glNormal3fv(normals);
  47.             glVertex3fv(box1[0]);
  48.             glVertex3fv(box1[1]);
  49.             glVertex3fv(box1[2]);
  50.             glVertex3fv(box2[0]);
  51.             //do the same for the other boxes, box 4 uses a vertex from box1
  52.         glEnd();
  53.     glEndList();
  54. }
  55.  
  56. int main()
  57. {
  58.     //setup glut window & callbacks
  59.  
  60.     glClearColor(0,0,0,1);
  61.     glColor3f(0.0f, 1.0f, 0.0f);
  62.  
  63.     glEnable(GL_DEPTH_TEST);
  64.     //glEnable(GL_LIGHTING);
  65.     glEnable(GL_COLOR_MATERIAL);
  66.     //glEnable(GL_CULL_FACE);
  67.     //glCullFace(GL_BACK);
  68.     glEnable(GL_TEXTURE_GEN_S);
  69.     glEnable(GL_TEXTURE_GEN_T);
  70.  
  71.     GLfloat am[] = { 1.0, 1.0, 1.0, 1.0 };
  72.     GLfloat df[] = { .4f, .4f, .4f };
  73.     GLfloat pos[] = { -10.0f, -10.0f, -50.0f, 1.0f };
  74.  
  75.     glLightfv(GL_LIGHT0, GL_AMBIENT, am);
  76.     glLightfv(GL_LIGHT0, GL_DIFFUSE, df);
  77.     glLightfv(GL_LIGHT0, GL_POSITION, pos);
  78.     glEnable(GL_LIGHT0);
  79.     glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  80.  
  81.     glNewList(texList = glGenLists(1), GL_COMPILE);
  82.         texture = LoadBitmap("wood.bmp");
  83.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  84.         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
  85.         gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, Pixels );
  86.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, Pixels);
  87.     glEndList();
  88.  
  89.     init();
  90.  
  91.     glutMainLoop();
  92.  
  93.     delete[] Pixels;
  94.     return EXIT_SUCCESS;
  95. }
  96. unsigned int LoadBitmap(string FilePath)
  97. {
  98.     unsigned char bitmapHeader[54];
  99.     unsigned int pos, texID;
  100.     glGenTextures(1, &texID);
  101.     glBindTexture(GL_TEXTURE_2D, texID);
  102.     FILE* fp = fopen(FilePath.c_str(), "rb");
  103.     if(!fp)
  104.     {
  105.         cout << "Bad file\n";
  106.         return NULL;
  107.     }
  108.     fread(bitmapHeader, 1, sizeof(bitmapHeader), fp);
  109.     if(bitmapHeader[0] != 'B' || bitmapHeader[1] != 'M')
  110.     {
  111.         cout << "Not a bitmap\n";
  112.         return NULL;
  113.     }
  114.     imgSize = *(int*)&(bitmapHeader[0x22]);
  115.     pos = *(int*)&(bitmapHeader[0x0A]);
  116.     width = *(int*)&(bitmapHeader[0x12]);
  117.     height = *(int*)&(bitmapHeader[0x16]);
  118.     if (imgSize == NULL)
  119.         imgSize = width * height * 3;
  120.     if (pos == NULL)
  121.         pos = 54;
  122.     if(Pixels)
  123.         delete[] Pixels;
  124.     else
  125.         Pixels = new unsigned char[imgSize];
  126.     fseek(fp, pos, SEEK_SET);
  127.     fread(Pixels, 1, imgSize, fp);
  128.     fclose(fp);
  129.     return texID;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement