#pragma warning(disable:4996)
#include <iostream>
#include <vector>
#include <conio.h>
#include <gl/glut.h>
#include <gl/gl.h>
#include <gl/glu.h>
using namespace std;
//#define _1D 1
#ifdef _1D
// The number of control points for this curve
GLint nNumPoints = 6;
GLfloat ctrlPoints[6][3] = {
{ -4.0f, 0.0f, 0.0f }, // End Point
{ -6.0f, 4.0f, 0.0f }, // Control Point
{ 0.0f, 4.0f, 0.0f }, // Control Point
{ 0.0f, -4.0f, 0.0f }, // Control Point
{ 6.0f, -4.0f, 0.0f }, // Control Point
{ 4.0f, 0.0f, 0.0f } // End Point
};
float range = 100.0f;
#else
// The number of control points for this curve
GLint nNumPoints = 3;
GLfloat ctrlPoints[3][3][3] = {
{
{ -4.0f, 0.0f, 4.0f },
{ -2.0f, 4.0f, 4.0f },
{ 4.0f, 0.0f, 4.0f }
},
{
{ -4.0f, 0.0f, 0.0f },
{ -2.0f, 4.0f, 0.0f },
{ 4.0f, 0.0f, 0.0f }
},
{
{ -4.0f, 0.0f, -4.0f },
{ -2.0f, 4.0f, -4.0f },
{ 4.0f, 0.0f, -4.0f }
}
};
#endif // _1D
// This function is used to superimpose the control points over the curve
void DrawPoints(void)
{
// Set point size larger to make more visible
glPointSize(5.0f);
// Loop through all control points for this example
glBegin(GL_POINTS);
#ifdef _1D
for (int i = 0; i < nNumPoints; i++)
glVertex2fv(ctrlPoints[i]);
#else
for (int i = 0; i < nNumPoints; i++)
for (int j = 0; j < 3; j++)
glVertex3fv(ctrlPoints[i][j]);
#endif // _1D
glEnd();
}
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
#ifdef _1D
// Sets up the bezier
// This actually only needs to be called once and could go in
// the setup function
glMap1f(GL_MAP1_VERTEX_3, // Type of data generated
0.0f, // Lower u range
range, // Upper u range
3, // Distance between points in the data
nNumPoints, // number of control points
&ctrlPoints[0][0]); // array of control points
// Enable the evaluator
glEnable(GL_MAP1_VERTEX_3);
#if _1D
// Use a line strip to "connect-the-dots"
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= range; i++)
{
// Evaluate the curve at this point
glEvalCoord1f((GLfloat)i);
}
glEnd();
#else
// Use higher level functions to map to a grid, then evaluate the
// entire thing.
// Put these two functions in to replace above loop
// 100개를 0부터 100까지
glMapGrid1d(100,0.0,100.0);
// Evaluate the grid, using lines
glEvalMesh1(GL_LINE,0,100);
#endif
// 단지 지정한 점을 그린다.
DrawPoints();
#else
// Save the modelview matrix stack
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Rotate the mesh around to make it easier to see
glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
glRotatef(60.0f, 1.0f, 0.0f, 0.0f);
// Sets up the Bezier
// This actually only needs to be called once and could go in
// the setup function
glMap2f(GL_MAP2_VERTEX_3, // Type of data generated
0.0f, // Lower u range
10.0f, // Upper u range
3, // Distance between points in the data
3, // Dimension in u direction (order)
0.0f, // Lover v range
10.0f, // Upper v range
9, // Distance between points in the data
3, // Dimension in v direction (order)
&ctrlPoints[0][0][0]); // array of control points
// Enable the evaluator
glEnable(GL_MAP2_VERTEX_3);
// Use higher level functions to map to a grid, then evaluate the
// entire thing.
// Map a grid of 10 points from 0 to 10
glMapGrid2f(10, 0.0f, 10.0f, 10, 0.0f, 10.0f);
// Evaluate the grid, using lines
glEvalMesh2(GL_LINE, 0, 10, 0, 10);
DrawPoints();
// Restore the modelview matrix
glPopMatrix();
#endif // _1D
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Clear Window to white
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// Draw in Blue
glColor3f(0.0f, 0.0f, 1.0f);
}
///////////////////////////////////////
// Set 2D Projection
void ChangeSize(int w, int h)
{
// Prevent a divide by zero
if (h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifdef _1D
gluOrtho2D(-10.0f, 10.0f, -10.0f, 10.0f);
#else
glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -10.0f, 10.0f);
#endif // _1D
// Modelview matrix reset
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("2D Bezier Curve");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}