Android:Renderer 的两种不同用途

发布于 2025-01-06 01:31:35 字数 4120 浏览 1 评论 0原文

我使用Renderer的两种方式来绘制三角形。一种是工作,一种是没有工作。

这是我的两个解决方案。

第一个解决方案:运行时无法工作。我实现了 Renderer (您应该看到以下行:glView.setRenderer 方法。这是我的两个解决方案的基本区别。)

package com.test;

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

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

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

public class TriangleOpenGLTest extends Activity implements Renderer{

    FloatBuffer vertices;
    ByteBuffer byteBuffer;
    GL10 gl;
    GLSurfaceView glView;


    @Override 
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        byteBuffer = ByteBuffer.allocateDirect(3*2*4);
        byteBuffer.order(ByteOrder.nativeOrder());

        vertices = byteBuffer.asFloatBuffer();
        vertices.put(new float[] { 0.0f, 0.0f, 319.0f, 0.0f, 160.0f, 479.0f});
        vertices.flip();

        glView = new GLSurfaceView(this);
        glView.setRenderer(this);
        setContentView(glView);

    }

    @Override
    public void onResume(){
        super.onResume();
            gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);
            gl.glColor4f(1, 0, 0, 1);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);       
    }

            @Override
        public void onDrawFrame(GL10 gl) {
        }

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

        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        }
}

解决方案 2:我为我的渲染器。

package com.test;

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

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

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

public class TriangleOpenGLTest extends Activity {

    FloatBuffer vertices;
    ByteBuffer byteBuffer;
    GL10 gl;
    GLSurfaceView glView;


    @Override 
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        byteBuffer = ByteBuffer.allocateDirect(3*2*4);
        byteBuffer.order(ByteOrder.nativeOrder());

        vertices = byteBuffer.asFloatBuffer();
        vertices.put(new float[] { 0.0f, 0.0f, 319.0f, 0.0f, 160.0f, 479.0f});
        vertices.flip();

        glView = new GLSurfaceView(this);
        glView.setRenderer(new DrawTriangle());
        setContentView(glView);

    }

    @Override
    public void onResume(){
        super.onResume();
    }

    public class DrawTriangle implements Renderer{

        public DrawTriangle(){

        }
        @Override
        public void onDrawFrame(GL10 gl) {

            gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);
            gl.glColor4f(1, 0, 0, 1);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);       

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            // TODO Auto-generated method stub

        }

    }

}

谁能帮我解释一下上述两种解决方案有什么区别。

谢谢 :)

I use two ways of Renderer to draw a triangle. One is work and one is none.

Here is my two solutions.

First Solution: cannot work when run. I emplements Renderer (You should see the line :glView.setRenderer method. That the basic difference of my two solutions.)

package com.test;

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

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

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

public class TriangleOpenGLTest extends Activity implements Renderer{

    FloatBuffer vertices;
    ByteBuffer byteBuffer;
    GL10 gl;
    GLSurfaceView glView;


    @Override 
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        byteBuffer = ByteBuffer.allocateDirect(3*2*4);
        byteBuffer.order(ByteOrder.nativeOrder());

        vertices = byteBuffer.asFloatBuffer();
        vertices.put(new float[] { 0.0f, 0.0f, 319.0f, 0.0f, 160.0f, 479.0f});
        vertices.flip();

        glView = new GLSurfaceView(this);
        glView.setRenderer(this);
        setContentView(glView);

    }

    @Override
    public void onResume(){
        super.onResume();
            gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);
            gl.glColor4f(1, 0, 0, 1);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);       
    }

            @Override
        public void onDrawFrame(GL10 gl) {
        }

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

        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        }
}

Solution 2: I create a new sub-class for my Renderer.

package com.test;

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

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

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

public class TriangleOpenGLTest extends Activity {

    FloatBuffer vertices;
    ByteBuffer byteBuffer;
    GL10 gl;
    GLSurfaceView glView;


    @Override 
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        byteBuffer = ByteBuffer.allocateDirect(3*2*4);
        byteBuffer.order(ByteOrder.nativeOrder());

        vertices = byteBuffer.asFloatBuffer();
        vertices.put(new float[] { 0.0f, 0.0f, 319.0f, 0.0f, 160.0f, 479.0f});
        vertices.flip();

        glView = new GLSurfaceView(this);
        glView.setRenderer(new DrawTriangle());
        setContentView(glView);

    }

    @Override
    public void onResume(){
        super.onResume();
    }

    public class DrawTriangle implements Renderer{

        public DrawTriangle(){

        }
        @Override
        public void onDrawFrame(GL10 gl) {

            gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);
            gl.glColor4f(1, 0, 0, 1);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);       

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            // TODO Auto-generated method stub

        }

    }

}

Who can explain for me what the difference between two above solutions,please.

thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2025-01-13 01:31:35

在第一个解决方案中,您有一个空的 onDrawFrame() 方法。这可能就是它不起作用的原因。请参阅 GLSurfaceView.Renderer

In the first solution you have an empty onDrawFrame() method. This is probably why it's not working. See GLSurfaceView.Renderer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文