📄 support4.cpp
字号:
////////////////////////////////////////////////////////////////////////
//
// Support code for assignment 4 (cs411 f07)
//
////////////////////////////////////////////////////////////////////////
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#define ABS(x) ((x)<0 ? -(x) : (x))
typedef struct{
float x,y;
} Point;
int vxMax,vyMax; // current viewport
float wxMin,wxMax,wyMin,wyMax; // current window
Point pt[100]; // global point array
int ptNum = 0; // current number of points
bool visible=true;
int activePt = -1;
int findMatchingPointIndex(float x, float y)
{
//
// Find a point in the list
//
int i;
for(i=0;i<ptNum;i++)
if((ABS(pt[i].x - x) < 1) && (ABS(pt[i].y - y) < 1)) break;
if (i<ptNum) return i;
else return -1;
}
void transformGlSelection(int x, int y, float *xv, float *yv)
{
//
// perform the inverse of the window-to-viewport transform
//
*xv=((float)(x-0) /vxMax)*(wxMax-wxMin)+wxMin;
*yv=((float)(vyMax-y)/vyMax)*(wyMax-wyMin)+wyMin;
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable (GL_POINT_SMOOTH);
glPointSize(8.0);
glLineWidth(2.0);
}
void reshape (int w, int h)
{
//
// Map [-50..50][-50..50] to the entire viewport (aspect ratio may change)
//
glViewport(0, 0, vxMax = (GLsizei) w, vyMax = (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(wxMin = -50.0, wxMax = 50.0, wyMin = -50.0, wyMax = 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
}
}
void mouse(int button, int state, int x, int y)
{
float xv,yv;
int i;
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
transformGlSelection(x,y,&xv,&yv);
i=findMatchingPointIndex(xv, yv);
if(i < 0){
pt[ptNum].x=xv;
pt[ptNum].y=yv;
activePt = ptNum;
ptNum++;
}
else activePt = i;
}
else if (state == GLUT_UP){
activePt = -1;
}
break;
case GLUT_RIGHT_BUTTON:
if(state == GLUT_DOWN){
if(visible)
{
visible = false;
}
else
{
visible = true;
}
}
break;
default:
break;
}
glutPostRedisplay();
}
void motion(int x, int y)
{
float xv,yv;
if(activePt < 0) return;
transformGlSelection(x,y,&xv,&yv);
pt[activePt].x=xv;
pt[activePt].y=yv;
glutPostRedisplay();
}
float B0(float u)
{
float B0;
if(u>=0 && u<1)
B0=0.5*u*u;
else if(u>=1 && u<2)
B0=0.5*u*(2-u)+0.5*(u-1)*(3-u);
else if(u>=2 && u<3)
B0=0.5*(3-u)*(3-u);
else
B0=0;
return B0;
}
float B1(float u)
{
float B1;
if(u>=1 && u<2)
B1=0.5*(u-1)*(u-1);
else if(u>=2 && u<3)
B1=0.5*(u-1)*(3-u)+0.5*(u-2)*(4-u);
else if(u>=3 && u<4)
B1=0.5*(4-u)*(4-u);
else
B1=0;
return B1;
}
float B2(float u)
{
float B2;
if(u>=2 && u<3)
B2=0.5*(u-2)*(u-2);
else if(u>=3 && u<4)
B2=0.5*(u-2)*(4-u)+0.5*(u-3)*(5-u);
else if(u>=4 && u<5)
B2=0.5*(5-u)*(5-u);
else
B2=0;
return B2;
}
float B3(float u)
{
float B3;
if(u>=3 && u<4)
B3=0.5*(u-3)*(u-3);
else if(u>=4 && u<5)
B3=0.5*(u-3)*(5-u)+0.5*(u-4)*(6-u);
else if(u>=5 && u<6)
B3=0.5*(6-u)*(6-u);
else
B3=0;
return B3;
}
void display(void)
{
int i,j;
float u;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POINTS);
for(i=0;i<ptNum;i++) glVertex2f(pt[i].x,pt[i].y);
if(activePt>=0){
glColor3f(1.0,0.0,0.0);
glVertex2f(pt[activePt].x,pt[activePt].y);
glColor3f(1.0,1.0,1.0);
}
glEnd();
//
// YOUR CODE FOR GENERATING THE CURVE HERE
//
if(visible)
{
glBegin(GL_POINTS);
Point curPt;
for(j=0;j+3<ptNum;j++){
for(u=2;u<=4;){
curPt.x = pt[j].x*B0(u)+pt[j+1].x*B1(u)+pt[j+2].x*B2(u)+pt[j+3].x*B3(u);
curPt.y = pt[j].y*B0(u)+pt[j+1].y*B1(u)+pt[j+2].y*B2(u)+pt[j+3].y*B3(u);
glColor3f(0.0,0.0,1.0);
glVertex2f(curPt.x,curPt.y);
glColor3f(1.0,1.0,1.0);
u+=0.005;
}
}
glEnd();
}
// THIS IS AN EXAMPLE OF HOW TO ACCESS THE POINTS
//
for(i=0;i<ptNum;i++) printf("(%4.1f,%4.1f) ",pt[i].x,pt[i].y);
printf("\n-----------------------------\n");
glFlush ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -