GLES实战01

正常程序都是在CPU上执行,而gles是在GPU上运行的。

先简单实现一个直角三角形

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.opengl.GLSurfaceView
        android:id="@+id/gl_surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.lzc.gles01;

import androidx.appcompat.app.AppCompatActivity;

import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private GLSurfaceView mGlSurfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGlSurfaceView = findViewById(R.id.gl_surface);
        mGlSurfaceView.setRenderer(new MyRender());
    }

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

    @Override
    protected void onResume() {
        super.onResume();
        mGlSurfaceView.onResume();
    }
package com.lzc.gles01;

import android.opengl.GLES30;
import android.opengl.GLSurfaceView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

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

public class MyRender implements GLSurfaceView.Renderer {

    //是一个直角三角形的坐标系
    private final float mVertexes[] = {
        0.5f, 0.5f, 0f,
        -0.5f, -0.5f, 0f,
        0.5f, -0.5f, 0f,
    };

    //将坐标转换为OpenGl理解的形式
    private FloatBuffer mVertexBuffer;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    	//初始化清理颜色
        GLES30.glClearColor(0f, 0f, 0f, 0f);
        init();
    }

    private void init() {
    	//这里是将坐标转换为OpenGl可以理解的坐标
        ByteBuffer vbb = ByteBuffer.allocateDirect(mVertexes.length * Float.BYTES);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(mVertexes);
        mVertexBuffer.position(0);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        //宽和高可以不是手机的宽和高,可以自己更改
        GLES30.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
    	//初始化清理GLES30.GL_COLOR_BUFFER_BIT
        GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
        //启用顶点数组
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //点是由3个浮点数组成,类型为float
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
        //这三个点要绘制一个三角形,first代表从第一个点开始绘制
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
    }
}

运行截图如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值