问题描述
这段代码几乎可以工作,但问题是它留下了最后一个控制点,就像它不存在一样。我找不到错误,我不知道为什么,我检查了很多次转换矩阵和转换后的控制点,但没有
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <math.h>
// Begin globals.
static int numVal = 0;
// Final control points.
static float controlPoints[4][3] =
{
{-30.0,0.0,0.0},{ -10.0,20.0,{10.0,-20.0,{30.0,0.0}
};
//Matrix multiplication Mb^-1 * Mi
static float conversion[4][4] =
{
{1.0,{-0.8333,3.0,-1.5,0.3333},{0.3333,-0.8333},{0.0,1.0}
};
//End globals
//Control point conversion for evaluator
void convertcp(float ctrlpoints[4][3]){
int i,j,k;
float tot = 0.0;
for(i = 0; i < 4;i++){
for(j = 0;j < 4;j++){
ctrlpoints[i][j] = 0;
for(k = 0; k < 3;k++){
ctrlpoints[i][j] += conversion[i][k] * controlPoints[k][j];
}
}
}
}
// End globals.
// Initialization routine.
void setup(void){glClearColor(1.0,1.0,0.0);}
// Drawing routine.
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0,0.0);
int indx;
float ctrlpoints[4][3];
convertcp(ctrlpoints);
// Draw the control polygon in light gray.
glColor3f(0.7,0.7,0.7);
glBegin(GL_LINE_STRIP);
for (indx = 0; indx < 4; indx++)
glVertex3fv(controlPoints[indx]);
glEnd();
// Specify and enable the Bezier curve.
glMap1f(GL_MAP1_VERTEX_3,3,4,&ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
// Draw the Bezier curve by defining a sample grid and evaluating on it.
glColor3f(0.0,0.0);
glMapGrid1f(100,1.0);
glEvalMesh1(GL_LINE,100);
// Draw the Bezier control points as dots.
glPointSize(5.0);
glColor3f(0.0,0.0);
glBegin(GL_POINTS);
for (indx = 0; indx < 4; indx++)
glVertex3fv(controlPoints[indx]);
glEnd();
// Highlight selected control point,glColor3f(1.0,0.0);
glBegin(GL_POINTS);
glVertex3fv(controlPoints[numVal]);
glEnd();
glutSwapBuffers();
glutPostRedisplay();
}
// OpenGL window reshape routine.
void resize(int w,int h)
{
glViewport(0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0,50.0,-50.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Keyboard input processing routine.
void keyInput(unsigned char key,int x,int y)
{
switch (key)
{
case 27:
exit(0);
break;
case ' ':
if (numVal < 3) numVaL++;
else numVal = 0;
glutPostRedisplay();
break;
default:
break;
}
}
// Callback routine for non-ASCII key entry.
void specialKeyInput(int key,int y)
{
if(key == gluT_KEY_UP) controlPoints[numVal][2] += 1.0;
if(key == gluT_KEY_DOWN) controlPoints[numVal][2] -= 1.0;
if(key == gluT_KEY_LEFT) controlPoints[numVal][0] -= 1.0;
if(key == gluT_KEY_RIGHT) controlPoints[numVal][0] += 1.0;
glutPostRedisplay();
}
// Routine to output interaction instructions to the console window.
void printInteraction(void)
{
printf("Interaction:\n");
printf("Press space to select a control point.\n");
printf("Press the arrow keys to move the selected control point.\n");
}
// Main routine.
int main(int argc,char **argv)
{
glutinit(&argc,argv);
glutinitContextVersion(4,3);
glutinitContextProfile(gluT_COMPATIBILITY_PROFILE);
glutinitdisplayMode(gluT_DOUBLE | gluT_RGBA);
glutinitwindowSize(500,500);
glutinitwindowPosition(100,100);
glutCreateWindow("Interpolating Curves");
glutdisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
glutSpecialFunc(specialKeyInput);
glewExperimental = GL_TRUE;
glewInit();
setup();
glutMainLoop();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)