android平台下OpenGL ES 3.0绘制纯色背景

OpenGL ES 3.0学习实践

目录

新建工程

RendererActivity文件

public class RendererActivity extends AbsBaseActivity {

    private GLSurfaceView mGLSurfaceView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupViews();
    }

    private void setupViews() {
        mGLSurfaceView = new GLSurfaceView(this);
        setContentView(mGLSurfaceView);
        //设置版本
        mGLSurfaceView.setEGLContextClientVersion(3);
        //GLSurfaceView.Renderer renderer = new ColorRenderer(Color.GRAY);
        GLSurfaceView.Renderer renderer = new NativeColorRenderer(Color.GRAY);
        mGLSurfaceView.setRenderer(renderer);
    }

}
基于SDK实现渲染器

新建渲染器ColorRenderer文件

public class ColorRenderer implements GLSurfaceView.Renderer {

    private int color;

    public ColorRenderer(int color) {
        this.color = color;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //设置背景颜色
        float redF = (float) Color.red(color) / 255;
        float greenF = (float) Color.green(color) / 255;
        float blueF = (float) Color.blue(color) / 255;
        float alphaF = (float) Color.alpha(color) / 255;
        GLES30.glClearColor(redF, greenF, blueF, alphaF);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        //设置视图窗口
        GLES30.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        //把颜色缓冲区设置为我们预设的颜色
        GLES30.glClear(GL10.GL_COLOR_BUFFER_BIT);
    }
}
基于NDK实现渲染器

新建渲染器NativeColorRenderer文件

public class NativeColorRenderer implements GLSurfaceView.Renderer {

    static {
        System.loadLibrary("native-color");
    }

    public native void surfaceCreated(int color);

    public native void surfaceChanged(int width, int height);

    public native void onDrawFrame();

    private int color;

    public NativeColorRenderer(int color) {
        this.color = color;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        surfaceCreated(color);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        surfaceChanged(width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        onDrawFrame();
    }
}

配置对应的CMakeLists.txt文件


cmake_minimum_required(VERSION 3.4.1)

##官方标准配置
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions -Wall")

##ANDROID_PLATFORM_LEVEL=18
add_definitions("-DDYNAMIC_ES3")
set(OPENGL_LIB GLESv3)

add_library(native-color
           SHARED
           src/main/cpp/native_color.cpp)

target_link_libraries(native-color
            ${OPENGL_LIB}
            android
            EGL
            log
            m)

编写对应的native_color.cpp类


#include <jni.h>
#include <EGL/egl.h>
#include <GLES3/gl3.h>

#include "native_color.h"

/**
 * 动态注册
 */
JNINativeMethod methods[] = {
        {"surfaceCreated", "(I)V",  (void *) surfaceCreated},
        {"surfaceChanged", "(II)V", (void *) surfaceChanged},
        {"onDrawFrame",    "()V",   (void *) onDrawFrame}
};

/**
 * 动态注册
 * @param env
 * @return
 */
jint registerNativeMethod(JNIEnv *env) {
    jclass cl = env->FindClass("com/onzhou/opengles/color/NativeColorRenderer");
    if ((env->RegisterNatives(cl, methods, sizeof(methods) / sizeof(methods[0]))) < 0) {
        return -1;
    }
    return 0;
}

/**
 * 加载默认回调
 * @param vm 
 * @param reserved 
 * @return 
 */
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env = NULL;
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        return -1;
    }
    //注册方法
    if (registerNativeMethod(env) != JNI_OK) {
        return -1;
    }
    return JNI_VERSION_1_6;
}

JNIEXPORT void JNICALL surfaceCreated(JNIEnv *env, jobject obj, jint color) {
	//分离RGBA的百分比
    GLfloat redF = ((color >> 16) & 0xFF) * 1.0f / 255;
    GLfloat greenF = ((color >> 8) & 0xFF) * 1.0f / 255;
    GLfloat blueF = (color & 0xFF) * 1.0f / 255;
    GLfloat alphaF = ((color >> 24) & 0xFF) * 1.0f / 255;
    glClearColor(redF, greenF, blueF, alphaF);
}

JNIEXPORT void JNICALL surfaceChanged(JNIEnv *env, jobject obj, jint width, jint height) {
    glViewport(0, 0, width, height);
}

JNIEXPORT void JNICALL onDrawFrame(JNIEnv *env, jobject obj) {
    //把颜色缓冲区设置为我们预设的颜色
    glClear(GL_COLOR_BUFFER_BIT);
}

实现效果:

项目地址:
https://github.com/byhook/opengles4android

参考:
《OpenGL ES 3.0 编程指南第2版》
《OpenGL ES应用开发实践指南Android卷》

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用OpenGL ES 3.0绘制球形的示例代码: ``` #include <GLES3/gl3.h> #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <vector> // 定义球形的顶点和索引 const int SPHERE_LONG = 36; const int SPHERE_LAT = 18; std::vector<GLfloat> sphereVertices; std::vector<GLuint> sphereIndices; void generateSphere() { sphereVertices.clear(); sphereIndices.clear(); for (int j = 0; j <= SPHERE_LAT; j++) { float lat0 = glm::pi<float>() * (-0.5f + (float)(j - 1) / SPHERE_LAT); float z0 = sin(lat0); float zr0 = cos(lat0); float lat1 = glm::pi<float>() * (-0.5f + (float)j / SPHERE_LAT); float z1 = sin(lat1); float zr1 = cos(lat1); for (int i = 0; i <= SPHERE_LONG; i++) { float lng = 2 * glm::pi<float>() * (float)(i - 1) / SPHERE_LONG; float x = cos(lng); float y = sin(lng); sphereVertices.push_back(x * zr0); sphereVertices.push_back(y * zr0); sphereVertices.push_back(z0); sphereVertices.push_back(x * zr1); sphereVertices.push_back(y * zr1); sphereVertices.push_back(z1); } } for (int i = 0; i < SPHERE_LAT; i++) { for (int j = 0; j < SPHERE_LONG; j++) { int a = (SPHERE_LONG + 1) * i + j; int b = (SPHERE_LONG + 1) * (i + 1) + j; int c = (SPHERE_LONG + 1) * (i + 1) + (j + 1); int d = (SPHERE_LONG + 1) * i + (j + 1); sphereIndices.push_back(a); sphereIndices.push_back(b); sphereIndices.push_back(c); sphereIndices.push_back(a); sphereIndices.push_back(c); sphereIndices.push_back(d); } } } // 渲染球形 void renderSphere(GLuint program) { GLuint positionLocation = glGetAttribLocation(program, "a_Position"); glEnableVertexAttribArray(positionLocation); glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), &sphereVertices[0]); glDrawElements(GL_TRIANGLES, sphereIndices.size(), GL_UNSIGNED_INT, &sphereIndices[0]); glDisableVertexAttribArray(positionLocation); } ``` 使用时需要先调用`generateSphere()`函数生成球形的顶点和索引,然后在渲染时调用`renderSphere()`函数即可。需要注意的是,需要在OpenGL ES 3.0环境下运行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值