Advertisement
RohamCsiga

Vik Wiki Grafika - Végre egy kocka!

Jan 30th, 2014
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 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.   Vector operator/(const Vector& v) const { return Vector(x / v.x, y / v.y, z / v.z); }
  31.   Vector& operator+=(const Vector& v) { x += v.x, y += v.y, z += v.z; return *this; }
  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 { return Vector(-x, -y, -z); }
  36.   float dot(const Vector& v) const { return x*v.x + y*v.y + z*v.z; }
  37.   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); }
  38.   friend Vector cross(const Vector& a, const Vector& b) { return a.cross(b); }
  39.   float length() const { return sqrt(x*x + y*y + z*z); }
  40.   Vector normalize() const { float l = length(); if(l > 1e-3) { return (*this/l); } else { return Vector(); } }
  41.   bool isNull() const { return length() < 1e-3; }
  42. };
  43.  
  44. void glVertex3f(const Vector& v) {
  45.   glVertex3f(v.x, v.y, v.z);
  46. }
  47.  
  48. void glQuad(const Vector& a, const Vector& b, const Vector& c, const Vector& d) {
  49.   Vector normal = cross(b-a, c-a).normalize();
  50.   glColor3f(fabs(normal.x), fabs(normal.y), fabs(normal.z));
  51.   glVertex3f(a); glVertex3f(b); glVertex3f(c); glVertex3f(d);
  52. }
  53.  
  54. void drawCube(const Vector& size) {
  55.   glBegin(GL_QUADS); {
  56.     /*       (E)-----(A)
  57.              /|      /|
  58.             / |     / |
  59.           (F)-----(B) |
  60.            | (H)---|-(D)
  61.            | /     | /
  62.            |/      |/
  63.           (G)-----(C)        */
  64.  
  65.     Vector s = size / 2;
  66.  
  67.     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),
  68.            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);
  69.  
  70.     glQuad(A, B, C, D); glQuad(E, H, G, F); glQuad(A, E, F, B);
  71.     glQuad(D, C, G, H); glQuad(B, F, G, C); glQuad(A, D, H, E);
  72.  
  73.   } glEnd();
  74. }
  75.  
  76. void onDisplay() {
  77.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  78.  
  79.   glColor3f(1, 1, 1);
  80.   drawCube(1.0f);
  81.  
  82.   glutSwapBuffers();
  83. }
  84.  
  85. void onIdle() {
  86.   static bool first_call = true;
  87.   if(first_call) {
  88.     glutPostRedisplay();
  89.     first_call = false;
  90.   }
  91. }
  92.  
  93. void onInitialization() {
  94.   glEnable(GL_DEPTH_TEST);
  95.   glMatrixMode(GL_PROJECTION);
  96.   gluPerspective(60, 1, 0.1, 10);
  97.   glMatrixMode(GL_MODELVIEW);
  98.   gluLookAt(-3, 2, -2, 0, 0, 0, 0, 1, 0);
  99. }
  100.  
  101. void onKeyboard(unsigned char key, int, int) {}
  102.  
  103. void onKeyboardUp(unsigned char key, int, int) {}
  104.  
  105. void onMouse(int, int, int, int) {}
  106.  
  107. void onMouseMotion(int, int) {}
  108.  
  109. int main(int argc, char **argv) {
  110.   glutInit(&argc, argv);
  111.   glutInitWindowSize(600, 600);
  112.   glutInitWindowPosition(100, 100);
  113.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  114.  
  115.   glutCreateWindow("Grafika pelda program");
  116.  
  117.   glMatrixMode(GL_MODELVIEW);
  118.   glLoadIdentity();
  119.   glMatrixMode(GL_PROJECTION);
  120.   glLoadIdentity();
  121.  
  122.   onInitialization();
  123.  
  124.   glutDisplayFunc(onDisplay);
  125.   glutMouseFunc(onMouse);
  126.   glutIdleFunc(onIdle);
  127.   glutKeyboardFunc(onKeyboard);
  128.   glutKeyboardUpFunc(onKeyboardUp);
  129.   glutMotionFunc(onMouseMotion);
  130.  
  131.   glutMainLoop();
  132.  
  133.   return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement