glTexCoordPointer 输出不符合预期

glTexCoordPointer output not as expected

我正在尝试使用 glDrawElementsglTexCoordPointerglTexImage1D 创建一维纹理、渲染表面数据集并根据顶点的高度值对顶点着色。

GLuint tex = 0;

我的纹理生成:

glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_1D, tex );
unsigned char pixels[] =
{
    255, 0, 0, 255,
    0, 255, 0, 255,
    0, 0, 255, 255,
};
glTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, 3, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

vArray是一个数组,存储渲染的顶点。

float vArray[12] = {  
   0.0, 1.0, 0.0,
   0.0, 3.0, 1.0,
   1.0, 2.0, 0.0,
   1.0, 1.0, 1.0,
}

iArray 是一个存储渲染索引的数组。

int iSize = 6;
int iArray[6] = {  
   0, 2, 1,
   2, 1, 4,
}

tArray是一个存储归一化高度的数组。

GLfloat tArray[4] = {  
   0.0, 0.3, 1.0, 0.0,
}

我的渲染代码:

  glEnable(GL_TEXTURE_1D);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glBindTexture(GL_TEXTURE_1D, tex);
  glTexCoordPointer(1, GL_FLOAT, sizeof(GL_FLOAT), tArray);

  glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(3 ,GL_FLOAT,0, vArray);    

  glDrawElements(GL_TRIANGLES, iSize, GL_UNSIGNED_INT, iArray);

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  glDisable(GL_TEXTURE_1D);

最后的输出不是我想的那样,希望有人能指出我的错误。

  • 回复:您的 glTexCoordPointer() 调用:Tightly-packed 数组通常使用 0 作为 stride
  • vArraytArray 只有四个元素,而不是五个,因此 iArray 中的 4 将导致 OpenGL 将这些数组的末尾读入垃圾(如果你很幸运)或段错误(如果你不是)。

总计:

#include <GL/glew.h>
#include <GL/glut.h>

GLuint tex = 0;
void display()
{
    glClear( GL_COLOR_BUFFER_BIT );

    float vArray[] =
    {
       -0.5, -0.5,
        0.5, -0.5,
        0.5,  0.5,
       -0.5,  0.5,
    };

    GLfloat tArray[] =
    {
       0.0, 0.3, 1.0, 0.0,
    };

    unsigned int iArray[] =
    {
       0, 1, 2,
       2, 3, 0,
    };

    glEnable( GL_TEXTURE_1D );
    glBindTexture( GL_TEXTURE_1D, tex );
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
    glTexCoordPointer( 1, GL_FLOAT, 0, tArray );

    glEnableClientState( GL_VERTEX_ARRAY );
    glVertexPointer( 2, GL_FLOAT, 0, vArray );

    glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, iArray );

    glDisableClientState( GL_TEXTURE_COORD_ARRAY );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisable( GL_TEXTURE_1D );

    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 400, 400 );
    glutCreateWindow( "GLUT" );
    glewInit();

    glGenTextures( 1, &tex );
    glBindTexture( GL_TEXTURE_1D, tex );
    unsigned char pixels[] =
    {
        255, 0, 0, 255,
        0, 255, 0, 255,
        0, 0, 255, 255,
    };
    glTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, 3, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels );
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
    glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}

编辑:二维版本:​​

#include <GL/glew.h>
#include <GL/glut.h>

GLuint tex = 0;
void display()
{
    glClear( GL_COLOR_BUFFER_BIT );

    float vArray[] =
    {
       -0.5, -0.5,
        0.5, -0.5,
        0.5,  0.5,
       -0.5,  0.5,
    };

    GLfloat tArray[] =
    {
       0.0, 0.0,
       1.0, 0.0,
       1.0, 1.0,
       0.0, 1.0,
    };

    unsigned int iArray[] =
    {
       0, 1, 2,
       2, 3, 0,
    };

    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, tex );
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
    glTexCoordPointer( 2, GL_FLOAT, 0, tArray );

    glEnableClientState( GL_VERTEX_ARRAY );
    glVertexPointer( 2, GL_FLOAT, 0, vArray );

    glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, iArray );

    glDisableClientState( GL_TEXTURE_COORD_ARRAY );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisable( GL_TEXTURE_2D );

    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 400, 400 );
    glutCreateWindow( "GLUT" );
    glewInit();

    glGenTextures( 1, &tex );
    glBindTexture( GL_TEXTURE_2D, tex );
    unsigned char pixels[] =
    {
        255, 0, 0, 255,
        0, 255, 0, 255,
        0, 0, 255, 255,
        0, 0, 255, 255,
    };
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels );
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
    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_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}