android 使用OPENGL ES绘制一个圆柱体-三维空间

本文展示了如何在Android平台上使用OpenGL ES进行三维空间图形渲染,通过编写jiem.java、MyGLSurfaceView.java和zgyCH.java三个关键文件,实现圆柱体的绘制效果。
摘要由CSDN通过智能技术生成

效果图:


编写jiem.java

    *指定屏幕所要显示的假面,并对见、界面进行相关设置
    *为Activity设置恢复处理,当Acitvity恢复设置时显示界面同样应该恢复
    *当Activity暂停设置时,显示界面同样应该暂停

  
package com.scout.eeeeeee;

import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class jiem extends Activity {
    private MyGLSurfaceView mGLSurfaceView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        mGLSurfaceView = new MyGLSurfaceView(this);
        setContentView(mGLSurfaceView);
        mGLSurfaceView.setFocusableInTouchMode(true);//设置为可触控
        mGLSurfaceView.requestFocus();//获取焦点
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGLSurfaceView.onPause();
    }
}

编写MyGLSurfaceView.java实现场景加载和渲染功能

package com.scout.eeeeeee;

/**
 * Created by liuguodong on 2017/10/29.
 */

import java.io.IOException;
import java.io.InputStream;

import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.MotionEvent;

public class MyGLSurfaceView extends GLSurfaceView {
    private final float suo = 180.0f/320;//角度缩放比例
    private SceneRenderer mRenderer;//场景渲染器
    private float shangY;//上次的触控位置Y坐标
    private float shangX;//上次的触控位置Y坐标
    private int lightAngle=90;//灯的当前角度

    public MyGLSurfaceView(Context context) {
        super(context);
        mRenderer = new SceneRenderer();   //创建场景渲染器
        setRenderer(mRenderer);             //设置渲染器
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
    }

    //触摸事件回调方法
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        float y = e.getY();
        float x = e.getX();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dy = y - shangY;//计算触控笔Y位移
                float dx = x - shangX;//计算触控笔Y位移
                mRenderer.cylinder.mAngleX += dy * suo;//设置沿x轴旋转角度
                mRenderer.cylinder.mAngleZ += dx * suo;//设置沿z轴旋转角度
                requestRender();//重绘画面
        }
        shangY = y;//记录触控笔位置
        shangX = x;//记录触控笔位置
        return true;
    }

    private class SceneRenderer implements GLSurfaceView.Renderer
    {
        int textureId;//纹理名称ID
        zhuCH cylinder;//创建圆柱体

        public SceneRenderer()
        {

        }

        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.glPushMatrix();//保护变换矩阵现场

            float lx=0; //设定光源的位置
            float ly=(float)(7*Math.cos(Math.toRadians(lightAngle)));
            float lz=(float)(7*Math.sin(Math.toRadians(lightAngle)));
            float[] positionParamsRed={
   lx,ly,lz,0};
            gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionParamsRed,0);

            initMaterial(gl);<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值