Cube Texture Mapping

 

Cube Mapping是一種將周圍環境投影到無限大且無死角的360°平面上,就像天空一般。所謂Cube Map即是六張不同的2D影像,利用opengl將這六張影像投影到一個Cube上。Load Texture方式有兩種,一種是最簡單的2D Texture Mapping (GL_TEXTURE_2D);另一種是GL_TEXTURE_CUBE_MAP,使用GL_TEXTURE_CUBE_MAP必須include GL/glew.h這個head檔才能使用,GL_TEXTURE_CUBE_MAP已經將Cube面向給定義好了,只要依循下列位置擺放六張圖就可以了。

GL_TEXTURE_CUBE_MAP


OpenGL環境設定

  1. Download : http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
  2. 將glut32.dll放到C:\WINDOWS\system32目錄下
  3. 將glut32.lib和opengl32.lib放到C:\Program Files\Microsoft Visual Studio 9.0\VC\lib目錄下
  4. 將glut.h放到C:\Program Files\Microsoft Visual Studio 9.0\VC\ Include\GL\目錄下
  5. Download GLEW file : http://glew.sourceforge.net/
  6. 將glew.h、glxew.h和wglew.h放到C:\Program Files\Microsoft Visual Studio 9.0\VC\ Include\GL\目錄下

code::GL_TEXTURE_2D Texture Mapping

// FRONT
 if ( !buf.load( filename.c_str() ) ){
qWarning( "Could not read image file, using single-color instead." );
QImage dummy( 128, 128,QImage::Format_RGB32);
dummy.fill( Qt::green);
buf = dummy;}
tex = QGLWidget::convertToGLFormat( buf );
glGenTextures( 1, &texture[0] ); 
glBindTexture( GL_TEXTURE_2D, texture[0] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // for texture seamless from glew.h
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // for texture seamless from glew.h
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // for texture seamless from glew.h
// BACK
filename = folder + "BACK" + subfile;
buf.load(filename.c_str());
tex = QGLWidget::convertToGLFormat( buf );
glGenTextures( 1, &texture[1] ); 
glBindTexture( GL_TEXTURE_2D, texture[1] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // for texture seamless from glew.h
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // for texture seamless from glew.h
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // for texture seamless from glew.h
// TOP
filename = folder + "TOP" + subfile;
buf.load(filename.c_str());
tex = QGLWidget::convertToGLFormat( buf );
glGenTextures( 1, &texture[2] ); 
glBindTexture( GL_TEXTURE_2D, texture[2] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// DOWN
filename = folder + "DOWN" + subfile;
buf.load(filename.c_str());
tex = QGLWidget::convertToGLFormat( buf );
glGenTextures( 1, &texture[3] ); 
glBindTexture( GL_TEXTURE_2D, texture[3] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// RIGHT
filename = folder + "RIGHT" + subfile;
buf.load(filename.c_str());
tex = QGLWidget::convertToGLFormat( buf );
glGenTextures( 1, &texture[4] );
glBindTexture( GL_TEXTURE_2D, texture[4] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// LEFT
filename = folder + "LEFT" + subfile;
buf.load(filename.c_str());
tex = QGLWidget::convertToGLFormat( buf );
glGenTextures( 1, &texture[5] );
glBindTexture( GL_TEXTURE_2D, texture[5] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

code::GL_TEXTURE_CUBE_MAP Texture Mapping

// FRONT
 if ( !buf.load( filename.c_str() ) ) 
 {
 qWarning( "Could not read image file, using single-color instead." );
 QImage dummy( 128, 128,QImage::Format_RGB32);
 dummy.fill( Qt::green);
 buf = dummy;
 }
 tex = QGLWidget::convertToGLFormat( buf );
 glGenTextures( 1, &texture[0] ); 
 glBindTexture( GL_TEXTURE_CUBE_MAP, texture[0] );
 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, 3, tex.width(), tex.height(), 0,
 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
 // BACK
 filename = folder + "LEFT" + subfile;
 buf.load(filename.c_str());
 tex = QGLWidget::convertToGLFormat( buf );
 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, 3, tex.width(), tex.height(), 0,
 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );

// TOP
 filename = folder + "TOP" + subfile;
 buf.load(filename.c_str());
 tex = QGLWidget::convertToGLFormat( buf );
 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, 3, tex.width(), tex.height(), 0,
 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );

// DOWN
 filename = folder + "DOWN" + subfile;
 buf.load(filename.c_str());
 tex = QGLWidget::convertToGLFormat( buf );
 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, 3, tex.width(), tex.height(), 0,
 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );

// RIGHT
 filename = folder + "BACK" + subfile;
 buf.load(filename.c_str());
 tex = QGLWidget::convertToGLFormat( buf );
 glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, 3, tex.width(), tex.height(), 0,
 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );

// LEFT
 filename = folder + "FRONT" + subfile;
 buf.load(filename.c_str());
 tex = QGLWidget::convertToGLFormat( buf );
 glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, 3, tex.width(), tex.height(), 0,
 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );

// Texture Filter
 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

Textures :

cube

Cube Texture Mapping 結果:

Cube

 


 

reference :

http://learnopengl.com/#!Advanced-OpenGL/Cubemaps

http://www.braynzarsoft.net/index.php?p=D3D11CUBEMAP

 

Leave a Reply

Your email address will not be published. Required fields are marked *