logo资料库

计算机图形学直线的转换生成实验报告含源代码实验结果.doc

第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
资料共5页,全文预览结束
实验一:直线的转换生成 一丶实验目的 理解基本图形元素光栅化的基本原理,掌握一种基本图形元素光栅化 算法,利用 Opengl 实现直线光栅化的 DDA 算法。 二丶实验内容 1. 利用 DDA 的算法原理,编程实现对直线的扫描抓换。 2. 加强对 DDA 算法的理解和掌握。 三丶实验原理 DDA 算法其实就是利用直线方程来生成直线的算法,给定起点(x0,y0)和终点 (xEnd,yEnd),这条直线就唯一确定了,它的斜率是 k=(yEnd-y0)/(xEnd-x0)。 对于 x 方向我们取增量为 1,那么下一个 x 值,即 xi+1=xi+1,这样一来,y 方 向的增量就是斜率 k,那么 yi+1=yi+k。利用这两个加粗的方程,我们就可以遍 历这条直线,每到一个地方就把这里的像素点填充上颜色,一条直线就绘制好了 四丶实验代码 ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include /* initialization: */ void myinit(void) { /* attributes */ glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /* draw in red */ /* set up viewing: */ /* 500 x 500 window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); glMatrixMode(GL_MODELVIEW); } void dda_line(int xa, int ya, int xb, int yb) { GLfloat delta_x, delta_y, x, y; int dx, dy, steps; dx = xb - xa; dy = yb - ya; if (abs(dx)>abs(dy)) steps = abs(dx); else steps = abs(dy); delta_x = (GLfloat)dx / (GLfloat)steps; delta_y = (GLfloat)dy / (GLfloat)steps; x = xa; y = ya;
glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); glVertex3f(x, y, 0); for (int k = 1; k <= steps; k++) x += delta_x; y += delta_y; glBegin(GL_POINTS); glVertex3f(x, y, 0); glEnd(); { } } /* the display callback: */ void display(void) { glClear(GL_COLOR_BUFFER_BIT); /*clear the window */ /*----------------------------------------*/ /* viewport stuff */ /*----------------------------------------*/ /* set up a viewport in the screen window */ /* args to glViewport are left, bottom, width, height */
glViewport(0, 0, 500, 500); /* NB: default viewport has same coords as in myinit, */ /* so this could be omitted: */ dda_line(200, 200, 2000, 2000); /* and flush that buffer to the screen */ glFlush(); } int main(int argc, char** argv) { /* Standard GLUT initialization */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); /* default, not needed */ glutInitWindowSize(500, 500); /* 500 x 500 pixel window */ glutInitWindowPosition(0, 0); /* place window top left on display */ glutCreateWindow("Digital Differential Analyser Line"); /* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */
myinit(); /* set attributes */ glutMainLoop(); /* enter event loop */ } 四丶实验结果
分享到:
收藏