glTexCoordPointer output not as expected

208 Views Asked by At

I am trying to use glDrawElements , glTexCoordPointer and glTexImage1D to create a 1D texture, render a surface dataset and color the vertices according to their height value.

GLuint tex = 0;

My texture generation:

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 is an array which stores the vertices for rendering.

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 is an array which stores the indices for rendering.

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

tArray is an array which stores the normalised heights.

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

My render code:

  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);

The final output is not as I expected, hoping somebody can point out my mistakes.

1

There are 1 best solutions below

3
On BEST ANSWER
  • RE: your glTexCoordPointer() call: Tightly-packed arrays usually use 0 for stride.
  • vArray and tArray only have four elements, not five, so the 4 in iArray will cause OpenGL to read off the ends of those arrays into garbage (if you're lucky) or segfaults (if you're not).

All together:

#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;
}

EDIT: 2D version:

#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;
}