用LWJGL重绘线条

时间:2013-02-20 15:31:01

标签: java opengl lwjgl

我想制作一个小的应用程序,你有4行,每个都有第一个坐标设置在窗口的一侧随机的某个地方,第二个坐标将跟随鼠标,所以你得到这个效果:

img

您无法看到光标,但线条与中心的光标相符。

我的问题是我无法让它随机移动线条。因此,每个帧或者滴答或毫秒线都会随机改变位置并删除前一行。在此之后我想要随机改变线条的颜色,这样它就会选择一个位置和一个随机位置,几乎可以给任何人和癫痫发作,但我需要先找出位置。

我尝试过一些glClear(GL _...)命令,但它们似乎不起作用。有没有办法完全清除屏幕并重做glBegin(GL_LINES)命令将它放在另一个地方?

这是我的代码:

    package tests;

import static org.lwjgl.opengl.GL11.*;
import java.util.Random;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;

public class game {

int wx = 600;
int hy = 400;

int rx = new Random().nextInt(wx);
int ry = new Random().nextInt(hy);

public game() throws LWJGLException {

    int mouseX = Mouse.getX();
    int mouseY = hy-Mouse.getY()-1;

    Display.setDisplayMode(new DisplayMode(wx, hy));
    Display.create();
    Display.setTitle("Game");

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, wx, hy, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);

    while(!Display.isCloseRequested()) {
        lines();

        while(Keyboard.next()) {
            if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
                Display.destroy();
                System.exit(0);
            }
        }

        while(Mouse.next()) {
            if(Mouse.isButtonDown(0)) {
                System.out.println("(" + mouseX + ", " + mouseY + ")");
            }
        }
    }
}

public void lines() {

    glClear(GL_COLOR_BUFFER_BIT);

    int mouseX = Mouse.getX();
    int mouseY = hy-Mouse.getY()-1;

    glBegin(GL_LINES);
        glVertex2i(rx, 0);
        glVertex2i(mouseX, mouseY);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(0, ry);
        glVertex2i(mouseX, mouseY);
    glEnd();
    glBegin(GL_LINES);
        glVertex2i(rx, hy);
        glVertex2i(mouseX, mouseY);
    glEnd();
    glBegin(GL_LINES);
        glVertex2i(wx, ry);
        glVertex2i(mouseX, mouseY);
    glEnd();
        glLineWidth(5);
        glColor3f(1f, 0f, 0f);

    Display.update();
    Display.sync(500);

}


public static void main(String[] args) throws LWJGLException {
    new game();
    }
}

1 个答案:

答案 0 :(得分:0)

尝试在rx内移动ry / lines()计算:

public void lines() 
{
    int rx = new Random().nextInt(wx);
    int ry = new Random().nextInt(hy);

    glClear(GL_COLOR_BUFFER_BIT);

    int mouseX = Mouse.getX();
    int mouseY = hy-Mouse.getY()-1;

    glLineWidth(5);
    glColor3f(1f, 0f, 0f);

    glBegin(GL_LINES);
        glVertex2i(rx, 0);
        glVertex2i(mouseX, mouseY);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(0, ry);
        glVertex2i(mouseX, mouseY);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(rx, hy);
        glVertex2i(mouseX, mouseY);
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(wx, ry);
        glVertex2i(mouseX, mouseY);
    glEnd();

    Display.update();
    Display.sync(500);
}