Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.FloatBuffer;
- import java.util.Random;
- import javax.media.opengl.GL2;
- import javax.media.opengl.GLAutoDrawable;
- import javax.media.opengl.GLCapabilities;
- import javax.media.opengl.GLEventListener;
- import javax.media.opengl.glu.GLU;
- import com.jogamp.common.nio.Buffers;
- import com.jogamp.newt.opengl.GLWindow;
- import com.jogamp.opengl.util.Animator;
- public class IsolatedVBOTest {
- private static final float[] POINT_DISTANCE_ATTENUATION = { 0.0f, 0.0f,
- 0.00005f };
- //On machines I've tested, reducing the point size OR the total number
- // of vertices prevents crashing.
- private static int POINT_SIZE = 64;
- private static int NUMBER_OF_VERTICES = 3000000;
- private static int VERTEX_BUFFER_SIZE = NUMBER_OF_VERTICES * 3;
- public static void main(String[] args) {
- Animator animator;
- GLCapabilities caps = new GLCapabilities(null);
- GLWindow window = GLWindow.create(caps);
- window.addGLEventListener(new GLEventListener() {
- private GLU glu = new GLU();
- int[] vbufferId = new int[1];
- @Override
- public void reshape(GLAutoDrawable drawable, int x, int y,
- int width, int height) {
- GL2 gl = (GL2) drawable.getGL();
- if (height <= 0) {
- height = 1;
- }
- final float h = (float) width / (float) height;
- gl.glViewport(0, 0, width, height);
- gl.glMatrixMode(GL2.GL_PROJECTION);
- gl.glLoadIdentity();
- glu.gluPerspective(45.0f, h, 1.0, 200.0);
- gl.glMatrixMode(GL2.GL_MODELVIEW);
- gl.glLoadIdentity();
- }
- @Override
- public void init(GLAutoDrawable drawable) {
- GL2 gl = (GL2) drawable.getGL();
- gl.glPointSize(POINT_SIZE);
- gl.glPointParameterfv(GL2.GL_POINT_DISTANCE_ATTENUATION,
- POINT_DISTANCE_ATTENUATION, 0);
- gl.glGenBuffers(1, vbufferId, 0);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbufferId[0]);
- FloatBuffer floatBuffer = Buffers
- .newDirectFloatBuffer(VERTEX_BUFFER_SIZE);
- Random random = new Random();
- for (int i = 0; i < NUMBER_OF_VERTICES; i++) {
- floatBuffer.put((random.nextFloat() * 8) - 4);
- floatBuffer.put((random.nextFloat() * 8) - 4);
- floatBuffer.put(0.0f);
- }
- floatBuffer.rewind();
- gl.glBufferData(GL2.GL_ARRAY_BUFFER, VERTEX_BUFFER_SIZE
- * Buffers.SIZEOF_FLOAT, floatBuffer, GL2.GL_STATIC_DRAW);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
- }
- @Override
- public void dispose(GLAutoDrawable drawable) {
- }
- @Override
- public void display(GLAutoDrawable drawable) {
- final GL2 gl = (GL2) drawable.getGL();
- gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
- gl.glLoadIdentity();
- glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
- gl.glColor4f(1.0f, 0f, 0f, 1.0f);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbufferId[0]);
- gl.glEnableVertexAttribArray(0);
- gl.glVertexAttribPointer(0, 3, GL2.GL_FLOAT, false, 0, 0);
- gl.glDrawArrays(GL2.GL_POINTS, 0, NUMBER_OF_VERTICES);
- gl.glDisableVertexAttribArray(0);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
- }
- });
- animator = new Animator(window);
- animator.start();
- window.setSize(800, 800);
- window.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement