Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- // #include <map>
- // #include <string>
- // GLEW
- #define GLEW_STATIC
- #include <GL/glew.h>
- // GLFW
- //#include <GLFW/glfw3.h>
- #include <SDL.h>
- #include <SDL_keyboard.h>
- #include <SDL_keysym.h>
- #include <stdio.h>
- //#include <stdlib.h>
- // #include <GL/freeglut.h>
- #include <glm.hpp>
- #include <vector>
- //#include <deque>
- #include <array>
- #include <limits>
- #define _USE_MATH_DEFINES
- #include <math.h>
- //#include <ft2build.h>
- //#include FT_FREETYPE_H
- //#include <Shader.h>
- using namespace std;
- #define GRAVITY 4.0
- #define MAX_NUM_LIGHTS 8
- #define PI 3.14159265358979
- //#define MAXZ 8.0
- //#define MINZ -8.0
- // std::map<int, bool> keyboard; // Saves the state(true=pressed; false=released) of each SDL_Key.
- bool keydown_left, keydown_right, keydown_up, keydown_down, keydown_alt;
- const GLuint WIDTH = 1024, HEIGHT = 768;
- GLuint VAO, VBO;
- /// Holds all state information relevant to a character as loaded using FreeType
- //struct Character {
- // GLuint TextureID; // ID handle of the glyph texture
- // glm::ivec2 Size; // Size of glyph
- // glm::ivec2 Bearing; // Offset from baseline to left/top of glyph
- // GLuint Advance; // Horizontal offset to advance to next glyph
- //};
- //std::map<char, Character> Characters;
- struct TColor {
- short R, G, B, A;
- };
- struct TLight {
- double position[4];
- double spotdirection[3];
- short ambient[4];
- short diffusion[4];
- short specular[4];
- float cutoff;
- float exponent;
- short n;
- bool enabled;
- };
- std::vector<TLight>lights;
- unsigned int lights_counter;
- unsigned int light_number;
- struct TView {
- glm::vec3 eye;
- glm::vec3 vector;
- glm::vec3 up;
- float angle;
- const char* mode;
- };
- //struct szejder {
- // std::string shader1;
- // std::string shader2;
- //};
- TColor bkgcolor;
- TView view;
- glm::vec3 area;
- //typedef
- //int ctrlpoints2[][3];
- //ctrlpoints2 ctrlpoints[];
- //std::vector<float>crosspoints;
- std::vector<vector<std::array<float,3>>>crosspoints;
- struct quad {
- std::array<glm::vec3, 4>vertices;
- GLfloat* ambient;
- GLfloat* diffuse;
- GLfloat* specular;
- GLfloat* emission;
- double shininess;
- bool filled;
- bool visible;
- };
- std::vector<quad>quads;
- unsigned int quads_counter;
- struct ball {
- glm::vec3 position;
- glm::vec3 velocity;
- glm::vec3 rotation_angle;
- glm::vec3 rotation_speed;
- glm::vec3 rotation_dir;
- double radius;
- std::vector<short>color1_RGB;
- std::vector<short>color2_RGB;
- GLuint texture;
- double mass;
- const char* ID;
- GLUquadricObj *sphere;
- };
- std::vector<ball>balls;
- unsigned int balls_counter;
- GLfloat mat_solid[] = { 0.75, 0.75, 0.75, 1.0 };
- GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
- GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat mat_shininess[] = { 50.0 };
- GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
- GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
- GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
- float ctrlpoints[9][9][3] {
- { { -3.5, -1.0, -3.5 }, { -2.5, -1.0, -3.5 },
- { -1.5, -1.0, -3.5 }, { -0.5, -1.0, -3.5 },
- { 0.5, -1.0, -3.5 }, { 1.5, -1.0, -3.5 },
- { 2.5, -1.0, -3.5 }, { 3.5, -1.0, -3.5 },
- { 4.5, -1.0, -3.5 } },
- { { -3.5, -1.0, -2.5 }, { -2.5, -1.0, -2.5 },
- { -1.5, -1.0, -2.5 }, { -0.5, -1.0, -2.5 },
- { 0.5, -1.0, -2.5 }, { 1.5, -1.0, -2.5 },
- { 2.5, -1.0, -2.5 }, { 3.5, -1.0, -2.5 },
- { 4.5, -1.0, -2.5 } },
- { { -3.5, -1.0, -1.5 }, { -2.5, -1.0, -1.5 },
- { -1.5, -1.0, -1.5 }, { -0.5, -1.0, -1.5 },
- { 0.5, -1.0, -1.5 }, { 1.5, -1.0, -1.5 },
- { 2.5, -1.0, -1.5 }, { 3.5, -1.0, -1.5 },
- { 4.5, -1.0, -1.5 } },
- { { -3.5, -1.0, -0.5 }, { -2.5, -1.0, -0.5 },
- { -1.5, -1.0, -0.5 }, { -0.5, -1.0, -0.5 },
- { 0.5, -1.0, -0.5 }, { 1.5, -1.0, -0.5 },
- { 2.5, -1.0, -0.5 }, { 3.5, -1.0, -0.5 },
- { 4.5, -1.0, -0.5 } },
- { { -3.5, -1.0, 0.5 }, { -2.5, -1.0, 0.5 },
- { -1.5, -1.0, 0.5 }, { -0.5, -1.0, 0.5 },
- { 0.5, -1.0, 0.5 }, { 1.5, -1.0, 0.5 },
- { 2.5, -1.0, 0.5 }, { 3.5, -1.0, 0.5 },
- { 4.5, -1.0, 0.5 } },
- { { -3.5, -1.0, 1.5 }, { -2.5, -1.0, 1.5 },
- { -1.5, -1.0, 1.5 }, { -0.5, -1.0, 1.5 },
- { 0.5, -1.0, 1.5 }, { 1.5, -1.0, 1.5 },
- { 2.5, -1.0, 1.5 }, { 3.5, -1.0, 1.5 },
- { 4.5, -1.0, 1.5 } },
- { { -3.5, -1.0, 2.5 }, { -2.5, -1.0, 2.5 },
- { -1.5, -1.0, 2.5 }, { -0.5, -1.0, 2.5 },
- { 0.5, -1.0, 2.5 }, { 1.5, -1.0, 2.5 },
- { 2.5, -1.0, 2.5 }, { 3.5, -1.0, 2.5 },
- { 4.5, -1.0, 2.5 } },
- { { -3.5, -1.0, 3.5 }, { -2.5, -1.0, 3.5 },
- { -1.5, -1.0, 3.5 }, { -0.5, -1.0, 3.5 },
- { 0.5, -1.0, 3.5 }, { 1.5, -1.0, 3.5 },
- { 2.5, -1.0, 3.5 }, { 3.5, -1.0, 3.5 },
- { 4.5, -1.0, 3.5 } },
- { { -3.5, -1.0, 4.5 }, { -2.5, -1.0, 4.5 },
- { -1.5, -1.0, 4.5 }, { -0.5, -1.0, 4.5 },
- { 0.5, -1.0, 4.5 }, { 1.5, -1.0, 4.5 },
- { 2.5, -1.0, 4.5 }, { 3.5, -1.0, 4.5 },
- { 4.5, -1.0, 4.5 } }
- };
- unsigned int collision_counter;
- bool run;
- bool show_coords;
- bool bouncing;
- bool quit;
- //static float solidZ = MAXZ;
- //static float transparentZ = MINZ;
- double sqr(double number) { return (number * number); }
- double cotan(double number) { return 1.0 / tan(number); }
- //void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color);
- static void inputSDL(void);
- // grawitacja, zmiana pozycji pilek, odbijanie od podlogi i od scian
- void RunPhysics3(float dt, ball *object) {
- double obj_newpos_X = object->position.x + object->velocity.x * 1;
- double obj_newpos_Y = object->position.y - object->velocity.y / (double)65536.0;
- double obj_newpos_Z = object->position.z + object->velocity.z * 1;
- // odbijanie od scian
- if ((obj_newpos_X + object->radius >= (double)area.x / 2) || (obj_newpos_X - object->radius <= 0 - (double)area.x / 2)) {
- object->velocity.x = -object->velocity.x;
- object->rotation_dir.x = -1.0 * object->rotation_dir.x;
- }
- if ((obj_newpos_Z + object->radius >= (double)area.z / 2) || (obj_newpos_Z - object->radius <= 0 - (double)area.z / 2)) {
- object->velocity.z = -object->velocity.z;
- object->rotation_dir.z = -1.0 * object->rotation_dir.z;
- }
- // cout << "1velocity_y: " << object->velocity.y << "\n";
- // if (object->position.y - object->radius < -1) {
- if (object->position.y - object->radius > -1) object->velocity.y += GRAVITY;
- //}
- // cout << "2velocity_y: " << object->velocity.y << "\n";
- object->rotation_angle.x += object->rotation_speed.x * object->rotation_dir.x;
- object->rotation_angle.y += object->rotation_speed.y * object->rotation_dir.y;
- object->rotation_angle.z += object->rotation_speed.z * object->rotation_dir.z;
- object->position.x = object->position.x + object->velocity.x;
- object->position.y = object->position.y - object->velocity.y / (double)65536.0;
- object->position.z = object->position.z + object->velocity.z;
- if (object->position.y - object->radius < -1.0) {
- object->position.y = -1.0 + object->radius;
- if (!bouncing) object->velocity.y = 0.0;
- object->velocity.y = -object->velocity.y;
- }
- }
- // detekcja kolizji miedzy pilkami
- // ta funkcja wykorzystuje wartosci odleglosci miedzy kulami ze znakiem
- // liczy kat fi z arcus tangens cotangensa + 0.5 PI
- // liczy katy theta z arcus tangens tangensa plus PI, jesli sinus theta jest mniejszy od 0
- // uwzglednia znak predkosci X i Z w obliczeniach skladowych predkosci koncowych
- // zakladajac poprawne dzialanie calosci 80% z tego mozna wywalic... (+ pozbyc sie petli)
- void CollisionDetect7(ball *object1, ball *object2) {
- // przypuszczalne wspolrzedne srodkow kul w chwili t+1
- double obj1_newpos_X = object1->position.x + object1->velocity.x * 3;
- double obj1_newpos_Y = object1->position.y - object1->velocity.y / (double)65536.0;
- double obj1_newpos_Z = object1->position.z + object1->velocity.z * 3;
- double obj2_newpos_X = object2->position.x + object2->velocity.x * 3;
- double obj2_newpos_Y = object2->position.y - object2->velocity.y / (double)65536.0;
- double obj2_newpos_Z = object2->position.z + object2->velocity.z * 3;
- // obliczenie odleglosci miedzy srodkami kul w chwili t+1
- double newdistance_x = abs(obj1_newpos_X - obj2_newpos_X);
- double newdistance_y = abs(obj1_newpos_Y - obj2_newpos_Y);
- double newdistance_z = abs(obj1_newpos_Z - obj2_newpos_Z);
- double newdistance_xyz = sqrt(sqr(newdistance_x) + sqr(newdistance_y) + sqr(newdistance_z));
- // jesli odleglosc w chwili t+1 <= sumie promieni obu kul, nastepuje kolizja
- if (newdistance_xyz < object1->radius + object2->radius) {
- collision_counter++;
- cout << collision_counter << ". Kolizja " << object1->ID << " z " << object2->ID << "\n";
- double obj1_velocity_X = object1->velocity.x;
- double obj1_velocity_Y = object1->velocity.y;
- double obj1_velocity_Z = object1->velocity.z;
- double obj2_velocity_X = object2->velocity.x;
- double obj2_velocity_Y = object2->velocity.y;
- double obj2_velocity_Z = object2->velocity.z;
- double obj1_currpos_X = object1->position.x;
- double obj1_currpos_Y = object1->position.y;
- double obj1_currpos_Z = object1->position.z;
- double obj2_currpos_X = object2->position.x;
- double obj2_currpos_Y = object2->position.y;
- double obj2_currpos_Z = object2->position.z;
- double currdistance_x = obj1_currpos_X - obj2_currpos_X;
- double currdistance_y = obj1_currpos_Y - obj2_currpos_Y;
- double currdistance_z = obj1_currpos_Z - obj2_currpos_Z;
- double currdistance_xz = sqrt(sqr(currdistance_x) + sqr(currdistance_z));
- double currdistance_xyz = sqrt(sqr(currdistance_x) + sqr(currdistance_y) + sqr(currdistance_z));
- double mass = object1->mass / object2->mass;
- double restitution = 1.0;
- double sin_fi = currdistance_x / currdistance_xz;
- double cos_fi = currdistance_z / currdistance_xz;
- double tg_fi = currdistance_x / currdistance_z;
- double ctg_fi = currdistance_z / currdistance_x;
- double fi = 0.0 + atan(ctg_fi) + 0.5 * PI;
- double psi = acos(currdistance_xz / currdistance_xyz);
- double obj1_newpos_X2, obj1_newpos_Y2, obj1_newpos_Z2, obj2_newpos_X2, obj2_newpos_Y2, obj2_newpos_Z2;
- double newdistance2_x, newdistance2_y, newdistance2_z, newdistance2_xyz = 0.0;
- double obj1_velocity_XZ, obj2_velocity_XZ, obj1_velocity_XYZ, obj2_velocity_XYZ;
- double sin_theta1, sin_theta2, cos_theta1, cos_theta2, tg_theta1, tg_theta2, ctg_theta1, ctg_theta2, theta1, theta2;
- double sin_gamma1, sin_gamma2, cos_gamma1, cos_gamma2, tg_gamma1, tg_gamma2, ctg_gamma1, ctg_gamma2, gamma1, gamma2;
- double dzeta_eta_W1x, dzeta_eta_W1y, dzeta_eta_W1z, dzeta_eta_W2x, dzeta_eta_W2y, dzeta_eta_W2z, dzeta_eta_W1xz, dzeta_eta_W2xz;
- double dzeta_eta_W1x_s1, dzeta_eta_W2x_s1, dzeta_eta_W1y_s1, dzeta_eta_W2y_s1, dzeta_eta_W1z_s1, dzeta_eta_W1z_s2, dzeta_eta_W2z_s1, dzeta_eta_W2z_s2;
- double obj1_velocity_Y_s1, obj1_velocity_Y_s2, obj2_velocity_Y_s1, obj2_velocity_Y_s2;
- unsigned int collision_counter2 = 0;
- while (newdistance2_xyz < newdistance_xyz) {
- collision_counter2++;
- cout << collision_counter << ". #" << collision_counter2 << " " << newdistance2_xyz << ", " << newdistance_xyz << ", " << currdistance_xyz << "\n";
- // dlugosci wektorow predkosci wypadkowych na plaszczyznie XZ i w XYZ
- obj1_velocity_XZ = sqrt(sqr(obj1_velocity_X) + sqr(obj1_velocity_Z));
- obj2_velocity_XZ = sqrt(sqr(obj2_velocity_X) + sqr(obj2_velocity_Z));
- obj1_velocity_XYZ = sqrt(sqr(obj1_velocity_X) + sqr(obj1_velocity_Y) + sqr(obj1_velocity_Z));
- obj2_velocity_XYZ = sqrt(sqr(obj2_velocity_X) + sqr(obj2_velocity_Y) + sqr(obj2_velocity_Z));
- // obliczanie katow pomiedzy wektorami predkosci wypadkowych kul na plaszczyznie XZ a osia X ukladu
- // wspolrzednych OX
- sin_theta1 = obj1_velocity_Z / obj1_velocity_XZ;
- sin_theta2 = obj2_velocity_Z / obj2_velocity_XZ;
- cos_theta1 = obj1_velocity_X / obj1_velocity_XZ;
- cos_theta2 = obj2_velocity_X / obj2_velocity_XZ;
- tg_theta1 = obj1_velocity_Z / obj1_velocity_X;
- tg_theta2 = obj2_velocity_Z / obj2_velocity_X;
- ctg_theta1 = obj1_velocity_X / obj1_velocity_Z;
- ctg_theta2 = obj2_velocity_X / obj2_velocity_Z;
- // kat theta musi byc liczony z (co)tangensa!! Ze wzgledu na wartosc sinusa/cosinusa,
- // ktory w mianowniku zawsze ma liczbe dodatnia
- theta1 = 0.0 + atan(tg_theta1);
- theta2 = 0.0 + atan(tg_theta2);
- // if (isinf(tg_theta1)) theta1 = 0.0 + atan(ctg_theta1);
- // if (isinf(tg_theta2)) theta2 = 0.0 + atan(ctg_theta2);
- if (sin_theta1 < 0) theta1 = theta1 + PI;
- if (sin_theta2 < 0) theta2 = theta2 + PI;
- // ponizsze jest, zeby miara katow nie byla ujemna. W sumie zbedne.
- if (theta1 < 0) theta1 = 2 * PI + theta1;
- if (theta2 < 0) theta2 = 2 * PI + theta2;
- // obliczanie katow GAMMA pomiedzy wektorami predkosci wypadkowych w przestrzeni XYZ a plaszczyzna XZ
- // ukladu wspolrzednych OX
- sin_gamma1 = obj1_velocity_X / obj1_velocity_XYZ;
- sin_gamma2 = obj2_velocity_X / obj2_velocity_XYZ;
- cos_gamma1 = obj1_velocity_XZ / obj1_velocity_XYZ;
- cos_gamma2 = obj2_velocity_XZ / obj2_velocity_XYZ;
- tg_gamma1 = obj1_velocity_X / obj1_velocity_XZ;
- tg_gamma2 = obj2_velocity_X / obj2_velocity_XZ;
- ctg_gamma1 = obj1_velocity_XZ / obj1_velocity_X;
- ctg_gamma2 = obj2_velocity_XZ / obj2_velocity_X;
- if (((obj1_velocity_X < 0) && (obj1_velocity_Z >= 0)) || ((obj1_velocity_X >= 0) && (obj1_velocity_Z < 0))) {
- tg_gamma1 = -tg_gamma1;
- ctg_gamma1 = -ctg_gamma1;
- }
- if (((obj2_velocity_X < 0) && (obj2_velocity_Z >= 0)) || ((obj2_velocity_X >= 0) && (obj2_velocity_Z < 0))) {
- tg_gamma2 = -tg_gamma2;
- ctg_gamma2 = -ctg_gamma2;
- }
- gamma1 = 0.0 + atan(tg_gamma1);
- gamma2 = 0.0 + atan(tg_gamma2);
- // ponizsze jest, zeby miara katow nie byla ujemna. W sumie zbedne.
- if (gamma1 < 0) gamma1 = 2 * PI + gamma1;
- if (gamma2 < 0) gamma2 = 2 * PI + gamma2;
- cout << collision_counter << ". fi=" << fi * 180 / PI << " theta1 (" << object1->ID << ")=" << theta1 * 180 / PI << " theta2 (" << object2->ID << ")=" << theta2 * 180 / PI << "\n";
- // obliczanie skladowych predkosci koncowych W obu kul po zderzeniu w ukladzie dzeta-eta.
- // *_s1, *_s2 - skladniki do koncowych rownan
- dzeta_eta_W1x_s1 = obj1_velocity_XZ * cos(theta1 - fi);
- dzeta_eta_W2x_s1 = obj2_velocity_XZ * cos(theta2 - fi);
- dzeta_eta_W1y_s1 = obj1_velocity_XYZ * sin(gamma1 - psi);
- dzeta_eta_W2y_s1 = obj2_velocity_XYZ * sin(gamma1 - psi);
- dzeta_eta_W1z_s1 = obj1_velocity_XZ * ((mass - restitution) / (mass + restitution)) * sin(theta1 - fi);
- dzeta_eta_W1z_s2 = obj2_velocity_XZ * ((restitution + 1) / (mass + 1)) * sin(theta2 - fi);
- dzeta_eta_W2z_s1 = obj1_velocity_XZ * ((mass * (restitution + 1)) / (mass + 1)) * sin(theta1 - fi);
- dzeta_eta_W2z_s2 = obj2_velocity_XZ * ((1 - restitution * mass) / (mass + 1)) * sin(theta2 - fi);
- if (((obj1_velocity_X < 0) && (obj1_velocity_Z >= 0)) || ((obj1_velocity_X >= 0) && (obj1_velocity_Z < 0))) {
- dzeta_eta_W1x_s1 = -dzeta_eta_W1x_s1;
- dzeta_eta_W1z_s1 = -dzeta_eta_W1z_s1;
- dzeta_eta_W2z_s1 = -dzeta_eta_W2z_s1;
- }
- if (((obj2_velocity_X < 0) && (obj2_velocity_Z >= 0)) || ((obj2_velocity_X >= 0) && (obj2_velocity_Z < 0))) {
- dzeta_eta_W2x_s1 = -dzeta_eta_W2x_s1;
- dzeta_eta_W1z_s2 = -dzeta_eta_W1z_s2;
- dzeta_eta_W2z_s2 = -dzeta_eta_W2z_s2;
- }
- dzeta_eta_W1x = dzeta_eta_W1x_s1;
- dzeta_eta_W2x = dzeta_eta_W2x_s1;
- dzeta_eta_W1y = dzeta_eta_W1y_s1;
- dzeta_eta_W2y = dzeta_eta_W2y_s1;
- dzeta_eta_W1z = dzeta_eta_W1z_s1 + dzeta_eta_W1z_s2;
- dzeta_eta_W2z = dzeta_eta_W2z_s1 + dzeta_eta_W2z_s2;
- dzeta_eta_W1xz = sqrt(sqr(dzeta_eta_W1x) + sqr(dzeta_eta_W1z));
- dzeta_eta_W2xz = sqrt(sqr(dzeta_eta_W2x) + sqr(dzeta_eta_W2z));
- // obliczanie skladowych wypadkowych predkosci koncowych w ukladzie XZ
- obj1_velocity_Y_s1 = dzeta_eta_W1y * cos(psi);
- obj1_velocity_Y_s2 = dzeta_eta_W1xz * sin(psi);
- obj2_velocity_Y_s1 = dzeta_eta_W2y * cos(psi);
- obj2_velocity_Y_s2 = dzeta_eta_W2xz * sin(psi);
- if (((obj1_velocity_X < 0) && (obj1_velocity_Z >= 0)) || ((obj1_velocity_X >= 0) && (obj1_velocity_Z < 0))) {
- obj1_velocity_Y_s2 = -obj1_velocity_Y_s2;
- }
- if (((obj2_velocity_X < 0) && (obj2_velocity_Z >= 0)) || ((obj2_velocity_X >= 0) && (obj2_velocity_Z < 0))) {
- obj2_velocity_Y_s2 = -obj2_velocity_Y_s2;
- }
- obj1_velocity_X = dzeta_eta_W1x * cos(fi) - dzeta_eta_W1z * sin(fi);
- obj1_velocity_Z = dzeta_eta_W1x * sin(fi) + dzeta_eta_W1z * cos(fi);
- obj2_velocity_X = dzeta_eta_W2x * cos(fi) - dzeta_eta_W2z * sin(fi);
- obj2_velocity_Z = dzeta_eta_W2x * sin(fi) + dzeta_eta_W2z * cos(fi);
- // obj1_velocity_Y = dzeta_eta_W1y * cos(psi) + dzeta_eta_W1xz * sin(psi);
- // obj2_velocity_Y = dzeta_eta_W2y * cos(psi) + dzeta_eta_W2xz * sin(psi);
- obj1_velocity_Y = obj1_velocity_Y_s1 + obj1_velocity_Y_s2;
- obj2_velocity_Y = obj2_velocity_Y_s1 + obj2_velocity_Y_s2;
- // sprawdzanie kolizji ze scianami...
- if ((obj1_currpos_X + object1->radius >= (double)area.x / 2) || (obj1_currpos_X - object1->radius <= 0 - (double)area.x / 2)) {
- obj2_velocity_X = -obj2_velocity_X;
- object2->rotation_dir.x = -1.0 * object2->rotation_dir.x;
- }
- if ((obj1_currpos_Z + object1->radius >= (double)area.z / 2) || (obj1_currpos_Z - object1->radius <= 0 - (double)area.z / 2)) {
- obj2_velocity_Z = -obj2_velocity_Z;
- object2->rotation_dir.z = -1.0 * object2->rotation_dir.z;
- }
- if ((obj2_currpos_X + object2->radius >= (double)area.x / 2) || (obj2_currpos_X - object2->radius <= 0 - (double)area.x / 2)) {
- obj1_velocity_X = -obj1_velocity_X;
- object1->rotation_dir.x = -1.0 * object1->rotation_dir.x;
- }
- if ((obj2_currpos_Z + object2->radius >= (double)area.z / 2) || (obj2_currpos_Z - object2->radius <= 0 - (double)area.z / 2)) {
- obj1_velocity_Z = -obj1_velocity_Z;
- object1->rotation_dir.z = -1.0 * object1->rotation_dir.z;
- }
- // zapisanie do zmiennej odleglosci miedzy kulami po przesunieciu o wektor obliczonej predkosci,
- // dla porownania WHILE.
- // przy prawidlowym dzialaniu calosci petla jest zbedna, wiec mozna wywalic.
- obj1_newpos_X2 = obj1_currpos_X + obj1_velocity_X * 3;
- // obj1_newpos_Y2 = obj1_currpos_Y - obj1_velocity_Y / (double)65536.0;
- // obj1_newpos_Y2 = obj1_currpos_Y;
- obj1_newpos_Y2 = obj1_currpos_Y + obj1_velocity_Y * 3;
- obj1_newpos_Z2 = obj1_currpos_Z + obj1_velocity_Z * 3;
- obj2_newpos_X2 = obj2_currpos_X + obj2_velocity_X * 3;
- // obj2_newpos_Y2 = obj2_currpos_Y - obj2_velocity_Y / (double)65536.0;
- // obj2_newpos_Y2 = obj2_currpos_Y;
- obj2_newpos_Y2 = obj2_currpos_Y + obj2_velocity_Y * 3;
- obj2_newpos_Z2 = obj2_currpos_Z + obj2_velocity_Z * 3;
- newdistance2_x = abs(obj2_newpos_X2 - obj1_newpos_X2);
- newdistance2_y = abs(obj2_newpos_Y2 - obj1_newpos_Y2);
- newdistance2_z = abs(obj2_newpos_Z2 - obj1_newpos_Z2);
- newdistance2_xyz = sqrt(sqr(newdistance2_x) + sqr(newdistance2_y) + sqr(newdistance2_z));
- }
- object1->velocity.x = obj1_velocity_X;
- object1->velocity.y = obj1_velocity_Y;
- object1->velocity.z = obj1_velocity_Z;
- object2->velocity.x = obj2_velocity_X;
- object2->velocity.y = obj2_velocity_Y;
- object2->velocity.z = obj2_velocity_Z;
- }
- }
- // tworzy teksture boing balli
- void make_tex(ball *object) {
- unsigned char data[128][128][3];
- for (unsigned short y = 0; y < 128; y++) {
- for (unsigned short x = 0; x < 128; x++) {
- unsigned char *p = data[y][x];
- if ((x ^ y) & 2) {
- p[0] = object->color1_RGB[0];
- p[1] = object->color1_RGB[1];
- p[2] = object->color1_RGB[2];
- }
- else {
- p[0] = object->color2_RGB[0];
- p[1] = object->color2_RGB[1];
- p[2] = object->color2_RGB[2];
- }
- }
- }
- glGenTextures(1, &(object->texture));
- glBindTexture(GL_TEXTURE_2D, object->texture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, (const GLvoid *)data);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- }
- // rysowanie wszystkiego
- void draw() {
- glClearColor((float)bkgcolor.R/256, (float)bkgcolor.G/256, (float)bkgcolor.B/256, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Shader shader("shader.vs", "shader.frag");
- // RenderText(shader, "This is sample text", 25.0f, 25.0f, 1.0f, glm::vec3(0.5, 0.8f, 0.2f));
- // RenderText(shader, "(C) LearnOpenGL.com", 540.0f, 570.0f, 0.5f, glm::vec3(0.3, 0.7f, 0.9f));
- // font.setPenPosition(16, 16);
- // font.draw("Hello, gltext!");
- // rysowanie siatki na podlodze
- glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
- glLineWidth(0.5);
- unsigned short i, j;
- for (j = 0; j <= 8; j++) {
- glBegin(GL_LINE_STRIP);
- for (i = 0; i <= 30; i++)
- glEvalCoord2f((GLfloat)i / 30.0, (GLfloat)j / 8.0);
- glEnd();
- glBegin(GL_LINE_STRIP);
- for (i = 0; i <= 30; i++)
- glEvalCoord2f((GLfloat)j / 8.0, (GLfloat)i / 30.0);
- glEnd();
- }
- // rysowanie kulek
- glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
- for (i = 0; i < balls_counter; i++) {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslated(balls[i].position.x, balls[i].position.y, balls[i].position.z);
- glRotated(150, 0.0, 1.0, 1.0);
- glRotated(balls[i].rotation_angle.x, 0.0, 0.0, 2.0);
- gluQuadricDrawStyle(balls[i].sphere, GLU_FILL);
- glBindTexture(GL_TEXTURE_2D, balls[i].texture);
- gluQuadricTexture(balls[i].sphere, GL_TRUE);
- gluQuadricNormals(balls[i].sphere, GLU_SMOOTH);
- gluSphere(balls[i].sphere, balls[i].radius, 32, 16);
- }
- // rysowanie scian, sufitu i podlogi
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- for (int i = 0; i < quads_counter; i++) {
- if (quads[i].visible == true) {
- if (quads[i].filled == true) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- else if (quads[i].filled == false) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glBegin(GL_QUADS);
- glMaterialfv(GL_FRONT, GL_EMISSION, quads[i].emission);
- glMaterialfv(GL_FRONT, GL_SPECULAR, quads[i].specular);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- if ((quads[i].vertices[0].x == quads[i].vertices[1].x) && (quads[i].vertices[0].x == quads[i].vertices[2].x) && (quads[i].vertices[0].x == quads[i].vertices[3].x)) glNormal3f(1.0, 0.0, 0.0);
- else if ((quads[i].vertices[0].y == quads[i].vertices[1].y) && (quads[i].vertices[0].y == quads[i].vertices[2].y) && (quads[i].vertices[0].y == quads[i].vertices[3].y)) glNormal3f(0.0, 1.0, 0.0);
- else if ((quads[i].vertices[0].z == quads[i].vertices[1].z) && (quads[i].vertices[0].z == quads[i].vertices[2].z) && (quads[i].vertices[0].z == quads[i].vertices[3].z)) glNormal3f(0.0, 0.0, 1.0);
- for (int j = 0; j < 4; j++) {
- glVertex3d(quads[i].vertices[j].x, quads[i].vertices[j].y, quads[i].vertices[j].z);
- }
- for (int j = 3; j >= 0; j--) {
- glVertex3d(quads[i].vertices[j].x, quads[i].vertices[j].y, quads[i].vertices[j].z);
- }
- glEnd();
- }
- }
- //-------------------------- smieciuchy -------------------------------
- glMaterialfv(GL_FRONT, GL_EMISSION, mat_transparent);
- // glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
- // glColor3f(0.0, 1.0, 1.0);
- //glEnd();
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- // glColor3f(0.0, 1.0, 1.0);
- // glTranslatef(0.15, 0.15, transparentZ);
- // glRotatef(15.0, 1.0, 1.0, 0.0);
- // glRotatef(30.0, 0.0, 1.0, 0.0);
- glMaterialfv(GL_FRONT, GL_EMISSION, mat_solid);
- // glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
- // glEnable(GL_BLEND);
- glDepthMask(GL_FALSE);
- // glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- // glCallList(cubeList);
- glDepthMask(GL_TRUE);
- // glDisable(GL_BLEND);
- // glutWireCube(1.0);
- //--------------------------- koniec smieciuchow :=( -----------------------------
- // ustawienie obserwatora
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLdouble)1024 / 768, 1.0, 100);
- gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.eye.y + view.vector.y, view.eye.z - view.vector.z, 0.0, view.up.y, 0.0);
- // glutPostRedisplay();
- // glutSwapBuffers();
- SDL_GL_SwapBuffers();
- }
- // zmiana rozmiarow okna... przy korzystaniu z okienek SDL niepotrzebne.
- void resize(unsigned int w, unsigned int h) {
- if (!h) h = 1;
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, 1.00*w / h , 1.0, 100);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.eye.y + view.vector.y, view.eye.z - view.vector.z, 0.0, view.up.y, 0.0);
- }
- // zmiana widoku
- void changeView(const char* mode) {
- if (mode == "viewAngleX") {
- // przelaczenie na widok pod katem na os X
- view.eye.x = -0.2f;
- view.eye.y = 7.0f;
- view.eye.z = 7.0f;
- view.angle = 0.0f;
- view.up.y = 1.0f;
- view.vector.y = -1.0f;
- view.mode = "angleX";
- }
- else if (mode == "viewAngleZ") {
- // przelaczenie na widok pod katem na os Z
- view.eye.x = -7.0f;
- view.eye.y = 7.0f;
- view.eye.z = -0.2f;
- view.angle = 90.0f;
- view.up.y = 1.0f;
- view.vector.y = -1.0f;
- view.mode = "angleZ";
- }
- else if (mode == "viewTopX") {
- // przelaczenie na widok z gory
- view.eye.x = -0.2f;
- view.eye.y = 10.0f;
- view.eye.z = 2.5f;
- view.angle = 0.0f;
- view.up.y = 1.0f;
- view.vector.y = -5.0f;
- view.mode = "topX";
- }
- else if (mode == "viewTopZ") {
- // przelaczenie na widok z gory
- view.eye.x = -2.5f;
- view.eye.y = 10.0f;
- view.eye.z = -0.2f;
- view.angle = 90.0f;
- view.up.y = 1.0f;
- view.vector.y = -5.0f;
- view.mode = "topZ";
- }
- else if (mode == "viewX") {
- // przelaczenie na widok na os X
- view.eye.x = 0.0f;
- view.eye.y = 0.0f;
- view.eye.z = 10.0f;
- view.angle = 0.0f;
- view.up.y = 1.0f;
- view.vector.y = 0.0f;
- view.mode = "X";
- }
- else if (mode == "viewZ") {
- // przelaczenie na widok na os Z
- view.eye.x = -10.0f;
- view.eye.y = 0.0f;
- view.eye.z = 0.0f;
- view.angle = 90.0f;
- view.up.y = 1.0f;
- view.vector.y = 0.0f;
- view.mode = "Z";
- }
- view.vector.x = (float)sin(view.angle * PI / 180);
- view.vector.z = (float)cos(view.angle * PI / 180);
- }
- // zmiana pozycji obserwatora
- void move(const char* direction) {
- keydown_alt = ((SDL_GetModState() & KMOD_ALT) != 0);
- if (direction == "up") {
- if (view.angle < 90) {
- if ((int)view.angle % 90 == 0) view.eye.z = view.eye.z -= 0.5;
- else {
- view.eye.z = view.eye.z - view.vector.z;
- view.eye.x = view.eye.x + view.vector.x;
- }
- }
- else if (view.angle < 180) {
- if ((int)view.angle % 90 == 0) view.eye.x = view.eye.x += 0.5;
- else {
- view.eye.z = view.eye.z - view.vector.z;
- view.eye.x = view.eye.x + view.vector.x;
- }
- }
- else if (view.angle < 270) {
- if ((int)view.angle % 90 == 0) view.eye.z = view.eye.z += 0.5;
- else {
- view.eye.z = view.eye.z - view.vector.z;
- view.eye.x = view.eye.x + view.vector.x;
- }
- }
- else if (view.angle < 360) {
- if ((int)view.angle % 90 == 0) view.eye.x = view.eye.x -= 0.5;
- else {
- view.eye.z = view.eye.z - view.vector.z;
- view.eye.x = view.eye.x + view.vector.x;
- }
- }
- }
- else if (direction == "down") {
- if (view.angle < 90) {
- if ((int)view.angle % 90 == 0) view.eye.z = view.eye.z += 0.5;
- else {
- view.eye.z = view.eye.z + view.vector.z;
- view.eye.x = view.eye.x - view.vector.x;
- }
- }
- else if (view.angle < 180) {
- if ((int)view.angle % 90 == 0) view.eye.x = view.eye.x -= 0.5;
- else {
- view.eye.z = view.eye.z + view.vector.z;
- view.eye.x = view.eye.x - view.vector.x;
- }
- }
- else if (view.angle < 270) {
- if ((int)view.angle % 90 == 0) view.eye.z = view.eye.z -= 0.5;
- else {
- view.eye.z = view.eye.z + view.vector.z;
- view.eye.x = view.eye.x - view.vector.x;
- }
- }
- else if (view.angle < 360) {
- if ((int)view.angle % 90 == 0) view.eye.x = view.eye.x += 0.5;
- else {
- view.eye.z = view.eye.z + view.vector.z;
- view.eye.x = view.eye.x - view.vector.x;
- }
- }
- }
- else if (direction == "left") {
- if (keydown_alt == 1) {
- if ((int)view.angle % 90 == 0) {
- if (view.angle < 90) view.eye.x -= 0.5;
- else if (view.angle < 180) view.eye.z -= 0.5;
- else if (view.angle < 270) view.eye.x += 0.5;
- else if (view.angle < 360) view.eye.z += 0.5;
- }
- else {
- view.eye.x -= view.vector.z;
- view.eye.z -= view.vector.x;
- }
- }
- else {
- view.angle -= 3;
- if (view.angle == -3) view.angle = 357;
- view.vector.x = (float)sin(view.angle * PI / 180.0);
- view.vector.z = (float)cos(view.angle * PI / 180.0);
- }
- }
- else if (direction == "right") {
- if (keydown_alt == 1) {
- if ((int)view.angle % 90 == 0) {
- if (view.angle < 90) view.eye.x += 0.5;
- else if (view.angle < 180) view.eye.z += 0.5;
- else if (view.angle < 270) view.eye.x -= 0.5;
- else if (view.angle < 360) view.eye.z -= 0.5;
- }
- else {
- view.eye.x += view.vector.z;
- view.eye.z += view.vector.x;
- }
- }
- else {
- view.angle += 3;
- if (view.angle == 360) view.angle = 0;
- view.vector.x = (float)sin(view.angle * PI / 180);
- view.vector.z = (float)cos(view.angle * PI / 180);
- }
- }
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLdouble)1024 / 768, 1.0, 100);
- gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.eye.y + view.vector.y, view.eye.z - view.vector.z, 0.0, view.up.y, 0.0);
- // glutPostRedisplay();
- draw();
- // cout << "VIEW angle=" << view.angle << " eye=(" << view.eye.x << "," << view.eye.y << "," << view.eye.z << ") vector=(" << view.vector.x << "," << view.vector.y << "," << view.vector.z << ") center=(" << view.eye.x + view.vector.x << "," << view.eye.y + view.vector.y << "," << view.eye.z - view.vector.z << ")\n";
- }
- // tworzenie scian
- void add_quad(glm::vec3 const (&coords)[4], GLfloat (&emission), GLfloat (&specular), bool fill, bool visibility) {
- quads.resize(quads_counter+1);
- quads[quads_counter].vertices[0] = coords[0];
- quads[quads_counter].vertices[1] = coords[1];
- quads[quads_counter].vertices[2] = coords[2];
- quads[quads_counter].vertices[3] = coords[3];
- quads[quads_counter].emission = &emission;
- quads[quads_counter].specular = &specular;
- quads[quads_counter].filled = fill;
- quads[quads_counter].visible = visibility;
- quads_counter++;
- }
- // tworzenie pilek
- void add_ball(const glm::vec3(&position), const glm::vec3(&velocity), const glm::vec3(&rotation_angle), const glm::vec3(&rotation_speed), const glm::vec3(&rotation_dir),
- double radius, const std::vector<short> &color1RGB, const std::vector<short> &color2RGB, const char* ID) {
- balls.resize(balls_counter + 1);
- balls[balls_counter].position = position;
- balls[balls_counter].velocity = velocity;
- balls[balls_counter].rotation_angle = rotation_angle;
- balls[balls_counter].rotation_speed = rotation_speed;
- balls[balls_counter].rotation_dir = rotation_dir;
- balls[balls_counter].radius = radius;
- balls[balls_counter].color1_RGB = color1RGB;
- balls[balls_counter].color2_RGB = color2RGB;
- balls[balls_counter].mass = 1.25 * PI * sqr(balls[balls_counter].radius) * balls[balls_counter].radius;
- balls[balls_counter].ID = ID;
- make_tex(&(balls[balls_counter]));
- balls[balls_counter].sphere = gluNewQuadric();
- balls_counter++;
- }
- // tworzenie swiatel
- void add_light(const array<double, 4> &position, const array<double, 3> &direction, const array<short, 4> &ambient,
- const array<short, 4> &diffusion, const array<short, 4> &specular, float cutoff, float exponent, bool enable) {
- lights.resize(lights_counter + 1);
- lights[lights_counter].n = light_number;
- lights[lights_counter].enabled = (enable) ? true : false;
- GLfloat lightposition[] = { (float)position[0], (float)position[1], (float)position[2], (float)position[3] };
- GLfloat spotdirection[] = { (float)direction[0], (float)direction[1], (float)direction[2] };
- GLfloat specularRGBA[] = { (float)specular[0] / 256, (float)specular[1] / 256, (float)specular[2] / 256, (float)specular[3] / 256 };
- GLfloat diffusionRGBA[] = { (float)diffusion[0] / 256, (float)diffusion[1] / 256, (float)diffusion[2] / 256, (float)diffusion[3] / 256 };
- GLfloat ambientRGBA[] = { (float)ambient[0] / 256, (float)ambient[1] / 256, (float)ambient[2] / 256, (float)ambient[3] / 256 };
- glLightfv(light_number, GL_POSITION, lightposition);
- glLightfv(light_number, GL_SPOT_DIRECTION, spotdirection);
- glLightfv(light_number, GL_AMBIENT, ambientRGBA);
- glLightfv(light_number, GL_DIFFUSE, diffusionRGBA);
- glLightfv(light_number, GL_SPECULAR, specularRGBA);
- glLightf(light_number, GL_SPOT_CUTOFF, cutoff);
- glLightf(light_number, GL_SPOT_EXPONENT, exponent);
- lights_counter++;
- light_number++;
- }
- // swiatlo wl/wyl
- void switchLight(short light_number) {
- if (lights_counter < light_number) return;
- lights[light_number - 1].enabled = (lights[light_number - 1].enabled) ? false : true;
- (lights[light_number - 1].enabled) ? glEnable(lights[light_number - 1].n) : glDisable(lights[light_number - 1].n);
- }
- // inicjalizacja...
- void init(void) {
- quit = false;
- bouncing = true;
- area = glm::vec3(7.0f, 3.0f, 7.0f);
- bkgcolor.R = 169;
- bkgcolor.G = 167;
- bkgcolor.B = 169;
- quads_counter = 0;
- balls_counter = 0;
- lights_counter = 0;
- light_number = GL_LIGHT0;
- // sciana przednia
- add_quad({ glm::vec3(0.0 - area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
- glm::vec3(area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
- glm::vec3(area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
- glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0) },
- *mat_transparent, *mat_specular, true, true);
- // sciana prawa
- add_quad({ glm::vec3(area.x / 2.0, area.y / 2.0, area.z / 2.0),
- glm::vec3(area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
- glm::vec3(area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
- glm::vec3(area.x / 2.0, -1.0, area.z / 2.0) },
- *mat_transparent, *mat_specular, true, true);
- // sciana tylna
- add_quad({ glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, area.z / 2.0),
- glm::vec3(area.x / 2.0, area.y / 2.0, area.z / 2.0),
- glm::vec3(area.x / 2.0, -1.0, area.z / 2.0),
- glm::vec3(0.0 - area.x / 2.0, -1.0, area.z / 2.0) },
- *mat_transparent, *mat_specular, true, false);
- // sciana lewa
- add_quad({ glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
- glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, area.z / 2.0),
- glm::vec3(0.0 - area.x / 2.0, -1.0, area.z / 2.0),
- glm::vec3(0.0 - area.x / 2.0, -1.0, 0.0 - area.z / 2.0) },
- *mat_transparent, *mat_specular, true, true);
- // sufit
- add_quad({ glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
- glm::vec3(area.x / 2.0, area.y / 2.0, 0.0 - area.z / 2.0),
- glm::vec3(area.x / 2.0, area.y / 2.0, area.z / 2.0),
- glm::vec3(0.0 - area.x / 2.0, area.y / 2.0, area.z / 2.0) },
- *mat_transparent, *mat_specular, true, false);
- // podloga
- add_quad({ glm::vec3(0.0 - area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
- glm::vec3(area.x / 2.0, -1.0, 0.0 - area.z / 2.0),
- glm::vec3(area.x / 2.0, -1.0, area.z / 2.0),
- glm::vec3(0.0 - area.x / 2.0, -1.0, area.z / 2.0) },
- *mat_zero, *mat_transparent, false, true);
- // utworzenie wspolrzednych siatki podlogi. Work in progress... zapomnialem juz, jak to dziala. :=)
- GLfloat x = 0.0 - area.x / 2.0;
- GLfloat y = 1.0;
- GLfloat z = 0.0 - area.z / 2.0;
- unsigned int x_slices = (int)area.x;
- unsigned int z_slices = (int)area.z;
- GLfloat row = 0.0;
- GLfloat column = 0.0;
- cout << "x slices, z slices: " << x_slices << " " << z_slices << "\n";
- // a = new double ctrlpoints*[];
- // &ctrlpoints[z_slices];
- unsigned int i, j;
- // for (ctrlpoints[i]::iterator i = ctrlpoints.begin(); i != ctrlpoints.end(); i++)
- crosspoints.resize(x_slices + 1);
- crosspoints[0].resize(z_slices + 1);
- crosspoints[1].resize(z_slices + 1);
- crosspoints[2].resize(z_slices + 1);
- crosspoints[3].resize(z_slices + 1);
- crosspoints[4].resize(z_slices + 1);
- crosspoints[5].resize(z_slices + 1);
- crosspoints[6].resize(z_slices + 1);
- crosspoints[7].resize(z_slices + 1);
- /* for (i = 0; i <= z_slices; i++) {
- crosspoints[i].resize(z_slices + 1);
- for (j = 0; j <= x_slices; j++) {
- crosspoints[i][j][0] = x + column * j;
- crosspoints[i][j][1] = z + row * i;
- crosspoints[i][j][2] = y;
- // crosspoints[i][j].resize(10);
- // crosspoints[i][j].push_back(x + column * x);
- // crosspoints[i][j].push_back(z + row * z);
- // crosspoints[i][j].push_back(y);
- // ctrlpoints[i][j][0] = x + column * x;
- // ctrlpoints[i][j][1] = z + row * z;
- // ctrlpoints[i][j][2] = y;
- column = column + 1.0;
- j++;
- }
- //ctrlpoints[i][i][i] = 11";
- row = row + 1.0;
- i++;
- }*/
- crosspoints[0][0][0] = -3.5;
- crosspoints[0][0][1] = -3.5;
- crosspoints[0][0][2] = 1.0;
- crosspoints[0][1][0] = -2.5;
- crosspoints[0][1][1] = -3.5;
- crosspoints[0][1][2] = 1.0;
- crosspoints[0][2][0] = -1.5;
- crosspoints[0][2][1] = -3.5;
- crosspoints[0][2][2] = 1.0;
- crosspoints[0][3][0] = -0.5;
- crosspoints[0][3][1] = -3.5;
- crosspoints[0][3][2] = 1.0;
- crosspoints[0][4][0] = 0.5;
- crosspoints[0][4][1] = -3.5;
- crosspoints[0][4][2] = 1.0;
- crosspoints[0][5][0] = 1.5;
- crosspoints[0][5][1] = -3.5;
- crosspoints[0][5][2] = 1.0;
- crosspoints[0][6][0] = 2.5;
- crosspoints[0][6][1] = -3.5;
- crosspoints[0][6][2] = 1.0;
- crosspoints[0][7][0] = 3.5;
- crosspoints[0][7][1] = -3.5;
- crosspoints[0][7][2] = 1.0;
- crosspoints[1][0][0] = -3.5;
- crosspoints[1][0][1] = -2.5;
- crosspoints[1][0][2] = 1.0;
- crosspoints[1][1][0] = -2.5;
- crosspoints[1][1][1] = -2.5;
- crosspoints[1][1][2] = 1.0;
- crosspoints[1][2][0] = -1.5;
- crosspoints[1][2][1] = -2.5;
- crosspoints[1][2][2] = 1.0;
- crosspoints[1][3][0] = -0.5;
- crosspoints[1][3][1] = -2.5;
- crosspoints[1][3][2] = 1.0;
- crosspoints[1][4][0] = 0.5;
- crosspoints[1][4][1] = -2.5;
- crosspoints[1][4][2] = 1.0;
- crosspoints[1][5][0] = 1.5;
- crosspoints[1][5][1] = -2.5;
- crosspoints[1][5][2] = 1.0;
- crosspoints[1][6][0] = 2.5;
- crosspoints[1][6][1] = -2.5;
- crosspoints[1][6][2] = 1.0;
- crosspoints[1][7][0] = 3.5;
- crosspoints[1][7][1] = -2.5;
- crosspoints[1][7][2] = 1.0;
- /*crosspoints.resize(5);
- crosspoints[0].resize(3);
- crosspoints[1].resize(3);
- crosspoints[2].resize(3);
- crosspoints[3].resize(3);
- crosspoints[4].resize(3);
- crosspoints[0][0][0] = -1.5;
- crosspoints[0][1] = { -0.5, -1.5, 4.0 };
- crosspoints[0][2] = { 1.5, -1.5, 4.0 };
- crosspoints[1][0] = { -1.5, -0.5, 4.0 };
- crosspoints[1][1] = { -0.5, -0.5, 4.0 };
- crosspoints[1][2] = { 0.5, -0.5, 4.0 };
- crosspoints[2][0] = { 1.5, -0.5, 4.0 };
- crosspoints[2][1] = { -1.5, 0.5, 4.0 };
- crosspoints[2][2] = { -0.5, 0.5, 0.0 };
- crosspoints[3][0] = { 0.5, 0.5, 3.0 };
- crosspoints[3][1] = { 1.5, 0.5, 4.0 };
- crosspoints[3][2] = { -1.5, 1.5, -2.0 };
- crosspoints[4][1] = { -0.5, 1.5, -2.0 };
- crosspoints[4][2] = { 0.5, 1.5, 0.0 };
- crosspoints[4][3] = { 1.5, 1.5, -1.0 };*/
- // { { -1.5, -1.5, 4.0 }, { -0.5, -1.5, 4.0 },
- // { 0.5, -1.5, 4.0 }, { 1.5, -1.5, 4.0 } },
- // { { -1.5, -0.5, 4.0 }, { -0.5, -0.5, 4.0 },
- // { 0.5, -0.5, 4.0 }, { 1.5, -0.5, 4.0 } },
- // { { -1.5, 0.5, 4.0 }, { -0.5, 0.5, 0.0 },
- // { 0.5, 0.5, 3.0 }, { 1.5, 0.5, 4.0 } },
- // { { -1.5, 1.5, -2.0 }, { -0.5, 1.5, -2.0 },
- // { 0.5, 1.5, 0.0 }, { 1.5, 1.5, -1.0 } }
- //---------------------------- KONIEC TWORZENIA SIATKI NA PODLODZE ------------------------------
- // KULKA CZERWONA
- // rozstawienie na rogach
- add_ball(glm::vec3 { -2.2f, 1.0f, -2.2f },
- glm::vec3 { 0.0, 0.0f, -0.003f },
- glm::vec3 { 0.0f, 0.0f, 0.0f },
- glm::vec3 { 0.2f, 0.0f, 0.0f },
- glm::vec3 { -1, 0, 0 },
- 0.5, { 255, 255, 255 }, { 255, 0, 0 }, "red_ball");
- // ruch wzdluz osi X
- // add_ball(glm::vec3(-2.2f, 1.0f, 0.0f),
- // glm::vec3(-0.003, 0.0f, 0.0f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.2f, 0.0f, 0.0f),
- // glm::vec3(-1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 255, 0, 0 }, "red_ball");
- // ruch wzdluz osi Z
- // add_ball(glm::vec3(0.0f, 1.0f, -2.2f),
- // glm::vec3(0.0, 0.0f, -0.003f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.2f, 0.0f, 0.0f),
- // glm::vec3(-1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 255, 0, 0 }, "red_ball");
- // KULKA NIEBIESKA
- // rozstawienie na rogach
- add_ball(glm::vec3(0.0f, 0.2f, 0.0f),
- glm::vec3(0.007f, 0.0f, 0.007f),
- glm::vec3(0.0f, 0.0f, 0.0f),
- glm::vec3(0.2f, 0.1f, 0.0f),
- glm::vec3(1, 0, 0),
- 0.5, { 255, 255, 255 }, { 0, 0, 255 }, "blue_ball");
- // ruch wzdluz osi X
- // add_ball(glm::vec3(0.0f, 0.2f, 0.0f),
- // glm::vec3(0.007f, 0.0f, 0.0f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.2f, 0.1f, 0.0f),
- // glm::vec3(1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 0, 0, 255 }, "blue_ball");
- // ruch wzdluz osi Z
- // add_ball(glm::vec3(0.0f, 0.2f, 0.0f),
- // glm::vec3(0.0f, 0.0f, 0.007f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.2f, 0.1f, 0.0f),
- // glm::vec3(1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 0, 0, 255 }, "blue_ball");
- // KULKA ZIELONA
- // rozstawienie na rogach
- add_ball(glm::vec3(1.5f, 0.3f, 1.5f),
- glm::vec3(-0.003f, 0.0f, -0.003f),
- glm::vec3(0.0f, 0.0f, 0.0f),
- glm::vec3(0.1f, 0.0f, 0.0f),
- glm::vec3(1, 0, 0),
- 0.5, { 255, 255, 255 }, { 0, 192, 0 }, "green_ball");
- // ruch wzdluz osi X
- // add_ball(glm::vec3(1.5f, 0.3f, 0.0f),
- // glm::vec3(-0.003f, 0.0f, 0.0f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.1f, 0.0f, 0.0f),
- // glm::vec3(1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 0, 192, 0 }, "green_ball");
- // ruch wzdluz osi Z
- // add_ball(glm::vec3(0.0f, 0.3f, 1.5f),
- // glm::vec3(0.0f, 0.0f, -0.003f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.1f, 0.0f, 0.0f),
- // glm::vec3(1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 0, 192, 0 }, "green_ball");
- // KULKA JASNONIEBISKA
- // rozstawienie na rogach
- add_ball(glm::vec3(-2.2f, 1.0f, 2.2f),
- glm::vec3(-0.003f, 0.0f, 0.003f),
- glm::vec3(0.0f, 0.0f, 0.0f),
- glm::vec3(0.2f, 0.0f, 0.0f),
- glm::vec3(-1, 0, 0),
- 0.5, { 255, 255, 255 }, { 0, 192, 192 }, "lightblue_ball");
- // ruch wzdluz osi X
- // add_ball(glm::vec3(-2.2f, 1.0f, 0.0f),
- // glm::vec3(-0.003f, 0.0f, 0.0f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.2f, 0.0f, 0.0f),
- // glm::vec3(-1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 0, 192, 192 }, "lightblue_ball");
- // ruch wzdluz osi Z
- // add_ball(glm::vec3(0.0f, 1.0f, -2.2f),
- // glm::vec3(-0.003f, 0.0f, 0.003f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.2f, 0.0f, 0.0f),
- // glm::vec3(-1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 0, 192, 192 }, "lightblue_ball");
- // KULKA ZLOTA
- // rozstawienie na rogach
- add_ball(glm::vec3(1.5f, 0.3f, -1.5f),
- glm::vec3(0.003f, 0.0f, -0.003f),
- glm::vec3(0.0f, 0.0f, 0.0f),
- glm::vec3(0.1f, 0.0f, 0.0f),
- glm::vec3(1, 0, 0),
- 0.5, { 255, 255, 255 }, { 255, 192, 0 }, "golden_ball");
- // ruch wzdluz osi X
- // add_ball(glm::vec3(1.5f, 0.3f, 0.0f),
- // glm::vec3(0.003f, 0.0f, 0.0f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.1f, 0.0f, 0.0f),
- // glm::vec3(1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 255, 192, 0 }, "golden_ball");
- // ruch wzdluz osi Z
- // add_ball(glm::vec3(0.0f, 0.3f, 1.5f),
- // glm::vec3(0.003f, 0.0f, 0.003f),
- // glm::vec3(0.0f, 0.0f, 0.0f),
- // glm::vec3(0.1f, 0.0f, 0.0f),
- // glm::vec3(1, 0, 0),
- // 0.5, { 255, 255, 255 }, { 255, 192, 0 }, "golden_ball");
- // SWIATLO 0
- add_light(array<double, 4> {{ 0.0, 2.0, 3.0, 0.0 }},
- array<double, 3> {{ 1.0, 1.0, -1.0 }},
- array<short, 4> {{ 51, 51, 51, 256 }},
- array<short, 4> {{ 204, 204, 204, 256 }},
- array<short, 4> {{ 204, 204, 204, 256 }},
- 180.0f, 0.0f, true
- );
- // SWIATLO 1
- add_light(array<double, 4> {{ 0.0, 2.0, -3.0, 0.0 }},
- array<double, 3> {{ 1.0, 1.0, 11.0 }},
- array<short, 4> {{ 51, 51, 51, 256 }},
- array<short, 4> {{ 204, 204, 204, 256 }},
- array<short, 4> {{ 204, 204, 204, 256 }},
- 180.0f, 0.0f, true
- );
- //------------- nizej wlaczenie swiatel i jakies smieci ----------------------------
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glColor3f(1.0, 0.0, 0.0);
- glShadeModel(GL_SMOOTH);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glEnable(GL_LIGHTING);
- for (short i = 0; i < lights_counter; i++) {
- if (lights[i].enabled == true) glEnable(lights[i].n);
- }
- glEnable(GL_DEPTH_TEST);
- // cubeList = glGenLists(1);
- // glNewList(cubeList, GL_COMPILE);
- // glutSolidCube(0.6);
- // glEndList();
- glEnable(GL_TEXTURE_2D);
- // glMatrixMode(GL_MODELVIEW);
- // glLoadIdentity();
- // gluLookAt(view.eye.x, view.eye.y, view.eye.z, view.eye.x + view.vector.x, view.vector.y, view.eye.z - view.vector.z, 0.0, 1.0, 0.0);
- glColor3d(0, 0, 0);
- // glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
- // 0, 1, 12, 4, &*ctrlpoints[0][0][0]);
- glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 8,
- 0, 1, 27, 8, &ctrlpoints[0][0][0]);
- glEnable(GL_MAP2_VERTEX_3);
- glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
- glEnable(GL_DEPTH_TEST);
- glShadeModel(GL_FLAT);
- //-------------------------- koniec smieci ---------------------------------
- keydown_left = 0;
- keydown_right = 0;
- keydown_up = 0;
- keydown_down = 0;
- keydown_alt = 0;
- changeView("viewAngleX");
- run = 1;
- }
- void myIdle() {
- if (run == 1) {
- short i, j;
- for (i = 0; i < balls_counter - 1; i++) {
- for (j = i + 1; j < balls_counter; j++) {
- CollisionDetect7(&balls[j], &balls[i]);
- }
- }
- for (i = 0; i < balls_counter; i++) {
- RunPhysics3(0.003f, &balls[i]);
- }
- }
- inputSDL();
- draw();
- }
- // eventy klawiszkowe SDL
- static void inputSDL(void) {
- /* Our SDL event placeholder. */
- SDL_Event event;
- /* Grab all the events off the queue. */
- while (SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_KEYDOWN: {
- /* Handle key presses. */
- SDLKey key = event.key.keysym.sym;
- switch (key) {
- case SDLK_ESCAPE:
- quit = true;
- break;
- case SDLK_1:
- (SDL_GetModState() & KMOD_CTRL) ? switchLight(1) : changeView("viewX");
- break;
- case SDLK_2:
- (SDL_GetModState() & KMOD_CTRL) ? switchLight(2) : changeView("viewZ");
- break;
- case SDLK_3:
- (SDL_GetModState() & KMOD_CTRL) ? switchLight(3) : changeView("viewTopX");
- break;
- case SDLK_4:
- (SDL_GetModState() & KMOD_CTRL) ? switchLight(4) : changeView("viewTopZ");
- break;
- case SDLK_5:
- (SDL_GetModState() & KMOD_CTRL) ? switchLight(5) : changeView("viewAngleX");
- break;
- case SDLK_6:
- (SDL_GetModState() & KMOD_CTRL) ? switchLight(6) : changeView("viewAngleZ");
- break;
- case SDLK_7:
- (SDL_GetModState() & KMOD_CTRL) ? switchLight(7) : void();
- break;
- case SDLK_8:
- (SDL_GetModState() & KMOD_CTRL) ? switchLight(8) : void();
- break;
- case SDLK_LEFT:
- keydown_left = 1;
- break;
- case SDLK_RIGHT:
- keydown_right = 1;
- break;
- case SDLK_UP:
- keydown_up = 1;
- break;
- case SDLK_DOWN:
- keydown_down = 1;
- break;
- case SDLK_s:
- run = (run) ? 0 : 1;
- break;
- case SDLK_b:
- bouncing = (bouncing) ? 0 : 1;
- break;
- default:
- break;
- }
- }
- break;
- case SDL_KEYUP: {
- /* Handle key presses. */
- SDLKey key = event.key.keysym.sym;
- switch (key) {
- case SDLK_LEFT:
- keydown_left = 0;
- break;
- case SDLK_RIGHT:
- keydown_right = 0;
- break;
- case SDLK_UP:
- keydown_up = 0;
- break;
- case SDLK_DOWN:
- keydown_down = 0;
- break;
- default:
- break;
- }
- }
- break;
- case SDL_QUIT:
- quit = true;
- break;
- }
- }
- }
- /*void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
- {
- // Activate corresponding render state
- shader.Use();
- glUniform3f(glGetUniformLocation(shader.Program, "textColor"), color.x, color.y, color.z);
- glActiveTexture(GL_TEXTURE0);
- glBindVertexArray(VAO);
- // Iterate through all characters
- std::string::const_iterator c;
- for (c = text.begin(); c != text.end(); c++)
- {
- Character ch = Characters[*c];
- GLfloat xpos = x + ch.Bearing.x * scale;
- GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
- GLfloat w = ch.Size.x * scale;
- GLfloat h = ch.Size.y * scale;
- // Update VBO for each character
- GLfloat vertices[6][4] = {
- { xpos, ypos + h, 0.0, 0.0 },
- { xpos, ypos, 0.0, 1.0 },
- { xpos + w, ypos, 1.0, 1.0 },
- { xpos, ypos + h, 0.0, 0.0 },
- { xpos + w, ypos, 1.0, 1.0 },
- { xpos + w, ypos + h, 1.0, 0.0 }
- };
- // Render glyph texture over quad
- glBindTexture(GL_TEXTURE_2D, ch.TextureID);
- // Update content of VBO memory
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- // Render quad
- glDrawArrays(GL_TRIANGLES, 0, 6);
- // Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
- x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
- }
- glBindVertexArray(0);
- glBindTexture(GL_TEXTURE_2D, 0);
- }*/
- // ustawienia FreeType
- void setup_freetype() {
- /* glewExperimental = GL_TRUE;
- glewInit();
- glEnable(GL_CULL_FACE);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- Shader shader("shader.vs", "shader.frag");
- glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), 0.0f, static_cast<GLfloat>(HEIGHT));
- shader.Use();
- glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
- // FreeType
- FT_Library ft;
- // All functions return a value different than 0 whenever an error occurred
- if (FT_Init_FreeType(&ft))
- std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
- // Load font as face
- FT_Face face;
- if (FT_New_Face(ft, "C:/Windows/Fonts/arial.ttf", 0, &face))
- std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
- // Set size to load glyphs as
- FT_Set_Pixel_Sizes(face, 0, 48);
- // Disable byte-alignment restriction
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- // Load first 128 characters of ASCII set
- for (GLubyte c = 0; c < 128; c++)
- {
- // Load character glyph
- if (FT_Load_Char(face, c, FT_LOAD_RENDER))
- {
- std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
- continue;
- }
- // Generate texture
- GLuint texture;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexImage2D(
- GL_TEXTURE_2D,
- 0,
- GL_RED,
- face->glyph->bitmap.width,
- face->glyph->bitmap.rows,
- 0,
- GL_RED,
- GL_UNSIGNED_BYTE,
- face->glyph->bitmap.buffer
- );
- // Set texture options
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- // Now store character for later use
- Character character = {
- texture,
- glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
- glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
- face->glyph->advance.x
- };
- Characters.insert(std::pair<char, Character>(c, character));
- }
- glBindTexture(GL_TEXTURE_2D, 0);
- // Destroy FreeType once we're finished
- FT_Done_Face(face);
- FT_Done_FreeType(ft);
- // Configure VAO/VBO for texture quads
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)* 6 * 4, NULL, GL_DYNAMIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);*/
- }
- // ustawienia openGL
- static void setup_opengl(unsigned int width, unsigned int height)
- {
- float ratio = (float)width / (float)height;
- /* Our shading model--Gouraud (smooth). */
- glShadeModel(GL_SMOOTH);
- /* Culling. */
- glCullFace(GL_BACK);
- glFrontFace(GL_CCW);
- glEnable(GL_CULL_FACE);
- /* Set the clear color. */
- glClearColor(0, 0, 0, 0);
- /* Setup our viewport. */
- glViewport(0, 0, width, height);
- /*
- * Change to the projection matrix and set
- * our viewing volume.
- */
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- /*
- * EXERCISE:
- * Replace this with a call to glFrustum.
- */
- gluPerspective(60.0, ratio, 1.0, 1024.0);
- }
- // ustawienia SDL
- void setup_SDL() {
- /* Information about the current video settings. */
- const SDL_VideoInfo* info = NULL;
- /* Dimensions of our window. */
- unsigned int width = 0;
- unsigned int height = 0;
- /* Color depth in bits of our window. */
- unsigned int bpp = 0;
- /* Flags we will pass into SDL_SetVideoMode. */
- unsigned int flags = 0;
- /* First, initialize SDL's video subsystem. */
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- /* Failed, exit. */
- fprintf(stderr, "Video initialization failed: %s\n",
- SDL_GetError());
- }
- /* Let's get some video information. */
- info = SDL_GetVideoInfo();
- if (!info) {
- /* This should probably never happen. */
- fprintf(stderr, "Video query failed: %s\n",
- SDL_GetError());
- }
- /*
- * Set our width/height to 640/480 (you would
- * of course let the user decide this in a normal
- * app). We get the bpp we will request from
- * the display. On X11, VidMode can't change
- * resolution, so this is probably being overly
- * safe. Under Win32, ChangeDisplaySettings
- * can change the bpp.
- */
- width = 1024;
- height = 768;
- bpp = info->vfmt->BitsPerPixel;
- /*
- * Now, we want to setup our requested
- * window attributes for our OpenGL window.
- * We want *at least* 5 bits of red, green
- * and blue. We also want at least a 16-bit
- * depth buffer.
- *
- * The last thing we do is request a double
- * buffered window. '1' turns on double
- * buffering, '0' turns it off.
- *
- * Note that we do not use SDL_DOUBLEBUF in
- * the flags to SDL_SetVideoMode. That does
- * not affect the GL attribute state, only
- * the standard 2D blitting setup.
- */
- SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
- SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
- SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- /*
- * We want to request that SDL provide us
- * with an OpenGL window, in a fullscreen
- * video mode.
- *
- * EXERCISE:
- * Make starting windowed an option, and
- * handle the resize events properly with
- * glViewport.
- */
- flags = SDL_OPENGL;
- /*
- * Set the video mode
- */
- if (SDL_SetVideoMode(width, height, bpp, flags) == 0) {
- /*
- * This could happen for a variety of reasons,
- * including DISPLAY not being set, the specified
- * resolution not being available, etc.
- */
- fprintf(stderr, "Video mode set failed: %s\n",
- SDL_GetError());
- }
- /*
- * At this point, we should have a properly setup
- * double-buffered window for use with OpenGL.
- */
- setup_opengl(width, height);
- }
- int main(int argc, char **argv) {
- // ponizsze na potrzeby testow
- cout.precision(std::numeric_limits<double>::max_digits10);
- setup_SDL();
- //---> GLUT
- // glutInit(&argc, argv);
- //---> GLFW
- // GLFWwindow* window = glfwCreateWindow(1024, 768, "My Title", glfwGetPrimaryMonitor(), NULL);
- setup_freetype();
- // glutDisplayFunc(draw);
- // glutIdleFunc(myIdle);
- // glutReshapeFunc(resize);
- init();
- SDL_Init(SDL_INIT_TIMER);
- SDL_Init(SDL_INIT_EVENTTHREAD);
- unsigned int lastTime = 0, currentTime;
- while (quit == false) {
- // glutMainLoopEvent();
- myIdle();
- currentTime = SDL_GetTicks();
- if (currentTime > lastTime + 40) {
- lastTime = currentTime;
- if (keydown_up == 1) move("up");
- if (keydown_down == 1) move("down");
- if (keydown_left == 1) move("left");
- if (keydown_right == 1) move("right");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement