gpt4 book ai didi

android - 没有出现带有 Open-GL 的三角形?

转载 作者:行者123 更新时间:2023-11-29 18:04:33 30 4
gpt4 key购买 nike

我做了一个显示三角形的 Activity ,但在运行时它没有显示三角形;我无法弄清楚错误在哪里,但肯定每次都有一个随机的彩色背景,而且我没有任何force closes 或一些错误

三角obj.class:

package com.exmple.open_gl;

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

import javax.microedition.khronos.opengles.GL10;

public class Triangleobj {
// -------------------------
FloatBuffer VBuff, CBuff;
ByteBuffer Ibuff;
private Random r = new Random();
private float[] vertices = { -1.0f, -1.0f, 1.0f, -1.0f, 0f, 1.0f },
color = { r.nextFloat(), r.nextFloat(), r.nextFloat(),
r.nextFloat(), r.nextFloat(), r.nextFloat(), r.nextFloat(),
r.nextFloat(), r.nextFloat(), r.nextFloat(), r.nextFloat(),
r.nextFloat(), };
private byte[] indices = { 0, 1, 2 };

// -------------------------
public Triangleobj() {
// ----------------------------------------------------------
ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
bb.order(ByteOrder.nativeOrder());
VBuff = bb.asFloatBuffer();
VBuff.put(vertices);
VBuff.position(0);

// ----------------------------------------------------------
Ibuff = ByteBuffer.allocateDirect(indices.length);
Ibuff.order(ByteOrder.nativeOrder());
Ibuff.put(indices);
Ibuff.position(0);

// ----------------------------------------------------------
bb = ByteBuffer.allocateDirect(color.length * 4);
bb.order(ByteOrder.nativeOrder());
CBuff = bb.asFloatBuffer();
CBuff.put(color);
CBuff.position(0);

// ----------------------------------------------------------
}

public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, VBuff);
gl.glColorPointer(4, GL10.GL_BYTE, 0, CBuff);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
GL10.GL_UNSIGNED_BYTE, Ibuff);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}}

三角渲染器类:

package com.exmple.open_gl;

import java.util.Random;

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

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.os.SystemClock;

public class triangleRenderer implements Renderer {
Triangleobj t;

public triangleRenderer() {
t = new Triangleobj();
}

@Override
public void onDrawFrame(GL10 gl) {

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

t.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0f, 1.0f, 0f, 1.0f, -1.0f, 1.0f);

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
Random r = new Random();
gl.glClearColor(r.nextFloat(), r.nextFloat(), r.nextFloat(),
r.nextFloat());
gl.glClearDepthf(1.0f);

}

}

三角形类:

package com.exmple.open_gl;

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

public class Triangle extends Activity {
private GLSurfaceView gls;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gls = new GLSurfaceView(this);
gls.setRenderer(new triangleRenderer());
setContentView(gls);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
gls.onPause();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
gls.onResume();
}

}

最佳答案

我使用您的代码编写了我自己的 opengl 应用程序,该应用程序在具有随机颜色的背景前显示了一个彩色三角形。我认识到设置颜色缓冲区的问题。因此,我没有使用颜色缓冲区,而是在三角形绘制函数中使用了以下代码行来设置三角形的颜色。您可能会这样做,因为它会阻止绘制三角形。

gl.glColor4f(0.3f, 0.3f, 0.3f, 1.0f);

我还编写了自己的顶点和索引数组,因为我不理解您的顶点、颜色和索引数组。

private float[] vertices = { 0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, -1.0f };
private byte[] indices = { 0, 1, 2 };

以下代码示例显示了我的完整代码。

三角对象.java

public class TriangleObject {

private FloatBuffer vertexBuffer;
private ByteBuffer indexBuffer;

private float[] vertices = { 0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, -1.0f };

private byte[] indices = { 0, 1, 2 };

public TriangleObject() {

ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);

indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);

}

public void draw(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glFrontFace(GL10.GL_CCW);
gl.glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}

三角渲染器.java

public class TriangleRenderer implements Renderer {

TriangleObject t;

public TriangleRenderer() {
t = new TriangleObject();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
Random r = new Random();
gl.glClearColor(r.nextFloat(), r.nextFloat(), r.nextFloat(), r.nextFloat());
gl.glClearDepthf(1.0f);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
t.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
}
}

三角 Activity .java

public class TriangleActivity extends Activity {

private GLSurfaceView glSurface;
private TriangleRenderer glRenderer;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurface = new GLSurfaceView(this);
glRenderer = new TriangleRenderer();
glSurface.setRenderer(glRenderer);
setContentView(glSurface);
glSurface.requestFocus();
glSurface.setFocusableInTouchMode(true);
}

@Override
protected void onPause() {
super.onPause();
glSurface.onPause();
}

@Override
protected void onResume() {
super.onResume();
glSurface.onResume();
}
}

编辑:

我继续弄清楚为什么颜色缓冲区不起作用。每个顶点都需要一种由四个浮点值 (rgba) 组成的颜色。因此,我添加了以下颜色数组。

private float[] colors = { 0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f,
0.1f, 0.0f, 0.0f, 1.0f};

byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
colorBuffer = byteBuf.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);

...
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
...
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
...
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

颜色缓冲区的整个过程都在工作。

关于android - 没有出现带有 Open-GL 的三角形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14081422/

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