Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 18fc7b85 authored by Luan Nguyen's avatar Luan Nguyen Committed by Android Git Automerger
Browse files

am ba795593: am 9b432bbe: am e49fbfa2: Merge "docs: Fix various issues with...

am ba795593: am 9b432bbe: am e49fbfa2: Merge "docs: Fix various issues with incorrect code samples." into lmp-docs

* commit 'ba795593':
  docs: Fix various issues with incorrect code samples.
parents f35e8159 ba795593
Loading
Loading
Loading
Loading
−4.07 KiB (7.28 KiB)
Loading image diff...
−3.08 KiB (9.46 KiB)
Loading image diff...
−3.96 KiB (6.96 KiB)
Loading image diff...
+70 −26
Original line number Diff line number Diff line
@@ -50,6 +50,12 @@ android.opengl.GLSurfaceView.Renderer#onSurfaceCreated onSurfaceCreated()} metho
for memory and processing efficiency.</p>

<pre>
public class MyGLRenderer implements GLSurfaceView.Renderer {

    ...
    private Triangle mTriangle;
    private Square   mSquare;

    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        ...

@@ -58,6 +64,8 @@ public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        // initialize a square
        mSquare = new Square();
    }
    ...
}
</pre>


@@ -77,9 +85,12 @@ one or more shapes.</li>

<p>You need at least one vertex shader to draw a shape and one fragment shader to color that shape.
These shaders must be complied and then added to an OpenGL ES program, which is then used to draw
the shape. Here is an example of how to define basic shaders you can use to draw a shape:</p>
the shape. Here is an example of how to define basic shaders you can use to draw a shape in the
<code>Triangle</code> class:</p>

<pre>
public class Triangle {

    private final String vertexShaderCode =
        "attribute vec4 vPosition;" +
        "void main() {" +
@@ -92,6 +103,9 @@ private final String fragmentShaderCode =
        "void main() {" +
        "  gl_FragColor = vColor;" +
        "}";

    ...
}
</pre>

<p>Shaders contain OpenGL Shading Language (GLSL) code that must be compiled prior to using it in
@@ -125,13 +139,28 @@ get created once and then cached for later use.</p>
public class Triangle() {
    ...

    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    private final int mProgram;

    public Triangle() {
        ...

        int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
                                        vertexShaderCode);
        int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
                                        fragmentShaderCode);

        // create empty OpenGL ES Program
        mProgram = GLES20.glCreateProgram();

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);                  // creates OpenGL ES program executables
        // add the vertex shader to program
        GLES20.glAttachShader(mProgram, vertexShader);

        // add the fragment shader to program
        GLES20.glAttachShader(mProgram, fragmentShader);

        // creates OpenGL ES program executables
        GLES20.glLinkProgram(mProgram);
    }
}
</pre>

@@ -145,6 +174,12 @@ color values to the shape’s vertex shader and fragment shader, and then execut
function.</p>

<pre>
private int mPositionHandle;
private int mColorHandle;

private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

public void draw() {
    // Add program to OpenGL ES environment
    GLES20.glUseProgram(mProgram);
@@ -176,8 +211,17 @@ public void draw() {

<p>Once you have all this code in place, drawing this object just requires a call to the
{@code draw()} method from within your renderer’s {@link
android.opengl.GLSurfaceView.Renderer#onDrawFrame onDrawFrame()} method. When you run the
application, it should look something like this:</p>
android.opengl.GLSurfaceView.Renderer#onDrawFrame onDrawFrame()} method:

<pre>
public void onDrawFrame(GL10 unused) {
    ...

    mTriangle.draw();
}
</pre>

<p>When you run the application, it should look something like this:</p>

<img src="{@docRoot}images/opengl/ogl-triangle.png">
<p class="img-caption">
+10 −16
Original line number Diff line number Diff line
@@ -129,28 +129,22 @@ just create an inner class in the activity that uses it:</p>
<pre>
class MyGLSurfaceView extends GLSurfaceView {

    private final MyGLRenderer mRenderer;

    public MyGLSurfaceView(Context context){
        super(context);

        // Create an OpenGL ES 2.0 context
        setEGLContextClientVersion(2);

        mRenderer = new MyGLRenderer();

        // Set the Renderer for drawing on the GLSurfaceView
        setRenderer(new MyRenderer());
        setRenderer(mRenderer);
    }
}
</pre>

<p>When using OpenGL ES 2.0, you must add another call to your {@link android.opengl.GLSurfaceView}
constructor, specifying that you want to use the 2.0 API:</p>

<pre>
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
</pre>

<p class="note"><strong>Note:</strong> If you are using the OpenGL ES 2.0 API, make sure you declare
this in your application manifest. For more information, see <a href="#manifest">Declare OpenGL ES
Use
in the Manifest</a>.</p>

<p>One other optional addition to your {@link android.opengl.GLSurfaceView} implementation is to set
the render mode to only draw the view when there is a change to your drawing data using the
{@link android.opengl.GLSurfaceView#RENDERMODE_WHEN_DIRTY GLSurfaceView.RENDERMODE_WHEN_DIRTY}
@@ -186,7 +180,7 @@ the geometry of the view changes, for example when the device's screen orientati
</ul>

<p>Here is a very basic implementation of an OpenGL ES renderer, that does nothing more than draw a
gray background in the {@link android.opengl.GLSurfaceView}:</p>
black background in the {@link android.opengl.GLSurfaceView}:</p>

<pre>
public class MyGLRenderer implements GLSurfaceView.Renderer {
@@ -208,7 +202,7 @@ public class MyGLRenderer implements GLSurfaceView.Renderer {
</pre>

<p>That’s all there is to it! The code examples above create a simple Android application that
displays a gray screen using OpenGL. While this code does not do anything very interesting, by
displays a black screen using OpenGL. While this code does not do anything very interesting, by
creating these classes, you have laid the foundation you need to start drawing graphic elements with
OpenGL.</p>

Loading