Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================================
- // Szamitogepes grafika hazi feladat keret. Ervenyes 2014-tol.
- // A //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // sorokon beluli reszben celszeru garazdalkodni, mert a tobbit ugyis toroljuk.
- // A beadott program csak ebben a fajlban lehet, a fajl 1 byte-os ASCII karaktereket tartalmazhat.
- // Tilos:
- // - mast "beincludolni", illetve mas konyvtarat hasznalni
- // - faljmuveleteket vegezni (printf is fajlmuvelet!)
- // - new operatort hivni az onInitialization fĂźggvĂŠnyt kivĂŠve, a lefoglalt adat korrekt felszabadĂtĂĄsa nĂŠlkĂźl
- // - felesleges programsorokat a beadott programban hagyni
- // - tovabbi kommenteket a beadott programba irni a forrasmegjelolest kommentjeit kiveve
- // ---------------------------------------------------------------------------------------------
- // A feladatot ANSI C++ nyelvu forditoprogrammal ellenorizzuk, a Visual Studio-hoz kepesti elteresekrol
- // es a leggyakoribb hibakrol (pl. ideiglenes objektumot nem lehet referencia tipusnak ertekul adni)
- // a hazibeado portal ad egy osszefoglalot.
- // ---------------------------------------------------------------------------------------------
- // A feladatmegoldasokban csak olyan gl/glu/glut fuggvenyek hasznalhatok, amelyek
- // 1. Az oran a feladatkiadasig elhangzottak ES (logikai AND muvelet)
- // 2. Az alabbi listaban szerepelnek:
- // Rendering pass: glBegin, glVertex[2|3]f, glColor3f, glNormal3f, glTexCoord2f, glEnd, glDrawPixels
- // Transzformaciok: glViewport, glMatrixMode, glLoadIdentity, glMultMatrixf, gluOrtho2D,
- // glTranslatef, glRotatef, glScalef, gluLookAt, gluPerspective, glPushMatrix, glPopMatrix,
- // Illuminacio: glMaterialfv, glMaterialfv, glMaterialf, glLightfv
- // Texturazas: glGenTextures, glBindTexture, glTexParameteri, glTexImage2D, glTexEnvi,
- // Pipeline vezerles: glShadeModel, glEnable/Disable a kovetkezokre:
- // GL_LIGHTING, GL_NORMALIZE, GL_DEPTH_TEST, GL_CULL_FACE, GL_TEXTURE_2D, GL_BLEND, GL_LIGHT[0..7]
- //
- // NYILATKOZAT
- // ---------------------------------------------------------------------------------------------
- // Nev : <RÉTI> <MARCELL>
- // Neptun : <SWESQ1>
- // ---------------------------------------------------------------------------------------------
- // ezennel kijelentem, hogy a feladatot magam keszitettem, es ha barmilyen segitseget igenybe vettem vagy
- // mas szellemi termeket felhasznaltam, akkor a forrast es az atvett reszt kommentekben egyertelmuen jeloltem.
- // A forrasmegjeloles kotelme vonatkozik az eloadas foliakat es a targy oktatoi, illetve a
- // grafhazi doktor tanacsait kiveve barmilyen csatornan (szoban, irasban, Interneten, stb.) erkezo minden egyeb
- // informaciora (keplet, program, algoritmus, stb.). Kijelentem, hogy a forrasmegjelolessel atvett reszeket is ertem,
- // azok helyessegere matematikai bizonyitast tudok adni. Tisztaban vagyok azzal, hogy az atvett reszek nem szamitanak
- // a sajat kontribucioba, igy a feladat elfogadasarol a tobbi resz mennyisege es minosege alapjan szuletik dontes.
- // Tudomasul veszem, hogy a forrasmegjeloles kotelmenek megsertese eseten a hazifeladatra adhato pontokat
- // negativ elojellel szamoljak el es ezzel parhuzamosan eljaras is indul velem szemben.
- //=============================================================================================
- #define _USE_MATH_DEFINES
- #include <math.h>
- #include <stdlib.h>
- #include <iostream> /////////szeddki!!!
- using namespace std; ////////////eztis!!
- #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
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // Innentol modosithatod...
- //--------------------------------------------------------
- // 3D Vektor
- //--------------------------------------------------------
- struct Vector {
- float x, y, z;
- Vector() {
- x = y = z = 0;
- }
- Vector(float x0, float y0, float z0 = 0) {
- x = x0; y = y0; z = z0;
- }
- Vector operator*(float a) {
- return Vector(x * a, y * a, z * a);
- }
- Vector operator+(const Vector& v) {
- return Vector(x + v.x, y + v.y, z + v.z);
- }
- Vector operator-(const Vector& v) {
- return Vector(x - v.x, y - v.y, z - v.z);
- }
- float operator*(const Vector& v) { // dot product
- return (x * v.x + y * v.y + z * v.z);
- }
- Vector operator%(const Vector& v) { // cross product
- return Vector(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
- }
- float Length() { return sqrt(x * x + y * y + z * z); }
- };
- const int screenWidth = 600; // alkalmazĂĄs ablak felbontĂĄsa
- const int screenHeight = 600;
- bool bouncestart = false;
- struct Vector2D {
- float x, y;
- Vector2D() {
- x = y = 0;
- }
- Vector2D(float x0, float y0) {
- x = x0; y = y0;
- }
- Vector2D operator*(float a) {
- return Vector2D(x * a, y * a);
- }
- Vector2D operator/(float a) {
- return Vector2D(x / a, y / a);
- }
- Vector2D operator+(const Vector2D& v) {
- return Vector2D(x + v.x, y + v.y);
- }
- Vector2D operator-(const Vector2D& v) {
- return Vector2D(x - v.x, y - v.y);
- }
- float operator*(const Vector2D& v) { // dot product
- return (x * v.x + y * v.y);
- }
- float Length() { return sqrt(x * x + y * y); }
- };
- struct CatmullRom {
- Vector2D center;
- Vector2D speed;
- float time;
- CatmullRom()
- {
- center = Vector2D(0, 0);
- speed = Vector2D(0, 0);
- time = 0;
- }
- CatmullRom(Vector2D xy0, Vector2D vxy0, float t0)
- {
- center = xy0;
- speed = vxy0;
- time = t0;
- }
- };
- Vector2D ConvertMousePoints(float x, float y)
- {
- Vector2D ret;
- ret.x = x / 600 * 1000;
- ret.y = y / 600 * 1000;
- return ret;
- }
- Vector2D CalculateCatmullSpeed(Vector2D r0, Vector2D r1, Vector2D r2, float t0, float t1, float t2)
- {
- Vector2D ret;
- ret = ((r2 - r1) / (t2 - t1) + (r1 - r0) / (t1 - t0)) * 0.5;
- return ret;
- }
- //--------------------------------------------------------
- // Spektrum illetve szin
- //--------------------------------------------------------
- struct Color {
- float r, g, b;
- Color() {
- r = g = b = 0;
- }
- Color(float r0, float g0, float b0) {
- r = r0; g = g0; b = b0;
- }
- Color operator*(float a) {
- return Color(r * a, g * a, b * a);
- }
- Color operator*(const Color& c) {
- return Color(r * c.r, g * c.g, b * c.b);
- }
- Color operator+(const Color& c) {
- return Color(r + c.r, g + c.g, b + c.b);
- }
- };
- Color image[screenWidth*screenHeight]; // egy alkalmazĂĄs ablaknyi kĂŠp
- CatmullRom splinepoints[1500];
- Vector2D parabolapoints[4000];
- int indexp = 0;
- int index = 0;
- int last_time = glutGet(GLUT_ELAPSED_TIME);
- struct Camera{
- Vector2D center;
- Vector2D speed;
- Camera()
- {
- center = Vector2D(0, 0);
- speed = Vector2D(0, 0);
- }
- };
- Camera camera;
- // Inicializacio, a program futasanak kezdeten, az OpenGL kontextus letrehozasa utan hivodik meg (ld. main() fv.)
- void onInitialization() {
- glViewport(0, 0, screenWidth, screenHeight);
- glClearColor(0.1f, 0.2f, 0.3f, 1.0f); // torlesi szin beallitasa
- //glClearColor(0, 1, 1, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // kepernyo torles
- camera.center = Vector2D(0, 0);
- //camera.speed = Vector2D(20, 30);
- camera.speed = Vector2D(-20, -30);
- gluOrtho2D(0, 1000,1000, 0);
- last_time = glutGet(GLUT_ELAPSED_TIME);
- }
- void DrawCircle(CatmullRom v)
- {
- float radius = 5; //1000-es skálán nézve
- int RESOLUTION = 150;
- glBegin(GL_TRIANGLE_FAN);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex2f(v.center.x, v.center.y);
- for (int j = 0; j <= RESOLUTION; j++)
- {
- float angle = float(j) / RESOLUTION*2.0f*M_PI;
- glVertex2f(v.center.x + radius*cos(angle), v.center.y + radius*sin(angle));
- }
- glEnd();
- glBegin(GL_LINE_STRIP);
- glColor3f(1.0f, 1.0f, 1.0f);
- for (int j = 0; j <= RESOLUTION; j++)
- {
- float angle = float(j) / RESOLUTION*2.0f*M_PI;
- glVertex2f(v.center.x + radius*cos(angle), v.center.y + radius*sin(angle));
- }
- glEnd();
- }
- void DrawCatmullRomSpline()
- {
- glColor3f(1.0f, 1.0f, 1.0f);
- for (int i = 0; i < index - 1; i++)
- {
- glBegin(GL_LINE_STRIP);
- Vector2D a0 = splinepoints[i].center;
- Vector2D a1 = splinepoints[i].speed;
- Vector2D a2 = ((splinepoints[i + 1].center - splinepoints[i].center) * 3) / (pow((splinepoints[i + 1].time - splinepoints[i].time), 2)) - (splinepoints[i + 1].speed + splinepoints[i].speed * 2) / (splinepoints[i + 1].time - splinepoints[i].time);
- Vector2D a3 = ((splinepoints[i].center - splinepoints[i + 1].center) * 2) / (pow((splinepoints[i + 1].time - splinepoints[i].time), 3)) + (splinepoints[i + 1].speed + splinepoints[i].speed) / (pow((splinepoints[i + 1].time - splinepoints[i].time),2));
- for (float j = splinepoints[i].time; j < splinepoints[i + 1].time; j ++)
- {
- float t = j - splinepoints[i].time;
- Vector2D v = a3*pow(t, 3) + a2*pow(t, 2) + a1*t + a0;
- glVertex2f(v.x,v.y);
- }
- glEnd();
- }
- glBegin(GL_LINE_STRIP);
- Vector2D a0 = splinepoints[index - 1].center;
- Vector2D a1 = splinepoints[index - 1].speed;
- Vector2D a2 = ((splinepoints[0].center - splinepoints[index - 1].center) * 3) / (pow(splinepoints[0].time, 2)) - (splinepoints[0].speed + splinepoints[index - 1].speed * 2) / (splinepoints[0].time);
- Vector2D a3 = ((splinepoints[index - 1].center - splinepoints[0].center) * 2) / (pow(splinepoints[0].time, 3)) + (splinepoints[0].speed + splinepoints[index - 1].speed) / (pow(splinepoints[0].time, 2));
- for (float j = splinepoints[index - 1].time; j < (splinepoints[index - 1].time + splinepoints[0].time); j ++)
- {
- float t = j - splinepoints[index - 1].time;
- Vector2D v = a3*pow(t, 3) + a2*pow(t, 2) + a1*t + a0;
- glVertex2f(v.x, v.y);
- }
- glEnd();
- }
- void DrawParabole()
- {
- Vector2D p1 = splinepoints[0].center;
- Vector2D p2 = splinepoints[1].center;
- Vector2D p3 = splinepoints[2].center;
- Vector2D i = p2 - p1;
- Vector2D n(i.y*(-1.0),i.x);
- float C = -1.0*(n.x * p1.x + n.y * p1.y);
- float A = n.x;
- float B = n.y;
- float distance = (abs(A * p3.x + B * p3.y + C)) / (sqrt(A * A + B * B));
- distance = round(distance);
- if (bouncestart)
- {
- glBegin(GL_POINTS);
- glColor3f(1.0f, 1.0f, 0.0f);
- for (float i = 0; i < 1000; i=i+0.5)
- {
- for (float j = 0; j < 1000; j=j+0.5)
- {
- float distancefromline = round((abs(A * i + B * j + C)) / (sqrt(A * A + B * B)));
- float distancefrompoint = round(sqrt(pow(i - p3.x, 2) + pow(j - p3.y, 2)));
- if (distancefrompoint == distancefromline)
- {
- glColor3f(1, 1, 0);
- }
- else if (distancefrompoint < distancefromline)
- {
- glColor3f(1, 1, 0);
- }
- else
- {
- glColor3f(0, 1, 1);
- }
- glVertex2f(i, j);
- }
- }
- glEnd();
- }
- else
- {
- glBegin(GL_POINTS);
- glColor3f(1.0f, 1.0f, 0.0f);
- for (int i = 0; i < 1000; i++)
- {
- for (int j = 0; j < 1000; j++)
- {
- float distancefromline = round((abs(A * i + B * j + C)) / (sqrt(A * A + B * B)));
- float distancefrompoint = round(sqrt(pow(i - p3.x, 2) + pow(j - p3.y, 2)));
- if (distancefrompoint == distancefromline)
- {
- parabolapoints[indexp] = Vector2D(i, j);
- glColor3f(1, 1, 0);
- indexp++;
- }
- else if (distancefrompoint < distancefromline)
- {
- glColor3f(1, 1, 0);
- }
- else
- {
- glColor3f(0, 1, 1);
- }
- glVertex2f(i, j);
- }
- }
- glEnd();
- }
- }
- void DrawTouchLine()
- {
- Vector2D p1 = splinepoints[0].center;
- Vector2D p2 = splinepoints[1].center;
- Vector2D p3 = splinepoints[2].center;
- Vector2D a0 = splinepoints[1].center;
- Vector2D a1 = splinepoints[1].speed;
- Vector2D a2 = ((splinepoints[2].center - splinepoints[1].center) * 3) / (pow((splinepoints[2].time - splinepoints[1].time), 2)) - (splinepoints[2].speed + splinepoints[1].speed * 2) / (splinepoints[2].time - splinepoints[1].time);
- Vector2D a3 = ((splinepoints[1].center - splinepoints[2].center) * 2) / (pow((splinepoints[2].time - splinepoints[1].time), 3)) + (splinepoints[2].speed + splinepoints[1].speed) / (pow((splinepoints[2].time - splinepoints[1].time), 2));
- bool found = false;
- for (float j = splinepoints[1].time; j < splinepoints[2].time; j++)
- {
- float t = j - splinepoints[1].time;
- Vector2D v = a3*pow(t, 3) + a2*pow(t, 2) + a1*t + a0;
- for (int i = 0; i < indexp; i++)
- {
- if (abs(parabolapoints[i].x - v.x) < 3 && abs(parabolapoints[i].y - v.y) < 3)
- {
- found = true;
- Vector2D C;
- float distance = sqrt(pow(p3.x - parabolapoints[i].x, 2) + pow(p3.y - parabolapoints[i].y, 2));
- for (float t = -2; t < 2; t+=0.1)
- {
- int x = p1.x * (1 - t) + p2.x * t;
- int y = p1.y * (1 - t) + p2.y * t;
- float distancefrompoint = sqrt(pow(x - parabolapoints[i].x, 2) + pow(y - parabolapoints[i].y, 2));
- if (abs(distance - distancefrompoint) < 5)
- {
- C.x = x;
- C.y = y;
- break;
- }
- }
- Vector2D S(((p3.x + C.x) / 2), ((p3.y + C.y) / 2));
- glColor3f(0.0, 1.0, 0.0);
- glLineWidth(3);
- glBegin(GL_LINE_STRIP);
- for (int t = -1000; t < 1000; t++)
- {
- int x = S.x * (1 - t) + parabolapoints[i].x * t;
- int y = S.y * (1 - t) + parabolapoints[i].y * t;
- glVertex2f(x, y);
- }
- glEnd();
- glBegin(GL_LINE_STRIP);
- Vector2D n(a1.y*(-1.0), a1.x);
- for (int t = -1000; t < 1000; t++)
- {
- int x = n.x * (1 - t) + parabolapoints[i].x * t;
- int y = n.y * (1 - t) + parabolapoints[i].y * t;
- glVertex2f(x, y);
- }
- glEnd();
- glLineWidth(1);
- break;
- }
- }
- if (found)
- {
- break;
- }
- }
- }
- // Rajzolas, ha az alkalmazas ablak ervenytelenne valik, akkor ez a fuggveny hivodik meg
- void onDisplay() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if (index >= 3)
- {
- DrawParabole();
- DrawCatmullRomSpline();
- DrawTouchLine();
- }
- for (int i = 0; i < index; i++)
- {
- DrawCircle(splinepoints[i]);
- }
- glutSwapBuffers();
- }
- // Billentyuzet esemenyeket lekezelo fuggveny (lenyomas)
- void onKeyboard(unsigned char key, int x, int y) {
- if (key == ' ')
- {
- bouncestart = true;
- glTranslatef(camera.center.x + 250, camera.center.y + 250, 0);
- glScalef(0.5, 0.5, 0);
- glutPostRedisplay();
- }
- }
- // Billentyuzet esemenyeket lekezelo fuggveny (felengedes)
- void onKeyboardUp(unsigned char key, int x, int y) {
- }
- // Eger esemenyeket lekezelo fuggveny
- void onMouse(int button, int state, int x, int y) {
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) // A GLUT_LEFT_BUTTON / GLUT_RIGHT_BUTTON illetve GLUT_DOWN / GLUT_UP
- {
- CatmullRom cr;
- cr.center = ConvertMousePoints((float)x, (float)y);
- cr.time = glutGet(GLUT_ELAPSED_TIME);
- splinepoints[index] = cr;
- index++;
- if (index >= 3)
- {
- splinepoints[index - 2].speed = CalculateCatmullSpeed(splinepoints[index - 3].center, splinepoints[index - 2].center, splinepoints[index - 1].center, splinepoints[index - 3].time, splinepoints[index - 2].time, splinepoints[index - 1].time);
- splinepoints[0].speed = CalculateCatmullSpeed(splinepoints[0].center, splinepoints[1].center, splinepoints[2].center, splinepoints[0].time, splinepoints[1].time, splinepoints[2].time);
- splinepoints[index - 1].speed = CalculateCatmullSpeed(splinepoints[index - 2].center, splinepoints[index - 1].center, splinepoints[0].center, splinepoints[index - 2].time, splinepoints[index - 1].time, splinepoints[0].time);
- }
- glutPostRedisplay();
- }
- }
- // Eger mozgast lekezelo fuggveny
- void onMouseMotion(int x, int y)
- {
- }
- // `Idle' esemenykezelo, jelzi, hogy az ido telik, az Idle esemenyek frekvenciajara csak a 0 a garantalt minimalis ertek
- void onIdle() {
- int current_time = glutGet(GLUT_ELAPSED_TIME);
- int diff = current_time - last_time;
- if (bouncestart)
- {
- if (diff >= 1000)
- {
- diff = diff / 1000;
- //if (camera.center.x <= -250 || camera.center.x >= 250 )
- if (camera.center.x <= -500 || camera.center.x >= 500)
- {
- camera.speed.x = -camera.speed.x;
- }
- //if (camera.center.y <= -250 || camera.center.y >= 250 )
- if (camera.center.y <= -500 || camera.center.y >= 500)
- {
- camera.speed.y = -camera.speed.y;
- }
- camera.center = camera.center + (camera.speed * diff);
- last_time = current_time;
- glMatrixMode(GL_MODELVIEW); // A MODELVIEW transzformaciot egysegmatrixra inicializaljuk
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION); // A PROJECTION transzformaciot egysegmatrixra inicializaljuk
- glLoadIdentity();
- gluOrtho2D(0, 1000, 1000, 0);
- //glTranslatef(camera.center.x + 250, camera.center.y + 250, 0);
- //glScalef(0.5, 0.5, 0);
- glTranslatef(camera.center.x - 500, camera.center.y - 500, 0);
- glScalef(2, 2, 0);
- cout << camera.center.x << camera.center.y << "\n";
- glutPostRedisplay();
- }
- }
- }
- // ...Idaig modosithatod
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // A C++ program belepesi pontja, a main fuggvenyt mar nem szabad bantani
- int main(int argc, char **argv) {
- glutInit(&argc, argv); // GLUT inicializalasa
- glutInitWindowSize(600, 600); // Alkalmazas ablak kezdeti merete 600x600 pixel
- glutInitWindowPosition(100, 100); // Az elozo alkalmazas ablakhoz kepest hol tunik fel
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // 8 bites R,G,B,A + dupla buffer + melyseg buffer
- glutCreateWindow("Grafika hazi feladat"); // Alkalmazas ablak megszuletik es megjelenik a kepernyon
- glMatrixMode(GL_MODELVIEW); // A MODELVIEW transzformaciot egysegmatrixra inicializaljuk
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION); // A PROJECTION transzformaciot egysegmatrixra inicializaljuk
- glLoadIdentity();
- onInitialization(); // Az altalad irt inicializalast lefuttatjuk
- glutDisplayFunc(onDisplay); // Esemenykezelok regisztralasa
- glutMouseFunc(onMouse);
- glutIdleFunc(onIdle);
- glutKeyboardFunc(onKeyboard);
- glutKeyboardUpFunc(onKeyboardUp);
- glutMotionFunc(onMouseMotion);
- glutMainLoop(); // Esemenykezelo hurok
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement