Skip to content

Commit

Permalink
android open gl test
Browse files Browse the repository at this point in the history
  • Loading branch information
chongbo2013 committed Feb 15, 2016
0 parents commit 00c71f4
Show file tree
Hide file tree
Showing 37 changed files with 719 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "xu.ferris.launcher3deffect"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\ruanjian\adt\android-sdk-windows/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xu.ferris.launcher3deffect;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
20 changes: 20 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xu.ferris.launcher3deffect">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".EffectActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
15 changes: 15 additions & 0 deletions app/src/main/java/xu/ferris/launcher3deffect/EffectActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package xu.ferris.launcher3deffect;

import android.opengl.GLSurfaceView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class EffectActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView view=new HelloOpenGLES10SurfaceView(this);
setContentView(view);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package xu.ferris.launcher3deffect;

import android.opengl.GLSurfaceView;
import android.opengl.GLU;

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

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

/**
* Created by ferris on 2016/2/15.
*/
public class HelloOpenGLES10Renderer implements GLSurfaceView.Renderer {

float rotateQuad;


// 正方形的四个顶点
private FloatBuffer quateBuffer;

/**
* 初始化正方形
* 将float数组转换存储在字节缓冲数组
*/
private void initQuate() {

float[] four = { -1f, -1f, 1f,
1f, -1f, 1f,
-1f, 1f, 1f,
1f, 1f, 1f, };

ByteBuffer vbb = ByteBuffer.allocateDirect(
//分配缓冲空间,一个float占4个字节
four.length * 4);
vbb.order(ByteOrder.nativeOrder());//设置字节顺序, 其中ByteOrder.nativeOrder()是获取本机字节顺序
quateBuffer = vbb.asFloatBuffer();//转换为float型
quateBuffer.put(four); //添加数据
quateBuffer.position(0); //设置数组的起始位置

}



public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// 设置背景色
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
initQuate();
// 指定需要启用定点数组
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

}

//所有的绘图操作都在此方法中执行
public void onDrawFrame(GL10 gl) {

//清楚屏幕和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//切换至模型观察矩阵
gl.glMatrixMode(GL10.GL_MODELVIEW);
//重置当前的模型观察矩阵
gl.glLoadIdentity();
//确定视图点

//相当于我们的脑袋位置在(0, 0, -5)处,眼睛望向(0f, 0f, 0f),即原点。
// 后面的三个参数(0.0,1.0,0.0),y轴为1,其余为0,表示脑袋朝上,就是正常的情况。看到的情况如下图:
GLU.gluLookAt(gl, 0f, 0f, 5f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

// 创建一个旋转的正方形 沿X轴旋转
gl.glRotatef(rotateQuad, 0.0f, 1f, 0f);
//平移到中心
gl.glTranslatef(0, 0, -1f);
// 画一个正方形
gl.glColor4f(0.63671875f, 0.76953125f, 0.22265625f, 0.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, quateBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
// 改变旋转角度
rotateQuad += 0.5f;
}

public void onSurfaceChanged(GL10 gl, int width, int height) {

//设置OpenGL的场景大小
gl.glViewport(0, 0, width, height);

float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION); //设置投影矩阵
gl.glLoadIdentity(); //设置矩阵为单位矩阵,相当于重置矩阵
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7); // 应用投影矩阵

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package xu.ferris.launcher3deffect;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

/**
* Created by Administrator on 2016/2/15.
*/
public class HelloOpenGLES10SurfaceView extends GLSurfaceView {

public HelloOpenGLES10SurfaceView(Context context) {
super(context);
init();
}

public HelloOpenGLES10SurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init(){
setRenderer(new HelloOpenGLES10Renderer());
}
}
Loading

0 comments on commit 00c71f4

Please sign in to comment.