opengles绘制圆柱体(光照+纹理)

先看效果图



圆面的绘制:

package test.com.opengles8_1;



import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
/**
 * Created by hbin on 2016/9/5.
 * 圆面
 */
public class Circle {
    int mProgram;//自定义渲染管线着色器程序id
    int muMVPMatrixHandle;//总变换矩阵引用
    int maPositionHandle; //顶点位置属性引用
    int maTexCoorHandle; //顶点纹理坐标属性引用
    int muMMatrixHandle;

    int maCameraHandle; //摄像机位置属性引用
    int maNormalHandle; //顶点法向量属性引用
    int maLightLocationHandle;//光源位置属性引用


    String mVertexShader;//顶点着色器代码脚本
    String mFragmentShader;//片元着色器代码脚本

    FloatBuffer   mVertexBuffer;//顶点坐标数据缓冲
    FloatBuffer   mTexCoorBuffer;//顶点纹理坐标数据缓冲
    FloatBuffer   mNormalBuffer;//顶点法向量数据缓冲
    int vCount=0;
    float xAngle=0;//绕x轴旋转的角度
    float yAngle=0;//绕y轴旋转的角度
    float zAngle=0;//绕z轴旋转的角度

    public Circle(MySurfaceView mv,float scale,float r,int n)
    {
        //调用初始化顶点数据的initVertexData方法
        initVertexData(scale,r,n);
        //调用初始化着色器的intShader方法
        initShader(mv);
    }

    public void initVertexData(
            float scale,//大小
            float r,//半径
            int n //切分的份数
    ){
        r=r*scale;
        float angdegSpan=360.0f/n;//顶角的读书
        vCount=3*n;//顶点个数,共有n个三角形,每个三角形都有3个顶点

        float[] vertices=new float[vCount*3];//顶点坐标数据
        float[] textures=new float[vCount*2];//顶点纹理S,T坐标值数组

        //坐标数据初始化
        int count=0;
        int stCount=0;
        for (float angdeg=0;Math.ceil(angdeg)<360;angdeg+=angdegSpan){
            double angrad=Math.toRadians(angdeg);//当前弧度
            double angradNext=Math.toRadians(angdeg+angdegSpan);//下一弧度

            //中心点
            vertices[count++]=0;//顶点坐标
            vertices[count++]=0;
            vertices[count++]=0;

            textures[stCount++]=0.5F;//ST坐标
            textures[stCount++]=0.5f;

            //当前点
            vertices[count++]=(float)(-r*Math.sin(angrad));//顶点坐标
            vertices[count++]=(float)(r*Math.cos(angrad));
            vertices[count++]=0;

            textures[stCount++]=(float)(0.5-0.5*Math.sin(angrad));//st坐标
            textures[stCount++]=(float)(0.5-0.5*Math.cos(angrad));

            //下一点
            vertices[count++]=(float)(-r*Math.sin(angradNext));//顶点坐标
            vertices[count++]&#
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值