Skip to content

Commit 4233252

Browse files
committed
Sample code for OpenGL Android training class
( Original Id: Ib1650025a3c4b018c60d70ede3f25113660f68d8 ) Change-Id: I1d760b75d1f2bfe1ec90c71471867577bd146241 (cherry picked from commit 66f6fe6)
1 parent 8180be1 commit 4233252

File tree

20 files changed

+1221
-0
lines changed

20 files changed

+1221
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Files ignored for git source control
2+
#
3+
# Add this file to source control so the following files
4+
# are not tracked and added to git:
5+
.project
6+
.classpath
7+
project.properties
8+
bin/
9+
gen/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2011 The Android Open Source Project.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<manifest
18+
xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.example.android.opengl"
20+
android:versionCode="1"
21+
android:versionName="1.0" >
22+
23+
<uses-sdk
24+
android:minSdkVersion="4"
25+
android:targetSdkVersion="17" />
26+
27+
<!-- Tell the system this app requires OpenGL ES 1.0 or higher-->
28+
<uses-feature android:glEsVersion="0x00010000" />
29+
30+
<application
31+
android:icon="@drawable/ic_launcher"
32+
android:label="@string/app_name"
33+
android:allowBackup="true" >
34+
<activity
35+
android:name="com.example.android.opengl.OpenGLES10Activity"
36+
android:label="@string/app_name" >
37+
<intent-filter>
38+
<action android:name="android.intent.action.MAIN" />
39+
<category android:name="android.intent.category.LAUNCHER" />
40+
</intent-filter>
41+
</activity>
42+
</application>
43+
44+
</manifest>
5.11 KB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2011 The Android Open Source Project.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<LinearLayout
18+
xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:layout_width="fill_parent"
20+
android:layout_height="fill_parent"
21+
android:orientation="vertical" >
22+
23+
<TextView
24+
android:layout_width="fill_parent"
25+
android:layout_height="wrap_content"
26+
android:text="@string/hello" />
27+
28+
</LinearLayout>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2011 The Android Open Source Project.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<resources>
18+
<string name="hello">Hello, OpenGL ES 1.0!</string>
19+
<string name="app_name">Hello OpenGL ES 1.0</string>
20+
</resources>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.android.opengl;
17+
18+
import javax.microedition.khronos.egl.EGLConfig;
19+
import javax.microedition.khronos.opengles.GL10;
20+
21+
import android.opengl.GLSurfaceView;
22+
import android.opengl.GLU;
23+
24+
/**
25+
* Provides drawing instructions for a GLSurfaceView object. This class
26+
* must override the OpenGL ES drawing lifecycle methods:
27+
* <ul>
28+
* <li>{@link android.opengl.GLSurfaceView.Renderer#onSurfaceCreated}</li>
29+
* <li>{@link android.opengl.GLSurfaceView.Renderer#onDrawFrame}</li>
30+
* <li>{@link android.opengl.GLSurfaceView.Renderer#onSurfaceChanged}</li>
31+
* </ul>
32+
*/
33+
public class MyGLRenderer implements GLSurfaceView.Renderer {
34+
35+
private Triangle mTriangle;
36+
private Square mSquare;
37+
private float mAngle;
38+
39+
@Override
40+
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
41+
// Set the background frame color
42+
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
43+
44+
mTriangle = new Triangle();
45+
mSquare = new Square();
46+
}
47+
48+
@Override
49+
public void onDrawFrame(GL10 gl) {
50+
51+
// Draw background color
52+
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
53+
54+
// Set GL_MODELVIEW transformation mode
55+
gl.glMatrixMode(GL10.GL_MODELVIEW);
56+
gl.glLoadIdentity(); // reset the matrix to its default state
57+
58+
// When using GL_MODELVIEW, you must set the view point
59+
GLU.gluLookAt(gl, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
60+
61+
// Draw square
62+
mSquare.draw(gl);
63+
64+
// Create a rotation for the triangle
65+
66+
// Use the following code to generate constant rotation.
67+
// Leave this code out when using TouchEvents.
68+
// long time = SystemClock.uptimeMillis() % 4000L;
69+
// float angle = 0.090f * ((int) time);
70+
71+
gl.glRotatef(mAngle, 0.0f, 0.0f, 1.0f);
72+
73+
// Draw triangle
74+
mTriangle.draw(gl);
75+
}
76+
77+
@Override
78+
public void onSurfaceChanged(GL10 gl, int width, int height) {
79+
// Adjust the viewport based on geometry changes
80+
// such as screen rotations
81+
gl.glViewport(0, 0, width, height);
82+
83+
// make adjustments for screen ratio
84+
float ratio = (float) width / height;
85+
gl.glMatrixMode(GL10.GL_PROJECTION); // set matrix to projection mode
86+
gl.glLoadIdentity(); // reset the matrix to its default state
87+
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7); // apply the projection matrix
88+
}
89+
90+
/**
91+
* Returns the rotation angle of the triangle shape (mTriangle).
92+
*
93+
* @return - A float representing the rotation angle.
94+
*/
95+
public float getAngle() {
96+
return mAngle;
97+
}
98+
99+
/**
100+
* Sets the rotation angle of the triangle shape (mTriangle).
101+
*/
102+
public void setAngle(float angle) {
103+
mAngle = angle;
104+
}
105+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.android.opengl;
17+
18+
import android.content.Context;
19+
import android.opengl.GLSurfaceView;
20+
import android.view.MotionEvent;
21+
22+
/**
23+
* A view container where OpenGL ES graphics can be drawn on screen.
24+
* This view can also be used to capture touch events, such as a user
25+
* interacting with drawn objects.
26+
*/
27+
public class MyGLSurfaceView extends GLSurfaceView {
28+
29+
private final MyGLRenderer mRenderer;
30+
31+
public MyGLSurfaceView(Context context) {
32+
super(context);
33+
34+
// Set the Renderer for drawing on the GLSurfaceView
35+
mRenderer = new MyGLRenderer();
36+
setRenderer(mRenderer);
37+
38+
// Render the view only when there is a change in the drawing data
39+
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
40+
}
41+
42+
private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
43+
private float mPreviousX;
44+
private float mPreviousY;
45+
46+
@Override
47+
public boolean onTouchEvent(MotionEvent e) {
48+
// MotionEvent reports input details from the touch screen
49+
// and other input controls. In this case, we are only
50+
// interested in events where the touch position changed.
51+
52+
float x = e.getX();
53+
float y = e.getY();
54+
55+
switch (e.getAction()) {
56+
case MotionEvent.ACTION_MOVE:
57+
58+
float dx = x - mPreviousX;
59+
float dy = y - mPreviousY;
60+
61+
// reverse direction of rotation above the mid-line
62+
if (y > getHeight() / 2) {
63+
dx = dx * -1 ;
64+
}
65+
66+
// reverse direction of rotation to left of the mid-line
67+
if (x < getWidth() / 2) {
68+
dy = dy * -1 ;
69+
}
70+
71+
mRenderer.setAngle(
72+
mRenderer.getAngle() +
73+
((dx + dy) * TOUCH_SCALE_FACTOR)); // = 180.0f / 320
74+
requestRender();
75+
}
76+
77+
mPreviousX = x;
78+
mPreviousY = y;
79+
return true;
80+
}
81+
82+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.android.opengl;
17+
18+
import android.app.Activity;
19+
import android.opengl.GLSurfaceView;
20+
import android.os.Bundle;
21+
22+
public class OpenGLES10Activity extends Activity {
23+
24+
private GLSurfaceView mGLView;
25+
26+
@Override
27+
public void onCreate(Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
30+
// Create a GLSurfaceView instance and set it
31+
// as the ContentView for this Activity.
32+
mGLView = new MyGLSurfaceView(this);
33+
setContentView(mGLView);
34+
}
35+
36+
@Override
37+
protected void onPause() {
38+
// The following call pauses the rendering thread.
39+
// If your OpenGL application is memory intensive,
40+
// you should consider de-allocating objects that
41+
// consume significant memory here.
42+
super.onPause();
43+
mGLView.onPause();
44+
}
45+
46+
@Override
47+
protected void onResume() {
48+
// The following call resumes a paused rendering thread.
49+
// If you de-allocated graphic objects for onPause()
50+
// this is a good place to re-allocate them.
51+
super.onResume();
52+
mGLView.onResume();
53+
}
54+
55+
}

0 commit comments

Comments
 (0)