Screen blink when drawing a simple triangle

Started by kgmaven, November 28, 2014, 07:04:19

Previous topic - Next topic

kgmaven

Hi:

I am new in OpenGL, and I prefer to learn it in java since I have never used cpp before, then I found lwigl.

And I am following this tutorial http://www.arcsynthesis.org/gltut/index.html, with the java codes:https://github.com/ra4king/LWJGL-OpenGL-Tutorials.

The example ran as expected.

However once I tried to write my own codes, I can not make it ran,the codes are simple, so I post here:

public class Main {
	FloatBuffer fb = (FloatBuffer) BufferUtils.createFloatBuffer(12).put(new float[]{
			0.75f, 0.75f, 0.0f, 1.0f,
			0.75f, -0.75f, 0.0f, 1.0f,
			-0.75f, -0.75f, 0.0f, 1.0f
	}).flip();

	public static void main(String[] args) {
		new Main().start();

	}

	public void start() {
		try {
			Display.create(new PixelFormat());
			Display.setDisplayMode(new DisplayMode(300, 300));
			Display.setResizable(true);
			render();
		} catch (Exception exc) {
			exc.printStackTrace();
			System.exit(1);
		}
	}

	private void render() {
		int bufferId = glGenBuffers();
		glBindBuffer(GL_ARRAY_BUFFER, bufferId);//{1}
		glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);
		glBindVertexArray(glGenVertexArrays());

		glClearColor(0, 0, 0, 0);
		glClear(GL_COLOR_BUFFER_BIT);

		while (!shouldStop()) {
			if (Display.wasResized())
				resized();

			glBindBuffer(GL_ARRAY_BUFFER, bufferId);//{2}
			glEnableVertexAttribArray(0);
			glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);

			glDrawArrays(GL_TRIANGLES, 0, 3);

			glDisableVertexAttribArray(0);
			glBindBuffer(GL_ARRAY_BUFFER, 0);


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

	private void resized() {
		glViewport(0, 0, Display.getWidth(), Display.getHeight());
	}

	private boolean shouldStop() {
		return Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE);
	}
}


Now I meet two problems:

1 When I run this application, I will see a triangle, however once I resize the window, the screen will blink, and it seems that the old triangle is still being rendered..

2 Note the two lines commented with {1} and {2}, once I remove the line{2}, I will get error, but I can not understand why I have to bind the GL_ARRAY_BUFFER to bufferId again?

Cornix

You need to clear (glClear(int)) the display repeatedly, inside your loop. Right now you only clear it once before the loop.