import java.awt.Dimension;
import java.awt.Toolkit;
import java.nio.FloatBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import com.sun.opengl.util.BufferUtil;
import com.sun.opengl.util.FPSAnimator;
import com.sun.opengl.util.GLUT;
public class MiscTest extends JFrame
implements GLEventListener {
private float mXLeft = -50;
private float mXRight = 50;
private float mZNear = 0;
private float mZFar = -100;
private float mFOV = 40;
private float mElev = 30;
private GLCanvas mCanvas = new GLCanvas();
private FPSAnimator mAnimator;
private float mTranslate;
private int yRotation;
private int mFacet;
private int mVBOVertex[] = new int[1];
private FloatBuffer mVBOVertices;
public MiscTest() {
super("Miscellaneous Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mCanvas.addGLEventListener(this);
getContentPane().add(mCanvas);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screenSize.width - 40, screenSize.height - 40);
setLocationRelativeTo(null);
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
gl.glClearDepth(1.0);
gl.glShadeModel(GL.GL_SMOOTH);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glEnable(GL.GL_DEPTH_TEST);
buildVBOData(gl, 7.2f, 4f, 4f, 10, 10, 30f);
mAnimator = new FPSAnimator(drawable, 60);
mAnimator.setRunAsFastAsPossible(false);
mAnimator.start();
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslated(0, 0, mTranslate);
gl.glRotated(mElev, 1, 0, 0);
gl.glRotated(yRotation, 0, 1, 0);
yRotation = (yRotation + 1) % 360;
float xoffset = (mXLeft+mXRight)/2.0f;
gl.glTranslated(0, 0, -xoffset);
float zoffset = (mZNear+mZFar)/2.0f;
gl.glTranslated(0, 0, -zoffset);
paintVBOData(gl);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
float xdiff = Math.abs(mXRight - mXLeft);
float zdiff = Math.abs(mZFar - mZNear);
float radius = (float) (Math.sqrt(xdiff*xdiff + zdiff*zdiff) / 2.0);
float angle1 = 180 - mFOV/2.0f - mElev;
float dist = (float) (radius / Math.sin(mFOV/2.0f*Math.PI/180.0f) * Math.sin(angle1*Math.PI/180.0f));
float angle2 = 180 - 90 - mElev;
float segment = (float) (radius / Math.sin(Math.PI/2.0) * Math.sin(angle2*Math.PI/180.0));
float znear = (mElev < 85) ? (dist - segment) : dist*0.5f;
float zfar = (mElev < 85) ? (dist + segment) : dist*1.5f;
float ytop = (float) (znear * Math.atan(mFOV/2.0*Math.PI/180.0));
float ybottom = -ytop;
float aspect = (float)width/(float)height;
float xright = ytop * aspect;
float xleft = -xright;
mTranslate = -dist;
gl.glFrustum(xleft, xright, ybottom, ytop, znear, zfar);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged) {}
public void paintVBOData(GL gl) {
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
// gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, mVBOVertex[0]);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
gl.glDrawArrays(GL.GL_QUADS, 0, mFacet*4);
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
}
public void buildVBOData(GL gl, float height, float width, float depth,
int rows, int cols, float angle) {
float range = 0.8f;
float xdiff = Math.abs(mXRight - mXLeft);
float zdiff = Math.abs(mZFar - mZNear);
float xstep = (xdiff*range)/(cols-1);
float zstep = (zdiff*range)/(rows-1);
mFacet = rows*cols*6; // 6 facets
mVBOVertices = BufferUtil.newFloatBuffer(mFacet*4*3); // 4 vertices each facet
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
float x = (float) (xstep*j - xdiff*range/2.0);
float z = (float) (zstep*i - zdiff*(range+(1-range)/2.0));
float v1[] = {x-width/2.0f, height, z-depth/2.0f};
float v2[] = {x-width/2.0f, height, z+depth/2.0f};
float v3[] = {x+width/2.0f, height, z+depth/2.0f};
float v4[] = {x+width/2.0f, height, z-depth/2.0f};
float v5[] = {x-width/2.0f, 0, z-depth/2.0f};
float v6[] = {x-width/2.0f, 0, z+depth/2.0f};
float v7[] = {x+width/2.0f, 0, z+depth/2.0f};
float v8[] = {x+width/2.0f, 0, z-depth/2.0f};
mVBOVertices.put(v1[0]);
mVBOVertices.put(v1[1]);
mVBOVertices.put(v1[2]);
mVBOVertices.put(v2[0]);
mVBOVertices.put(v2[1]);
mVBOVertices.put(v2[2]);
mVBOVertices.put(v3[0]);
mVBOVertices.put(v3[1]);
mVBOVertices.put(v3[2]);
mVBOVertices.put(v4[0]);
mVBOVertices.put(v4[1]);
mVBOVertices.put(v4[2]);
mVBOVertices.put(v5[0]);
mVBOVertices.put(v5[1]);
mVBOVertices.put(v5[2]);
mVBOVertices.put(v8[0]);
mVBOVertices.put(v8[1]);
mVBOVertices.put(v8[2]);
mVBOVertices.put(v7[0]);
mVBOVertices.put(v7[1]);
mVBOVertices.put(v7[2]);
mVBOVertices.put(v6[0]);
mVBOVertices.put(v6[1]);
mVBOVertices.put(v6[2]);
mVBOVertices.put(v2[0]);
mVBOVertices.put(v2[1]);
mVBOVertices.put(v2[2]);
mVBOVertices.put(v6[0]);
mVBOVertices.put(v6[1]);
mVBOVertices.put(v6[2]);
mVBOVertices.put(v7[0]);
mVBOVertices.put(v7[1]);
mVBOVertices.put(v7[2]);
mVBOVertices.put(v3[0]);
mVBOVertices.put(v3[1]);
mVBOVertices.put(v3[2]);
mVBOVertices.put(v3[0]);
mVBOVertices.put(v3[1]);
mVBOVertices.put(v3[2]);
mVBOVertices.put(v7[0]);
mVBOVertices.put(v7[1]);
mVBOVertices.put(v7[2]);
mVBOVertices.put(v8[0]);
mVBOVertices.put(v8[1]);
mVBOVertices.put(v8[2]);
mVBOVertices.put(v4[0]);
mVBOVertices.put(v4[1]);
mVBOVertices.put(v4[2]);
mVBOVertices.put(v4[0]);
mVBOVertices.put(v4[1]);
mVBOVertices.put(v4[2]);
mVBOVertices.put(v8[0]);
mVBOVertices.put(v8[1]);
mVBOVertices.put(v8[2]);
mVBOVertices.put(v5[0]);
mVBOVertices.put(v5[1]);
mVBOVertices.put(v5[2]);
mVBOVertices.put(v1[0]);
mVBOVertices.put(v1[1]);
mVBOVertices.put(v1[2]);
mVBOVertices.put(v1[0]);
mVBOVertices.put(v1[1]);
mVBOVertices.put(v1[2]);
mVBOVertices.put(v5[0]);
mVBOVertices.put(v5[1]);
mVBOVertices.put(v5[2]);
mVBOVertices.put(v6[0]);
mVBOVertices.put(v6[1]);
mVBOVertices.put(v6[2]);
mVBOVertices.put(v2[0]);
mVBOVertices.put(v2[1]);
mVBOVertices.put(v2[2]);
}
}
mVBOVertices.flip();
gl.glGenBuffersARB(1, mVBOVertex, 0);
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, mVBOVertex[0]);
gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, mFacet*4*3*BufferUtil.SIZEOF_FLOAT,
mVBOVertices, GL.GL_STATIC_DRAW_ARB);
}
}
VBO data painted even w/t glBindBufferARB
hi, all,
The following code is really driving me crazy.
In method paintVBOData, I have commented out gl.glBindBufferARB, but with/without it, the cubes are always painted, how can this happen?
Any ideas are always welcome.
[Edited by - thinhare on October 29, 2009 5:41:13 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement