opengl es 绘制圆柱体关于圆柱的问题

先上个效果图:
直接上关键类Render了:
package com.example.opengl.
import javax.microedition.khronos.egl.EGLCimport javax.microedition.khronos.opengles.GL10;
import com.example.opengl.document.C
import android.opengl.GLSurfaceVimport android.opengl.GLSurfaceView.Rimport android.view.MotionEimport android.view.Vimport android.view.View.OnTouchL
public class CylinderRender implements Renderer{ public static class MyTouchListener implements OnTouchListener {
private CylinderR
private GLSurfaceV
private float previousX;
private float previousY;
public MyTouchListener(CylinderRender tgl,GLSurfaceView surface)
this.surface=
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg1.getAction()==MotionEvent.ACTION_MOVE)
float dX=(arg1.getX()-previousX)*0.4f;
float dY=(arg1.getY()-previousY)*0.4f;
tgl.sety(dX);
tgl.setx(dY);
previousX=arg1.getX();
previousY=arg1.getY();
private Cylinder cLinder=new Cylinder(10,2,5,18); @Override public void onDrawFrame(GL10 gl) {
//启用背景色和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
//设置坐标偏移量
gl.glTranslatef(0, 0, -10f);
gl.glPushMatrix();
//绘制圆柱体
cLinder.drawSelf(gl);//
gl.glPopMatrix(); }
@Override public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float ratio=(float)width/
gl.glFrustumf(-ratio, ratio,-1,1,1,100); }
@Override public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(1, 1, 1, 1);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);
gl.glEnable(GL10.GL_DEPTH_TEST); } private void setx(float x) {
cLinder.setxAngle(x); } private void sety(float y) {
cLinder.setyAngle(y); } private void setz(float z) {
cLinder.setzAngle(z); }
还有一个封装的圆柱体类:
package com.example.opengl.
import java.nio.ByteBimport java.nio.ByteOimport java.nio.FloatBimport java.util.ArrayLimport java.util.L
import javax.microedition.khronos.opengles.GL10;
public class Cylinder { private FloatBuffer vertexB private int vC private float xA private float yA private float zA
public Cylinder(float length,float radius,int blocks,float sectorBlocks) {
float len=length/
float sectorAngle=360/sectorB
List&Float& arrayList=new ArrayList&Float&();
for(int i=0;i&i++)
for(float angle=360;angle&0;angle=angle-sectorAngle)
//左上角顶点坐标
float x1=-(length/2-i*len);
float y1=(float) (radius*Math.sin(Math.toRadians(angle)));
float z1=(float) (radius*Math.cos(Math.toRadians(angle)));
//右上角顶点坐标
float x2=x1+
float y2=y1;
float z2=z1;
//右下角顶点坐标
float x3=x2;
float y3=(float) (radius*Math.sin(Math.toRadians(angle-sectorAngle)));
float z3=(float) (radius*Math.cos(Math.toRadians(angle-sectorAngle)));
//左下角顶点坐标
float x4=x1;
float y4=y3;
float z4=z3;
//第一条线
arrayList.add(x1);
arrayList.add(y1);
arrayList.add(z1);
arrayList.add(x2);
arrayList.add(y2);
arrayList.add(z2);
//第二条线
arrayList.add(x2);
arrayList.add(y2);
arrayList.add(z2);
arrayList.add(x4);
arrayList.add(y4);
arrayList.add(z4);
//第三条线
arrayList.add(x4);
arrayList.add(y4);
arrayList.add(z4);
arrayList.add(x1);
arrayList.add(y1);
arrayList.add(z1);
//第四条线
arrayList.add(x2);
arrayList.add(y2);
arrayList.add(z2);
arrayList.add(x3);
arrayList.add(y3);
arrayList.add(z3);
//第五条线
arrayList.add(x3);
arrayList.add(y3);
arrayList.add(z3);
arrayList.add(x4);
arrayList.add(y4);
arrayList.add(z4);
int size=arrayList.size();
vCount=size/3;
float[] vertex=new float[size];
for(int i=0;i&i++)
vertex[i]=arrayList.get(i);
ByteBuffer vB=ByteBuffer.allocateDirect(size*4);
vB.order(ByteOrder.nativeOrder());
vertexBuffer=vB.asFloatBuffer();
vertexBuffer.put(vertex);
vertexBuffer.flip();
vertexBuffer.position(0);
public void drawSelf(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);
gl.glColor4f(0.2f, 0.2f, 0.2f, 0.2f);//设置绘制线的颜色
gl.glRotatef(xAngle,1, 0, 0);
gl.glRotatef(yAngle,0, 1, 0);
gl.glRotatef(zAngle,0, 0, 1);
gl.glDrawArrays(GL10.GL_LINES,0,vCount); }
* @param xAngle the xAngle to set
*/ public void setxAngle(float xAngle) {
this.xAngle += xA }
* @param yAngle the yAngle to set
*/ public void setyAngle(float yAngle) {
this.yAngle += yA }
* @param zAngle the zAngle to set
*/ public void setzAngle(float zAngle) {
this.zAngle += zA }}
阅读(...) 评论()请教:如何用OPENGL 实现有金属感的圆柱体。主要是灯光和颜色如何设置?-------------------------------------------------------------- GLfloat model_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat light_position0[]= { 0.6f, 0.6f, 3.0f, 0.0f }; GLfloat light_color0[] = { 1.0f, 1.0f, 1.0f, 1.0f };
//环境光 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, model_ambient );
//光源位置 glLightfv( GL_LIGHT0, GL_POSITION, light_position0 );
//光源颜色 glLightfv( GL_LIGHT0, GL_DIFFUSE, light_color0 );
//打开光源 glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 );
GLfloat gray_ambient[]
= { 0.2f, 0.2f, 0.2f };&
GLfloat gray_diffuse[]
= { 0.3f, 0.3f, 0.3f };&
GLfloat gray_specular[] = { 0.5f, 0.5f, 0.5f };&
GLfloat gray_shininess[]= { 22.0f };
glMaterialfv( GL_FRONT, GL_AMBIENT, gray_ambient);
glMaterialfv( GL_FRONT, GL_DIFFUSE, gray_diffuse );
glMaterialfv( GL_FRONT, GL_SPECULAR,gray_specular);
glMaterialfv( GL_FRONT, GL_SHININESS, gray_shininess );用opengl 画圆柱体,怎么显示圆柱体内部?
[问题点数:40分,结帖人ollyice2012]
用opengl 画圆柱体,怎么显示圆柱体内部?
[问题点数:40分,结帖人ollyice2012]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 opengl es 绘制圆柱体 的文章

 

随机推荐