2014-01-23 56 views
2

试图根据纹理颜色对地形点进行着色(目前硬编码为vec2(0.5,0.5)用于测试目的 - 应该是浅蓝色),但所有点都是灰色的。 glGetError在整个过程中返回0。我想我可能会做渲染过程中错误或与我的着色问题OpenGL纹理,没有错误,但灰色

的Vertex Shader(?):

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

片段着色器:

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

我会尝试的事情:调试你的bmp加载测试数据。你甚至不用你的'dataPos'变量,所以摆脱它。我不确定'glTexEnvf'是什么,所以请暂时将它评论出来。你怎么知道没有错误发生?你打印两次'glGetError()'而不打印任何东西。打印每一个电话。 – BWG

回答

3

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

我已经测试过多个图像 - 但事实证明这是我选择的图形应用程序中的一个错误;即使我明确地告诉它导出24位位图,它已经导出了8位位图。所以基本上 - 恢复到MS Paint解决了我的解决方案。 MS Paint的3张欢呼声。