2
votes

Trying to colour terrain points based on texture colour (currently hard coded to vec2(0.5, 0.5) for test purposes - which should be light blue) but all the points are grey. glGetError returns 0 throughout the whole process. I think I might be doing the render process wrong or have a problem with my shaders(?)

Vertex Shader:

void main(){
    gl_Position =  gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment Shader:

uniform sampler2D myTextureSampler;

void main(){
    gl_FragColor = texture2D(myTextureSampler, vec2(0.5, 0.5));
}

Terrain Class:

class Terrain 
{
public:
Terrain(GLuint pProgram, char* pHeightmap, char* pTexture){
    if(!LoadTerrain(pHeightmap))
    {
        OutputDebugString("Loading terrain failed.\n");
    }
    if(!LoadTexture(pTexture))
    {
        OutputDebugString("Loading terrain texture failed.\n");
    }
    mProgram = pProgram;
    mTextureLocation = glGetUniformLocation(pProgram, "myTextureSampler");
};

~Terrain(){};

void Draw()
{
    glEnableClientState(GL_VERTEX_ARRAY); // Uncommenting this causes me to see nothing at all

    glBindBuffer(GL_ARRAY_BUFFER, mVBO);
    glVertexPointer(3, GL_FLOAT, 0, 0);

    glEnable( GL_TEXTURE_2D );
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, mBMP);
    glProgramUniform1i(mProgram, mTextureLocation, 0);

    GLenum a = glGetError();

    glPointSize(5.0f);
    glDrawArrays(GL_POINTS, 0, mNumberPoints);

    a = glGetError();

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glDisable( GL_TEXTURE_2D );
    glDisableClientState(GL_VERTEX_ARRAY); 
}

private:
GLuint mVBO, mBMP, mUV, mTextureLocation, mProgram;
int mWidth;
int mHeight;
int mNumberPoints;

bool LoadTerrain(char* pFile)
{
    /* Definitely no problem here - Vertex data is fine and rendering nice and dandy */
}

// TEXTURES MUST BE POWER OF TWO!!
bool LoadTexture(char *pFile) 
{
    unsigned char header[54]; // Each BMP file begins by a 54-bytes header
    unsigned int dataPos;     // Position in the file where the actual data begins
    unsigned int width, height;
    unsigned int imageSize;   
    unsigned char * data;

    FILE * file = fopen(pFile, "rb");
    if(!file)
        return false;
    if(fread(header, 1, 54, file) != 54)
    {
        fclose(file);
        return false;
    }
    if ( header[0]!='B' || header[1]!='M' )
    {
        fclose(file);
        return false;
    }
    // Read ints from the byte array
    dataPos    = *(int*)&(header[0x0A]);
    imageSize  = *(int*)&(header[0x22]);
    width      = *(int*)&(header[0x12]);
    height     = *(int*)&(header[0x16]);
    // Some BMP files are misformatted, guess missing information
    if (imageSize==0)    imageSize=width*height*3; // 3 : one byte for each Red, Green and Blue component
    if (dataPos==0)      dataPos=54; // The BMP header is done that way
    // Create a buffer
    data = new unsigned char [imageSize];

    // Read the actual data from the file into the buffer
    fread(data,1,imageSize,file);

    //Everything is in memory now, the file can be closed
    fclose(file);

    // Create one OpenGL texture
    glGenTextures(1, &mBMP);

    // "Bind" the newly created texture : all future texture functions will modify this texture
    glBindTexture(GL_TEXTURE_2D, mBMP);

    // Give the image to OpenGL
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

            delete [] data;
            data = 0;

    return true;
}
};
1
Things I would try: Debug your bmp loading test data. You don't even use your dataPos variable, so get rid of it. I'm not sure what that glTexEnvf does, so try commenting it out temporarily. How do you know that no error is occurring? You call glGetError() twice without printing anything. Print it every call. - BWG

1 Answers

3
votes

Answering own question incase anyone has a similar problem:

I had tested this with multiple images - but it turns out theres a bug in my graphics application of choice; which has been exporting 8-bit Bitmap's even though I explicitally told it to export 24-bit Bitmap's. So basically - reverting back to MS Paint solved my solution. 3 cheers for MS Paint.