📄 main.cpp
字号:
// Below, we will draw a bezier curve with 4 control points
// (including the start/end point). We will use GL_LINES to draw the
// line segments of the curve. Then, using Quadrics, we will draw a sphere
// on the curve. You can use the LEFT and RIGHT arrow keys to move the
// sphere along the curve. If you are not familiar with quadrics, you can
// view our quadric tutorial at www.GameTutorials.com. I also include some
// comments from the tutorial in here in case you just want the basics.
// Quadrics are used to create cylinders and spheres quickly and easily.
// We set up our camera back a little bit to get the whole curve in view.
// Position View Up Vector
gluLookAt(0, 0.5f, 10.0f, 0, 0.5f, 0, 0, 1, 0); // Set up our camera position and view
// Below we disable the lighting so you can clearly see the bezier curve.
glDisable(GL_LIGHTING); // Disable lighting for now
glColor3ub(0, 255, 0); // Set the color to Green
CVector3 vPoint = {0.0f, 0.0f, 0.0f}; // Initialize a CVector3 to hold points.
// Just so we can see the curve better, I rotate the curve and sphere
glRotatef(rotateY, 0.0f, 1.0f, 0.0f); // Rotate the curve around the Y-Axis
rotateY += 0.1f; // Increase the current rotation
// Here we tell OpenGL to render lines with a greater thickness (default is 1.0)
glLineWidth(1.5); // Increase the size of a line for visibility
// We haven't used GL_LINE_STRIP before so let me explain. Instead of passing
// in the first point of the line, then the next point of the line, then passing
// in that same point again for the first point of the next line, etc... we can
// do a line strip. That means we just pass in ONE point and it connects them
// for us. If we just did GL_LINES it would render the curve is broken segments.
// Strips are very usefull, as well as fast. You can do quad and triangle strips too.
glBegin(GL_LINE_STRIP); // Start drawing lines
// Here we actually go through the curve and get the points
// that make up the curve. Since our PointOnCurve() function
// Take 4 points and a time value, we use a for loop starting
// "t" at 0 (the starting point of the curve) and then increase
// "t" by constant value of time. Because time is measured from
// 0 being the beginning of the curve, and 1 being the end, we divide
// 1 by the amount of steps we want to draw the curve. Basically the
// amount steps defines the detail of the curve. If we just had 4
// steps, the curve would be created out of 4 lines. The lowest the
// the amount of steps to make the curve is 3. Otherwise it is just
// a straight line. The more steps, the more rounded the curve is.
// Go through the curve starting at 0, ending at 1 + another step.
// Since we are using line strips, we need to go past the end point by 1 step.
for(float t = 0; t <= (1 + (1.0f / MAX_STEPS)); t += 1.0f / MAX_STEPS)
{
// Here we pass in our 4 points that make up the curve to PointOnCurve().
// We also pass in "t", which is the current time from 0 to 1. If we pass
// in 0 for t, we should get the starting point of the curve, if we pass in
// 1 for t, we should get the end point of the curve. So anything in between
// 0 and 1 will be another point along the curve (IE, .5 would be a point halfway
// along the curve).
// Get the current point on the curve, depending on the time.
vPoint = PointOnCurve(g_vStartPoint, g_vControlPoint1, g_vControlPoint2, g_vEndPoint, t);
// Draw the current point at distance "t" of the curve.
glVertex3f(vPoint.x, vPoint.y, vPoint.z);
}
glEnd();
// Now that we drew the curve, we can turn lighting back on so the sphere
// has some more realistic properties (shading).
glEnable(GL_LIGHTING); // Turn lighting back on
// In order to move the sphere along the curve, we use the same function do draw
// the curve, except that we pass in the current time from 0 to 1 that the sphere
// is at. In the beginning the sphere's time is 0, so it's at the beginning of the
// curve. As we hit the RIGHT arrow key, g_CurrentTime will increase and will move
// the sphere along the curve with a certain fixed speed (BALL_SPEED). When g_CurrentTime
// get to 1, we stop. If the time gets below 0, we stop again.
// Get the current point on the curve, depending on the time.
vPoint = PointOnCurve(g_vStartPoint, g_vControlPoint1,
g_vControlPoint2, g_vEndPoint, g_CurrentTime);
glColor3ub(255, 0, 0); // Set the color to red
// Draw the sphere at the current point along of the curve. Give it a radius of 0.2f
DrawSphere(vPoint.x, vPoint.y, vPoint.z, 0.2f);
glColor3ub(255, 255, 0); // Set the color to yellow
// Now, we should display the control points so you can better visualize what they do.
// We represent them as small yellow spheres.
// Draw the first control point as a small yellow sphere
DrawSphere(g_vControlPoint1.x, g_vControlPoint1.y, g_vControlPoint1.z, 0.1f);
// Draw the second control point as a small yellow sphere
DrawSphere(g_vControlPoint2.x, g_vControlPoint2.y, g_vControlPoint2.z, 0.1f);
//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////
SwapBuffers(g_hDC); // Swap the backbuffers to the foreground
}
///////////////////////////////// WIN PROC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function handles the window messages.
/////
///////////////////////////////// WIN PROC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LONG lRet = 0;
PAINTSTRUCT ps;
switch (uMsg)
{
case WM_SIZE: // If the window is resized
if(!g_bFullScreen) // Do this only if we are NOT in full screen
{
SizeOpenGLScreen(LOWORD(lParam),HIWORD(lParam));// LoWord=Width, HiWord=Height
GetClientRect(hWnd, &g_rRect); // Get the window rectangle
}
break;
case WM_PAINT: // If we need to repaint the scene
BeginPaint(hWnd, &ps); // Init the paint struct
EndPaint(hWnd, &ps); // EndPaint, Clean up
break;
case WM_KEYDOWN:
switch(wParam) { // Check if we hit a key
case VK_ESCAPE: // If we hit the escape key
PostQuitMessage(0); // Send a QUIT message to the window
break;
//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////
// Here we check to the input of the keyboard to move the
// ball along the curve. We can use the RIGHT or LEFT arrow keys.
case VK_LEFT: // If we hit the LEFT arrow
g_CurrentTime -= BALL_SPEED; // Increase the position of the ball along the curve
if(g_CurrentTime < 0) // If we go below 0
g_CurrentTime = 0; // Set it back to 0
break;
case VK_RIGHT: // If we hit the RIGHT arrow key
g_CurrentTime += BALL_SPEED; // Decrease the position of the ball along the curve
if(g_CurrentTime > 1) // If we go over 1
g_CurrentTime = 1; // Set it back to 1
break;
//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////
}
break;
case WM_CLOSE: // If the window is being closes
PostQuitMessage(0); // Send a QUIT Message to the window
break;
default: // Return by default
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}
return lRet; // Return by default
}
/////////////////////////////////////////////////////////////////////////////////
//
// * QUICK NOTES *
//
// This tutorial creates a bezier curve and allows a sphere to move along it
// with the LEFT and RIGHT arrow keys. You will also note that we display
// 2 of the curve control points to get a better visualization of what is going on.
//
// Here is the equation we are using:
//
// B(t) = P1 * ( 1 - t )^3 + P2 * 3 * t * ( 1 - t )^2 + P3 * 3 * t^2 * ( 1 - t ) + P4 * t^3
//
// This is pretty straight forward, even though it looks complicated. If you got
// up to Trig, you will notice that this is a polynomial. That is what a curve is.
// "t" is the time from 0 to 1. You could also think of it as the distance along the
// curve, because that is really what it is. P1 - P4 are the 4 control points.
// They each have an (x, y, z) associated with them. P1 is the starting point. P2 is
// the first control point, P3 is the second control point, and P4 is the end point of the
// curve. You will hear that this is a 4 control point equation. I usually think of it
// as 2 control points because the 2 end points are just the start and end of the curve.
//
// Bezier curves are awesome. You can use them to make realistic camera movements.
// Tunnel effects most frequently use bezier curves to shape the tunnel. Once you
// understand one single bezier curve, you can then branch off and learn how to create
// curved surfaces. This will also prepare you to learn about NURBS and SPLINES.
//
// I would like to thank Paul Bourke at http://astronomy.swin.edu.au/pbourke/curves/bezier/
//
// His web site helped TREMENDOUSLY with understanding Bezier Curves and his source code.
// I suggest checking it out because it has a HUGE range of other graphics programming topics.
//
// Once again, go to http://astronomy.swin.edu.au/pbourke/curves/bezier/ to get a more clear
// understanding of the math behind Bezier Curves as well as excellent visual examples.
//
// I would suggest changing the control points around so you could get a better idea of
// how they effect the curve. The higher the control points are, the higher the arc is.
//
// Good Luck!
//
//
// Ben Humphrey (DigiBen)
// Game Programmer
// DigiBen@GameTutorials.com
// Co-Web Host of www.GameTutorials.com
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -