SHOW:
|
|
- or go back to the newest paste.
| 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 | Vector operator*(const Vector& v) const { return Vector(x * v.x, y * v.y, z * v.z); }
| |
| 29 | Vector operator/(const Vector& v) const { return Vector(x / v.x, y / v.y, z / v.z); }
| |
| 30 | Vector& operator+=(const Vector& v) { x += v.x, y += v.y, z += v.z; return *this; }
| |
| 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 { return Vector(-x, -y, -z); }
| |
| 35 | float dot(const Vector& v) const { return x*v.x + y*v.y + z*v.z; }
| |
| 36 | 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); }
| |
| 37 | float length() const { return sqrt(x*x + y*y + z*z); }
| |
| 38 | Vector normalize() const { float l = length(); if(l > 1e-3) { return (*this/l); } else { return Vector(); } }
| |
| 39 | bool isNull() const { return length() < 1e-3; }
| |
| 40 | }; | |
| 41 | ||
| 42 | void glVertex3f(const Vector& v) {
| |
| 43 | glVertex3f(v.x, v.y, v.z); | |
| 44 | } | |
| 45 | ||
| 46 | void glTriangle(const Vector& a, const Vector& b, const Vector& c) {
| |
| 47 | glVertex3f(a); glVertex3f(b); glVertex3f(c); | |
| 48 | } | |
| 49 | ||
| 50 | void drawCube(const Vector& size) {
| |
| 51 | glBegin(GL_TRIANGLES); {
| |
| 52 | /* (E)-----(A) | |
| 53 | /| /| | |
| 54 | / | / | | |
| 55 | (F)-----(B) | | |
| 56 | | (H)---|-(D) | |
| 57 | | / | / | |
| 58 | |/ |/ | |
| 59 | (G)-----(C) */ | |
| 60 | ||
| 61 | Vector s = size / 2; | |
| 62 | ||
| 63 | 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), | |
| 64 | 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); | |
| 65 | ||
| 66 | glTriangle(A, D, B); glTriangle(C, B, D); glTriangle(A, B, E); glTriangle(F, E, B); | |
| 67 | glTriangle(B, C, F); glTriangle(G, F, C); glTriangle(F, G, E); glTriangle(H, E, G); | |
| 68 | glTriangle(H, G, D); glTriangle(C, D, G); glTriangle(E, H, A); glTriangle(D, A, H); | |
| 69 | ||
| 70 | } glEnd(); | |
| 71 | } | |
| 72 | ||
| 73 | void onDisplay() {
| |
| 74 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| 75 | ||
| 76 | glColor3f(1, 1, 1); | |
| 77 | drawCube(1.0f); | |
| 78 | ||
| 79 | glutSwapBuffers(); | |
| 80 | } | |
| 81 | ||
| 82 | void onIdle() {
| |
| 83 | - | static bool first_call = true; |
| 83 | + | static bool first_call = true; |
| 84 | if(first_call) {
| |
| 85 | glutPostRedisplay(); | |
| 86 | first_call = false; | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | - | void onInitialization() {}
|
| 90 | + | void onInitialization() {
|
| 91 | glMatrixMode(GL_PROJECTION); | |
| 92 | gluPerspective(60, 1, 0.1, 10); | |
| 93 | glMatrixMode(GL_MODELVIEW); | |
| 94 | } | |
| 95 | ||
| 96 | void onKeyboard(unsigned char key, int, int) {}
| |
| 97 | ||
| 98 | void onKeyboardUp(unsigned char key, int, int) {}
| |
| 99 | ||
| 100 | void onMouse(int, int, int, int) {}
| |
| 101 | ||
| 102 | - | glutInitWindowSize(300, 300); |
| 102 | + | |
| 103 | ||
| 104 | int main(int argc, char **argv) {
| |
| 105 | glutInit(&argc, argv); | |
| 106 | glutInitWindowSize(600, 600); | |
| 107 | glutInitWindowPosition(100, 100); | |
| 108 | glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); | |
| 109 | ||
| 110 | glutCreateWindow("Grafika pelda program");
| |
| 111 | ||
| 112 | glMatrixMode(GL_MODELVIEW); | |
| 113 | glLoadIdentity(); | |
| 114 | glMatrixMode(GL_PROJECTION); | |
| 115 | glLoadIdentity(); | |
| 116 | ||
| 117 | onInitialization(); | |
| 118 | ||
| 119 | glutDisplayFunc(onDisplay); | |
| 120 | glutMouseFunc(onMouse); | |
| 121 | glutIdleFunc(onIdle); | |
| 122 | glutKeyboardFunc(onKeyboard); | |
| 123 | glutKeyboardUpFunc(onKeyboardUp); | |
| 124 | glutMotionFunc(onMouseMotion); | |
| 125 | ||
| 126 | glutMainLoop(); | |
| 127 | ||
| 128 | return 0; | |
| 129 | } |