计算机图形学实验报告

1、planet程序:

#include <GL/glut.h>

#include <stdlib.h>

static int year = 0, day = 0;

void init(void)

{

glClearColor (0.0, 0.0, 0.0, 0.0);

glShadeModel (GL_FLAT);

}

void display(void)

{

glClear (GL_COLOR_BUFFER_BIT);

glColor3f (1.0, 1.0, 1.0);

glPushMatrix();

glRotatef ((GLfloat) year, 0.0, 1.0, 0.0); glutWireSphere(1.0, 20, 16); /* draw sun */

glRotatef ((GLfloat) year, 0.0, 1.0, 0.0); glTranslatef (2.0, 0.0, 0.0);

glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);

glutWireSphere(0.2, 10, 8); /* draw smaller planet */

glRotatef ((GLfloat) day, 0.0, 1.0, 0.0); glTranslatef (-4.0, 0.0, 0.0);

glutWireSphere(0.2, 10, 8); /* draw smaller planet */ glPopMatrix();

glutSwapBuffers();

}

void reshape (int w, int h)

{

glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION);

glLoadIdentity ();

gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); }

void keyboard (unsigned char key, int x, int y)

{

switch (key) {

case 'd':

day = (day + 10) % 360;

glutPostRedisplay();

break;

case 'D':

day = (day - 10) % 360;

glutPostRedisplay();

break;

case 'y':

year = (year + 5) % 360;

glutPostRedisplay();

break;

case 'Y':

year = (year - 5) % 360;

glutPostRedisplay();

break;

case 27:

exit(0);

break;

default:

break;

}

}

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500);

glutInitWindowPosition (100, 100);

glutCreateWindow (argv[0]);

init ();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutKeyboardFunc(keyboard);

glutMainLoop();

return 0;

}

2、robot程序: #include <GL/glut.h>

#include <stdlib.h>

static int shoulder = 0, elbow = 0, finger=0; void init(void)

{

glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);

}

void display(void)

{

glClear (GL_COLOR_BUFFER_BIT); glPushMatrix();

glTranslatef (-1.0, 0.0, 0.0); //胳膊转 glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0);

glPushMatrix();

glScalef (2.0, 0.4, 1.0);

glutWireCube (1.0);

glPopMatrix();

glTranslatef (1.0, 0.0, 0.0); //轴心平移 glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);//手关节转 glTranslatef (1.0, 0.0, 0.0);

glPushMatrix();

glScalef (2.0, 0.4, 1.0);

glutWireCube (1.0);

glPopMatrix();

glTranslatef (0.7, 0.0, 0.0); //轴心平移 glRotatef ((GLfloat) finger, 0.0, 0.0, 1.0);//手指转 glTranslatef (0.7, 0.0, 0.0);

glPushMatrix();

glScalef (0.6, 0.1, 0.1);

glutWireCube (1.0);

glPopMatrix();

glPopMatrix();

glutSwapBuffers();

}

void reshape (int w, int h)

{

glViewport (0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode (GL_PROJECTION);

glLoadIdentity ();

gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef (0.0, 0.0, -5.0);

}

void keyboard (unsigned char key, int x, int y)

{

switch (key) {

case 's':

shoulder = (shoulder + 5) % 360;

glutPostRedisplay();

break;

case 'S':

shoulder = (shoulder - 5) % 360;

glutPostRedisplay();

break;

case 'e':

elbow = (elbow + 5) % 360;

glutPostRedisplay();

break;

case 'E':

elbow = (elbow - 5) % 360;

glutPostRedisplay();

break;

case 'f':

finger = (finger + 5) % 45;

glutPostRedisplay();

break;

case 'F':

finger= (finger - 5) % 45;

glutPostRedisplay();

break;

case 27:

exit(0);

break;

default:

break;

}

}

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500);

glutInitWindowPosition (100, 100);

glutCreateWindow (argv[0]);

init ();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutKeyboardFunc(keyboard);

glutMainLoop();

return 0;

}

 

第二篇:计算机图形学实验报告模板

西华大学计算机系实验报告

一、提交文档(认真仔细阅读!不符合要求者,拒收)

RAR压缩软件,将下列文件压缩为一个RAR文件提交,命名为:

学号_姓名_实验1_实验文档.RAR

1. 本实验报告,命名为:学号_姓名_实验1_实验报告.DOC

2. 源代码文件(CPP文件),包括自编的头文件。

3. 可执行程序(EXE文件)。

1:除上述文件外,其余文件不得放入。

2:上述文件放在同一个文件夹里,不得建立子文件夹。

3:本实验报告模板所有格式(包括版面、字体、间距等等)不得更改!

二、实验环境

1. 操作系统:windows 7

2. 编程软件:visual studio 2008

三、实验内容

流程图(Word 绘图工具绘制符合规范的流程图,禁止贴图片)

 

源程序

结果图

 

 

四、实验总结

心得体会(包括实验过程中的重要问题及解决办法,实验收获等)

实验建议(若无,删除此行)

相关推荐