OpenGL基础(2)
依据上篇文章(计算机图形学OpenGL基础)为基础向下扩展在上篇代码的基础上我们今天加入了
一、点的代码
glColor3f(1.0f,0.0f,0.0f);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex2i(100,100);
glVertex2i(50,50);
glEnd();
二、线的代码
glColor3f(1.0f,0.0f,0.0f);
glPointSize(10.0f);
glBegin(GL_LINES);
glVertex2i(100,100);
glVertex2i(50,50);
glEnd();
三、多边形的代码
glColor3f(1.0f,0.0f,0.0f);
glPointSize(10.0f);
glBegin(GL_POLYGON);
glVertex2i(100,100);
glVertex2i(50,50);
glEnd();
如果我们把GL_POLYGON改成GL_TRIANGLES就变成了三角形的代码
下面我们根据基础代码+以上说的代码
进行一个代码模块儿的练习
要求:
窗口里面含有点、线、多边形和三角形
#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>
#include <gl/glut.h>
void SetupRC(){
glClearColor(1.0f,1.0f,1.0f,1.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,400.0,0.0,300.0);
}
void showTime(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex2i(100,100);
glVertex2i(50,50);
glEnd();
glColor3f(0.0f,1.0f,0.0f);
glPointSize(10.0f);
glBegin(GL_LINES);
glVertex2i(100,100);
glVertex2i(50,50);
glEnd();
int p1[]={100,130};
int p2[]={130,100};
int p3[]={160,100};
int p4[]={170,130};
int p5[]={160,160};
int p6[]={130,160};
glColor3f(0.5f,1.0f,0.0f);
glPointSize(10.0f);
glBegin(GL_POLYGON);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glEnd();
int p11[]={200,230};
int p12[]={230,200};
int p13[]={260,200};
int p14[]={270,230};
int p15[]={260,260};
int p16[]={230,260};
glColor3f(0.5f,0.5f,0.5f);
glPointSize(10.0f);
glBegin(GL_TRIANGLES);
glVertex2iv(p11);
glVertex2iv(p12);
glVertex2iv(p13);
glVertex2iv(p14);
glVertex2iv(p15);
glVertex2iv(p16);
glEnd();
glFlush();
}
int main(int argc,char *argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,300);
glutInitWindowPosition(100,120);
glutCreateWindow(" Ŀһ") ;
glutDisplayFunc(showTime);
SetupRC();
glutMainLoop();
}