libgdx通过鼠标点击绘制矩形

时间:2016-08-30 00:02:21

标签: java opengl libgdx drawing

我正在尝试使用libgdx或更好地使用OpenGL。由于我的演讲,我必须处理Java。

我想要绘制一个背景,我可以用鼠标点击它并在鼠标位置(当我点击时)的位置绘制一个新的矩形。所以“旧的”矩形必须留在那里。

到目前为止,这是我的代码:

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();
}
}

不幸的是我不知道如何动态地“保存”矩形(或坐标)。

有人为我提供了一个好的提示,或者是如何做到这一点的简短代码示例?

1 个答案:

答案 0 :(得分:0)

只需要一个变量来存储最新矩形顶点的起始索引,另一个变量来保存矩形数。

private int m_numRectangles;
private int m_nextRectIndex;

//initialize both above variables to 0

//...

if(Gdx.input.justTouched())
{
    m_nextRectIndex = m_numRectangles * 8;
    m_numRectangles++;

    //save a copy of the old rectangles
    vertexBuffer.rewind();
    Float[] oldRects = new Float[ vertexBuffer.remaining() ];
    vertexBuffer.get(oldRects);

    //allocate more memory for old + one more rectangle
    vertexBuffer = BufferUtils.newFloatBuffer( m_numRectangles * 8 );

    //copy back the old rectangles
    vertexBuffer.put( oldbuffer );
    vectexBuffer.rewind();

    position_x = Gdx.input.getX();
    position_y = screenHeight - Gdx.input.getY();

    vertexBuffer.put(m_nextRectIndex + 0, position_x - size_rect);
    vertexBuffer.put(m_nextRectIndex + 1, position_y - size_rect);
    vertexBuffer.put(m_nextRectIndex + 2, position_x - size_rect);
    vertexBuffer.put(m_nextRectIndex + 3, position_y + size_rect);
    vertexBuffer.put(m_nextRectIndex + 4, position_x + size_rect);
    vertexBuffer.put(m_nextRectIndex + 5, position_y - size_rect);
    vertexBuffer.put(m_nextRectIndex + 6, position_x + size_rect);
    vertexBuffer.put(m_nextRectIndex + 7, position_y + size_rect);

}
Gdx.gl.glVertexAttribPointer(positionLoc, 2, GL20.GL_FLOAT, false, 0, vertexBuffer);
for( int i=0; i<m_numRectangles * 4; i+=4 )
{
    Gdx.gl.glDrawArrays(GL20.GL_TRIANGLE_STRIP, i, 4);
}