Android:Renderer 的两种不同用途
我使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个解决方案中,您有一个空的 onDrawFrame() 方法。这可能就是它不起作用的原因。请参阅 GLSurfaceView.Renderer
In the first solution you have an empty onDrawFrame() method. This is probably why it's not working. See GLSurfaceView.Renderer