java - Images showing in weird colors
问题描述
I am creating a new 2d game engine in lwjgl 3 using OpenGL to try out lwjgl 3 as this is the first project I'm using lwjgl 3 in all my other projects have had lwjgl 2. But when I render an image using that new engine all the colors change and some don't even show up.
Code for loading textures
public Texture(String filename){
IntBuffer width = BufferUtils.createIntBuffer(1);
IntBuffer height = BufferUtils.createIntBuffer(1);
IntBuffer comp = BufferUtils.createIntBuffer(1);
ByteBuffer data = stbi_load(filename, width, height, comp, 4);
id = glGenTextures();
this.width = width.get();
this.height = height.get();
glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.height, 0, GL_RGBA, GL_BYTE, data);
stbi_image_free(data);
}
If you know how i could this prevent from happening please let me know.
解决方案
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.height, 0, GL_RGBA, GL_BYTE, data); ^^^^^^^
GL_BYTE
是有符号的8 位整数类型,因此所有大于 127 的值最终都将被解释为负值(假设您的平台至少使用标准二进制补码表示)并被限制为 0。
只需使用GL_UNSIGNED_BYTE
.
推荐阅读
- c++ - Yolo 训练 API(暗网)
- autodesk-forge - 发送 api 活动请求时出现“未为帐户启用路由”错误
- reactjs - React HighCharts 自定义图例
- css - 如何隐藏 v-text-field 键入日期的图标
- c++ - 为 LeetCode 问题 Max Frequency Stack 获取未对齐地址错误的引用绑定
- kubernetes - Kubespray 和 Ansible,如何分离配置(即清单和其他配置文件)
- python - 在 matplotlib 散点图中添加过滤
- perl - 在@INC 中找不到 MIME/Types.pm
- apache-kafka - Apache Kafka 的 Spring 中的侦听器容器是什么?
- xcodebuild - xcdebuild ios14 与 googleMap 框架构建