标签:
效果图如上:
步骤:略。
常见问题:
1、编译提示"gl/glaux.h"不存在,多发生在VS2008版本以上。
解决办法:下载包含"glaux.h"的库文件。
下载地址:http://yunpan.cn/cKZPLCcQufqRi 访问密码 ba74
2、运行时提示”text1.bmp“打开失败。
解决办法:将2个贴图文件放在项目文件夹下,如:X:...\Textures1\textures1内。
实现代码如下:
main.cpp
1 /********************************************************************** 2 3 Texture mapping with OpenGL 4 5 June, 13th, 2000 6 7 This tutorial was written by Philipp Crocoll 8 Contact: 9 philipp.crocoll@web.de 10 www.codecolony.de 11 12 Every comment would be appreciated. 13 14 If you want to use parts of any code of mine: 15 let me know and 16 use it! 17 18 *********************************************************************** 19 ESC: exit 20 21 CAMERA movement: 22 w : forwards 23 s : backwards 24 a : turn left 25 d : turn right 26 x : turn up 27 y : turn down 28 v : strafe right 29 c : strafe left 30 r : move up 31 f : move down 32 b : switch between BfC and drawing the frontfaces wired 33 ***********************************************************************/ 34 35 #pragma comment ( lib , "glaux.lib" ) 36 37 #include <gl\glut.h> 38 #include <gl\glaux.h> //Used for loading the textures 39 #include <windows.h> 40 #include "camera.h" 41 #include "textures.h" 42 43 44 CCamera Camera; 45 46 GLfloat YRotated = 0.0; 47 48 COGLTexture Tex1, Tex2; 49 50 void InitTextures(void) 51 { 52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 54 55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); 57 58 Tex1.LoadFromFile("tex1.bmp"); 59 Tex2.LoadFromFile("tex2.bmp"); 60 61 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 62 63 } 64 65 void DrawCube(void) 66 { 67 //Draws a cube with two shaded, two one-colored and two textured faces 68 glBegin(GL_QUADS); 69 glColor3f(1.0,0.0,0.0); 70 //front: 71 glVertex3f(-0.5,-0.5,0.5); 72 glVertex3f(-0.5,0.5,0.5); 73 glVertex3f(0.5,0.5,0.5); 74 glVertex3f(0.5,-0.5,0.5); 75 76 //back: 77 glColor3f(0.0,0.0,1.0); 78 glVertex3f(-0.5,-0.5,-0.5); 79 glVertex3f(0.5,-0.5,-0.5); 80 glVertex3f(0.5,0.5,-0.5); 81 glVertex3f(-0.5,0.5,-0.5); 82 83 //top: 84 glColor3f(0.0,0.6,1.0); 85 glVertex3f(-0.5,0.5,-0.5); 86 glVertex3f(0.5,0.5,-0.5 ); 87 glColor3f(1.0,0.6,1.0); 88 glVertex3f(0.5,0.5,0.5); 89 glVertex3f(-0.5,0.5,0.5); 90 91 //bottom: 92 glColor3f(0.0,0.6,0.0); 93 glVertex3f(-0.5,-0.5,-0.5); 94 glColor3f(0.6,0.6,0.6); 95 glVertex3f(-0.5,-0.5,0.5); 96 glColor3f(1.0,1.0,0.3); 97 glVertex3f(0.5,-0.5,0.5); 98 glColor3f(0.0,1.0,0.0); 99 glVertex3f(0.5,-0.5,-0.5); 100 glEnd(); 101 glEnable(GL_TEXTURE_2D); 102 103 Tex1.SetActive(); 104 glBegin(GL_QUADS); 105 //left: 106 glTexCoord2f(1.0,0.0); 107 glVertex3f(-0.5,-0.5,-0.5); 108 glTexCoord2f(1.0,1.0); 109 glVertex3f(-0.5,0.5,-0.5); 110 glTexCoord2f(0.0,1.0); 111 glVertex3f(-0.5,0.5,0.5); 112 glTexCoord2f(0.0,0.0); 113 glVertex3f(-0.5,-0.5,0.5); 114 glEnd(); 115 //right: 116 Tex2.SetActive(); 117 glBegin(GL_QUADS); 118 glTexCoord2f(0.0,0.0); 119 glVertex3f(0.5,-0.5,-0.5); 120 glTexCoord2f(1.0,0.0); 121 glVertex3f(0.5,-0.5,0.5); 122 glTexCoord2f(1.0,1.0); 123 glVertex3f(0.5,0.5,0.5); 124 glTexCoord2f(0.0,1.0); 125 glVertex3f(0.5,0.5,-0.5); 126 glEnd(); 127 glDisable(GL_TEXTURE_2D); 128 129 130 } 131 132 void reshape(int x, int y) 133 { 134 if (y == 0 || x == 0) return; 135 136 glMatrixMode(GL_PROJECTION); 137 glLoadIdentity(); 138 gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0); 139 140 glMatrixMode(GL_MODELVIEW); 141 glViewport(0,0,x,y); 142 } 143 144 void Display(void) 145 { 146 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 147 glLoadIdentity(); 148 Camera.Render(); 149 glRotatef(YRotated,0.0,1.0,0.0); 150 DrawCube(); 151 152 glFlush(); 153 glutSwapBuffers(); 154 155 } 156 void KeyDown(unsigned char key, int x, int y) 157 { 158 switch (key) 159 { 160 case 27: //ESC 161 PostQuitMessage(0); 162 break; 163 case ‘a‘: 164 Camera.RotateY(5.0); 165 Display(); 166 break; 167 case ‘d‘: 168 Camera.RotateY(-5.0); 169 Display(); 170 break; 171 case ‘w‘: 172 Camera.MoveForwards( -0.1 ) ; 173 Display(); 174 break; 175 case ‘s‘: 176 Camera.MoveForwards( 0.1 ) ; 177 Display(); 178 break; 179 case ‘x‘: 180 Camera.RotateX(5.0); 181 Display(); 182 break; 183 case ‘y‘: 184 Camera.RotateX(-5.0); 185 Display(); 186 break; 187 case ‘c‘: 188 Camera.StrafeRight(-0.1); 189 Display(); 190 break; 191 case ‘v‘: 192 Camera.StrafeRight(0.1); 193 Display(); 194 break; 195 case ‘r‘: 196 Camera.Move(F3dVector(0.0,0.1,0.0)); 197 Display(); 198 break; 199 case ‘f‘: 200 Camera.Move(F3dVector(0.0,-0.1,0.0)); 201 Display(); 202 break; 203 case ‘b‘: 204 if (glIsEnabled(GL_CULL_FACE) == GL_TRUE) 205 { 206 glDisable(GL_CULL_FACE); 207 glPolygonMode(GL_FRONT, GL_LINE); 208 glPolygonMode(GL_BACK, GL_FILL); 209 } 210 else 211 { 212 glEnable(GL_CULL_FACE); 213 glPolygonMode(GL_FRONT, GL_FILL); 214 } 215 Display(); 216 break; 217 } 218 } 219 220 221 static long long times = 0; 222 void Idle(void) 223 { 224 times++; // 延迟 225 if(times>500000) 226 times =0; 227 if(times% 500000== 0) 228 { 229 YRotated += 0.4; 230 Display(); 231 } 232 } 233 234 int main(int argc, char **argv) 235 { 236 glutInit(&argc, argv); 237 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 238 glutInitWindowSize(300,300); 239 glutCreateWindow("Textures and BfC"); 240 glFrontFace(GL_CW); //Clockwise is front in "DrawCube()" // 顺时针为 正面 241 glCullFace(GL_BACK); // 剔除操作 242 glEnable(GL_CULL_FACE); // 开启剔除操作效果 243 glEnable(GL_DEPTH_TEST); // 开启深度测试 244 Camera.Move( F3dVector(0.0, 0.0, 3.0 )); // 镜头后退3个单位 245 Camera.MoveForwards( 1.0 ); // 镜头前移1个单位,(多余?) 246 InitTextures(); 247 glutDisplayFunc(Display); 248 glutReshapeFunc(reshape); 249 glutKeyboardFunc(KeyDown); 250 glutIdleFunc(Idle); 251 glutMainLoop(); 252 return 0; 253 }
textures.cpp
1 #include "textures.h" 2 void COGLTexture::LoadFromFile(char *filename) 3 { 4 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 5 glGenTextures(1,&ID); 6 glBindTexture( GL_TEXTURE_2D, ID); 7 Image = auxDIBImageLoadA( (const char*) filename ); 8 Width = Image->sizeX; 9 Height = Image->sizeY; 10 gluBuild2DMipmaps( GL_TEXTURE_2D, 11 3, 12 Image->sizeX, 13 Image->sizeY, 14 GL_RGB, 15 GL_UNSIGNED_BYTE, 16 Image->data); 17 delete Image; 18 } 19 20 void COGLTexture::SetActive() 21 { 22 glBindTexture( GL_TEXTURE_2D, ID); 23 } 24 25 unsigned int COGLTexture::GetID() 26 { 27 return ID; 28 }
textures.h
1 #include <GL\glaux.h> 2 3 class COGLTexture 4 { 5 public: 6 _AUX_RGBImageRec *Image; 7 unsigned int GetID(); 8 void LoadFromFile(char *filename); 9 void SetActive(); 10 int GetWidth(); 11 int GetHeight(); 12 private: 13 int Width, Height; 14 unsigned int ID; 15 };
原始代码来源: http://www.codecolony.de/
标签:
原文地址:http://www.cnblogs.com/yuwl26/p/4245926.html