mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 10:25:06 -04:00
rendering: convert more files to kotlin
This commit is contained in:
parent
34c0f5aa31
commit
4ad8c4933c
@ -2,10 +2,15 @@ package de.bixilon.minosoft.gui.rendering.hud.elements
|
||||
|
||||
class RenderStats {
|
||||
var fpsLastSecond = -1
|
||||
private set
|
||||
var minFrameTime = Long.MAX_VALUE
|
||||
private set
|
||||
var maxFrameTime = 0L
|
||||
private set
|
||||
var avgFrameTime = 0L
|
||||
private set
|
||||
var frames = 0L
|
||||
private set
|
||||
|
||||
private var lastFPSCalcTime = 0L
|
||||
private var framesLastSecond = 0
|
||||
|
@ -66,7 +66,6 @@ class HUDDebugScreenElement(private val hudTextElement: HUDTextElement) : HUDTex
|
||||
return nanoToMillis1d(hudTextElement.renderWindow.renderStats.maxFrameTime)
|
||||
}
|
||||
|
||||
|
||||
private fun getUsedMemory(): Long {
|
||||
return runtime.totalMemory() - runtime.freeMemory()
|
||||
}
|
||||
|
@ -1,92 +0,0 @@
|
||||
package de.bixilon.minosoft.gui.rendering.shader;
|
||||
|
||||
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException;
|
||||
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil;
|
||||
import glm_.mat4x4.Mat4;
|
||||
import glm_.vec3.Vec3;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.ARBFragmentShader;
|
||||
import org.lwjgl.opengl.ARBShaderObjects;
|
||||
import org.lwjgl.opengl.ARBVertexShader;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
import static org.lwjgl.system.MemoryUtil.NULL;
|
||||
|
||||
public class Shader {
|
||||
private static Shader usedShader;
|
||||
private final String vertex;
|
||||
private final String fragment;
|
||||
private int programId;
|
||||
|
||||
public Shader(String vertex, String fragment) {
|
||||
this.vertex = vertex;
|
||||
this.fragment = fragment;
|
||||
}
|
||||
|
||||
public int load() throws ShaderLoadingException, IOException {
|
||||
int vertexShader = ShaderUtil.createShader(this.vertex, ARBVertexShader.GL_VERTEX_SHADER_ARB);
|
||||
int fragmentShader = ShaderUtil.createShader(this.fragment, ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
|
||||
|
||||
this.programId = ARBShaderObjects.glCreateProgramObjectARB();
|
||||
|
||||
if (this.programId == NULL) {
|
||||
throw new ShaderLoadingException();
|
||||
}
|
||||
|
||||
ARBShaderObjects.glAttachObjectARB(this.programId, vertexShader);
|
||||
ARBShaderObjects.glAttachObjectARB(this.programId, fragmentShader);
|
||||
|
||||
ARBShaderObjects.glLinkProgramARB(this.programId);
|
||||
if (ARBShaderObjects.glGetObjectParameteriARB(this.programId, ARBShaderObjects.GL_OBJECT_LINK_STATUS_ARB) == GL11.GL_FALSE) {
|
||||
throw new ShaderLoadingException(OpenGLUtil.getLogInfo(this.programId));
|
||||
}
|
||||
|
||||
ARBShaderObjects.glValidateProgramARB(this.programId);
|
||||
if (ARBShaderObjects.glGetObjectParameteriARB(this.programId, ARBShaderObjects.GL_OBJECT_VALIDATE_STATUS_ARB) == GL11.GL_FALSE) {
|
||||
throw new ShaderLoadingException(OpenGLUtil.getLogInfo(this.programId));
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
return this.programId;
|
||||
}
|
||||
|
||||
public int getProgramId() {
|
||||
return this.programId;
|
||||
}
|
||||
|
||||
public Shader use() {
|
||||
if (usedShader != this) {
|
||||
glUseProgram(this.programId);
|
||||
usedShader = this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getUniformLocation(String variableName) {
|
||||
return glGetUniformLocation(this.programId, variableName);
|
||||
}
|
||||
|
||||
public void setFloat(String name, float value) {
|
||||
glUniform1f(getUniformLocation(name), value);
|
||||
}
|
||||
|
||||
public void setInt(String name, int value) {
|
||||
glUniform1i(getUniformLocation(name), value);
|
||||
}
|
||||
|
||||
public void set4f(String variableName, float[] floats) {
|
||||
glUniformMatrix4fv(getUniformLocation(variableName), false, floats);
|
||||
}
|
||||
|
||||
public void setMat4(String variableName, Mat4 mat4) {
|
||||
glUniformMatrix4fv(getUniformLocation(variableName), false, mat4.to(BufferUtils.createFloatBuffer(16)));
|
||||
}
|
||||
|
||||
public void setVec3(String name, Vec3 vec3) {
|
||||
glUniform3f(getUniformLocation(name), vec3.x, vec3.y, vec3.z);
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package de.bixilon.minosoft.gui.rendering.shader
|
||||
|
||||
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException
|
||||
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil
|
||||
import glm_.mat4x4.Mat4
|
||||
import glm_.vec3.Vec3
|
||||
import org.lwjgl.BufferUtils
|
||||
import org.lwjgl.opengl.ARBFragmentShader.GL_FRAGMENT_SHADER_ARB
|
||||
import org.lwjgl.opengl.ARBShaderObjects.*
|
||||
import org.lwjgl.opengl.ARBVertexShader.GL_VERTEX_SHADER_ARB
|
||||
import org.lwjgl.opengl.GL11.GL_FALSE
|
||||
import org.lwjgl.opengl.GL20.*
|
||||
import org.lwjgl.system.MemoryUtil
|
||||
|
||||
class Shader(private val vertexPath: String, private val fragmentPath: String) {
|
||||
private var programId = 0
|
||||
|
||||
fun load(): Int {
|
||||
val vertexShader = ShaderUtil.createShader(vertexPath, GL_VERTEX_SHADER_ARB)
|
||||
val fragmentShader = ShaderUtil.createShader(fragmentPath, GL_FRAGMENT_SHADER_ARB)
|
||||
programId = glCreateProgramObjectARB()
|
||||
|
||||
if (programId.toLong() == MemoryUtil.NULL) {
|
||||
throw ShaderLoadingException()
|
||||
}
|
||||
|
||||
glAttachObjectARB(programId, vertexShader)
|
||||
glAttachObjectARB(programId, fragmentShader)
|
||||
glLinkProgramARB(programId)
|
||||
|
||||
if (glGetObjectParameteriARB(programId, GL_OBJECT_LINK_STATUS_ARB) == GL_FALSE) {
|
||||
throw ShaderLoadingException(OpenGLUtil.getLogInfo(programId))
|
||||
}
|
||||
|
||||
glValidateProgramARB(programId)
|
||||
|
||||
if (glGetObjectParameteriARB(programId, GL_OBJECT_VALIDATE_STATUS_ARB) == GL_FALSE) {
|
||||
throw ShaderLoadingException(OpenGLUtil.getLogInfo(programId))
|
||||
}
|
||||
glDeleteShader(vertexShader)
|
||||
glDeleteShader(fragmentShader)
|
||||
|
||||
return programId
|
||||
}
|
||||
|
||||
fun use(): Shader {
|
||||
if (usedShader !== this) {
|
||||
glUseProgram(programId)
|
||||
usedShader = this
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
private fun getUniformLocation(variableName: String): Int {
|
||||
return glGetUniformLocation(programId, variableName)
|
||||
}
|
||||
|
||||
fun setFloat(name: String, value: Float) {
|
||||
glUniform1f(getUniformLocation(name), value)
|
||||
}
|
||||
|
||||
fun setInt(name: String, value: Int) {
|
||||
glUniform1i(getUniformLocation(name), value)
|
||||
}
|
||||
|
||||
fun set4f(variableName: String, floats: FloatArray) {
|
||||
glUniform4f(getUniformLocation(variableName), floats[0], floats[1], floats[2], floats[3])
|
||||
}
|
||||
|
||||
fun setMat4(variableName: String, mat4: Mat4) {
|
||||
glUniformMatrix4fv(getUniformLocation(variableName), false, mat4 to BufferUtils.createFloatBuffer(16))
|
||||
}
|
||||
|
||||
fun setVec3(name: String, vec3: Vec3) {
|
||||
glUniform3f(getUniformLocation(name), vec3.x, vec3.y, vec3.z)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private var usedShader: Shader? = null
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package de.bixilon.minosoft.gui.rendering.shader;
|
||||
|
||||
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException;
|
||||
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil;
|
||||
import de.bixilon.minosoft.util.Util;
|
||||
import org.lwjgl.opengl.ARBShaderObjects;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.lwjgl.opengl.ARBShaderObjects.*;
|
||||
import static org.lwjgl.system.MemoryUtil.NULL;
|
||||
|
||||
|
||||
public class ShaderUtil {
|
||||
public static int createShader(String shaderPath, int shaderType) throws ShaderLoadingException, IOException {
|
||||
int shaderId = glCreateShaderObjectARB(shaderType);
|
||||
|
||||
if (shaderId == NULL) {
|
||||
throw new ShaderLoadingException();
|
||||
}
|
||||
|
||||
glShaderSourceARB(shaderId, Util.readAssetResource("rendering/shader/" + shaderPath));
|
||||
glCompileShaderARB(shaderId);
|
||||
|
||||
if (glGetObjectParameteriARB(shaderId, ARBShaderObjects.GL_OBJECT_COMPILE_STATUS_ARB) == GL11.GL_FALSE) {
|
||||
throw new ShaderLoadingException(OpenGLUtil.getLogInfo(shaderId));
|
||||
}
|
||||
|
||||
return shaderId;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package de.bixilon.minosoft.gui.rendering.shader
|
||||
|
||||
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException
|
||||
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil
|
||||
import de.bixilon.minosoft.util.Util
|
||||
import org.lwjgl.opengl.ARBShaderObjects.*
|
||||
import org.lwjgl.opengl.GL11.GL_FALSE
|
||||
import org.lwjgl.system.MemoryUtil
|
||||
|
||||
object ShaderUtil {
|
||||
fun createShader(shaderPath: String, shaderType: Int): Int {
|
||||
val shaderId = glCreateShaderObjectARB(shaderType)
|
||||
if (shaderId.toLong() == MemoryUtil.NULL) {
|
||||
throw ShaderLoadingException()
|
||||
}
|
||||
|
||||
glShaderSourceARB(shaderId, Util.readAssetResource("rendering/shader/$shaderPath"))
|
||||
glCompileShaderARB(shaderId)
|
||||
|
||||
if (glGetObjectParameteriARB(shaderId, GL_OBJECT_COMPILE_STATUS_ARB) == GL_FALSE) {
|
||||
throw ShaderLoadingException(OpenGLUtil.getLogInfo(shaderId))
|
||||
}
|
||||
|
||||
return shaderId
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package de.bixilon.minosoft.gui.rendering.util;
|
||||
|
||||
import org.lwjgl.opengl.ARBShaderObjects;
|
||||
|
||||
public final class OpenGLUtil {
|
||||
public static String getLogInfo(int obj) {
|
||||
return ARBShaderObjects.glGetInfoLogARB(obj, ARBShaderObjects.glGetObjectParameteriARB(obj, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB));
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package de.bixilon.minosoft.gui.rendering.util
|
||||
|
||||
import org.lwjgl.opengl.ARBShaderObjects.*
|
||||
|
||||
object OpenGLUtil {
|
||||
fun getLogInfo(`object`: Int): String {
|
||||
return glGetInfoLogARB(`object`, glGetObjectParameteriARB(`object`, GL_OBJECT_INFO_LOG_LENGTH_ARB))
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user