Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL纹理,没有错误,只有灰色_C++_Opengl_Glsl_Shader_Terrain - Fatal编程技术网

C++ OpenGL纹理,没有错误,只有灰色

C++ OpenGL纹理,没有错误,只有灰色,c++,opengl,glsl,shader,terrain,C++,Opengl,Glsl,Shader,Terrain,尝试根据纹理颜色为地形点上色(出于测试目的,当前硬编码为vec2(0.5,0.5)-应为浅蓝色),但所有点均为灰色。glGetError在整个过程中返回0。我认为渲染过程可能出错,或者着色器有问题(?) 顶点着色器: void main(){ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } 片段着色器: uniform sampler2D myTextureSampler; void main(){ gl_

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

顶点着色器:

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

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


我已经用多幅图像对此进行了测试,但结果表明,我选择的图形应用程序中存在一个缺陷;它一直在导出8位位图,尽管我明确告诉它要导出24位位图。因此,基本上,回到MS Paint解决了我的问题。为MS Paint干杯。

我会尝试的事情:调试bmp加载测试数据。您甚至不使用
dataPos
变量,所以请去掉它。我不确定那
glTexEnvf
是做什么的,所以试着暂时注释一下。您如何知道没有发生错误?调用
glGetError()
两次,但不打印任何内容。每次打电话都打印出来。