1

我尝试使用 GL_TEXTURE_RECTANGLE_ARB 使用数据原始纹理:

void Display::tex(){

GLubyte Texture[16] =
{    
0,0,0,0, 0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF, 0,0,0,0
};
GLuint Nom;
glLoadIdentity();//load identity matrix
glTranslatef(0.0f,0.0f,-4.0f);//move forward 4 units

glEnable(GL_DEPTH_TEST);    //Active le depth test
glDisable( GL_CULL_FACE );

glEnable (GL_TEXTURE_RECTANGLE_ARB);

glPixelStorei(GL_UNPACK_ROW_LENGTH, 2);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(1, &Nom);

glBindTexture(GL_TEXTURE_RECTANGLE_ARB, Nom);

glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,

                GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 2, 2,

                0, GL_BGRA_EXT, GL_UNSIGNED_INT_8_8_8_8_REV, Texture);
angle  = 0.01 * glutGet ( GLUT_ELAPSED_TIME );
glRotatef(angle,0,1,1);

glBegin(GL_QUADS);  //Et c'est parti pour le cube !

glTexCoord2i(0,0);glVertex3i(-1,-1,-1);
glTexCoord2i(1,0);glVertex3i(+1,-1,-1);
glTexCoord2i(1,1);glVertex3i(+1,+1,-1);
glTexCoord2i(0,1);glVertex3i(-1,+1,-1);

//1 face

glTexCoord2i(0,0);glVertex3i(-1,-1,+1);
glTexCoord2i(1,0);glVertex3i(+1,-1,+1);
glTexCoord2i(1,1);glVertex3i(+1,+1,+1);
glTexCoord2i(0,1);glVertex3i(-1,+1,+1);

//2 faces

glTexCoord2i(0,0);glVertex3i(+1,-1,-1);
glTexCoord2i(1,0);glVertex3i(+1,-1,+1);
glTexCoord2i(1,1);glVertex3i(+1,+1,+1);
glTexCoord2i(0,1);glVertex3i(+1,+1,-1);

//3 faces

glTexCoord2i(0,0);glVertex3i(-1,-1,-1);
glTexCoord2i(1,0);glVertex3i(-1,-1,+1);
glTexCoord2i(1,1);glVertex3i(-1,+1,+1);
glTexCoord2i(0,1);glVertex3i(-1,+1,-1);

//4 faces

glTexCoord2i(1,0);glVertex3i(-1,+1,-1);
glTexCoord2i(1,1);glVertex3i(+1,+1,-1);
glTexCoord2i(0,1);glVertex3i(+1,+1,+1);
glTexCoord2i(0,0);glVertex3i(-1,+1,+1);

//5 faces

glTexCoord2i(1,0);glVertex3i(-1,-1,+1);
glTexCoord2i(1,1);glVertex3i(+1,-1,+1);
glTexCoord2i(0,1);glVertex3i(+1,-1,-1);
glTexCoord2i(0,0);glVertex3i(-1,-1,-1);

//6 faces
glEnd();

glFlush();
}

结果不是很好:

http://shareimage.ro/images/xdkyd12oty44c0qpuo1b.png

立方体的所有面都应具有 4 个正方形的纹理(2 个黑色和 2 个白色)。

我不知道错误在哪里......

4

3 回答 3

5

GL_ARB_texture_rectangle扩展使用维度相关的纹理坐标。使用[0..W]x[0..H]纹理坐标范围,而不是标准化坐标范围 [0..1]x[0..1]。

例如,在四边形上绘制完整图像;

glTexCoord2f(0, 0);                     glVertex3f(...); // top-left
glTexCoord2f(0, imageHeight);           glVertex3f(...); // bottom-left
glTexCoord2f(imageWidth, imageHeight);  glVertex3f(...); // bottom-right
glTexCoord2f(imageWidth, 0);            glVertex3f(...); // top-right

请注意,使用 GL_ARB_texture_rectangle 扩展有几个限制。

  1. 不支持 Mipmap 过滤。
  2. 不支持纹理边框。
  3. 不支持 GL_REPEAT 换行模式。
  4. 不支持调色板纹理。
  5. 纹理坐标由 [0..w]x[0..h] 寻址。

幸运的是,OpenGL 还提供了GL_ARB_texture_non_power_of_two扩展来解决上述限制,同时它仍然支持 NPOT(Non Power Of Two)纹理。

GL_ARB_texture_non_power_of_two 的最大优点是;

  1. GL_ARB_texture_non_power_of_two 使用传统的归一化纹理坐标[0..1]x[0..1]

  2. 它不需要额外的纹理目标标记、用于 glEnable()、glTexImage*D()、 glBindTexture () 等的 GL_TEXTURE_RECTANGLE_ARB。也就是说,您仍然可以像往常一样将 GL_TEXTURE_2D 用于 NPOT 纹理。

于 2012-07-06T14:12:45.093 回答
1

使用纹理矩形,纹理坐标是绝对像素位置。在您的情况下,您的纹理坐标将是 0,0 2,0 2,2 0,2

于 2012-07-06T13:35:09.267 回答
0

它现在正在工作,请参阅下一个:

GLubyte Texture[16] =
{
0,0xFF,0,0xFF, 0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF, 0,0,0,0
};
GLuint Nom;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );   //Efface le framebuffer et le depthbuffer
glMatrixMode(GL_MODELVIEW);     //Un petit gluLookAt()...
glLoadIdentity();//load identity matrix
glTranslatef(0.0f,0.0f,-4.0f);//move forward 4 units
angle  = 0.1 * glutGet ( GLUT_ELAPSED_TIME );
glRotatef(angle,0,2,2);
glEnable(GL_TEXTURE_2D);
glEnable (GL_TEXTURE_RECTANGLE_ARB);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 2);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1,&Nom);  //Génère un n° de texture
glBindTexture(GL_TEXTURE_2D,Nom);   //Sélectionne ce n°
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB,GL_UNSIGNED_BYTE, Texture);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // GL_CLAMP_TO_EDGE
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // GL_CLAMP_TO_EDGE
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 2, 2, 0, GL_BGRA_EXT, GL_UNSIGNED_INT_8_8_8_8_REV, Texture);
glBegin(GL_QUADS);
glTexCoord2i(0,0);glVertex3i(-1,-1,-1);
glTexCoord2i(2,0);glVertex3i(+1,-1,-1);
glTexCoord2i(2,2);glVertex3i(+1,+1,-1);
glTexCoord2i(0,2);glVertex3i(-1,+1,-1);
//1 face
glTexCoord2i(0,0);glVertex3i(-1,-1,+1);
glTexCoord2i(2,0);glVertex3i(+1,-1,+1);
glTexCoord2i(2,2);glVertex3i(+1,+1,+1);
glTexCoord2i(0,2);glVertex3i(-1,+1,+1);
//2 faces
glTexCoord2i(0,0);glVertex3i(+1,-1,-1);
glTexCoord2i(2,0);glVertex3i(+1,-1,+1);
glTexCoord2i(2,2);glVertex3i(+1,+1,+1);
glTexCoord2i(0,2);glVertex3i(+1,+1,-1);
//3 faces
glTexCoord2i(0,0);glVertex3i(-1,-1,-1);
glTexCoord2i(2,0);glVertex3i(-1,-1,+1);
glTexCoord2i(2,2);glVertex3i(-1,+1,+1);
glTexCoord2i(0,2);glVertex3i(-1,+1,-1);
glEnd();
glDisable(GL_TEXTURE_2D);
于 2012-08-04T23:12:04.023 回答