使用glDrawArrays()从数组中的不同索引绘制

时间:2014-05-29 15:15:10

标签: java lwjgl

我目前正在尝试创建一个简单的“菜单”(没有真正的目的,仅用于学习目的),而我正试图在我点击其中一个“按钮”时(实际上只是一个矩形) ,更改为其他州(从GAME_STATE = MAIN_MENU_STATEGAME_STATE = OPTIONS_MENU_STATE)。 OPTIONS_MENU_STATE的按钮顶点位置与前一个按钮位于同一个数组中。我可以将索引更改为从glDrawArrays()开始吗?我认为这是第二个参数,但它似乎不是。如果需要,会发布代码。

显示方法:

private void display() {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(theProgram);

    int colorData = (vertexData.length * FLOAT_SIZE) / 2;
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
    glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, colorData);

    //Right here is where I want to change the index, but it only works when I use 0
    glDrawArrays(GL_TRIANGLES, currentIndex, currentVerticeCount);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glUseProgram(0);
}

我的Gamestate方法:

private void checkGameState() {
    switch(GAME_STATE) {
    case MAIN_MENU_STATE:
        currentVerticeCount = 12;
        currentIndex = 0;

        break;
    case OPTIONS_MENU_STATE:
        currentVerticeCount = 18;
        currentIndex = 36; //When I do this, it doesn't draw the triangles anymore

        break;
    }
}

Keyupdate方法:

private void update() {
    while(Mouse.next()) {
     mouseX = -1.0 + 2.0 * (double) Mouse.getX() / Display.getWidth();
      mouseY = -(1.0 - 2.0 * (double) Mouse.getY() / Display.getHeight());

        //BUTTON 1 - HOVERED
        if(mouseX > -0.25f && mouseX < 0.25f) {
            if(mouseY > 0.5f && mouseY < 0.75f) {
                if(Mouse.isButtonDown(0)) {
                    //Change the color values(clicked) of the first button, index 90 - 108
                    changeColorData(90, 0f, 1.0f, 0.0f);
                } else {
                    //Change the color values(hover) of the first button, index 90 - 108
                    changeColorData(90, 0.5f, 0.0f, 0.0f);
                }
            } else { //if the y-value isn't the same
                //Reset the color to the normal value
                changeColorData(90, 1.0f, 0.0f, 0.0f);
            }

        }  else { //and if the x-value isn't the same
            //Reset the color to the normal value
            changeColorData(90, 1.0f, 0.0f, 0.0f);
        }

        adjustVertexBuffer();

        if(mouseX > -0.25f && mouseX < 0.25f) {
            if(mouseY > 0.15f && mouseY < 0.4f) {
                if(Mouse.isButtonDown(0)) {
                    //Change the color values(clicked) of the second button, index 108 - 126
                    changeColorData(108, 0f, 1.0f, 0.0f);
                    GAME_STATE = OPTIONS_MENU_STATE;

                } else {
                    //Change the color values(hover) of the first button, index 108 - 126
                    changeColorData(108, 0.5f, 0.0f, 0.0f);
                }
            } else {
                //Reset the color to the normal value
                changeColorData(108, 1.0f, 0.0f, 0.0f);
            }
        }  else {
            //Reset the color to the normal value
            changeColorData(108, 1.0f, 0.0f, 0.0f);
        }

        adjustVertexBuffer();

        if(mouseX > -0.25f && mouseX < 0.25f) {

        }
    }
}

adjustVertexData()只是将newColorData添加到顶点缓冲区glBufferSubData() changeColorData()获取索引和RGB浮点值,然后使用for循环将它们添加到newColorData数组

按钮颜色和顶点数据

private float vertexData[] = {
    //MAIN-MENU TRIANGLES

    //BUTTON ID 1
    -0.25f, 0.75f, 0.0f,
    0.25f, 0.75f, 0.0f,
    0.25f, 0.5f, 0.0f,

    0.25f, 0.5f, 0.0f,
    -0.25f, 0.5f, 0.0f,
    -0.25f, 0.75f, 0.0f,

    //BUTTON ID 2
    -0.25f, 0.4f, 0.0f,
    0.25f, 0.4f, 0.0f,
    0.25f, 0.15f, 0.0f,

    0.25f, 0.15f, 0.0f,
    -0.25f, 0.15f, 0.0f,
    -0.25f, 0.4f, 0.0f,

    //SUB-MENU TRIANGLES

    //BUTTON ID 3
    -0.25f, 0.5f, 0.0f,
    0.25f, 0.5f, 0.0f,
    0.25f, 0.3f, 0.0f,

    0.25f, 0.3f, 0.0f,
    -0.25f, 0.3f, 0.0f,
    -0.25f, 0.5f, 0.0f,

    //BUTTON ID 4
    -0.25f, 0.1f, 0.0f,
    -0.25f, 0.1f, 0.0f,
    0.25f, -0.2f, 0.0f,

    0.25f, -0.2f, 0.0f,
    -0.25f, -0.2f, 0.0f,
    -0.25f, 0.1f, 0.0f,

    //BUTTON ID 5
    0.25f, -0.3f, 0.0f,
    -0.25f, -0.3f, 0.0f,
    0.25f, -0.5f, 0.0f,

    0.25f, -0.5f, 0.0f,
    -0.25f, -0.5f, 0.0f,
    -0.25f, 0.3f, 0.0f,

    //COLOR MAIN MENU//

    //BUTTON ID 1
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,

    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,

    //BUTTON ID 2
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,

    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,

    //COLOR SUB MENU

    //BUTTON ID 3
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,

    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,

    //BUTTON ID 4
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,

    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,

    //BUTTON ID 5
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,

    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
};

0 个答案:

没有答案