Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdlib.h>
- #if defined(__APPLE__)
- #include <OpenGL/gl.h>
- #include <OpenGL/glu.h>
- #include <GLUT/glut.h>
- #else
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
- #include <windows.h>
- #endif
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #endif
- struct Vector {
- float x, y, z;
- Vector(float v = 0) : x(v), y(v), z(v) { }
- Vector(float x, float y, float z) : x(x), y(y), z(z) { }
- Vector operator+(const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
- Vector operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
- Vector& operator+=(const Vector& v) { x += v.x, y += v.y, z += v.z; return *this; }
- Vector operator*(const Vector& v) const { return Vector(x * v.x, y * v.y, z * v.z); }
- Vector operator/(const Vector& v) const { return Vector(x / v.x, y / v.y, z / v.z); }
- };
- Vector operator*(float f, const Vector& v) {
- return v*f;
- }
- struct Curve {
- Vector color;
- size_t num_cpts;
- static const size_t max_cpt_num = 50;
- Vector cpts[max_cpt_num]; // controll_points
- Curve(const Vector& color) : color(color), num_cpts(0) { }
- virtual ~Curve() { }
- // Az x, y koordináták tárolják a pont helyét, míg a 'z' a görbe paramétert
- void AddContollPoint(const Vector& v) {
- cpts[num_cpts++] = v;
- }
- void AddContollPoint(float x, float y, float z) {
- cpts[num_cpts++] = Vector(x, y, z);
- }
- virtual Vector r(float t) const = 0;
- const Vector& p(int i) const { return cpts[i]; }
- float t(int i) const { return cpts[i].z - cpts[0].z; }
- void DrawCurve(float time) const {
- const float resolution = (t(num_cpts - 1) - t(0)) / 200;
- glBegin(GL_LINE_STRIP); {
- glColor3fv(&color.x);
- for(float i = t(0); i <= t(num_cpts - 1); i += resolution) {
- if(time < p(0).z + i)
- break;
- Vector v = r(i);
- glVertex2f(v.x, v.y);
- }
- } glEnd();
- }
- };
- // Tenziós Catmull-Rom
- struct TCR : public Curve {
- float tension;
- TCR(const Vector& color = Vector(1, 1, 1), float tension = -0.5f)
- : Curve(color), tension(tension)
- { }
- Vector v(int i) const {
- if(i == 0 || i == num_cpts - 1) {
- return 0.0f; // Itt nullával osztanánk, ha az alsó képletet használnánk.
- } else {
- // TCR definíció szerinti sebesség képlete.
- Vector prev = (p(i) - p(i-1)) / (t(i) - t(i-1));
- Vector next = (p(i+1) - p(i)) / (t(i+1) - t(i));
- return 0.5f * (prev + next) * (1 - tension);
- }
- }
- // Visszaadja, hogy a paraméterben kapott értéknél kisebb
- // görbeparaméterű pontok közül, az adott értékhez a legközlebbit.
- int SearchIndex(float curr_t) const {
- for(int i = 0; i + 1 < num_cpts; ++i) {
- if(t(i+1) > curr_t) {
- return i;
- }
- }
- return num_cpts - 1;
- }
- // Cattmull-Rom képletébe behelyettesítés
- Vector r(float curr_t) const {
- int i = SearchIndex(curr_t);
- if(i == num_cpts - 1) {
- return p(i);
- }
- Vector a[4];
- a[0] = p(i);
- a[1] = v(i);
- a[2] = 3*(p(i+1) - p(i)) / pow(t(i+1) - t(i), 2) -
- (v(i+1) + 2*v(i)) / (t(i+1) - t(i));
- a[3] = 2*(p(i) - p(i+1)) / pow(t(i+1) - t(i), 3) +
- (v(i+1) + v(i)) / pow(t(i+1) - t(i), 2);
- Vector ret;
- for(int j = 0; j < 4; ++j) {
- ret += pow(curr_t - t(i), j) * a[j];
- }
- return ret;
- }
- };
- TCR tcr_s = TCR(Vector(0, 0, 0));
- TCR tcr_or = TCR(Vector(0, 0, 0));
- TCR tcr_left_dot = TCR(Vector(0, 0, 0));
- TCR tcr_right_dot = TCR(Vector(0, 0, 0));
- void onInitialization() {
- glClearColor(1, 1, 1, 1);
- glLineWidth(3.0f);
- tcr_s.AddContollPoint(-0.5f, 0.8f, 0.0f);
- tcr_s.AddContollPoint(-0.9f, 0.4f, 0.33f);
- tcr_s.AddContollPoint(-0.5f, -0.4f, 0.66f);
- tcr_s.AddContollPoint(-0.9f, -0.8f, 1.0f);
- tcr_or.AddContollPoint(-0.61f, -0.1f, 1.10f);
- tcr_or.AddContollPoint(-0.3f, 0.0f, 1.20f);
- tcr_or.AddContollPoint(0.0f, 0.15f, 1.25f);
- tcr_or.AddContollPoint(0.3f, 0.00f, 1.30f);
- tcr_or.AddContollPoint(0.3f, -0.4f, 1.40f);
- tcr_or.AddContollPoint(-0.3f, -0.4f, 1.50f);
- tcr_or.AddContollPoint(-0.3f, 0.0f, 1.60f);
- tcr_or.AddContollPoint(0.6f, 0.15f, 1.75f);
- tcr_or.AddContollPoint(0.6f, -0.4f, 1.90f);
- tcr_or.AddContollPoint(0.71f, -0.1f, 1.975f);
- tcr_or.AddContollPoint(0.8f, 0.15f, 2.05f);
- tcr_or.AddContollPoint(1.0f, 0.15f, 2.1f);
- tcr_left_dot.AddContollPoint(-0.20f, 0.42f, 2.2f);
- tcr_left_dot.AddContollPoint(-0.22f, 0.40f, 2.22f);
- tcr_left_dot.AddContollPoint(-0.20f, 0.38f, 2.24f);
- tcr_left_dot.AddContollPoint(-0.18f, 0.40f, 2.26f);
- tcr_left_dot.AddContollPoint(-0.20f, 0.42f, 2.28f);
- tcr_right_dot.AddContollPoint(0.20f, 0.42f, 2.3f);
- tcr_right_dot.AddContollPoint(0.22f, 0.40f, 2.32f);
- tcr_right_dot.AddContollPoint(0.20f, 0.38f, 2.34f);
- tcr_right_dot.AddContollPoint(0.18f, 0.40f, 2.36f);
- tcr_right_dot.AddContollPoint(0.20f, 0.42f, 2.38f);
- }
- void onDisplay() {
- glClear(GL_COLOR_BUFFER_BIT);
- float time = glutGet(GLUT_ELAPSED_TIME) / 2000.0f;
- tcr_s.DrawCurve(time);
- tcr_or.DrawCurve(time);
- tcr_left_dot.DrawCurve(time);
- tcr_right_dot.DrawCurve(time);
- glutSwapBuffers();
- }
- void onIdle() {
- glutPostRedisplay();
- }
- void onKeyboard(unsigned char key, int, int) {}
- void onKeyboardUp(unsigned char key, int, int) {}
- void onMouse(int, int, int, int) {}
- void onMouseMotion(int, int) {}
- int main(int argc, char **argv) {
- glutInit(&argc, argv);
- glutInitWindowSize(600, 600);
- glutInitWindowPosition(100, 100);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Grafika pelda program");
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- onInitialization();
- glutDisplayFunc(onDisplay);
- glutMouseFunc(onMouse);
- glutIdleFunc(onIdle);
- glutKeyboardFunc(onKeyboard);
- glutKeyboardUpFunc(onKeyboardUp);
- glutMotionFunc(onMouseMotion);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement