
大型工程 都会划分不同的功能模块。
所以 分各个功能模块
先了解大致的各个模块的功能,不考虑细节。
然后 了解整个程序的数据流
因为C是手困面向过程的,不考虑细节的实现, 仅了解调用的流程并不难。
这部分,可以从主函数告数开始一步步下去
也可以从最终实现功能,一步步跟踪打印或者实际效果。
也就是自顶向下,以及自底向上两种。过于复杂的 还可以两头凑。
大致功能了解了, 再逐个模块了毕毕念解各个模块的实现,就要简单多了。
这碧态个一个VC中画圆的代码(可能你还需要配置编译环境):#include <GL/glut.h> /喊宏/ 设置头文件
#include <stdlib.h>
#include <math.h>
/*void setPixel ( int x, int y)
{
glBegin ( GL_POINTS)
glVertex2i (x, y)
glEnd ()
}
inline int round (const float a ) { return int (a + 0.5)}
void lineDDA (int x0, int y0, int xEnd, int yEnd)
{
int dx = xEnd - x0, dy = yEnd - y0, steps, k
float xIncrement, yIncrement, x = x0, y = y0
if (fabs (dx) >fabs (dy))
steps = fabs (dx)
else
steps = fabs (dy)
xIncrement = float (dx) / float (steps)
yIncrement = float (dy) /郑慧册 float (steps)
setPixel (round (x), round (y))
for (k = 0k <stepsk++) {
x += xIncrement
y += yIncrement
setPixel (round (x), round (y))
}
} */
class screenPt
{
private:
GLint x,y
public:
screenPt()
{
x=y=0
}
void setCoords (GLint xCoordValue,GLint yCoordValue)
{
x=xCoordValue
y=yCoordValue
}
GLint getx() const{
return x
}
GLint gety() const{
return y
}
void incrementx()
{
x++
}
void decrementy()
{
y--
}
}
void setPixel(GLint xCoord,GLint yCoord)
{
glBegin (GL_POINTS)
glVertex2i(xCoord,yCoord)
glEnd()
}
void circleMidpoint(GLint xc,GLint yc,GLint radius)
{
screenPt circPt
GLint p=1-radius //决策参数
circPt.setCoords(0,radius)
void circlePlotPoints (GLint ,GLint,screenPt)
circlePlotPoints(xc,yc,circPt)
while(circPt.getx()<circPt.gety())
{
circPt.incrementx()
if(p<0)
p += 2*circPt.getx() + 1
else
{
circPt.decrementy()
p += 2*(circPt.getx() - circPt.gety()) + 1
}
circlePlotPoints(xc,yc,circPt)
}
}
void circlePlotPoints(GLint xc,GLint yc,screenPt circPt)
{
setPixel (xc + circPt.getx(),yc + circPt.gety())
setPixel (xc - circPt.getx(),yc + circPt.gety())
setPixel (xc + circPt.getx(),yc - circPt.gety())
setPixel (xc - circPt.getx(),yc - circPt.gety())
setPixel (xc + circPt.gety(),yc + circPt.getx())
setPixel (xc - circPt.gety(),yc + circPt.getx())
setPixel (xc + circPt.gety(),yc - circPt.getx())
setPixel (xc - circPt.gety(),yc - circPt.getx())
}
void init (void) // 初始化函数
{
glClearColor (1.0, 1.0, 1.0, 0.0)// 设置清屏的颜色(Red,green,blue),但不能让该颜色在显示窗上
// 出现
glMatrixMode (GL_PROJECTION) // 设置投影矩阵
glLoadIdentity ()// 单位化上述投影矩阵
gluOrtho2D (0.0, 800.0, 0.0, 600.0) // 设置具体投影 矩阵为平面正交投影 4/3
}
void lineSegment (void) //绘图函数
{
glClear (GL_COLOR_BUFFER_BIT)// 按glClearColor (1.0, 1.0, 1.0, 0.0)指定的颜色刷新颜色
glColor3f (1.0, 0.0, 0.0)//设置前景色,即画图的颜色
circleMidpoint(400,300,200)
glFlush ( ) // 在每一画面或场景的结尾处调用,强制前面发出的OpenGL 命令开始执行
}
void main (int argc, char** argv) // OpenGL 绘图主函数
{
glutInit (&argc, argv) // 初始化GLUT 库
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) // 设置 显示模式
glutInitWindowPosition (50, 100) // 窗口左上角像素坐标
glutInitWindowSize (800, 600)// 设置设备显示窗口大小 4/3
glutCreateWindow ("An Example OpenGL Program")//创建一个窗口,参数为窗口的标题
init ( ) // 函数调用.
glutDisplayFunc (lineSegment)// 绘制当前窗口
glutMainLoop ( )//通常用于程序的结尾,表示开始运行程序.显示出所有创建的窗口
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)