在书中,其中的错误尤其的严重,段落内容和程序中都是不可调试的错误。下面将修改的内容实现为下面:
#include<windows.h>
#include<math.h>
#include<time.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
//using namespace std;
//////////////////////GlintPoint//////////////////////////////////////////
class GlintPoint
{
private:
float x;
float y;
public:
GlintPoint(float ax,float ay)
{
x=ax;
y=ay;
}
void setGlintPoint(float ax,float ay)
{
x=ax;
y=ay;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
///////////////////////myInit//////////////////////////////////////////////
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800.0,0.0,600.0);
}
//////////////////////myDisplay////////////////////////////////////////////////
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glEnd();
glFlush();
}
/////////////////////drawDot///////////////////////////////////////////////////
void drawDot(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
//////////////////////sierpinski_render////////////////////////////////////////
void sierpinski_render(void)
{
glClear(GL_COLOR_BUFFER_BIT);
GlintPoint T[3]={GlintPoint(10,10),GlintPoint(600,10),GlintPoint(300,600)};
int index=(rand())%3;
GlintPoint point=T[index];
drawDot(point.getX(),point.getY());
for(int i =0 ; i <=55000;i++)
{
float getx,gety;
getx=point.getX();
gety=point.getY();
index=rand()%3;
getx=(getx+T[index].getX())/2;
gety=(gety+T[index].getY())/2;
point.setGlintPoint(getx,gety);
drawDot(getx,gety);
}
glFlush();
}
/////////////////////main//////////////////////////////////////////////
void main(int argc , char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(50,150);
glutCreateWindow("my second attempt");
glutDisplayFunc(sierpinski_render);
myInit();
glutMainLoop();
}
窗口的内容和点的数量作者可以自己修改