注意代码中要添加依赖库!!。soil还是非常好用的,下载地址网上有,注意版本,在不同的文件中有对应的vs的版本
转载自:http://blog.csdn.net/mutex86/article/details/8905813 copy
- // TextureTest.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <GL/glew.h>
- #include <GL/glut.h>
- #include <GL/SOIL.h>
- static GLuint texture;
- //装载一个bmp图像使之成为纹理,其中貌似包含了 glTexImage2D这个函数的功能
- int LoadGLTextures(char *textureFilePath)
- {
- texture = SOIL_load_OGL_texture(
- textureFilePath,
- SOIL_LOAD_AUTO,
- SOIL_CREATE_NEW_ID,
- SOIL_FLAG_INVERT_Y
- );
- if(texture == 0)
- return -1;
- glBindTexture(GL_TEXTURE_2D,texture);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
- return 0;
- }
- void init()
- {
- if ( !LoadGLTextures("texture2.bmp"))
- return;
- //glEnable( Gl_TEXTURE_2D);
- glShadeModel( GL_FLAT );
- glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );
- glEnable ( GL_DEPTH_TEST );
- }
- void display( void )
- {
- glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glEnable( GL_TEXTURE_2D); //激活纹理
- glBindTexture(GL_TEXTURE_2D,texture);
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0); glVertex2f( -1.0, -1.0); //纹理坐标的设置
- glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
- glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
- glTexCoord2f( 1.0, 0.0 ); glVertex2f ( 1.0, -1.0 );
- glEnd( );
- glFlush();
- glDisable( GL_TEXTURE_2D ); //终止纹理
- }
- int _tmain(int argc, char* argv[])
- {
- glutInit( &argc, argv );
- glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
- glutInitWindowSize ( 400, 400 );
- glutInitWindowPosition( 100, 100 );
- glutCreateWindow(" 简单纹理贴图实验 ");
- init();
- glutDisplayFunc ( display );
- glutMainLoop();
- return 0;
- }