您的位置:首页 > 编程语言

三种方法绘制矩形 源代码

2013-10-21 13:39 225 查看
#include<windows.h>

#include <gl/glut.h>

struct GLPoint{

GLint x, y;

};

const GLint screenWidth = 320;

const GLint screenHeight = 320;

const GLint xPosition = 100;

const GLint yPosition = 100;

void myDisplay(void);

void myInit(void);

void drawRectangleCenter(GLPoint pt,GLint widht,GLint height);

void drawRectangleCornersize(GLPoint pt,GLint width, GLdouble proportion);

int main(int argc, char ** argv){

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(screenWidth,screenHeight);

glutInitWindowPosition(xPosition,yPosition);

glutCreateWindow("ChessBoard");

glutDisplayFunc(myDisplay);

myInit();

glutMainLoop();

return 0;

}

void drawRectangleCenter(GLPoint centerPoint,GLint width,GLint height){

glRecti(centerPoint.x-width/2,centerPoint.y-height/2,centerPoint.x+width/2,centerPoint.y+height/2);

}

void drawRectangleCornersize(GLPoint leftTopPoint,GLint width, GLdouble proportion){

//10, 290 300 3.75

//proportion = width / height

GLint x1,y1,x2,y2;

x1 = leftTopPoint.x; //10

y1 = leftTopPoint.y - (GLdouble)width/proportion; //210

x2 = leftTopPoint.x + width;

y2 = leftTopPoint.y;

//glRectf(x1,x2,y1,y2);

glRecti(x1,y1,x2,y2);

}

void myDisplay(void){

glClear(GL_COLOR_BUFFER_BIT);

//方法一:

glColor3f(0.7f,0.7f,0.7f);

glRecti(10,10,310,90);

//方法二:中心,高,宽

glColor3f(0.2f,0.2f,0.2f);

GLPoint centerPoint= {160,160};

GLint width= 300;

GLint height = 80;

drawRectangleCenter(centerPoint,width,height);

//方法三: 左上角,宽,宽长比

glColor3f(0.1f,0.1f,0.1f);

GLPoint leftTopPoint = {10,290};

GLdouble proportion = (GLdouble)width/height;

drawRectangleCornersize(leftTopPoint,width,proportion);

glFlush();

}

void myInit(void){

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.0f,0.0f,0.0f);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: