Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mypanelopengl.h"
- #include <cmath>
- #include <GL/glu.h>
- #include <iostream>
- #include <vector>
- #define PI 3.14159265
- using namespace std;
- MyPanelOpenGL::MyPanelOpenGL(QWidget *parent) :
- QGLWidget(parent)
- {
- //set number of iterations
- chaikin_iters = 0;
- //initializing the points
- vector<double> A (2);
- A[0] = 2;
- A[1] = 2;
- vector<double> B (2);
- B[0] = 8;
- B[1] = 10;
- vector<double> C (2);
- C[0] = 10;
- C[1] = 7;
- vector<double> D (2);
- D[0] = 5;
- D[1] = 6;
- chaikin_points_initial.push_back(A);
- chaikin_points_initial.push_back(B);
- chaikin_points_initial.push_back(C);
- chaikin_points_initial.push_back(D);
- chaikin_points_generated = generateChaikinPoints(chaikin_iters, chaikin_points_initial);
- }
- void MyPanelOpenGL::initializeGL(){
- glShadeModel(GL_SMOOTH);
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- }
- void MyPanelOpenGL::resizeGL(int width, int height){
- double xMin = 0, xMax = 10, yMin = 0, yMax = 10;
- glViewport(0, 0, (GLint)width, (GLint)height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(-1,1,-1,1);
- if (width > height){
- height = height?height:1;
- double newWidth = (xMax - xMin) * width / height;
- double difWidth = newWidth - (xMax - xMin);
- xMin = 0.0 + difWidth / 2.0;
- xMax = 10 + difWidth / 2.0;
- } else {
- width = width?width:1;
- double newHeight = (yMax - yMin) * width / height;
- double difHeight = newHeight - (yMax - yMin);
- yMin = 0.0 + difHeight / 2.0;
- yMax = 10 + difHeight / 2.0;
- }
- gluOrtho2D(xMin, xMax, yMin, yMax);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void MyPanelOpenGL::paintGL(){
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- //draw control points
- glPointSize(3);
- glColor3f(1,0,0);
- glBegin(GL_POINTS);
- for (int i=0; i < chaikin_points_initial.size(); i++)
- {
- double x = chaikin_points_initial[i][0];
- double y = chaikin_points_initial[i][1];
- glVertex2f((GLfloat)x, (GLfloat)y);
- }
- glEnd();
- glLineWidth(1);
- glColor3f(0,0,0);
- glBegin(GL_LINE_LOOP);
- for (int i=0; i < chaikin_points_generated.size(); i++)
- {
- double x = chaikin_points_generated[i][0];
- double y = chaikin_points_generated[i][1];
- glVertex2f((GLfloat)x, (GLfloat)y);
- }
- glEnd();
- }
- vector<vector<double> > MyPanelOpenGL::findChaikinPoints(vector<vector<double> > initial_points)
- {
- vector<vector<double> > result_points; // we will add all the vectors we generate to this vector.
- for (int i = 0; i < initial_points.size(); i++){// we iterate over all of the sequential pairs of points
- //initialize the pair of points
- vector<double> start_p = initial_points[i];
- vector<double> end_p = initial_points[((i + 1) % initial_points.size())];
- //find 1/4 point and 3/4 point of the segment made by these points
- vector<double> one_quarter_mark (2);
- vector<double> three_quarter_mark (2);
- one_quarter_mark[0] = start_p[0] + (end_p[0] - start_p[0])/4;
- one_quarter_mark[1] = start_p[1] + (end_p[1] - start_p[1])/4;
- three_quarter_mark[0] = start_p[0] + 3*(end_p[0] - start_p[0])/4;
- three_quarter_mark[1] = start_p[1] + 3*(end_p[1] - start_p[1])/4;
- //add these 1/4 and 3/4 points to our results vector
- result_points.push_back(one_quarter_mark);
- result_points.push_back(three_quarter_mark);
- }
- return result_points;
- }
- vector<vector<double> > MyPanelOpenGL::generateChaikinPoints(int num_iters, vector<vector<double> > points)
- {
- for (int i = 0; i<num_iters; i++) points = findChaikinPoints(points);
- return points;
- }
Advertisement
Add Comment
Please, Sign In to add comment