本文在第9篇文章的基础上,为立方体加入纹理映射的功能。
Texture Mapping
Texture Mapping in OpenGL is a fairly straightforward concept. Every texture is nothing but an image of some sort. Texture mapping is basically applying a texture to a surface. Textures can be 1D, 2D or even 3D. A 1D texture is an image with either a width or a height, not both. They are not very useful. 2D textures have both width and height and are very useful. 3D textures are called Volume textures and are used in medical imaging applications for viewing CAT, MRI, and other 3D scans. We will look at using 2D textures in OpenGL as they are the most widely used in 3D Graphics.
Windows Bitmap Files
Images in Windows are typically stored in bitmap files. These images can be used as textures that will be applied to OpenGL surfaces. But before use them for texture mapping applications we should convert them to an OpenGL format. So we essentially have to read a Windows Bitmap into an OpenGL image. We can use the Auxiliary library to do so. It takes care of all the trouble involved in performing this conversion. Once a texture map is read into memory, the individual elements are called texels, just like an image's individual elements are called pixels. We wouldn't be dealing with these texels as we would be using the Aux library routine auxDIBImageLoadA to perform the conversion for us. Also, we need to make sure that the image dimensions are a power of 2. OpenGL images that we are going to use as a texture must have dimensions of a power of 2. Thus 32X32, 64X64, 128X64 etc. are all valid image sizes to be used as texture maps.
Defining 2D Textures
To define a 2D texture image in OpenGL we call glTexImage2D (when we are not using Mipmapping).
Mipmapping
When texture mapping is used with animation scaling of images cause some visual artifacts. This can be avoided by generating textures of various sizes from a large original texture and letting OpenGL automatically switch between the textures of various sizes. This technique is called Mipmapping and the individual textures are called Mipmaps. We can use the function gluBuild2DMipMaps to construct a series of mipmaps.
Texture Modes
OpenGL defines three texturing modes for different types of rendering. The first is GL_MODULATE, which modulates the current lighting and color information with the texture image. GL_DECAL is the second mode which only uses the texture image. Color and Lighting information will not affect the texture's appearance. The last mode is GL_BLEND, in which the texture image is blended with the current texture color and the current lighting and color information.
Texture Filters
OpenGL uses texture filters to interpolate between the texture pixels. It provides two types of texture filters: the minification filter (GL_TEXTURE_MIN_FILTER) for polygons smaller than the texture image and the magnification filter (GL_TEXTURE_MAG_FILTER) for polygons that are larger than the texture image. We'll look at how we will use these later in the tutorial.
Texture Coordinates
Texture Coordinates associate a particular location in the texture image with vertices in a polygon. These coordinates determine how the texture is mapped onto the polygon. Texture coordinates lie between 0.0 and 1.0 in case of 2D textures.
Texture Wrapping
When texture coordinates go outside the range of 0.0 to 1.0, they are either clamped to the surface or repeated. This can be specified by setting the GL_TEXTURE_WRAP_* parameter appropriately, to either GL_CLAMP or GL_REPEAT.
Texture Objects
Texture objects are a way of loading and maintaining multiple textures in memory without loading them each time before they are used. They are an optimization feature introduced recently in OpenGL.
1,在CCY457OpenGLView类中加入下列变量,分别代表纹理环绕,纹理过滤,纹理模式的参数。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->GLdoublem_texWrap,m_texFilter,m_texMode;
//AllTextureNames
GLuintm_Texture[3];//保存纹理对象的名称
并在构造函数中加入:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->CCY457OpenGLView::CCY457OpenGLView()
{
m_xRot=0.0f;
m_yRot=0.0f;
m_texWrap=GL_CLAMP;
m_texMode=GL_DECAL;
m_texFilter=GL_NEAREST;
}
2,加载纹理对象。
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->//TextureMappingFunctions
voidCCY457OpenGLView::LoadGLTextures()
{
//CreateTextureNames
glGenTextures(3,m_Texture);
LoadTexture("Apple.bmp",0);
LoadTexture("Fauve.bmp",1);
LoadTexture("Flower.bmp",2);
}
voidCCY457OpenGLView::LoadTexture(CStringfileName,inttexName)
{
//LoadTexture
AUX_RGBImageRec*m_texture;
m_texture=auxDIBImageLoad((constchar*)fileName);
if(!m_texture)
{
MessageBox("Picturecouldnotbeloaded");
exit(1);
}
glBindTexture(GL_TEXTURE_2D,m_Texture[texName]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,m_texWrap);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,m_texWrap);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,m_texFilter);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,m_texFilter);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,m_texMode);
gluBuild2DMipmaps(GL_TEXTURE_2D,3,m_texture->sizeX,m_texture->sizeY,GL_RGB,GL_UNSIGNED_BYTE,m_texture->data);
}
3,加入控制纹理映射效果的各个菜单项及其事件处理程序
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::OnTexturewrapGlclamp()
{
m_texWrap=GL_CLAMP;
LoadGLTextures();
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnUpdateTexturewrapGlclamp(CCmdUI*pCmdUI)
{
if(m_texWrap==GL_CLAMP)
pCmdUI->SetRadio();
else
pCmdUI->SetRadio(FALSE);
}
voidCCY457OpenGLView::OnTexturewrapGlrepeat()
{
m_texWrap=GL_REPEAT;
LoadGLTextures();
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnUpdateTexturewrapGlrepeat(CCmdUI*pCmdUI)
{
if(m_texWrap==GL_REPEAT)
pCmdUI->SetRadio();
else
pCmdUI->SetRadio(FALSE);
}
voidCCY457OpenGLView::OnTexturefilterGlnearest()
{
m_texFilter=GL_NEAREST;
LoadGLTextures();
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnUpdateTexturefilterGlnearest(CCmdUI*pCmdUI)
{
if(m_texFilter==GL_NEAREST)
pCmdUI->SetRadio();
else
pCmdUI->SetRadio(FALSE);
}
voidCCY457OpenGLView::OnTexturefilterGllinear()
{
m_texFilter=GL_LINEAR;
LoadGLTextures();
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnUpdateTexturefilterGllinear(CCmdUI*pCmdUI)
{
if(m_texFilter==GL_LINEAR)
pCmdUI->SetRadio();
else
pCmdUI->SetRadio(FALSE);
}
voidCCY457OpenGLView::OnTexturemodeGlmodulate()
{
m_texMode=GL_MODULATE;
LoadGLTextures();
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnUpdateTexturemodeGlmodulate(CCmdUI*pCmdUI)
{
if(m_texMode==GL_MODULATE)
pCmdUI->SetRadio();
else
pCmdUI->SetRadio(FALSE);
}
voidCCY457OpenGLView::OnTexturemodeGldecal()
{
m_texMode=GL_DECAL;
LoadGLTextures();
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnUpdateTexturemodeGldecal(CCmdUI*pCmdUI)
{
if(m_texMode==GL_DECAL)
pCmdUI->SetRadio();
else
pCmdUI->SetRadio(FALSE);
}
voidCCY457OpenGLView::OnTexturemodeGlblend()
{
m_texMode=GL_BLEND;
LoadGLTextures();
InvalidateRect(NULL,FALSE);
}
voidCCY457OpenGLView::OnUpdateTexturemodeGlblend(CCmdUI*pCmdUI)
{
if(m_texMode==GL_BLEND)
pCmdUI->SetRadio();
else
pCmdUI->SetRadio(FALSE);
}
4,在InitializeOpenGL()中加入如下调用:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->//加载纹理
LoadGLTextures();
5,绘制函数修改如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::RenderScene()
{//绘制函数
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(m_xRot,1.0f,0.0f,0.0f);
glRotatef(m_yRot,0.0f,1.0f,0.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,m_Texture[0]);
//FrontFace
glBegin(GL_POLYGON);
glTexCoord2f(0,0);
glVertex3f(-1.0f,-1.0f,0.0f);
glTexCoord2f(1,0);
glVertex3f(1.0f,-1.0f,0.0f);
glTexCoord2f(1,1);
glVertex3f(1.0f,1.0f,0.0f);
glTexCoord2f(0,1);
glVertex3f(-1.0f,1.0f,0.0f);
glEnd();
//BackFace
glBegin(GL_POLYGON);
glTexCoord2f(1,0);
glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1,1);
glVertex3f(-1.0f,1.0f,-1.0f);
glTexCoord2f(0,1);
glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(0,0);
glVertex3f(1.0f,-1.0f,-1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D,m_Texture[1]);
//LeftFace
glBegin(GL_POLYGON);
glTexCoord2f(1,0);
glVertex3f(-1.0f,-1.0f,0.0f);
glTexCoord2f(1,1);
glVertex3f(-1.0f,1.0f,0.0f);
glTexCoord2f(0,1);
glVertex3f(-1.0f,1.0f,-1.0f);
glTexCoord2f(0,0);
glVertex3f(-1.0f,-1.0f,-1.0f);
glEnd();
//RightFace
glBegin(GL_POLYGON);
glTexCoord2f(0,0);
glVertex3f(1.0f,-1.0f,0.0f);
glTexCoord2f(1,0);
glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(1,1);
glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(0,1);
glVertex3f(1.0f,1.0f,0.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D,m_Texture[2]);
//TopFace
glBegin(GL_POLYGON);
glTexCoord2f(0,0);
glVertex3f(-1.0f,1.0f,0.0f);
glTexCoord2f(0,1);
glVertex3f(1.0f,1.0f,0.0f);
glTexCoord2f(1,1);
glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(1,0);
glVertex3f(-1.0f,1.0f,-1.0f);
glEnd();
//BottonFace
glBegin(GL_POLYGON);
glTexCoord2f(0,1);
glVertex3f(-1.0f,-1.0f,0.0f);
glTexCoord2f(0,0);
glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1,0);
glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(1,1);
glVertex3f(1.0f,-1.0f,0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
分享到:
相关推荐
and describes the steps involved in projective texture mapping in OpenGL. Projective texture mapping refers both to the way texture coordinates are assigned to vertices, and the way they are computed ...
Texture Mapping
Texture mapping Texture mapping Texture mapping
一个原创的sphere texture mapping实例(附sphere mapping详解pdf) 一个原创的 OpenGL 键盘控制模型 rotation实例 键盘操作: 按空格键可切换兔子模型的自转轴:x-》y,y-》x (source包含loadpng.c(贴图loading)...
《基于MFC的OpenGL编程》Part 9 Texture Mapping源码 基于博客:http://www.cnblogs.com/phinecos/archive/2008/11/05/1327646.html作出了相应的修改和调整
OpenGLES 入门编程代码的第12课中讲解材质贴图的代码
graphics 中texture 是其中很重要的一块,这篇专利里面的方法可以借鉴一下
Simulating planar reflection using two-pass rendering and texture mapping 1. There ‘s a table with flat rectangular semi-reflective table-top in the 3D scene 2. On/Around the table are some 3D ...
Advanced Topics in Computer Graphics Texture Mapping by Steve Olivieri
An opengl example of a planet with texture
以“两个三角形”为例,说明“通过纹理映射给Triangle Meshes图形添加纹理”。
主要涉及图像处理技术中的前沿知识,纹理以及纹理映射
论文:QoE-Guided Warping for Stereoscopic ...在图像warping技术中,当计算得到理想坐标点后,利用texture_mapping进行坐标点的绘制,可将理想坐标点绘制出来,并且将空洞区域填补出来。自己写的代码,亲测好用。
OpenGL下导入PCX,BMP,TGA格式的贴图的类包括头文件和源文件
osg(OpenSceneGraph)设置渲染属性和模式、状态继承,渲染状态的示例代码建立了一个名为root 的Group 组节点,作为整个场景 图形的根节点,并设置根节点的渲染状态StateSet 为禁止光照。此处使用 ...
8 Texture Mapping: The Basics 9 Texture Mapping: Beyond the Basics 10 Curves and Surfaces 11 It’s All About the Pipeline: Faster Geometry Throughput 12 Interactive Graphics 13 Occlusion Queries: Why...
首先必须说,国内在OpenGL这方面特别是ndk上的分享太太太少 这中间遇到很多问题,而这些问题需要在书上,在网上,在各种资料上找,而且书上是java层调用,网上的缺少各种文件案例,只有在java层研究,在各种案例中找...
三维重建纹理贴图,已知ply网格模型、摄像机拍摄的图片以及对应的内外参数,可生成obj带mtl纹理的模型文件。基于pcl或者openmvs。
opengl es texture, 圆柱贴图说明,列子