RohamCsiga

Vik Wiki Grafika - Textúrázás

Feb 1st, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 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.   glTexCoord2f(0, 0); glVertex3f(a);
  143.   glTexCoord2f(1, 0); glVertex3f(b);
  144.   glTexCoord2f(1, 1); glVertex3f(c);
  145.   glTexCoord2f(0, 1); glVertex3f(d);
  146. }
  147.  
  148. void drawCube(const Vector& size) {
  149.     /*       (E)-----(A)
  150.              /|      /|
  151.             / |     / |
  152.           (F)-----(B) |
  153.            | (H)---|-(D)
  154.            | /     | /
  155.            |/      |/
  156.           (G)-----(C)        */
  157.  
  158.     Vector s = size / 2;
  159.  
  160.     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),
  161.            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);
  162.  
  163.     Vector vertices[6][4] = {
  164.       {A, B, C, D}, {G, F, E, H}, {A, E, F, B},
  165.       {D, C, G, H}, {B, F, G, C}, {E, A, D, H}
  166.     };
  167.  
  168.     glEnable(GL_TEXTURE_2D);
  169.  
  170.     for(int i = 0; i < 6; i++) {
  171.       glBindTexture(GL_TEXTURE_2D, tex[i]); // Ezt semmiképpen se rakd a glBegin - glEnd blokk közé
  172.  
  173.       glBegin(GL_QUADS); {
  174.         glQuad(vertices[i][0], vertices[i][1], vertices[i][2], vertices[i][3]);
  175.       } glEnd();
  176.     }
  177.  
  178.     glBindTexture(GL_TEXTURE_2D, 0);
  179.     glDisable(GL_TEXTURE_2D);
  180. }
  181.  
  182. void onDisplay() {
  183.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  184.  
  185.   CreateTextures();
  186.   drawCube(2.0f);
  187.  
  188.   glutSwapBuffers();
  189. }
  190.  
  191. void onIdle() {
  192.   static bool first_call = true;
  193.   if(first_call) {
  194.     glutPostRedisplay();
  195.     first_call = false;
  196.   }
  197. }
  198.  
  199. void onInitialization() {
  200.   glEnable(GL_DEPTH_TEST);
  201.   glMatrixMode(GL_PROJECTION);
  202.   gluPerspective(60, 1, 0.1, 10);
  203.   glMatrixMode(GL_MODELVIEW);
  204.   gluLookAt(-3, 2, -2, 0, 0, 0, 0, 1, 0);
  205.  
  206.   glFrontFace(GL_CCW);
  207.   glCullFace(GL_BACK);
  208.   glEnable(GL_CULL_FACE);
  209.  
  210.   glColor3f(0.7f, 0.7f, 0.7f);
  211. }
  212.  
  213. void onKeyboard(unsigned char key, int, int) {}
  214.  
  215. void onKeyboardUp(unsigned char key, int, int) {}
  216.  
  217. void onMouse(int, int, int, int) {}
  218.  
  219. void onMouseMotion(int, int) {}
  220.  
  221. int main(int argc, char **argv) {
  222.   glutInit(&argc, argv);
  223.   glutInitWindowSize(600, 600);
  224.   glutInitWindowPosition(100, 100);
  225.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  226.  
  227.   glutCreateWindow("Grafika pelda program");
  228.  
  229.   glMatrixMode(GL_MODELVIEW);
  230.   glLoadIdentity();
  231.   glMatrixMode(GL_PROJECTION);
  232.   glLoadIdentity();
  233.  
  234.   onInitialization();
  235.  
  236.   glutDisplayFunc(onDisplay);
  237.   glutMouseFunc(onMouse);
  238.   glutIdleFunc(onIdle);
  239.   glutKeyboardFunc(onKeyboard);
  240.   glutKeyboardUpFunc(onKeyboardUp);
  241.   glutMotionFunc(onMouseMotion);
  242.  
  243.   glutMainLoop();
  244.  
  245.   return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment