2014-01-23 2 views
2

텍스처 색상을 기반으로 지형 포인트를 색칠하려고합니다 (현재 테스트 용으로 vec2 (0.5, 0.5)로 하드 코딩 됨 - 밝은 파란색이어야 함). 그러나 모든 포인트는 회색입니다. glGetError는 전체 프로세스에서 0을 반환합니다. 나는 내가 잘못 과정을 렌더링하거나 내 쉐이더에 문제가있는 일을 할 수 있다고 생각OpenGL 텍스처링, 오류는 아니지만 회색

버텍스 쉐이더 (?) :

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

누구나 비슷한 문제가있는 경우 자신의 질문에 답하십시오.

여러 이미지로 테스트 해 보았습니다.하지만 그래픽 응용 프로그램의 버그가 있습니다. 8 비트 비트 맵을 내보내고있다. 명시 적으로 24 비트 비트 맵을 내보내라고했지만. 그래서 기본적으로 - MS Paint로 돌아와서 내 솔루션을 해결했습니다. 미스터 페인트를위한 3 건배.