Hat ich grad vor, mal sehn was sich machen lässt ^^
*** Edit ***
wieso machst du eignetlich so oft so viel final?
*** Edit ***
so, es funzt jetzt, aber die maus hat tatsächlich krasse Probleme. Ich weiss dieses kskb hat sehr viele performance technische
fehler, aber man merkt trozdem das die maus einfach nur rumlaggt… versuche gerade zu verstehen warum…
package de.skysoldier.lwjgltest;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;
import de.skysoldier.abstractgl.AGL;
import de.skysoldier.abstractgl.AGLCaps.AGLDisplayCap;
public class Cube {
private final int VERTEX_LOCATION = 0;
private final int TEXTURE_LOCATION = 1;
private int shaderProgramID;
private int vertexArrayObjectID;
private int vertexBufferObjectID;
private int textureID;
private Matrix4f projection;
public Cube(){
initDisplay();
initTexture();
initShaders();
initGl();
startGameLoop();
}
private void initDisplay(){
AGL.createLwjglContext(AGLDisplayCap.NONE);
}
private void initTexture(){
BufferedImage source = null;
try{
source = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic.png"));
}
catch(Exception e){
}
IntBuffer buffer = ByteBuffer.allocateDirect(4).asIntBuffer();
GL11.glGenTextures(buffer);
final BufferedImage textureImage;
int textureWidth = 2;
int textureHeight = 2;
while (textureWidth < source.getWidth())
textureWidth *= 2;
while (textureHeight < source.getHeight())
textureHeight *= 2;
textureID = buffer.get();
textureImage = new BufferedImage(textureWidth, textureHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D graphics = (Graphics2D) textureImage.getGraphics();
graphics.drawImage(source, 0, 0, null);
graphics.dispose();
byte[] data = (byte[]) textureImage.getRaster().getDataElements(0, 0, textureWidth, textureHeight, null);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(textureImage.getColorModel().getPixelSize() / 8 * textureWidth * textureHeight);
byteBuffer.put(data);
byteBuffer.rewind();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, textureWidth, textureHeight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, byteBuffer);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
}
private void initShaders(){
String vertexShaderCode =
"#version 120
"+
"uniform sampler2D tex;
"+
"attribute vec2 vertTexCoord;
"+
"varying vec2 fragTexCoord;
"+
"uniform mat4 camera;
"+
"uniform mat4 model;
"+
"attribute vec3 vert;
"+
"void main() {
"+
" fragTexCoord = vertTexCoord;
"+
" gl_Position = camera * model * vec4(vert, 1);
"+
"}";
String fragmentShaderCode =
"#version 120
"+
"uniform sampler2D fragTex;
"+
"varying vec2 fragTexCoord;
"+
"void main(void){
"+
" gl_FragColor = texture2D(fragTex, fragTexCoord);
"+
"}";
int vertexShaderID = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
GL20.glShaderSource(vertexShaderID, vertexShaderCode);
GL20.glCompileShader(vertexShaderID);
int fragmentShaderID = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
GL20.glShaderSource(fragmentShaderID, fragmentShaderCode);
GL20.glCompileShader(fragmentShaderID);
shaderProgramID = GL20.glCreateProgram();
GL20.glAttachShader(shaderProgramID, vertexShaderID);
GL20.glAttachShader(shaderProgramID, fragmentShaderID);
GL20.glBindAttribLocation(shaderProgramID, VERTEX_LOCATION, "vert");
GL20.glBindAttribLocation(shaderProgramID, TEXTURE_LOCATION, "vertTexCoord");
GL20.glLinkProgram(shaderProgramID);
GL20.glDetachShader(shaderProgramID, vertexShaderID);
GL20.glDetachShader(shaderProgramID, fragmentShaderID);
GL20.glUseProgram(shaderProgramID);
float fov = 45f, aspect = 800.0f / 600.0f, zNear = 1f, zFar = 1000.0f;
final double f = (1.0 / Math.tan(Math.toRadians(fov / 2.0)));
projection = new Matrix4f();
projection.m00 = (float) (f / aspect);
projection.m11 = (float) f;
projection.m22 = (zFar + zNear) / (zNear - zFar);
projection.m23 = -1;
projection.m32 = (2 * zFar + zNear) / (zNear - zFar);
projection.m33 = 0;
// System.out.println(projection);
// System.exit(0);
int compileStatus = GL20.glGetShaderi(vertexShaderID, GL20.GL_COMPILE_STATUS);
if (compileStatus == GL11.GL_FALSE) {
int infoLogLength = GL20.glGetShaderi(vertexShaderID, GL20.GL_INFO_LOG_LENGTH);
String infoLog = GL20.glGetShaderInfoLog(vertexShaderID, infoLogLength);
throw new RuntimeException(infoLog);
}
System.out.println("Compile Errors: " + GL20.glGetProgramInfoLog(shaderProgramID, GL20.glGetProgrami(shaderProgramID, GL20.GL_INFO_LOG_LENGTH)));
}
private void initGl(){
float vertices[] = new float[]{
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f,-1.0f, 1.0f, 0.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 0.0f,
1.0f, 1.0f,-1.0f, 1.0f, 0.0f,
0.0f, 3.0f, 0.0f, 0.5f, 1.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 0.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 0.0f,
0.0f, 3.0f, 0.0f, 0.5f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 3.0f, 0.0f, 0.5f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f,-1.0f, 0.0f, 0.0f,
0.0f, 3.0f, 0.0f, 0.5f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f
};
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
verticesBuffer.put(vertices);
verticesBuffer.flip();
vertexArrayObjectID = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vertexArrayObjectID);
vertexBufferObjectID = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferObjectID);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);
GL20.glEnableVertexAttribArray(VERTEX_LOCATION);
GL20.glVertexAttribPointer(VERTEX_LOCATION, 3, GL11.GL_FLOAT, false, 5 * Float.SIZE / 8, 0);
GL20.glEnableVertexAttribArray(TEXTURE_LOCATION);
GL20.glVertexAttribPointer(TEXTURE_LOCATION, 2, GL11.GL_FLOAT, true, 5 * Float.SIZE / 8, 3 * Float.SIZE / 8);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LESS);
}
private void startGameLoop(){
long lastFrame = System.nanoTime();
long thisFrame = System.nanoTime();
long lastTime = System.nanoTime();
ArrayList<Long> fpsCollection = new ArrayList<>();
long delta;
int mid = 0, min = Integer.MAX_VALUE, max = 0;
try{
Mouse.setGrabbed(true);
}
catch(Exception e){
e.printStackTrace();
}
while(!Display.isCloseRequested()){
thisFrame = System.nanoTime();
delta = thisFrame - lastFrame;
lastFrame = thisFrame;
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(0, 0, 0, 1);
GL20.glUseProgram(shaderProgramID);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL20.glUniform1i(GL20.glGetUniformLocation(shaderProgramID, "tex"), 0);
updateCamera(delta);
GL30.glBindVertexArray(vertexArrayObjectID);
for(int i = 0; i < 50; i++){
for(int j = 0; j < 50; j++){
GL20.glUniformMatrix4(GL20.glGetUniformLocation(shaderProgramID, "model"), false, testTranslate(2 * i + i * 0.5f, -5.0f, 2 * j + j * 0.5f, 0, delta));
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 18);
GL20.glUniformMatrix4(GL20.glGetUniformLocation(shaderProgramID, "model"), false, testTranslate(2 * i + i * 0.5f, 5.0f, 2 * j + j * 0.5f, 180, delta));
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 18);
}
}
double fpsRaw = (1e9 / delta);
int fps = (int) fpsRaw;
if(min != 0 && fps < min){
min = fps;
}
if(fps > max){
max = fps;
}
if(thisFrame / 1e9 - lastTime / 1e9 >= 1){
lastTime = thisFrame;
fpsCollection.add((long) fps);
mid = calcMid(fpsCollection);
}
Display.setTitle("mid: " + mid + " | min: " + min + " | max: " + max + " | now: " + fps + "");
Display.update();
}
destroy();
}
private float pitch;
private float yaw;
private float posX, posY, posZ;
public void updateCamera(float delta){
final double step = 10 * delta / 1e9;
final double keySpeed = step;
final double mouseSpeed = step * 0.04;
final float rotVmax = 1;
final float rotVMin = -1;
yaw += Mouse.getDX() * mouseSpeed;
pitch -= Mouse.getDY() * mouseSpeed;
if (pitch > rotVmax)
pitch = rotVmax;
if (pitch < rotVMin)
pitch = rotVMin;
if(Keyboard.isKeyDown(Keyboard.KEY_W)){
posX -= Math.sin(yaw) * keySpeed;
posZ += Math.cos(yaw) * keySpeed;
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
posX += Math.sin(yaw) * keySpeed;
posZ -= Math.cos(yaw) * keySpeed;
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
posX += Math.cos(yaw) * keySpeed;
posZ += Math.sin(yaw) * keySpeed;
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
posX -= Math.cos(yaw) * keySpeed;
posZ -= Math.sin(yaw) * keySpeed;
}
float rotX = (float) (Math.cos(yaw) * pitch);
float rotZ = (float) (Math.sin(yaw) * pitch);
Matrix4f mv = new Matrix4f();
final double sinX = Math.sin(rotX);
final double sinY = Math.sin(yaw);
final double sinZ = Math.sin(rotZ);
final double cosX = Math.cos(rotX);
final double cosY = Math.cos(yaw);
final double cosZ = Math.cos(rotZ);
mv.m00 = (float) (cosY * cosZ + sinY * sinX * sinZ);
mv.m01 = (float) (cosX * sinZ);
mv.m02 = (float) (-sinY * cosZ + cosY * sinX * sinZ);
mv.m10 = (float) (-cosY * sinZ + sinY * sinX * cosZ);
mv.m11 = (float) (cosX * cosZ);
mv.m12 = (float) (sinY * sinZ + cosY * sinX * cosZ);
mv.m20 = (float) (sinY * cosX);
mv.m21 = (float) (-sinX);
mv.m22 = (float) (cosY * cosX);
mv.m30 = mv.m00 * posX + mv.m10 * posY + mv.m20 * posZ;
mv.m31 = mv.m01 * posX + mv.m11 * posY + mv.m21 * posZ;
mv.m32 = mv.m02 * posX + mv.m12 * posY + mv.m22 * posZ;
mv.m33 = 1;
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
Matrix4f.mul(projection, mv, new Matrix4f()).store(buffer);
buffer.flip();
GL20.glUniformMatrix4(GL20.glGetUniformLocation(shaderProgramID, "camera"), false, buffer);
}
public FloatBuffer testTranslate(float x, float y, float z, float rot, long delta){
Matrix4f mat = new Matrix4f();
mat.translate(new Vector3f(x, y, z));
mat.rotate((float) Math.toRadians(rot), new Vector3f(1.0f, 0.0f, 0.0f));
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
mat.store(buffer);
buffer.flip();
return buffer;
}
public int calcMid(ArrayList<Long> fpsCollection){
long lm = 0;
for(long l : fpsCollection){
lm+=l;
}
return (int) lm / fpsCollection.size();
}
private void destroy(){
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glDeleteBuffers(vertexBufferObjectID);
GL30.glBindVertexArray(0);
GL30.glDeleteVertexArrays(vertexArrayObjectID);
Display.destroy();
}
public static void main(String[] args) {
// System.setProperty("org.lwjgl.util.Debug", "true");
new Cube();
}
}```