libgdx 通过鼠标单击绘制矩形

Posted

技术标签:

【中文标题】libgdx 通过鼠标单击绘制矩形【英文标题】:libgdx drawing rectangulars by mouse click 【发布时间】:2016-08-30 00:02:20 【问题描述】:

我正在尝试使用 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】:

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

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

【讨论】:

感谢您的解决方案 :) 不幸的是,我一点击游戏屏幕,应用程序就崩溃了。我认为这是因为动态内存分配.. Exception in thread "LWJGL Application" java.lang.UnsupportedOperationException at java.nio.FloatBuffer.array(FloatBuffer.java:994) at com.ru.tgra.lab1.Lab1Game.update(Lab1Game.java:137) at com.ru.tgra.lab1.Lab1Game.render(Lab1Game.java:170) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120) @linux_lover 对代码进行了更改,试一试。显然,您的 FloatBuffer 没有得到 float[] 的支持,这就是您遇到异常的原因。现在我将副本复制到 float[] 并将其放回新的FloatBuffer 好的,现在我得到了一个“FloatBuffer is not direct”的异常:/ Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: FloatBuffer is not direct at org.lwjgl.BufferChecks.checkDirect(BufferChecks.java:139) at org.lwjgl.opengl.GL20.glVertexAttribPointer(GL20.java:856) at

以上是关于libgdx 通过鼠标单击绘制矩形的主要内容,如果未能解决你的问题,请参考以下文章

VB.Net 如何通过鼠标单击和拖动来旋转矩形

使用 python 在打开的 cv 中使用鼠标事件绘制矩形或线条

OpenGL用鼠标拖动绘制矩形?

Tkinter:使用鼠标绘制矩形

在按下和释放鼠标时绘制一个矩形,opengl

通过在QT QML中单击鼠标重复矩形