注意代碼中要添加依賴庫!!。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;
- }