这是我的计算机图形学上机报告,写的比较详细,供大家参考
【首先是glut库的配置问题了,我简单提一下】
(这里是CB的配置,不同编译器的配置都差不多)
——以下是正文——
(我写的文档复制过来图片和公式全部丢失了,所以只能以截图的形式,都是原理性的东西,具体代码在后面都有附上。PS:代码是可以copy的,可以供大家学习参考~)
- 实验心得
说实话,第一次用比较底层的c语言写出图形窗口界面还是挺激动的。中间经历了许多挫折,如配置出错啊、各种bug啊,不过最终还是成功地利用OpenGL写出了中点线算法。最大的感受是一切问题都是纸老虎,都可以通过老师同学或者网络解决。当然非常开心的一点是弄清楚了直线的栅格显示问题,我觉得自己做的最重要的一步就是在报告中对中点线算法原理进行了细致的分析,即进行了很详细的数学建模,最后用代码实现了这个数学模型,并验证了该模型的正确性。
说到底,任何问题的第一步也是最难的一步就是建模,而对于理工科的学生而言,建模肯定不是用自然语言建模,一定是用详细明确的数学语言来建模,即数学建模。之后就是怎么实现了,但是代码实现说到底都是小问题,什么glut库的配置啊,语法啊,一些小bug啊。最重要的是要有建模思想和算法思想,我认为这是我本次实验最大的收获。
- 源代码
#include "windows.h"
#include <glut.h>
#include <math.h>
#include<iostream>
typedef struct {
int x,y;
}point;
void init()
{
glClearColor(1.0f,1.0f,1.0f,1.0f);
//glMatrixMode(GL_PROJECTION);
//gluOrtho2D(-250.0, 250.0, -250.0, 250.0);
}
int LineMidPoint(int x0,int y0,int x1,int y1,point pixels[])
{
using namespace std;
int num,x,y,dx,dy;
int a,b,d;
float k;
dx = x1-x0;
dy = y1-y0;
//cout<<"dy is "<<dy<<endl
// <<"dx is "<<dx<<endl;
if(dy == 0)
k=dx;
else
k = (float)dy/dx;
num = 0;
/*cout<<"k is "<<k<<endl
<<"x0 is "<<x0<<endl
<<"x1 is "<<x1<<endl
<<"y0 is "<<y0<<endl
<<"y1 is "<<y1<<endl
<<"k should be"<<dy/dx<<endl;*/
if(dx>0)
{
a=-dy;
b=dx;
}
else
{
a=dy;
b=-dx;
}
if(dx==0) //垂直线
{
for(y=y0;y<=y1;y++)
{
pixels[num].x = x0;
pixels[num].y = y;
num ++;
}
}
else if(dy==0) //水平线
{
for(x=x0;x<=x1;x++)
{
pixels[num].x = x;
pixels[num].y = y0;
num ++;
}
}
else if(k<=1&&k>0)//斜率的四种情况
{
x = x0;
y = y0;
d = 2*a+b;
pixels[num].x = x0;
pixels[num].y = y0;
num ++;
for(x=x0;x<x1;x++)
{
if(d>=0){
pixels[num].x = x+1;
pixels[num].y = y;
num ++;
d+=2*a;
}
else
{
pixels[num].x = x+1;
pixels[num].y = y+1;
num ++;
//cout<<"I'm here "<<endl;
y++;
d+=2*a+2*b;
}
}
}
else if(k>1)
{
x = x0;
y = y0;
d = a+2*b;
pixels[num].x = x0;
pixels[num].y = y0;
num ++;
for(y=y0;y<y1;y++)
{
if(d>0){
pixels[num].x = x+1;
pixels[num].y = y+1;
num ++;
x++;
d+=2*a+2*b;
}
else
{
pixels[num].x = x;
pixels[num].y = y+1;
num ++;
d+=2*b;
}
}
}
else if(k<0&&k>=-1)
{
x = x0;
y = y0;
d = 2*a-b;
pixels[num].x = x0;
pixels[num].y = y0;
num ++;
for(x=x0;x<x1;x++)
{
if(d>=0){
pixels[num].x = x+1;
pixels[num].y = y-1;
num ++;
y--;
d+=2*a-2*b;
}
else
{
pixels[num].x = x+1;
pixels[num].y = y;
num ++;
d+=2*a;
}
}
}
else
{
x = x0;
y = y0;
d = a-2*b;
pixels[num].x = x0;
pixels[num].y = y0;
num ++;
for(y=y0;y>y1;y--)
{
if(d>0){
pixels[num].x = x;
pixels[num].y = y-1;
num ++;
d-=2*b;
}
else
{
pixels[num].x = x+1;
pixels[num].y = y-1;
num ++;
x++;
d+=2*a-2*b;
}
}
}
return num;
}
void drawLine(int x1,int y1,int x2,int y2)
{
point pixels[1000];
int num;
int i;
num = LineMidPoint(x1,y1,x2,y2,pixels);
glBegin(GL_POINTS);
for(i=0;i<num;i++)
glVertex2d(pixels[i].x,pixels[i].y);
glEnd();
}
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.f);
drawLine(0,-50,0,50);//两个点的坐标
glFlush();
}
void ChangeSize(GLsizei w,GLsizei h)
{
GLfloat aspectRatio;
if(h==0)
h = 1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w/(GLfloat)h;
if(w<=h)
glOrtho(-100.0,100.0,-100.0/aspectRatio,100.0/aspectRatio,1.0,-1.0);
else
glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100.0,100.0,1.0,-1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main()
{
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutCreateWindow("My_MidPointLine");
init();
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutMainLoop();
return 0;
}