필요가 발명의 어머니라면, 불만은 진보의 아버지. ―데이비드 록펠러

- 원문링크 :
http://www.gamedev.net/reference/articles/article947.asp
- 알바외주일과 네트워크쪽 때문에 한동안 GL은 보지않아서... 기억력회복을 위해 번역해봅니다.
2 텍스쳐 업로드하기 #
OpenGL상에서 버퍼안에 raw RGB 이미지를 몇개 가지고 있고 이를 입체에 적용하기를 원합니까? 그렇다면 가장 먼저해야할일은 GL상에서 이 raw 텍스쳐 데이타를 사용하기전에 비디오 메모리에 이를 업로드하는 것입니다. 한번 비디오 메모리에 업로드되면 (별도로 해제하지 않는한) 어플리케이션이 동작하는동안 계속 사용할 수 있습니다. 텍스쳐를 비디오 메모리에 업로드하기전에 GL에게 업로드할 이미지 데이타가 무엇인지를 알려주기위해 해야할 몇가지 설정작업이 존재합니다. 아래에 설정작업에서 필요한 몇몇 호출함수들을 요약해두었습니다. 어플리케이션 시작때 각각의 텍스쳐마다 한번씩 실행해주어야한다는 것에 주의하세요.
2.1 glBindTexture #
텍스쳐를 업로드하는 과정중 가장먼저 해야할 일은 glBindTexture를 호출하는 것입니다. glBindTexture는 GL에게 작업할 텍스쳐 ID를 알려주는 역할을 합니다. 텍스쳐 ID는 단지 텍스쳐에 대응하는 숫자를 의미합니다. 다음은 예제입니다.
glBindTexture(GL_TEXTURE_2D, 13);
위 호출은 활성화된 텍스쳐에 13번 ID를 대응시킵니다. 이후에는 GL 텍스쳐 매핑상에서 할 모든 작업은 이 텍스쳐에 영향을 주게 됩니다. 차후 입체에 텍스쳐를 적용하는데 필요하므로 이 ID를 기억해두는 것은 매우 중요합니다.
2.2 glPixelStorei #
glPixelStorei는 GL에게 업로드된 데이타가 어떻게 정렬되어있는지를 알려줍니다. glPixelSrotei의 예는 아래와 같습니다.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
This call tells OpenGL that the pixel data which is going to be passed to it is aligned in byte order, this means that the data has one byte for each component, one for red, green and blue. Stick to the call above unless you have some sort of exotic data which I highly doubt. The alignment of data will probably change as you advance into OpenGL texture mapping.
2.3 glTexParameteri #
The glTexParameteri sets the various parameters for the current OpenGL texture. The parameters that are passed and their effects on the texture are an advanced topic. If you would like to further research what each call does please take a look at the links provided at the end of this document. Each of these lines are important so make sure to get each in your application.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Why did I leave out a description of what these do if they are so important? The properties that you can change with these calls are important to someone who would be more advanced. For someone who is just learning they should be seen as the "voodoo" that makes things work. Once you have a solid understanding of how texture mapping works in OpenGL then you should take a look at these properties.
2.4 glTexEnvf #
The glTexEnvf call sets environment variables for the current texture. What this does is tell OpenGL how the texture will act when it is rendered into a scene. Below is a sample call which I use in my applications.
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
What this does is sets the active texture to GL_MODULATE. The GL_MODULATE attribute allows you to apply effects such as lighting and coloring to your texture. If you do not want lighting and coloring to effect your texture and you would like to display the texture unchanged when coloring is applied replace GL_MODULATE with GL_DECAL.
2.5 glTexImage2D #
The glTexImage2D call is our goal. This call will upload the texture to the video memory where it will be ready for us to use in our programs. I am going to explain this call parameter by parameter since it is so important to what we are doing.
- target - The target of this call, it will always be GL_TEXTURE_2D.
- level - The level of detail number, this should be left at 0 for our purposes. Once you become more adept at OpenGL texture mapping this parameter will be something that you might change.
- internalformat - Internal components parameter. This tells OpenGL how many color components to represent internally from the texture that is uploaded. There are many symbolic constants for this parameter but the one which is most widely used is GL_RGB; this constant is equal to 3.
- width & height - The width and height of the image data. These must be integers that are equal to 2n+2(border) for some integer n. What this basically means is that the texture width and height must be a power of two (2,4,8,16,32,63,128,256,512, etc).
- border - Image border, must be 0 or 1. I always use 0 in my code since I do not use image borders.
- format - Format of the pixel data that will be uploaded. There are many constants which are accepted but GL_RGB is the value that is widely used.
- type - Type of data that will be uploaded. Again there are several symbolic constants but the one which I use is GL_UNSIGNED_BYTE.
- pixels - Pointer to the image data. This is the image data that will be uploaded to the video memory. Note that after your call to glTexImage2D you can free this memory since the texture is already uploaded into video memory.
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
위 함수들이 전부입니다! 위 순서대로 실행했다면 텍스쳐가 업로드되었을 것이고 이제 입체에 적용할 준비가 된 것입니다. 이제 입체에 텍스쳐를 적용하는 방법을 알아보도록 하겠습니다.
3 텍스쳐 적용하기 #
Well now that you have your texture uploaded you want to do something with it since its useless just sitting in memory. The process for applying a texture to geometry greatly depends on what type of data you are dealing with and how you would like things to run. Due to this fact in this section I will relay you some pointers on texture mapping, give an example of how to texture a quad and explain the texture coordinate system.
3.1 Pointers #
- Make sure that you have enabled texturing. You do this with the glEnable (GL_TEXTURE_2D) call.
- Make sure that you bind to a texture before you do any sort of glBegin/glEnd. You cannot bind to a texture in the middle of a begin/end pair.
- Make sure that you specify a texture coordinate before each vertex that makes up a face. If you have 3 verticies the pattern for texture mapping the triangle would go like this: TexCoord; VertexCoord; TexCoord; VertexCoord; TexCoord; VertexCoord;
- Make sure that you store your texture id's in variables, it makes things easier.
- Make use of glGenTextures. Its the easy way to get a free texture id.
3.2 텍스쳐가 적용된 사각형(Quad) #
Below is an example of how you would texture a quad. This code assumes that texturing has been enabled and that there has been a texture uploaded with the id of 13.
glBindTexture (GL_TEXTURE_2D, 13); glBegin (GL_QUADS); glTexCoord2f (0.0, 0.0); glVertex3f (0.0, 0.0, 0.0); glTexCoord2f (1.0, 0.0); glVertex3f (10.0, 0.0, 0.0); glTexCoord2f (1.0, 1.0); glVertex3f (10.0, 10.0, 0.0); glTexCoord2f (0.0, 1.0); glVertex3f (0.0, 10.0, 0.0); glEnd ();
4 텍스쳐 좌표 시스템 #
The image above shows the OpenGL texture coordinate system. In the code above the calls to glTexCoord2f are very important as to what the end result of the texture mapping will be. When you make a call to glTexCoord2f (x,y) OpenGL places the texture coordinate at that place on the image. If you are texturing a triangle there will be three texture coords on the image. Once a glEnd is reached the triangle which is formed by the texture coordinates is then mapped onto the triangle that is made up from the verticies.
5 텍스쳐 로딩 및 그리기 슈도 코드 #
void setupOpenGL (void)
{
glEnable (GL_TEXTURE_2D);
}
void loadAllTextures (void)
{
glBindTexture (..., 1);
glPixelStorei (...);
glTexParameteri (...);
glTexEnvf (...);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
glBindTexture (..., 2);
glPixelStorei (...);
glTexParameteri (...);
glTexEnvf (...);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth2, imageHeight2, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData2);
glBindTexture (..., 3);
glPixelStorei (...);
glTexParameteri (...);
glTexEnvf (...);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth3, imageHeight3, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData3);
glBindTexture (..., 3);
glPixelStorei (...);
glTexParameteri (...);
glTexEnvf (...);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth3, imageHeight3, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData3);
}
void drawTextureObjects (void)
{
glBindTexture (..., 1);
glBegin (...);
glVertex (...);
glTexCoord (...);
glEnd (...);
glBindTexture (..., 2);
glBegin (...);
glVertex (...);
glTexCoord (...);
glEnd (...);
glBindTexture (..., 3);
glBegin (...);
glVertex (...);
glTexCoord (...);
glEnd (...);
glBindTexture (..., 4);
glBegin (...);
glVertex (...);
glTexCoord (...);
glEnd (...);
}








