Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cmath>
- #include <OpenGL/gl.h>
- #include <GLUT/GLUT.h>
- #define CX_HEIGHT 5
- #define CX_WIDTH 5 // tamanho da caixa de clique
- using namespace std;
- const int WINDOW_W = 500;
- const int WINDOW_H = 500;
- void display(); // funcao de impressao na tela
- void handleKeypress(unsigned char key, int x, int y);
- void handleMouseClick(int button, int state, int x, int y);
- void reshape(int w, int h);
- void drag(int x, int y);
- // funcoes auxiliares
- unsigned long long factorial(int n);
- unsigned long long comb(int n, int p);
- bool are_same(double a, double b);
- int vertices = 0;
- int a_vertices[30][3] = {0}; // array que ira conter os vertices
- int indice = -1; // indice do vetor de vertices [caso vertice seja encontrado]
- int main(int argc, char ** argv)
- {
- //Inicalizate GLUT and OPENGL
- glutInit(&argc, argv);
- glutInitWindowPosition(0, 0);
- glutInitWindowSize(WINDOW_W, WINDOW_H);
- glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
- // Create Window
- glutCreateWindow("Curva de Bezier");
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- //Setup GLUT callback Functions
- glutDisplayFunc(display);
- glutKeyboardFunc(handleKeypress);
- glutMouseFunc(handleMouseClick);
- glutMotionFunc(drag);
- glutReshapeFunc(reshape);
- //Enter main loop
- glutMainLoop();
- return 0;
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- // glMap1f(GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, vertices, &a_vertices[0][0]);
- // glEnable(GL_MAP1_VERTEX_3);
- //
- // glBegin(GL_LINE_STRIP);
- // glColor3f(1.0f, 1.0f, 1.0f);
- // for (int i = 0; i <= 30; i++)
- // {
- // glEvalCoord1f((GLfloat) i/30);
- // }
- // glEnd();
- glBegin(GL_LINE_STRIP);
- glColor3f(1.0f, 1.0f, 1.0f);
- double x, y;
- if (vertices > 1)
- {
- double t = 0.0;
- for (t = 0; t <= 1.0; t += 0.01)
- {
- x = y = 0.0;
- for (int i = 0; i < vertices; i++)
- {
- x += comb(vertices-1, i) * pow((1 - t), vertices-1-i) * pow(t, i) * a_vertices[i][0];
- y += comb(vertices-1, i) * pow((1 - t), vertices-1-i) * pow(t, i) * a_vertices[i][1];
- }
- glVertex3d(x, y, 0);
- //cout << "Ponto2 (" << x << ", " << y << ")" << endl;
- //cout << "t: " << t << endl;
- }
- glVertex3d(a_vertices[vertices - 1][0], a_vertices[vertices - 1][1], 0);
- }
- else
- {
- x = (double)a_vertices[0][1];
- y = (double)a_vertices[0][1];
- glVertex3d(x, y, 0);
- }
- glEnd();
- // imprime os pontos de controle
- glPointSize(5.0f);
- glBegin(GL_POINTS);
- glColor3f(1.0f, 1.0f, 0.0f);
- for (int i = 0; i < vertices; i++)
- {
- glVertex3d(a_vertices[i][0], a_vertices[i][1], 0);
- }
- glEnd();
- // imprime retas base
- glBegin(GL_LINE_STRIP);
- glColor3f(1.0f, 1.0f, 1.0f);
- for (int i = 0; i < vertices; i++)
- {
- glVertex3d(a_vertices[i][0], a_vertices[i][1], 0);
- }
- glEnd();
- glFlush();
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0f, WINDOW_W, WINDOW_H, 0.0f, -5.0, 5.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void handleKeypress(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27:
- exit(0); // fecha o programa quando aperta ESC
- }
- }
- void handleMouseClick(int button, int state, int x, int y)
- {
- if (button == GLUT_LEFT_BUTTON)
- {
- bool achou = false;
- if (state == GLUT_DOWN)
- {
- for (int i = 0; i < vertices; i++)
- {
- if ((a_vertices[i][0] <= x + CX_WIDTH && a_vertices[i][0] >= x - CX_WIDTH) &&
- ((a_vertices[i][1] <= y + CX_HEIGHT) && a_vertices[i][1] >= y - CX_HEIGHT))
- {
- achou = true;
- indice = i;
- }
- }
- if (!achou)
- {
- // coloca ponto na tela
- a_vertices[vertices][0] = (float)x;
- a_vertices[vertices][1] = (float)y;
- a_vertices[vertices][2] = 0;
- vertices++;
- glutPostRedisplay(); // avisa que a janela atual deve ser reimpressa
- }
- }
- }
- }
- void drag(int x, int y)
- {
- a_vertices[indice][0] = x;
- a_vertices[indice][1] = y;
- glutPostRedisplay();
- }
- // funcoes auxiliares
- bool are_same(double a, double b)
- {
- return (fabs(a - b) < 0.00001);
- }
- unsigned long long factorial(int n)
- {
- long long fat;
- if (n <= 1)
- {
- fat = 1;
- }
- else
- {
- fat = n;
- while (n > 1)
- {
- fat *= (n - 1);
- n--;
- }
- }
- return fat;
- }
- unsigned long long comb(int n, int p)
- {
- unsigned long long ret = factorial(n)/(factorial(p) * factorial(n - p));
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement