2016-09-29 6 views
0

私はSpacelandという2Dゲームを開発しています。画面をクリアすると問題が発生しました。毎フレームglClear(GL_COLOR_BUFFER_BIT)と呼ぶと、私はそれを止めるまで画面を黒く保ちます。私はglClear()をキーに割り当ててこれをテストしました。画面を黒く押さえたままにしておくと、押していないと画面全体に広がっているクワッドが再びクリアされるまで大きくなります。glClear()は画面を黒色に保ちます

私はウィンドウを作成するときにglClearColor(0, 0, 0, 1)を使用しています。私はglfwSwapInterval()をオフにしてみました。私のWindowクラスで

create()機能:

public void create(boolean vsync) { 
    GLFWErrorCallback.createPrint(System.err).set(); 

    GLFWVidMode vid = glfwGetVideoMode(glfwGetPrimaryMonitor()); 

    keys = new boolean[GLFW_KEY_LAST]; 
    for (int i = 0; i < GLFW_KEY_LAST; i ++) { 
     keys[i] = false; 
    } 

    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); 

    ID = glfwCreateWindow(vid.width(), vid.height(), TITLE, glfwGetPrimaryMonitor(), 0); 
    if (ID == 0) 
     throw new IllegalStateException("Error whilst creating window: '" + TITLE + "'"); 

    glfwMakeContextCurrent(ID); 
    createCapabilities(); 

    glClearColor(0, 0, 0, 1); 

    camera = new Camera(getWidth(), getHeight()); 

    glfwSwapInterval(vsync ? 1 : 0); 
} 

スプライトクラス:

public class Sprite { 
private VertexArray vao; 
private VertexBuffer 
     pVbo, 
     iVbo; 

private int vertexCount; 

private float scale; 

private Vector3f position; 
private Vector3f rotation; 

private Matrix4f tMatrix; 

public Sprite(float[] pos, int[] indices) { 
    vertexCount = indices.length; 

    position = new Vector3f(0, 0, 0); 
    rotation = new Vector3f(0, 0, 0); 
    scale = 0.1f; 

    tMatrix = MatrixHelper.createTransformationMatrix(position, rotation, scale); 

    vao = new VertexArray(); 
    pVbo = new VertexBuffer(false); 
    iVbo = new VertexBuffer(true); 

    vao.bind(); 

    pVbo.bind(); 
    pVbo.add(pos); 
    vao.add(); 
    pVbo.unbind(); 

    iVbo.bind(); 
    iVbo.add(indices); 
    iVbo.unbind(); 

    vao.unbind(); 
} 

public void setPosition(float x, float y, float z) { 
    position.x = x; 
    position.y = y; 
    position.z = z; 
} 

public void setRotation(Vector3f rot) { 
    rotation = rot; 
} 

public void render(int renderType) { 
    MatrixHelper.setTMatrixPosition(tMatrix, position); 
    setPosition(getPosition().x + 0.0001f, 0, 0); 

    System.out.println(tMatrix); 

    Spaceland.shader.bind(); 
    Spaceland.shader.editValue("transformation", tMatrix); 

    vao.bind(); 
    glEnableVertexAttribArray(0); 
    iVbo.bind(); 

    glDrawElements(renderType, vertexCount, GL_UNSIGNED_INT, 0); 

    iVbo.unbind(); 
    glDisableVertexAttribArray(0); 
    vao.unbind(); 

    Spaceland.shader.unbind(); 
} 

public Vector3f getPosition() { 
    return position; 
} 
} 

問題を実装する前に発生したように私はあなたが私のCameraクラスまたはMatrixHelperクラスを参照してくださいする必要はないと思いますこの。

メインクラス(それは私がテストとして作られただけでクールなパターンだroseI[]rose[]を無視して):

public class Spaceland { 
public static Window window; 

public static Sprite sprite; 

public static Shader shader; 

public static float[] rose = { 
     -0.45f, 0f, 
     0.45f, 0f, 
     0f, 0.45f, 
     0f, -0.45f, 
     -0.4f, -0.2f, 
     -0.4f, 0.2f, 
     0.4f, -0.2f, 
     0.4f, 0.2f, 
     -0.2f, -0.4f, 
     -0.2f, 0.4f, 
     0.2f, -0.4f, 
     0.2f, 0.4f 
}; 

public static int[] roseI = { 
     0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 

     1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 

     2, 3, 2, 4, 2, 5, 2, 6, 2, 7, 2, 8, 2, 9, 2, 10, 2, 11, 

     3, 4, 3, 5, 3, 6, 3, 7, 3, 8, 3, 9, 3, 10, 3, 11, 

     4, 5, 4, 6, 4, 7, 4, 8, 4, 9, 4, 10, 4, 11, 

     5, 6, 5, 7, 5, 8, 5, 9, 5, 10, 5, 11, 

     6, 7, 6, 8, 6, 9, 6, 10, 6, 11, 

     7, 8, 7, 9, 7, 10, 7, 11, 

     8, 9, 8, 10, 8, 11, 

     9, 10, 9, 11, 

     10, 11, 
}; 

public static float[] quad = { 
     0.5f, 0.5f, 
     0.5f, -0.5f, 
     -0.5f, 0.5f, 
     -0.5f, -0.5f 
}; 

public static int[] quadI = { 
     2, 0, 3, 
     0, 1, 3 
}; 

public static void main(String[] args) { 
    init(); 
} 

public static void loop() { 
    while (!window.isCloseRequested()) { 
     update(); 
     render(); 
    } 

    destroy(0); 
} 

public static void init() { 
    if (!glfwInit()) 
     throw new IllegalStateException("Error whilst initialising GLFW"); 

    window = new Window("Spaceland"); 

    window.create(true); 

    shader = new Shader("src/main/java/com/spaceland/graphics/fragment.fs", "src/main/java/com/spaceland/graphics/vertex.vs"); 

    sprite = new Sprite(quad, quadI); 

    loop(); 
} 

public static void render() { 
    window.render(); 

    sprite.render(GL11.GL_TRIANGLES); 
} 

public static void update() { 
    window.update(); 

    if (window.isDown(GLFW_KEY_SPACE)) { 
     glClear(GL_COLOR_BUFFER_BIT); 
    } 
} 

public static void destroy(int error) { 
    window.destroy(); 

    glfwTerminate(); 
    glfwSetErrorCallback(null).free(); 

    shader.destroy(); 

    VertexBuffer.deleteAll(); 
    VertexArray.destroyAll(); 

    System.exit(error); 
} 
} 

あなたがShaderクラス、対シェーダとFSファイル、または何も表示する必要がある場合を教えてください。 else。

ありがとうございます!

+0

また、glClearColor()を '0、0、0、1'に設定しているので、画面が黒くなるのは完全に正常です。 0.42、0.28、0のような空想を試してみてください。78 – Zouch

答えて

0

私の質問ではあまり明確ではありませんが、答えは私が画面をクリアし、バッファを交換し、レンダリングしたなどです。動作しません。

glClear(...); 
glfwSwapBuffers(...); 
...render... 

これは現在の方法であり、これは機能しません。

glClear(...); 
...render... 
glfwSwapBuffers(...); 

これは私が今やっている方法であり、うまくいきます。

+0

私は本当にOpenGLをよく知っていませんでした... –

1

glClearは出力バッファに影響します。それはレンダリングの一部です。あなたのレンダリングの一部としてクリアしたい場合は、の機能の中にglClearを入れてください。

updateです。私は誰でもrenderupdate(LWJGL、おそらく?)を呼んでいる人が特定の発注を保証しているとは思わない。更新するように求められるたびに、あなたがレンダリングした最後のものの上を歩いています。

更新:

  • 通常、部分的に、時間の関数として、内部状態を調整します。

はレンダリング:

  • キャプチャ現在の状態を視覚的に。
+0

'render()'と 'update()'は 'loop()'関数によって呼び出されます。 –

関連する問題