我的问题可能很简单回答
我导入一个128x128纹理atlas RAW文件。我想让我的立方体中的一个面部存储在该纹理图集中的图像。我从像素(10,5)开始绘制尺寸为10x5的图像
所以我的图像显示在左上角(10,5)和右下角(20,10)
图像角落看起来像这样
(10,5)-----(20,5)
(20,5)-----(20,10)
如果我读得正确
glTexCoord2f()在实现时将整个图像从0 - 1缩放。如果我想在该纹理图集中获得一个子图像,我会选择我想要绘制的像素并将其除以128吗?
GLuint textAtlas = LoadTextureRAW("TextAtlas128", 1, 128,128 );
glPushAttrib(GL_ENABLE_BIT);
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, textAtlas );
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glPushMatrix();
glBegin(GL_QUADS);
.
.
.
glTexCoord2f(10.0f/128, 05.0f/128); glVertex3d(...
glTexCoord2f(10.0f/128, 10.0f/128); glVertex3d(...
glTexCoord2f(20.0f/128, 10.0f/128); glVertex3d(...
glTexCoord2f(20.0f/128, 05.0f/128); glVertex3d(...
任何帮助都会很棒。
void drawBlocks(){
float percentage = .3;
int i , j ,k;
int sX = -80; int sY = 90; int sZ = -620;
GLuint textAtlas = LoadTextureRAW("TextAtlas128", 1, 128,128 );
glPushAttrib(GL_ENABLE_BIT);
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, textAtlas );
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glPushMatrix();
glBegin(GL_QUADS);
for(i = 0; i < 4; i++){
for(j = 0; j < 8; j++){
for(k = 0; k < 9; k++){
if(block[i][j][k].exists == 1){
//Front Face
glNormal3f(0,0,1);
glTexCoord2f(10.0/128, 05.0/128); glVertex3d( sX + (i * 40) , sY - (j * 20) , sZ - (k * 20));
glTexCoord2f(10.0/128, 10.0/128); glVertex3d( sX + (i * 40) , sY - (j * 20) -20 , sZ - (k * 20));
glTexCoord2f(20.0/128, 10.0/128); glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) -20 , sZ - (k * 20));
glTexCoord2f(20.0/128, 05.0/128); glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) , sZ - (k * 20));
//Back Face
glVertex3d( sX + (i * 40) , sY - (j * 20) , sZ - (k * 20) - 20);
glVertex3d( sX + (i * 40) , sY - (j * 20) -20 , sZ - (k * 20) - 20);
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) -20 , sZ - (k * 20) - 20);
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) , sZ - (k * 20) - 20);
//Top Face
glVertex3d( sX + (i * 40) , sY - (j * 20) , sZ - (k * 20) - 20);
glVertex3d( sX + (i * 40) , sY - (j * 20) , sZ - (k * 20) );
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) , sZ - (k * 20) );
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) , sZ - (k * 20) - 20);
//Bottom Face
glVertex3d( sX + (i * 40) , sY - (j * 20) - 20 , sZ - (k * 20) - 20);
glVertex3d( sX + (i * 40) , sY - (j * 20) - 20 , sZ - (k * 20) );
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) - 20 , sZ - (k * 20) );
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) - 20 , sZ - (k * 20) - 20);
//Right Face
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) , sZ - (k * 20) );
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) - 20 , sZ - (k * 20) );
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) - 20 , sZ - (k * 20) - 20);
glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) , sZ - (k * 20) - 20);
//Left Face
glVertex3d( sX + (i * 40) , sY - (j * 20) , sZ - (k * 20) );
glVertex3d( sX + (i * 40) , sY - (j * 20) - 20 , sZ - (k * 20) );
glVertex3d( sX + (i * 40) , sY - (j * 20) - 20 , sZ - (k * 20) - 20);
glVertex3d( sX + (i * 40) , sY - (j * 20) , sZ - (k * 20) - 20);
}
}
}
}
glEnd();
glPopMatrix();
glPopAttrib();
}
void drawPlanes(){
/*GLuint texture = LoadTextureRAW( "Wall Texture", 1 );*/
GLuint texture = LoadTextureRAW( "walls", 1, 32,32 );
glPushAttrib(GL_ENABLE_BIT);
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBegin(GL_QUADS);
//Floor
glNormal3f( 0.0f , 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -100.0f , -100.0f , -725.0f);
glTexCoord2f(0.0f, 10.0f); glVertex3f(100.0f , -100.0f , -725.0f);
glTexCoord2f(10.0f, 10.0f); glVertex3f(100.0f , -100.0f , 000.0f);
glTexCoord2f(10.0f, 0.0f); glVertex3f( -100.0f , -100.0f , 000.0f);
//Ceiling
glNormal3f(0.0f,-1.0f,0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 100.0f, 100.0f, -725.0f);
glTexCoord2f(0.0f, 10.0f); glVertex3f(-100.0f, 100.0f, -725.0f);
glTexCoord2f(10.0f, 10.0f); glVertex3f(-100.0f, 100.0f, 000.0f);
glTexCoord2f(10.0f, 0.0f); glVertex3f( 100.0f, 100.0f, 000.0f);
//Right Wall
glNormal3f( -1.0f , 0.0f, 0.0f);
glTexCoord2f(10.0f, 10.0f); glVertex3f(100.0f , 100.0f , 000.0f);
glTexCoord2f(0.0f, 10.0f); glVertex3f(100.0f , 100.0f , -725.0f);
glTexCoord2f(0.0f, 00.0f); glVertex3f(100.0f ,-100.0f , -725.0f);
glTexCoord2f(10.0f, 0.0f); glVertex3f(100.0f ,-100.0f, 000.0f);
//LeftWall
glNormal3f( 1.0f , 0.0f, 0.0f);
glTexCoord2f(10.0f, 10.0f); glVertex3f(-100.0f , 100.0f , -725.0f);
glTexCoord2f(0.0f, 10.0f); glVertex3f(-100.0f , 100.0f , 000.0f);
glTexCoord2f(0.0f, 00.0f); glVertex3f(-100.0f , -100.0f , 000.0f);
glTexCoord2f(10.0f, 0.0f); glVertex3f(-100.0f , -100.0f , -725.0f);
//Back Wall
glNormal3f( 0.0f , 0.0f, 1.0f);
glTexCoord2f(10.0f, 10.0f); glVertex3f( 100.0f , 100.0f , -725.0f);
glTexCoord2f(0.0f, 10.0f); glVertex3f(-100.0f , 100.0f , -725.0f);
glTexCoord2f(0.0f, 00.0f); glVertex3f(-100.0f , -100.0f , -725.0f);
glTexCoord2f(10.0f, 0.0f); glVertex3f( 100.0f , -100.0f , -725.0f);
glEnd();
glPopAttrib();
}
/**
Done Drawing Stuff
*/
/**
Texture Stuff
*/
// load a 256x256 RGB .RAW file as a texture
GLuint LoadTextureRAW( const char * filename, int wrap , int width, int height )
{
GLuint texture;
GLbyte *data;
FILE * file;
// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) {
printf("Not found");
return 0;
}
data = (GLbyte*)malloc(width * height * 3);
// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );
// allocate a texture name
glGenTextures( 1, &texture );
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
wrap ? GL_REPEAT : GL_CLAMP );
// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );
// free buffer
free( data );
return texture;
}
编辑:屏幕截图
http://dl.dropbox.com/u/12914798/Texture%20Atlas.png
答案 0 :(得分:1)
我会选择我想要绘制的像素并将其除以128?
是的。这不适合你吗?好像你知道该怎么做,不知道为什么你不只是尝试它:)