页数:8页文件大小:341.4 KB来源: 计算机图形学ppt课件
本实验指导面向OpenGL纹理贴图:要求掌握OpenGL程序设计方法与纹理贴图方法,内容为参考地月系统程序完成月球围绕地球转动的三维示例,给出纹理载入、纹理坐标映射与复合变换的完整代码。
《计算机图形学》实验指导书 实验4:OpenGL纹理贴图
提示:可参考 OpenGL 教程_NeHe01-11_基础.chm 及如下程序 OpenGL_pic.cpp。程序运行时在同一目录应有图片 earth.bmp 与 Moonface.bmp。
#include "windows.h" #include <GL/gl.h> #include <GL/glu.h> #include <GL/glaux.h> #include <math.h> #pragma comment (lib, "opengl32.lib") #pragma comment (lib, "glu32.lib") #pragma comment (lib, "glaux.lib") void myinit (void); void CALLBACK myReshape (GLsizei w, GLsizei h); void CALLBACK display (void); GLfloat theta = 0;GLfloat zpos = -10;GLuint texture[2]; 第3页:纹理载入函数
// 打开图像 "earth.bmp",并把图像放入纹理 texture[0] int LoadGLTextures () { int Status = true; int i; AUX_RGBImageRec *TextureImage[1]; memset (TextureImage, 0, sizeof (void *) * 1); glGenTextures (1, &texture[0]); TextureImage[0] = auxDIBImageLoad ("earth.bmp"); if (TextureImage[0]) { glBindTexture (GL_TEXTURE_2D, texture[0]); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D (GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); } else Status = false; TextureImage[0] = auxDIBImageLoad ("Moonface.bmp"); if (TextureImage[0]) { glBindTexture (GL_TEXTURE_2D, texture[1]); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D (GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); } else Status = false; free (TextureImage[0]->data); free (TextureImage[0]); return Status; }
第4页:初始化与纹理启用
void myinit (void) { GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv (GL_LIGHT0, GL_POSITION, light_position); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glShadeModel (GL_SMOOTH); glDepthFunc (GL_LESS); glEnable (GL_DEPTH_TEST); LoadGLTextures (); glEnable (GL_TEXTURE_2D);}
第5页:纹理立方体的绘制
void CALLBACK display (void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); glTranslatef (0.0, 0.0, zpos); glRotatef (30, 1, 0, 0); glPushMatrix (); glRotatef (-5 * theta, 0, 1, 0); glBindTexture (GL_TEXTURE_2D, texture[0]); glBegin (GL_QUADS); glNormal3f (0.0f, 0.0f, 1.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (-1.0f, -1.0f, 1.0f); glTexCoord2f (1.0f, 0.0f); glVertex3f ( 1.0f, -1.0f, 1.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f ( 1.0f, 1.0f, 1.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (-1.0f, 1.0f, 1.0f); glEnd (); glPopMatrix (); }
第6页:地球的绘制与公转变换
// 画地球。多个变换复合时,先做最后一个变换,再依次向前! glRotatef (theta, 0, 1, 0);glTranslatef (-5.0, 0, 0.0);glRotatef (3 * theta, 0, 1, 0);glBindTexture (GL_TEXTURE_2D, texture[1]); GLUquadricObj *quadric = gluNewQuadric ();gluQuadricTexture (quadric, GL_TRUE); gluSphere (quadric, 1, 120, 120);glFlush (); auxSwapBuffers ();_sleep (50); void CALLBACK stepDisplay (void) { theta = theta + 1; if (theta >= 360) theta = 0.0; display (); }
第7页:窗口变化与事件处理
void CALLBACK myReshape (GLsizei w, GLsizei h) { glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-2.0, 2.0, -2.0, 2.0, 2.0, 30.0); glMatrixMode (GL_MODELVIEW); glViewport (0, 0, w, h);} void CALLBACK lbtnFunc (AUX_EVENTREC *event) { exit (0); } void CALLBACK upkeyFunc (void) { zpos = zpos + 0.1; }void CALLBACK downkeyFunc (void) { zpos = zpos - 0.1; }
第8页:主函数与程序收尾
void main (void) { auxInitDisplayMode (AUX_DOUBLE | AUX_RGBA); auxInitPosition (0, 0, 500, 500); auxInitWindow ("Perspective3-DCube"); myinit (); auxReshapeFunc (myReshape); auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, lbtnFunc); auxKeyFunc (AUX_UP, upkeyFunc); auxKeyFunc (AUX_DOWN, downkeyFunc); auxIdleFunc (stepDisplay); auxMainLoop (display); }
下载地址:{{downurl}}
{{{a}}}
{{#b}}{{{b}}}
{{/b}} {{/title}} {{#nolog}}登陆注册
{{/nolog}} {{#djsx}}点我刷新
{{/djsx}} {{#gocz}} {{/gocz}} {{#ffqr}} {{/ffqr}}