Texture: allow png/jpg to be used interchangeably
This commit is contained in:
@@ -37,7 +37,7 @@ public class SplashScene extends Scene {
|
||||
private void createLogo() {
|
||||
Shader texturedShader = new Shader("assets/shaders/texture");
|
||||
texturedShader.init();
|
||||
Texture logoTexture = new Texture(225, 225, "assets/textures/plink.jpg", 3);
|
||||
Texture logoTexture = new Texture(225, 225, "assets/textures/plink.png");
|
||||
logoTexture.init();
|
||||
Mesh logoMesh =
|
||||
new TexturedMesh(texturedShader, logoTexture, logoRectVertices, screenCoverRectElements);
|
||||
|
||||
@@ -13,15 +13,40 @@ import lombok.Setter;
|
||||
public class Texture {
|
||||
@Getter private int width;
|
||||
@Getter private int height;
|
||||
@Getter private int channelCount;
|
||||
@Getter private String texturePath;
|
||||
@Getter @Setter private int textureID;
|
||||
@Getter @Setter private int channelCount;
|
||||
@Getter @Setter private int internalFormat;
|
||||
|
||||
public Texture(int width, int height, String texturePath, int channelCount) {
|
||||
public Texture(int width, int height, String texturePath) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.texturePath = texturePath;
|
||||
this.channelCount = channelCount;
|
||||
guessChannelCountAndInternalFormat();
|
||||
}
|
||||
|
||||
private void guessChannelCountAndInternalFormat() {
|
||||
int fileExtensionIndex = getTexturePath().lastIndexOf(".");
|
||||
if (fileExtensionIndex == -1) {
|
||||
// No file extension, so just assume 4 channels (.png -> RGBA)
|
||||
setChannelCount(4);
|
||||
setInternalFormat(GL_RGBA);
|
||||
return;
|
||||
}
|
||||
|
||||
String fileExtension = getTexturePath().substring(fileExtensionIndex + 1);
|
||||
switch (fileExtension) {
|
||||
case "png":
|
||||
setChannelCount(4);
|
||||
setInternalFormat(GL_RGBA);
|
||||
case "jpg":
|
||||
setChannelCount(3);
|
||||
setInternalFormat(GL_RGB);
|
||||
default:
|
||||
// just pray at this point
|
||||
setChannelCount(3);
|
||||
setInternalFormat(GL_RGB);
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@@ -33,7 +58,11 @@ public class Texture {
|
||||
private ByteBuffer loadImageBytes() {
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
return stbi_load(
|
||||
getTexturePath(), new int[getWidth()], new int[getHeight()], new int[getChannelCount()], 0);
|
||||
getTexturePath(),
|
||||
new int[getWidth()],
|
||||
new int[getHeight()],
|
||||
new int[getChannelCount()],
|
||||
getChannelCount());
|
||||
}
|
||||
|
||||
private void createTexture(ByteBuffer imageBytes) {
|
||||
@@ -46,7 +75,15 @@ public class Texture {
|
||||
setupWrapParameters();
|
||||
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, GL_RGB, getWidth(), getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, imageBytes);
|
||||
GL_TEXTURE_2D,
|
||||
0,
|
||||
getInternalFormat(),
|
||||
getWidth(),
|
||||
getHeight(),
|
||||
0,
|
||||
getInternalFormat(),
|
||||
GL_UNSIGNED_BYTE,
|
||||
imageBytes);
|
||||
// glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user