OpenGL第四章源代码

cube1.c 观察一个立方体

#include

void display(void) //带上void参数,否则警告错误
{
glClear(GL_COLOR_BUFFER_BIT);
//glutWireCube(0.5);
glutSolidCube(0.5);
glFlush();
}

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}


int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
glutDisplayFunc(display); //可加或不加地址符号
init();
glutMainLoop();
}

--------------------------------------------------------------
将cube1.c在透视投影下显示

#include

void display(void) //带上void参数,否则警告错误
{
glClear(GL_COLOR_BUFFER_BIT);
//glutWireCube(0.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
glRotatef(30.0,1.0,1.0,1.0);
glutSolidCube(1.0);
glFlush();
}

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,1.0,1.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
glutDisplayFunc(display); //可加或不加地址符号
init();
glutMainLoop();
}

--------------------------------------------------------------
cube2.c 设定摄像机的位置

#include

void display(void) //带上void参数,否则警告错误
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);//eye(1.0,0.0,1.0)
glRotatef(45.0,1.0,1.0,1.0);//旋转与设定相机位置颠倒,结果不同//取消旋转
glutWireCube(1.5);
//glutSolidCube(1.5);
glFlush();
}

void reshape(GLsizei w,GLsizei h)
{
if(h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-4.0,4.0,-4.0*(GLfloat)h/(GLfloat)w,4.0*(GLfloat)h/(GLfloat)w,-4.0,4.0);
else
glOrtho(-4.0*(GLfloat)w/(GLfloat)h,4.0*(GLfloat)w/(GLfloat)h,-4.0,4.0,-4.0,4.0);
}

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);

}


int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("cube");
glutDisplayFunc(display); //可加或不加地址符号
glutReshapeFunc(reshape);
init();
glutMainLoop();
}

----------------------------------------------------------------------
设定摄像机位置探讨???????
#include

void display(void) //带上void参数,否

则警告错误
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);

//gluLookAt(0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0); 为何可以看到点?
//gluLookAt(0.5,0.5,0.5,0.0,0.0,0.0,0.0,1.0,0.0); (0.3,0.3,0.3)
//gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);
glColor3f(1.0,1.0,1.0);
glutSolidCube(1.0);
glColor3f(1.0,1.0,0.0);
glutWireCube(1.0);
glPointSize(10);
glBegin(GL_POINTS);
glColor3f(0.0,1.0,0.0);
glVertex3f(0.5,0.5,0.5);
glEnd();
glFlush();
}

void reshape(GLsizei w,GLsizei h)
{
if(h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-4.0,4.0,-4.0*(GLfloat)h/(GLfloat)w,4.0*(GLfloat)h/(GLfloat)w,-4.0,4.0);
else
glOrtho(-4.0*(GLfloat)w/(GLfloat)h,4.0*(GLfloat)w/(GLfloat)h,-4.0,4.0,-4.0,4.0);
}
void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);

}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("cube");
glutDisplayFunc(display); //可加或不加地址符号
//glutReshapeFunc(reshape);
init();
glutMainLoop();
}

----------------------------------------------------------------------------------
cube3.c 透视投影

#include

void display(void) //带上void参数,否则警告错误
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);

glutWireCube(0.5);
//glutSolidCube(1.5);
glFlush();
}

void reshape(GLsizei w,GLsizei h)
{
if(h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//if(w<=h)
// glOrtho(-4.0,4.0,-4.0*(GLfloat)h/(GLfloat)w,4.0*(GLfloat)h/(GLfloat)w,-4.0,4.0);
//else
// glOrtho(-4.0*(GLfloat)w/(GLfloat)h,4.0*(GLfloat)w/(GLfloat)h,-4.0,4.0,-4.0,4.0);
gluPerspective(45.0,(GLfloat)w/(GLfloat)h,0.0,10.0);

// glFrustum(-1.0,1.0,-1.0,1.0,0.0,10.0);//取消设定摄像机的位置
// glFrustum(-1.0,1.0,-1.0,1.0,1.0,2.0); //设定摄像机位置进行观察
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);

}


int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("cube");
glutDisplayFunc(display); //可加或不加地址符号
glutReshapeFunc(reshape);
init();
glutMainLoop();
}


----------------------------------------------------------------------------------------------

array1.c 顶点数组1


#include

GLfloat vertices[][3]={{-1.0,-1.0,1.0},{-1.0,1.0,1.0},{1.0,1.0,1.0},{1.0,-1.0,1.0},{-1.0,-1.0,-1.0},{-1.0,1.0,-1.0},{1.0,1.0,-1.0},{1.0,-1.0,-1.0}};
GL

float colors[][3]={{1.0,0.0,0.0},{0.0,1.0,1.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0}};

void polygon(int a,int b,int c,int d)
{
glBegin(GL_POLYGON);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[b]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
}

void cube()
{
glColor3fv(colors[0]);
polygon(0,3,2,1);

glColor3fv(colors[1]);
polygon(2,3,7,6);

glColor3fv(colors[2]);
polygon(3,0,4,7);

glColor3fv(colors[3]);
polygon(1,2,6,5);

glColor3fv(colors[4]);
polygon(4,7,6,5);

glColor3fv(colors[5]);
polygon(5,4,0,1);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
cube();
glFlush();
}

init()
{
glClearColor(0.0,0.0,0.0,0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
}

void main(int argc, char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE|GLUT_DEPTH);
glutCreateWindow("cube");
glutDisplayFunc(display);
init();
glutMainLoop();
}

-------------------------------------------------------------------
array2.c


#include

GLfloat vertices[][3]={{-1.0,-1.0,1.0},{-1.0,1.0,1.0},{1.0,1.0,1.0},{1.0,-1.0,1.0},{-1.0,-1.0,-1.0},{-1.0,1.0,-1.0},{1.0,1.0,-1.0},{1.0,-1.0,-1.0}};
GLfloat colors[][3]={{1.0,0.0,0.0},{0.0,1.0,1.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0}};

void display(void)
{
GLubyte cubeIndices[]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,7,6,5,0,1,5,4};
GLint i;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);//启用深度测试,有黑色的面。原因是数组colors下标取值6,越界
glShadeModel(GL_FLAT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
//启用数组
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

//指定数组数据
glVertexPointer(3,GL_FLOAT,0,vertices);
glColorPointer(3,GL_FLOAT,0,colors);

//渲染几何图形
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices);
//for(i=0;i<6;i++)
// glDrawElements(GL_POLYGON,4,GL_UNSIGNED_BYTE,cubeIndices+i*4);
glFlush();
}

init()
{
glClearColor(1.0,1.0,1.0,0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
}

void main(int argc, char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE|GLUT_DEPTH);
glutCreateWindow("cube");
glutDisplayFunc(display);
init();
glutMainLoop();
}

----------------------------------------------------------------------
quadric.c glu二次曲面

#include

void display_sphere(void)
{
GLUquadricObj *obj;
glClear(GL_COLOR_BUFFER_BIT);
obj=gluNewQuadric();
gluQuadricDrawStyle(obj,GLU_LINE);
gluSphere(obj,0.5,12,12);
glFlush()

;
}


void display_cylinder(void)
{
GLUquadricObj *obj;
glClear(GL_COLOR_BUFFER_BIT);

obj=gluNewQuadric();
gluQuadricDrawStyle(obj,GLU_LINE);

gluPartialDisk(obj,0.2,0.6,12,12,30.0,90.0);

glFlush();
}

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,0.0);
}


void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1,1,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}



void main(int argc,char * argv[])
{
glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("sphere");
glutDisplayFunc(display_sphere);
glutReshapeFunc(myreshape);

init();

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(320,0);
glutCreateWindow("cylinder");
glutDisplayFunc(display_cylinder);
glutReshapeFunc(myreshape);

glutMainLoop();
}
----------------------------------------------------------------
菜单控制绘制各种对象

#include
GLint ww,hh;
void display1()
{
GLUquadricObj *obj;
glClear(GL_COLOR_BUFFER_BIT);
obj=gluNewQuadric();
gluQuadricDrawStyle(obj,GLU_LINE);
gluSphere(obj,0.5,50,50);
glFlush();
}

void display2()
{
GLUquadricObj *obj;
glClear(GL_COLOR_BUFFER_BIT);
obj=gluNewQuadric();
gluQuadricDrawStyle(obj,GLU_LINE);
gluCylinder(obj,0.5,0.5,2,10,10);
glFlush();
}
void display3()
{
GLUquadricObj *obj;
glClear(GL_COLOR_BUFFER_BIT);
obj=gluNewQuadric();
gluQuadricDrawStyle(obj,GLU_LINE);
gluSphere(obj,0.5,50,50);
glFlush();
}

void reshape(GLsizei w,GLsizei h)
{

if(h==0) h=1;
glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(-1.0,1.0,-1.0,1.0); // 问题在于定义的剪裁区域有问题。


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

ww=(GLfloat)w;
hh=(GLfloat)h;

//glutPostRedisplay();
}

void submenu(int value)
{

switch(value)
{
case 1:
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(display1);
glutPostRedisplay();

break;
case 2:
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(display2);
glutPostRedisplay();

break;
/*case 3:

glutPostRedisplay();
break;*/
}
}
void mymenu(int value)
{
switch(value)
{
case 1:
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
break;
case 2:
exit(0);
break;

}
}



void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,0.0);
}

void main(int argc,char ** argv)
{
GLint id,n;

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("qumian");
glutDisplayFunc(display1);
glutReshapeFunc(reshape);

id=glutCreateMenu(submenu);
glutAddMenuEntry("球",1);
glutAddMenuEntry("圆柱",2);
//glutAddMenuEntry("blue",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);



glutCreateMenu(mymenu);
glutAddMenuEntry("clear",1);
glutAddMenuEntry(

"quit",2);
glutAddSubMenu("pattern",id);
glutAttachMenu(GLUT_RIGHT_BUTTON);


init();
glutMainLoop();
}

相关文档
最新文档