RohamCsiga

Vik Wiki Grafika - Megvilágított, textúrázott kocka

Feb 1st, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdlib.h>
  3.  
  4. #if defined(__APPLE__)
  5.   #include <OpenGL/gl.h>
  6.   #include <OpenGL/glu.h>
  7.   #include <GLUT/glut.h>
  8. #else
  9.   #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  10.     #include <windows.h>
  11.   #endif
  12.   #include <GL/gl.h>
  13.   #include <GL/glu.h>
  14.   #include <GL/glut.h>
  15. #endif
  16.  
  17. #ifndef M_PI
  18.   #define M_PI 3.14159265359
  19. #endif
  20.  
  21. struct Vector {
  22.   float x, y, z;
  23.  
  24.   Vector(float v = 0) : x(v), y(v), z(v) { }
  25.   Vector(float x, float y, float z) : x(x), y(y), z(z) { }
  26.   Vector operator+(const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
  27.   Vector operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
  28.   friend Vector operator-(float f, const Vector& v) { return Vector(f) - v; }
  29.   Vector operator*(const Vector& v) const { return Vector(x * v.x, y * v.y, z * v.z); }
  30.   friend Vector operator*(float f, const Vector& v) { return v*f; }
  31.   Vector operator/(const Vector& v) const { return Vector(x / v.x, y / v.y, z / v.z); }
  32.   Vector& operator+=(const Vector& v) { x += v.x, y += v.y, z += v.z; return *this; }
  33.   Vector& operator-=(const Vector& v) { x -= v.x, y -= v.y, z -= v.z; return *this; }
  34.   Vector& operator*=(const Vector& v) { x *= v.x, y *= v.y, z *= v.z; return *this; }
  35.   Vector& operator/=(const Vector& v) { x /= v.x, y /= v.y, z /= v.z; return *this; }
  36.   Vector operator-() const { return Vector(-x, -y, -z); }
  37.   float dot(const Vector& v) const { return x*v.x + y*v.y + z*v.z; }
  38.   Vector cross(const Vector& v) const { return Vector(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); }
  39.   friend Vector cross(const Vector& a, const Vector& b) { return a.cross(b); }
  40.   float length() const { return sqrt(x*x + y*y + z*z); }
  41.   Vector normalize() const { float l = length(); if(l > 1e-3) { return (*this/l); } else { return Vector(); } }
  42.   bool isNull() const { return length() < 1e-3; }
  43. };
  44.  
  45. GLuint tex[6];
  46.  
  47. void CreateTextures() {
  48.   glGenTextures(6, tex);
  49.  
  50.   const char* ascii_textures[6] = {
  51.     "........"
  52.     "...**..."
  53.     "....*..."
  54.     "....*..."
  55.     "....*..."
  56.     "....*..."
  57.     "...***.."
  58.     "........",
  59.  
  60.     "........"
  61.     "..***+.."
  62.     "....+*.."
  63.     "....+*.."
  64.     "...+*+.."
  65.     "..+*+..."
  66.     "..****.."
  67.     "........",
  68.  
  69.     "........"
  70.     "..***+.."
  71.     "....+*.."
  72.     "....+*.."
  73.     "...**+.."
  74.     "....+*.."
  75.     "..***+.."
  76.     "........",
  77.  
  78.     "........"
  79.     "....*..."
  80.     "...+*..."
  81.     "..+**..."
  82.     "..+.*..."
  83.     "..****.."
  84.     "....*..."
  85.     "........",
  86.  
  87.     "........"
  88.     "..****.."
  89.     "..*....."
  90.     "..***+.."
  91.     "....+*.."
  92.     "....+*.."
  93.     "..***+.."
  94.     "........",
  95.  
  96.     "........"
  97.     "........"
  98.     "..++**.."
  99.     "..*....."
  100.     "..*+*+.."
  101.     "..*..*.."
  102.     "..+**+.."
  103.     "........"
  104.   };
  105.  
  106.   GLubyte texture_data[6][64][3];
  107.   for(int t = 0; t < 6; t++) {
  108.     for(int i = 0; i < 64; i++) {
  109.       switch(ascii_textures[t][i]) {
  110.         case '*':
  111.           for(int j = 0; j < 3; j++) {
  112.             texture_data[t][i][j] = 0;
  113.           }
  114.           break;
  115.         case '+':
  116.           for(int j = 0; j < 3; j++) {
  117.             texture_data[t][i][j] = 127;
  118.           }
  119.           break;
  120.         default:
  121.           for(int j = 0; j < 3; j++) {
  122.             texture_data[t][i][j] = 255;
  123.           }
  124.           break;
  125.       }
  126.     }
  127.  
  128.     glBindTexture(GL_TEXTURE_2D, tex[t]);
  129.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  130.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  131.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data[t]);
  132.   }
  133.  
  134.   glBindTexture(GL_TEXTURE_2D, 0);
  135. }
  136.  
  137. void glVertex3f(const Vector& v) {
  138.   glVertex3f(v.x, v.y, v.z);
  139. }
  140.  
  141. void glQuad(const Vector& a, const Vector& b, const Vector& c, const Vector& d) {
  142.   Vector normal = cross(b-a, c-a).normalize();
  143.   glNormal3f(normal.x, normal.y, normal.z);
  144.  
  145.   int res = 8; // resolution
  146.   for(int i = 0; i < res; i++) {
  147.     for(int j = 0; j < res; j++) {
  148.       glTexCoord2f(float(i)/res, float(j)/res);
  149.       glVertex3f(a + i*(b-a)/res + j*(d-a)/res);
  150.  
  151.       glTexCoord2f(float(i+1)/res, float(j)/res);
  152.       glVertex3f(a + (i+1)*(b-a)/res + j*(d-a)/res);
  153.  
  154.       glTexCoord2f(float(i+1)/res, float(j+1)/res);
  155.       glVertex3f(a + (i+1)*(b-a)/res + (j+1)*(d-a)/res);
  156.  
  157.       glTexCoord2f(float(i)/res, float(j+1)/res);
  158.       glVertex3f(a + i*(b-a)/res + (j+1)*(d-a)/res);
  159.     }
  160.   }
  161. }
  162.  
  163. void drawCube(const Vector& size) {
  164.     /*       (E)-----(A)
  165.              /|      /|
  166.             / |     / |
  167.           (F)-----(B) |
  168.            | (H)---|-(D)
  169.            | /     | /
  170.            |/      |/
  171.           (G)-----(C)        */
  172.  
  173.     Vector s = size / 2;
  174.  
  175.     Vector A(+s.x, +s.y, -s.z), B(+s.x, +s.y, +s.z), C(+s.x, -s.y, +s.z), D(+s.x, -s.y, -s.z),
  176.            E(-s.x, +s.y, -s.z), F(-s.x, +s.y, +s.z), G(-s.x, -s.y, +s.z), H(-s.x, -s.y, -s.z);
  177.  
  178.     Vector vertices[6][4] = {
  179.       {A, B, C, D}, {G, F, E, H}, {A, E, F, B},
  180.       {D, C, G, H}, {B, F, G, C}, {E, A, D, H}
  181.     };
  182.  
  183.     glEnable(GL_TEXTURE_2D);
  184.  
  185.     for(int i = 0; i < 6; i++) {
  186.       glBindTexture(GL_TEXTURE_2D, tex[i]); // Ezt semmiképpen se rakd a glBegin - glEnd blokk közé
  187.  
  188.       glBegin(GL_QUADS); {
  189.         glQuad(vertices[i][0], vertices[i][1], vertices[i][2], vertices[i][3]);
  190.       } glEnd();
  191.     }
  192.  
  193.     glBindTexture(GL_TEXTURE_2D, 0);
  194.     glDisable(GL_TEXTURE_2D);
  195. }
  196.  
  197. void onDisplay() {
  198.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  199.  
  200.   CreateTextures();
  201.   drawCube(2.0f);
  202.  
  203.   glutSwapBuffers();
  204. }
  205.  
  206. void onIdle() {
  207.   static bool first_call = true;
  208.   if(first_call) {
  209.     glutPostRedisplay();
  210.     first_call = false;
  211.   }
  212. }
  213.  
  214. void onInitialization() {
  215.   glEnable(GL_DEPTH_TEST);
  216.   glMatrixMode(GL_PROJECTION);
  217.   gluPerspective(60, 1, 0.1, 10);
  218.   glMatrixMode(GL_MODELVIEW);
  219.   gluLookAt(-3, 2, -2, 0, 0, 0, 0, 1, 0);
  220.   glEnable(GL_LIGHTING);
  221.   glEnable(GL_LIGHT0);
  222.   glEnable(GL_LIGHT1);
  223.  
  224.   float a[4] = {0.3f, 0.3f, 0.3f, 1.0f};
  225.   glLightfv(GL_LIGHT0, GL_AMBIENT, a);
  226.  
  227.  
  228.   float p[4] = {-1.2f, 1.7f, -1.3f, 1};
  229.   glLightfv(GL_LIGHT0, GL_POSITION, p);
  230.   float p2[4] = {-1.2f, 1.7f, -1.3f, -1};
  231.   glLightfv(GL_LIGHT1, GL_POSITION, p2);
  232.   float c[4] = {1.0f, 1.0f, 1.0f, 1.0f};
  233.   glLightfv(GL_LIGHT0, GL_DIFFUSE, c);
  234.   glLightfv(GL_LIGHT1, GL_DIFFUSE, c);
  235.    
  236.   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c);
  237.  
  238.   glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);
  239.   glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.0f);
  240.   glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5f);
  241.   glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.5f);
  242.  
  243.   glFrontFace(GL_CCW);
  244.   glCullFace(GL_BACK);
  245.   glEnable(GL_CULL_FACE);
  246.  
  247.   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  248. }
  249.  
  250. void onKeyboard(unsigned char key, int, int) {}
  251.  
  252. void onKeyboardUp(unsigned char key, int, int) {}
  253.  
  254. void onMouse(int, int, int, int) {}
  255.  
  256. void onMouseMotion(int, int) {}
  257.  
  258. int main(int argc, char **argv) {
  259.   glutInit(&argc, argv);
  260.   glutInitWindowSize(600, 600);
  261.   glutInitWindowPosition(100, 100);
  262.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  263.  
  264.   glutCreateWindow("Grafika pelda program");
  265.  
  266.   glMatrixMode(GL_MODELVIEW);
  267.   glLoadIdentity();
  268.   glMatrixMode(GL_PROJECTION);
  269.   glLoadIdentity();
  270.  
  271.   onInitialization();
  272.  
  273.   glutDisplayFunc(onDisplay);
  274.   glutMouseFunc(onMouse);
  275.   glutIdleFunc(onIdle);
  276.   glutKeyboardFunc(onKeyboard);
  277.   glutKeyboardUpFunc(onKeyboardUp);
  278.   glutMotionFunc(onMouseMotion);
  279.  
  280.   glutMainLoop();
  281.  
  282.   return 0;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment