gpt4 book ai didi

java - Android:渲染器的两种不同用途

转载 作者:行者123 更新时间:2023-11-29 22:12:22 25 4
gpt4 key购买 nike

我使用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

}

}

}

谁能帮我解释一下以上两种解决方案的区别。

谢谢:)

最佳答案

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

关于java - Android:渲染器的两种不同用途,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9322388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com