PDA

View Full Version : Drawing a bezier spline



khaled1990khaled
10-10-2015, 05:28 PM
This is my code which when I run it, it shows this image

2147

#include "stdafx.h"
#include <stdlib.h>
#include <gl/glut.h>

// 4 control points for our cubic bezier curve
float Points[4][3] = {
{ 10,10,0 },
{ 5,10,2 },
{ -5,0,0 },
{-10,5,-2}
};

// the level of detail of the curve
unsigned int LOD=20;


void OnKeyPress(unsigned char key,int,int) {
switch(key) {

// increase the LOD
case '+':
++LOD;
break;

// decrease the LOD
case '-':
--LOD;

// have a minimum LOD value
if (LOD<3)
LOD=3;
break;
default:
break;
}

// ask glut to redraw the screen for us...
glutPostRedisplay();
}

void OnDraw() {

// clear the screen & depth buffer
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);

// clear the previous transform
glLoadIdentity();

// set the camera position
gluLookAt( 1,10,30, // eye pos
0,0,0, // aim point
0,1,0); // up direction

glColor3f(1,0,1);

// we will draw lots of little lines to make our curve
glBegin(GL_LINE_STRIP);

for(int i=0;i!=LOD;++i) {

// use the parametric time value 0 to 1
float t = (float)i/(LOD-1);

// nice to pre-calculate 1.0f-t because we will need it frequently
float it = 1.0f-t;

// calculate blending functions
float b0 = t*t*t;
float b1 = 3*t*t*it;
float b2 = 3*t*it*it;
float b3 = it*it*it;

// calculate the x,y and z of the curve point by summing
// the Control vertices weighted by their respective blending
// functions
//
float x = b0*Points[0][0] +
b1*Points[1][0] +
b2*Points[2][0] +
b3*Points[3][0] ;

float y = b0*Points[0][1] +
b1*Points[1][1] +
b2*Points[2][1] +
b3*Points[3][1] ;

float z = b0*Points[0][2] +
b1*Points[1][2] +
b2*Points[2][2] +
b3*Points[3][2] ;

// specify the point
glVertex3f( x,y,z );
}
glEnd();

// draw the Control Vertices
glColor3f(0,1,0);
glPointSize(3);
glBegin(GL_POINTS);
for(int i=0;i!=4;++i) {
glVertex3fv( Points[i] );
}
glEnd();

// draw the hull of the curve
glColor3f(0,1,1);
glBegin(GL_LINE_STRIP);
for(int i=0;i!=4;++i) {
glVertex3fv( Points[i] );
}
glEnd();

// currently we've been drawing to the back buffer, we need
// to swap the back buffer with the front one to make the image visible
glutSwapBuffers();
}


void OnInit() {
// enable depth testing
glEnable(GL_DEPTH_TEST);
}


void OnExit() {
}


void OnReshape(int w, int h)
{
if (h==0)
h=1;

// set the drawable region of the window
glViewport(0,0,w,h);

// set up the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// just use a perspective projection
gluPerspective(45,(float)w/h,0.1,100);

// go back to modelview matrix so we can move the objects about
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


int main(int argc,char** argv) {

// initialise glut
glutInit(&argc,argv);

// request a depth buffer, RGBA display mode, and we want double buffering
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGBA|GLUT_DOUB LE);

// set the initial window size
glutInitWindowSize(640,480);

// create the window
glutCreateWindow("Bezier Curve: +/- to Change Level of Detail");

// set the function to use to draw our scene
glutDisplayFunc(OnDraw);

// set the function to handle changes in screen size
glutReshapeFunc(OnReshape);

// set the function for the key presses
glutKeyboardFunc(OnKeyPress);

// run our custom initialisation
OnInit();

// set the function to be called when we exit
atexit(OnExit);

// this function runs a while loop to keep the program running.
glutMainLoop();
return 0;
}

So, how can I control the curve by the mouse (left click) instead of pressing + and - , also, how can I draw the points by myself after I run the program ?

Thanks

wmelgaard
10-12-2015, 05:05 PM
If all that you want is an increment and decrement, then draw a pair of boxes (somewhere) with a "+" in one box and a "_" in the other box.
In main, create a glutMouseFunc(mouse);
within mouse(int btn, int state, int x, int y){
If x & y are within the "+" box, you increment. If x & Y are within the "_" box, you decrement.

Alternately, create a glutMunuFunc( https://www.opengl.org/resources/libraries/glut/spec3/node36.html#SECTION00071000000000000000
with one menu choice to increment, and another to decrement.