图解-安卓中调用OpenGL – zlt

游戏开发中经常使用到OpenGL,当然很多人都喜欢直接用现有的游戏引擎,但相信了解的更多对你没有坏处

 安卓开发中,采用的OpenGL ex2的规范,前几天看了下这个规范,整体上难度比1.0规范难度加大了很多。

全面采用shader进行开发,不在是以前的固定管线方式了。是个很大的提升,估计在速度上也会有很大的提升。

(本人还没有亲自做这个实验,到底快多少)但是难度却是比之前大了很多,你至少要自己编写shader代码,对

数学有很大的要求了,废话不多说,进入正题。

   首先建立一个andriod的项目,这个过程在前两篇中都有介绍,不知道了,可以看下(安卓环境搭建)或者

图解安卓-c++开发-通过java 调用c++ jni的使用 这两篇文章。

  

到NDK的目录下 C:andriodandroid-ndk-r9bsampleshello-gl2srccomandroidgl2jni ,将GL2JNILib.java GL2JNIView.java加入到刚刚建立的工程中,可以直接通过

鼠标拖到工程中。

修改如下程序:

package com.example.gldemo;
// Wrapper for native library

public class GL2JNILib {

static {
System.loadLibrary(“gl2jni”);
}

/**
* @param width the current view width
* @param height the current view height
*/
public static native void init(int width, int height);
public static native void step();
}

  

修改 GL2JNIView.java中的代码:红色部分

/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.gldemo;
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import android.content.Context;

然后在MainActivity.java中增加调用程序:

package com.example.gldemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

GL2JNIView mView;

@Override protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mView
= new GL2JNIView(getApplication());
setContentView(mView);
}

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

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

}

到目前为止,java的部分就做完了。‘

然后增加对Jni的支持,调用OpenGL的代码,JNI部分可以参考 [置顶]图解安卓-c++开发-通过java 调用c++ jni的使用

在Andriod.mak中增加库支持:LOCAL_LDLIBS    := -llog -lGLESv2

 

c++ 代码如下:

/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// OpenGL ES 2.0 code

#include
<jni.h>
#include
<android/log.h>

#include <GLES2/gl2.h>
#include
<GLES2/gl2ext.h>

#include <stdio.h>
#include
<stdlib.h>
#include
<math.h>

#define LOG_TAG “libgl2jni”
#define LOGI(…) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(…) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

static void printGLString(const char *name, GLenum s) {
const char *v = (const char *) glGetString(s);
LOGI(
GL %s = %sn, name, v);
}

static void checkGlError(const char* op) {
for (GLint error = glGetError(); error; error
= glGetError()) {
LOGI(
after %s() glError (0x%x)n, op, error);
}
}

static const char gVertexShader[] =
attribute vec4 vPosition;n
void main() {n
gl_Position = vPosition;n
}n;

static const char gFragmentShader[] =
precision mediump float;n
void main() {n
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);n
}n;

GLuint loadShader(GLenum shaderType, const char* pSource) {
GLuint shader
= glCreateShader(shaderType);
if (shader) {
glShaderSource(shader,
1, &pSource, NULL);
glCompileShader(shader);
GLint compiled
= 0;
glGetShaderiv(shader, GL_COMPILE_STATUS,
&compiled);
if (!compiled) {
GLint infoLen
= 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH,
&infoLen);
if (infoLen) {
char* buf = (char*) malloc(infoLen);
if (buf) {
glGetShaderInfoLog(shader, infoLen, NULL, buf);
LOGE(
Could not compile shader %d:n%sn,
shaderType, buf);
free(buf);
}
glDeleteShader(shader);
shader
= 0;
}
}
}
return shader;
}

GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
GLuint vertexShader
= loadShader(GL_VERTEX_SHADER, pVertexSource);
if (!vertexShader) {
return 0;
}

GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
if (!pixelShader) {
return 0;
}

GLuint program = glCreateProgram();
if (program) {
glAttachShader(program, vertexShader);
checkGlError(
glAttachShader);
glAttachShader(program, pixelShader);
checkGlError(
glAttachShader);
glLinkProgram(program);
GLint linkStatus
= GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS,
&linkStatus);
if (linkStatus != GL_TRUE) {
GLint bufLength
= 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH,
&bufLength);
if (bufLength) {
char* buf = (char*) malloc(bufLength);
if (buf) {
glGetProgramInfoLog(program, bufLength, NULL, buf);
free(buf);
}
}
glDeleteProgram(program);
program
= 0;
}
}
return program;
}

GLuint gProgram;
GLuint gvPositionHandle;

bool setupGraphics(int w, int h) {
printGLString(
Version, GL_VERSION);
printGLString(
Vendor, GL_VENDOR);
printGLString(
Renderer, GL_RENDERER);
printGLString(
Extensions, GL_EXTENSIONS);

LOGI(setupGraphics(%d, %d), w, h);
gProgram
= createProgram(gVertexShader, gFragmentShader);
if (!gProgram) {
LOGE(
Could not create program.);
return false;
}
gvPositionHandle
= glGetAttribLocation(gProgram, vPosition);
checkGlError(
glGetAttribLocation);
LOGI(
glGetAttribLocation(“vPosition”) = %dn,
gvPositionHandle);

glViewport(0, 0, w, h);
checkGlError(
glViewport);
return true;
}

const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, –0.5f, –0.5f,
0.5f, –0.5f };

void renderFrame() {
static float grey;
grey
+= 0.01f;
if (grey > 1.0f) {
grey
= 0.0f;
}
glClearColor(grey, grey, grey,
1.0f);
checkGlError(
glClearColor);
glClear( GL_DEPTH_BUFFER_BIT
| GL_COLOR_BUFFER_BIT);
checkGlError(
glClear);

glUseProgram(gProgram);
checkGlError(glUseProgram);

glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
checkGlError(
glVertexAttribPointer);
glEnableVertexAttribArray(gvPositionHandle);
checkGlError(
glEnableVertexAttribArray);
glDrawArrays(GL_TRIANGLES,
0, 3);
checkGlError(
glDrawArrays);
}

extern C {
JNIEXPORT
void JNICALL Java_com_example_gldemo_GL2JNILib_init(JNIEnv * env, jobject obj, jint width, jint height);
JNIEXPORT
void JNICALL Java_com_example_gldemo_GL2JNILib_step(JNIEnv * env, jobject obj);
};

JNIEXPORT void JNICALL Java_com_example_gldemo_GL2JNILib_init(JNIEnv * env, jobject obj, jint width, jint height)
{
setupGraphics(width, height);
}

JNIEXPORT void JNICALL Java_com_example_gldemo_GL2JNILib_step(JNIEnv * env, jobject obj)
{
renderFrame();
}

编译运行效果如下所示:

 

完成 。

代码包下载

本文链接:http://www.cnblogs.com/zhanglitong/p/3448488.html,转载请注明。



You must enable javascript to see captcha here!

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress

无觅相关文章插件,快速提升流量