Search code examples
javaopengllibgdxdrawing

libgdx drawing rectangulars by mouse click


I am trying to work with libgdx or better with OpenGL. I have to deal with Java because of my lecture.

I want to draw a backgroud where I can click around with the mouse and where on the mouse position (when I clicked) a new rectangle is drawn. So the "old" rectangles have to stay there.

This is my code so far:

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

Unfortunately I have no idea how to "save" the rectangles (or the coordinates) dynamically..

Does someone have a good tip for me or a short code example of how you can do that?


Solution

  • Simply have a variable that stores the start index of the latest rectangle vertex and another variable to hold number of rectangles.

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