0
我正在嘗試使用libgdx或更好的OpenGL。因爲我的演講,我不得不面對Java。libgdx通過鼠標點擊繪製矩形
我想繪製一個背景,我可以用鼠標點擊鼠標並在鼠標位置(當我點擊時)繪製一個新的矩形。所以「舊」矩形必須留在那裏。
這是我到目前爲止的代碼:
package com.ru.tgra.lab1;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import java.nio.FloatBuffer;
import com.badlogic.gdx.utils.BufferUtils;
public class Lab1Game extends ApplicationAdapter {
private FloatBuffer vertexBuffer;
private FloatBuffer modelMatrix;
private FloatBuffer projectionMatrix;
private int renderingProgramID;
private int vertexShaderID;
private int fragmentShaderID;
private int positionLoc;
private int modelMatrixLoc;
private int projectionMatrixLoc;
private int colorLoc;
private float position_x;
private float position_y;
private float size_rect = 100;
private int screenWidth;
private int screenHeight;
@Override
public void create() {
String vertexShaderString;
String fragmentShaderString;
vertexShaderString = Gdx.files.internal("shaders/simple2D.vert").readString();
fragmentShaderString = Gdx.files.internal("shaders/simple2D.frag").readString();
vertexShaderID = Gdx.gl.glCreateShader(GL20.GL_VERTEX_SHADER);
fragmentShaderID = Gdx.gl.glCreateShader(GL20.GL_FRAGMENT_SHADER);
Gdx.gl.glShaderSource(vertexShaderID, vertexShaderString);
Gdx.gl.glShaderSource(fragmentShaderID, fragmentShaderString);
Gdx.gl.glCompileShader(vertexShaderID);
Gdx.gl.glCompileShader(fragmentShaderID);
renderingProgramID = Gdx.gl.glCreateProgram();
Gdx.gl.glAttachShader(renderingProgramID, vertexShaderID);
Gdx.gl.glAttachShader(renderingProgramID, fragmentShaderID);
Gdx.gl.glLinkProgram(renderingProgramID);
positionLoc = Gdx.gl.glGetAttribLocation(renderingProgramID, "a_position");
Gdx.gl.glEnableVertexAttribArray(positionLoc);
modelMatrixLoc = Gdx.gl.glGetUniformLocation(renderingProgramID, "u_modelMatrix");
projectionMatrixLoc = Gdx.gl.glGetUniformLocation(renderingProgramID, "u_projectionMatrix");
colorLoc = Gdx.gl.glGetUniformLocation(renderingProgramID, "u_color");
Gdx.gl.glUseProgram(renderingProgramID);
float[] pm = new float[16];
pm[0] = 2.0f/Gdx.graphics.getWidth(); pm[4] = 0.0f; pm[8] = 0.0f; pm[12] = -1.0f;
pm[1] = 0.0f; pm[5] = 2.0f/Gdx.graphics.getHeight(); pm[9] = 0.0f; pm[13] = -1.0f;
pm[2] = 0.0f; pm[6] = 0.0f; pm[10] = 1.0f; pm[14] = 0.0f;
pm[3] = 0.0f; pm[7] = 0.0f; pm[11] = 0.0f; pm[15] = 1.0f;
projectionMatrix = BufferUtils.newFloatBuffer(16);
projectionMatrix.put(pm);
projectionMatrix.rewind();
Gdx.gl.glUniformMatrix4fv(projectionMatrixLoc, 1, false, projectionMatrix);
float[] mm = new float[16];
mm[0] = 1.0f; mm[4] = 0.0f; mm[8] = 0.0f; mm[12] = 0.0f;
mm[1] = 0.0f; mm[5] = 1.0f; mm[9] = 0.0f; mm[13] = 0.0f;
mm[2] = 0.0f; mm[6] = 0.0f; mm[10] = 1.0f; mm[14] = 0.0f;
mm[3] = 0.0f; mm[7] = 0.0f; mm[11] = 0.0f; mm[15] = 1.0f;
modelMatrix = BufferUtils.newFloatBuffer(16);
modelMatrix.put(mm);
modelMatrix.rewind();
Gdx.gl.glUniformMatrix4fv(modelMatrixLoc, 1, false, modelMatrix);
//COLOR IS SET HERE
Gdx.gl.glUniform4f(colorLoc, 0.7f, 0.2f, 0, 1);
//VERTEX ARRAY IS FILLED HERE
float[] array = {-50.0f, -50.0f,
-50.0f, 50.0f,
50.0f, -50.0f,
50.0f, 50.0f};
vertexBuffer = BufferUtils.newFloatBuffer(8);
vertexBuffer.put(array);
vertexBuffer.rewind();
position_x = 300;
position_y = 300;
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
size_rect = size_rect/2;
}
private void update()
{
if(Gdx.input.justTouched())
{
position_x = Gdx.input.getX();
position_y = screenHeight - Gdx.input.getY();
vertexBuffer.put(0, position_x - size_rect);
vertexBuffer.put(1, position_y - size_rect);
vertexBuffer.put(2, position_x - size_rect);
vertexBuffer.put(3, position_y + size_rect);
vertexBuffer.put(4, position_x + size_rect);
vertexBuffer.put(5, position_y - size_rect);
vertexBuffer.put(6, position_x + size_rect);
vertexBuffer.put(7, position_y + size_rect);
}
Gdx.gl.glVertexAttribPointer(positionLoc, 2, GL20.GL_FLOAT, false, 0, vertexBuffer);
Gdx.gl.glDrawArrays(GL20.GL_TRIANGLE_STRIP, 0, 4);
}
@Override
public void render() {
Gdx.gl.glClearColor(0.7f, 1f, 1f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
update();
}
}
不幸的是我不知道如何「拯救」的矩形(或座標)動態..
是否有人有一個很好的提示我或者你如何做到這一點的簡短代碼示例?
感謝您的解決方案:)不幸的是,只要我點擊遊戲屏幕,應用程序就會崩潰。我認爲這是因爲動態內存分配。 –
'線程中的異常「LWJGL應用程序」java.lang.UnsupportedOperationException \t at java.nio.FloatBuffer.array(FloatBuffer.java:994) \t at com.ru. tgra.lab1.Lab1Game.update(Lab1Game.java:137) \t在com.ru.tgra.lab1.Lab1Game.render(Lab1Game.java:170) \t在com.badlogic.gdx.backends.lwjgl.LwjglApplication。 mainLoop(LwjglApplication.java:215) \t at com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:120)' –
@linux_lover對代碼進行了更改,試試看。顯然你的'FloatBuffer'沒有被float []支持,這就是你爲什麼會得到這個異常。現在我把一個副本放到一個float []中,然後放回到一個新的'FloatBuffer'中。 – Harish