note 06- use VBO(Value Buffer Object) to draw a Pyramid

 

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.JPanel;


public class PyramidPanel extends JPanel {
	
	
	private static float[] pyramidVertexList=new float[]{1,0,1,  1,0,-1,  -1,0,-1,  -1,0,1,   0,1,0};
	private static int[]   pyramidIndexList=new int[]{0,4,3,  1,4,0,  2,4,1,  3,4,2,  0,3,2,  0,2,1};
	
	private FloatBuffer pyramidVertexBuffer;
	private FloatBuffer pyramidNormalBuffer;
	private IntBuffer pyramidIndexBuffer;
	
	private int pyramidVertexBufferID;
	private int pyramidNormalBufferID;
	private int pyramidIndexBufferID;
	
	private GLJPanel drawable;
	private Camera camera;
	
	
	public PyramidPanel(){
		
		
		GLEventListener glListener=new GLEventListener(){
			
			public void init(GLAutoDrawable drawable){
				
				GL2 gl=drawable.getGL().getGL2();
				gl.glClearColor(0.75f,0.75f, 1,1);
				
				gl.glEnable(GL2.GL_LIGHTING);
				gl.glEnable(GL2.GL_LIGHT0);
				gl.glEnable(GL2.GL_DEPTH_TEST);
				gl.glEnable(GL2.GL_COLOR_MATERIAL);
				
				gl.glShadeModel(GL2.GL_SMOOTH);
				
				gl.glLightModeli(GL2.GL_LIGHT_MODEL_TWO_SIDE,GL2.GL_TRUE);
				
				
				
				
				//make vertex buffer and normal buffer
				int len=pyramidVertexList.length;
				pyramidVertexBuffer=FloatBuffer.allocate(len);
				pyramidNormalBuffer=FloatBuffer.allocate(len);
				for(int i=0;i<len;i++){
					pyramidVertexBuffer.put(pyramidVertexList[i]);
					pyramidNormalBuffer.put(pyramidVertexList[i]);
				}
				pyramidVertexBuffer.rewind();
				pyramidNormalBuffer.rewind();
				
				
				
				//make index buffer
				len=pyramidIndexList.length;
				pyramidIndexBuffer=IntBuffer.allocate(len);
				for(int j=0;j<len;j++){
					pyramidIndexBuffer.put(pyramidIndexList[j]);
				}
				pyramidIndexBuffer.rewind();
				
				
				//gen 3 buffer IDs;
				int[] bufferIDs=new int[3];
				gl.glGenBuffers(3, bufferIDs, 0);
				
				pyramidVertexBufferID=bufferIDs[0];
				pyramidNormalBufferID=bufferIDs[1];
				pyramidIndexBufferID=bufferIDs[2];
				
				
				//bind the buffer to the IDs
				gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, pyramidVertexBufferID);
				gl.glBufferData(GL2.GL_ARRAY_BUFFER, pyramidVertexBuffer.capacity()*4, pyramidVertexBuffer, GL2.GL_STATIC_DRAW);
				
				gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, pyramidNormalBufferID);
				gl.glBufferData(GL2.GL_ARRAY_BUFFER,pyramidNormalBuffer.capacity()*4,pyramidNormalBuffer,GL2.GL_STATIC_DRAW);
				
				gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
				
				
				gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, pyramidIndexBufferID);
				gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, pyramidIndexBuffer.capacity()*4, pyramidIndexBuffer, GL2.GL_STATIC_DRAW);
				
				gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
				
				
			}
			
			public void display(GLAutoDrawable drawable){
				
				GL2 gl=drawable.getGL().getGL2();
				gl.glClear(GL2.GL_COLOR_BUFFER_BIT|GL2.GL_DEPTH_BUFFER_BIT);
				
				camera.apply(gl);
				
				gl.glBindBuffer(GL2.GL_ARRAY_BUFFER,pyramidVertexBufferID);
				gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
				
				gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, pyramidNormalBufferID);
				gl.glNormalPointer(GL2.GL_FLOAT, 0, 0);
				
				gl.glColor3f(0.8f,0.2f, 0.2f);
				
				//draw!!!
				gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
				gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
				
//				pyramidIndexBuffer.position(0);
//				gl.glDrawElements(GL2.GL_TRIANGLES, 3, GL2.GL_UNSIGNED_INT, pyramidIndexBuffer);
//				
//				pyramidIndexBuffer.position(3);
//				gl.glDrawElements(GL2.GL_TRIANGLES, 3, GL2.GL_UNSIGNED_INT, pyramidIndexBuffer);
//				
//				pyramidIndexBuffer.position(6);
//				gl.glDrawElements(GL2.GL_TRIANGLES, 3, GL2.GL_UNSIGNED_INT, pyramidIndexBuffer);
//				
//				pyramidIndexBuffer.position(9);
//				gl.glDrawElements(GL2.GL_TRIANGLES, 3, GL2.GL_UNSIGNED_INT, pyramidIndexBuffer);
//				
//				pyramidIndexBuffer.position(12);
//				gl.glDrawElements(GL2.GL_TRIANGLES, 3, GL2.GL_UNSIGNED_INT, pyramidIndexBuffer);
//				
//				pyramidIndexBuffer.position(15);
//				gl.glDrawElements(GL2.GL_TRIANGLES, 3, GL2.GL_UNSIGNED_INT, pyramidIndexBuffer);
				
				gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 17, 18, GL2.GL_UNSIGNED_INT, pyramidIndexBuffer);
				
				gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
				gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);
				
			}

			public void reshape(GLAutoDrawable drawable,int x,int y,int width,int height){
					GL2 gl = drawable.getGL().getGL2();
					double s = 1.5; // limits of cube that we want to view go from -s to s.
					gl.glMatrixMode(GL2.GL_PROJECTION);
					gl.glLoadIdentity(); // Start with the identity transform.
					if (width > height) { // Expand x limits to match viewport aspect ratio.
						double ratio = (double)width/height;
						gl.glOrtho(-s*ratio, s*ratio, -s, s, -s, s);
					}
					else { // Expand y limits to match viewport aspect ratio.
						double ratio = (double)height/width;
						gl.glOrtho(-s, s, -s*ratio, s*ratio, -s, s);
					}	
					gl.glMatrixMode(GL2.GL_MODELVIEW);
			}

			public void dispose(GLAutoDrawable drawable){
	
			}
			
		};
		
		
		
		
		drawable=new GLJPanel();
		drawable.setPreferredSize(new Dimension(800,800));
		drawable.addGLEventListener(glListener);
		
		this.setLayout(new BorderLayout());
		this.add(drawable,BorderLayout.CENTER);
		
		camera=new Camera();
		camera.setScale(2);
		new TrackBall(this,camera);
		
	}

}

 

 

1.  make some buffers ,  vertexBuffer (FloatBuffer) , colorBuffer(FloatBuffer), normalBuffer(FloatBuffer),

of course an indexBuffer (INTBuffer) ( In OpenGL ES , must be unsigned-short ,not int)

 

2. use GL2.glGenBuffer to  gen a bufferID list.

 

3. pass the buffer to the binded ID   gl.glBindBuffer(...) ,  gl.glBufferData(...)

 

4. release the binding , (binding ID-0 );

 

5. in display function , Bind the buffers again and set the pointer.

 

6. enable all necessery ARRAY (can be set in the init function)

 

7. drawElement , or draw range element

 

 

Super crazy process to draw a scene ... !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值