c语言OpenGL绘制静态图形,c&OpenGL实现计算机图形学:双步绘制直线法绘制菱形

#include

#include

#include "glut.h"

int x,y;//用这两个全局变量传递当前窗口的大小。

void init(void)

{

glClearColor(1.0,1.0,1.0,0.0);

glMatrixMode(GL_PROJECTION);

gluOrtho2D(-150.0,150.0,-150.0,150.0);

}

int sign(int x)

{

if(x == 0)

{

return 0;

}

else

{

if(x>0)

return 1;

else

return -1;

}

}

void Doublestep(GLint x0,GLint y0,GLint x1,GLint y1)

{

x0*=y/300;y0*=y/300;x1*=y/300;y1*=y/300;//此行代码用于保持坐标与窗口相对位置不变

GLint d,dx,dy,current_x = x0,current_y = y0,cond;//声明有用的临时变量

dx = abs(x1-x0);         dy = abs(y1-y0);

int s_x = sign(x1-x0);   int  s_y = sign(y1-y0);

cond = 2*dx;

d = 4*dy-dx;

bool is_changed;//标明,象限改变了,dx和dy互相颠倒过了。是改变了的。

if(dy>dx)

{//此时与标准情况正好相反,如果本来x坐标增加,现在变为y坐标增加

int temp = dx;

dx = dy;

dy = temp;

is_changed = true;

}

else

is_changed = false;

for(int i = 1;i<=dx;i+=2)

{

if(d<0)

{  //绘制水平两点,即第一种情况,k<1/4。

glVertex2i(current_x,current_y);//1

if(is_changed)current_y+=s_y;//2

else

current_x+=s_x;

glVertex2i(current_x,current_y);

if(is_changed)current_y+=s_y;//3

else

current_x+=s_x;

}

else

{

if(d

{   //绘制第二种情况,k=>1/4 && k<2/4

glVertex2i(current_x,current_y);//1

if(is_changed)current_y+=s_y;//2

else

current_x+=s_x;

glVertex2i(current_x,current_y);

current_x+=s_x;current_y+=s_y;//3

}

else if(d<2*cond)

{  //绘制三种情况,k<=2/4 && k<3/4

glVertex2i(current_x,current_y);//1

current_x+=s_x;current_y+=s_y;//2

glVertex2i(current_x,current_y);

if(is_changed)current_y+=s_y;//3

else

current_x+=s_x;

}

else

{  //绘制第四种情况,k>=3/4

glVertex2i(current_x,current_y);//1

current_x+=s_x;current_y+=s_y;//2

glVertex2i(current_x,current_y);

current_x+=s_x;current_y+=s_y;//3

d-=2*dx;//情况4单独减2dx

}

d-=2*dx;//2,3,4情况统一减2dx

}

d+=4*dy;//所有情况统一加4dy;

}

}

void displayfunc(void)

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0,1.0,0.0);

//坐标轴。

glBegin(GL_LINES);

glVertex2i(-x/2,0);

glVertex2i(x/2-1,0);

glEnd();

glBegin(GL_LINES);

glVertex2i(0,-y/2);

glVertex2i(0,y/2);

glEnd();

glColor3f(1.0,0.0,0.0);

glBegin(GL_POINTS);

//菱形的4条边************//*

Doublestep(-100,0,0,-150);//*

Doublestep(100,0,0,-150);//*

Doublestep(100,0,0,150);//*

Doublestep(-100,0,0,150);//*

//***********************//*

glEnd();

glFlush();

}

void myReshapeFunc(GLsizei w,GLsizei h)

{

x = w;y = h;

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

//令坐标系统和窗口大小变化对应,即坐标始终和窗口大小保持1:1,

//否则窗口放大后所画直线将变成虚线,即画点的坐标偏离预期坐标。

gluOrtho2D(-w/2.0+0.5,w/2.0+0.5,-h/2.0+0.5,h/2.0+0.5);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity ();

glClear(GL_COLOR_BUFFER_BIT);

}

int main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowPosition(300,300);

glutInitWindowSize(301,301);

glutCreateWindow("My DrawRhombus Program");

init();

glutReshapeFunc (myReshapeFunc);

glutDisplayFunc(displayfunc);

glutMainLoop();

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值