gpt4 book ai didi

c++ - OpenGL Texturing,没有错误但是灰色

转载 作者:搜寻专家 更新时间:2023-10-31 01:45:59 26 4
gpt4 key购买 nike

尝试根据纹理颜色为地形点着色(目前硬编码为 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 欢呼 3 次。

关于c++ - OpenGL Texturing,没有错误但是灰色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21298585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com