OpenGL纹理,没有错误,但灰色

时间:2014-01-23 03:08:06

标签: c++ opengl glsl shader terrain

尝试根据纹理颜色为地形点着色(目前硬编码为vec2(0.5,0.5)用于测试目的 - 应该是浅蓝色)但所有点都是灰色的。 glGetError在整个过程中返回0。我想我可能会在渲染过程中出错或者我的着色器出现问题(?)

顶点着色器:

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

Fragment Shader:

uniform sampler2D myTextureSampler;

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

地形等级:

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 个答案:

答案 0 :(得分:3)

回答自己的问题,任何人都有类似的问题:

我用多张图片对此进行了测试 - 但事实证明这是我选择的图形应用程序中的一个错误;虽然我明确告诉它导出24位Bitmap,但它一直在导出8位Bitmap。所以基本上 - 恢复到MS Paint解决了我的解决方案。 MS Paint的3次欢呼。