将PNG作为纹理加载并将其结合到球体上
loading a png as a texture and binding it to a sphere
我找不到有关如何加载png文件并将其用作纹理将其绑定到球体的任何体面的教程。是否有库功能可以执行此操作?我将如何将其绑定到球体上?我已经选择了这个,但没有奏效,没有错误,但是质地没有加载到球体上。我在glutmainloop()
之前使用特定文件调用LoadTexture这是我加载文件的代码:
GLuint LoadTexture( const char * filename, int width, int height )
{
GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our PNG file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture ); //generate the texture with
glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE ); //set texture environment parameters
//even better quality, but this will do for now.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR );
//Here we are setting the parameter to repeat the texture
//to the edge of our shape.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT );
//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, data);
free( data ); //free the texture
return texture; //return whether it was successfull
}
这是我创建球的地方
void renderScene(void) {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable( GL_TEXTURE_2D );
// Reset transformations
glLoadIdentity();
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glPushMatrix();
glTranslatef(0.0,2.0,-6);
glRotatef(angle, 0.0f, 2.0, -6.0f);
glutSolidSphere(1,50,50);
glPopMatrix();
angle+=0.4f;
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
我做了什么吗?
您将压缩的PNG数据馈送到OpenGL。必须首先对其进行解压缩,因为OpenGL纹理功能无法理解PNG。您可以使用某些图像库进行解压缩,例如stb_image.c
png是压缩文件,您不能只是读取它们,并期望OpenGL知道如何解码它们。加载PNG的推荐方法是用libpng。
这是使用libpng的一个示例,该示例将在2D数组中同步读取PNG文件中的读数。OpenGL期望一个平坦的1D阵列,因此您需要自己将其弄平,但这很简单。
我意识到有100万个图书馆被扔给您,但我强烈建议土壤。加载PNG就像
一样容易GLuint tex_2d = SOIL_load_OGL_texture( "img.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
我建议使用诸如devil之类的图像加载库,它为您完成所有肮脏的工作。我也建议您使用Modern OpenGL API,但这最终是您的决定:)
相关文章:
- 多态性和功能结合
- 在CToolBar对象中使用PNG时出现问题
- 将 Vulkan 与 SFML 结合使用?
- 从存储为 Windows 资源 (c++) 的 png 中获取 png 文件数据
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 将 c++ 生成器应用程序与外部 dll 结合使用
- 如何将增强MSM与增强信号结合使用?
- MS Visual Studio 解决方案结合了非托管C++项目和 C# 项目
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- 将预编译的 C 共享库与 JNI/NDK 结合使用
- 将 FFMPEG 帧写入 png/jpeg 文件
- 将位图 (bmp) 转换为具有透明度的 png (Windows c++)
- C++ 将函数指针与最佳性能相结合
- 将 Lua 与 C++ DLL 结合使用
- 为什么push_back和emplace_back结合在一起时,会有不同的行为
- 将 aws-iot-device-sdk-cpp-v2 与 CMake 结合使用
- 如何启用libMagick++以保存.png文件格式
- Qt-png图像未显示在部署应用程序中
- 无法在CLion(macOS)上打开带有CImg的.png文件
- 将PNG作为纹理加载并将其结合到球体上