2016-09-29 1 views
0

저는 Spaceland라는 2D 게임을 개발 중이며 화면을 지우는 데 문제가 있습니다. 모든 프레임에 glClear(GL_COLOR_BUFFER_BIT)으로 전화 할 때마다 전화를 끊을 때까지 내 화면을 검은 색으로 유지합니다. 나는이 키를 glClear() 키로 지정하여 테스트했으며 화면을 검정으로 돌리면 키를 누르지 않은 상태에서 화면 전체에 퍼져있는 쿼드가 다시 지워질 때까지 커집니다.glClear()는 화면을 검은 색으로 유지합니다.

창을 만들 때 glClearColor(0, 0, 0, 1)을 사용하고 있습니다. 나는 glfwSwapInterval()을 껐다 켰다. 내 창 클래스의

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 클래스를 구현하기 전에 발생했습니다 볼 필요가 있다고 생각하지 않습니다 이.

Main 클래스 (rose[] 무시하고 roseI[] 그냥 멋진 테스트로 내가 만든 패턴입니다) :

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

당신이 대 및 FS 파일 Shader 클래스, 쉐이더를 참조해야하는 경우, 제발 말해 또는 아무것도 그밖에.

감사합니다!

+0

또는 glClearColor()를'0, 0, 0, 1 '로 설정했기 때문에 화면이 검게 지워지는 것이 정상입니다. 0.42, 0.28, 0과 같은 재미있는 것을 시도해보십시오.78 – Zouch

답변

0

내 질문에 명확하지 않지만 대답은 화면을 지우고, 버퍼를 교환하고, 렌더링 한 것 등입니다. 작동하지 않습니다.

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

이것은 현재 어떻게 작동하며 작동하지 않습니다.

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

이것은 현재 어떻게 작동하며 정상적으로 작동합니다.

+0

나는 그때 그때 잘 OpenGL을 알지 못했다 ... –

1

glClear은 출력 버퍼에 영향을줍니다. 그래서 그것은 렌더링의 일부입니다. 렌더링의 일부로 지우려면 render 함수 안에 glClear을 입력하십시오.

너는 내부에 update이 있습니다. 누구든지 renderupdate (LWJGL, 아마도?)을 부르는 사람이 특정 주문을 보장하지 않는다고 의심됩니다. 따라서 업데이트 할 때마다 마지막으로 렌더링 한 것을 맨 먼저 밟고 있습니다.

업데이트 :

  • 보통 부분적으로 시간의 함수로서 내부 상태를 조정한다.

렌더 :

  • 캡처 현재 상태를 시각적.
+0

'render()'와'update()'는'loop()'함수에 의해 호출됩니다. –

관련 문제