gpt4 book ai didi

java - 尝试使用 Android 显示 openGL 图形

转载 作者:行者123 更新时间:2023-11-30 02:50:51 27 4
gpt4 key购买 nike

我一直在关注向您展示如何使用 Android 显示 openGL 图形的 Android 文档。我很确定我已经正确地遵循了代码,但没有显示任何内容。这是我的代码:

OpenGLES20Activity.java

import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.content.Context;
import android.opengl.EGLConfig;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class OpenGLES20Activity extends Activity {

private GLSurfaceView mGLView;
private MyGLRenderer renderer;
private static Triangle mTriangle;
private static Square mSquare;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity.
mGLView = new MyGLSurfaceView(this);
setContentView(mGLView);

}

class MyGLSurfaceView extends GLSurfaceView {

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

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

// Set the Renderer for drawing on the GLSurfaceView
renderer = new MyGLRenderer();
setRenderer(renderer);
}
}

public static class MyGLRenderer implements GLSurfaceView.Renderer {

public void onSurfaceCreated(GL10 unused, EGLConfig config) {

}

public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
mTriangle.draw();
}

public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}

@Override
public void onSurfaceCreated(GL10 gl,
javax.microedition.khronos.egl.EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// initialize a triangle
mTriangle = new Triangle();
}

public static int loadShader(int type, String shaderCode){

// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);

// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);

return shader;
}

}

}

三角形.java

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
import com.example.test3d.OpenGLES20Activity.MyGLRenderer;

public class Triangle {

private FloatBuffer vertexBuffer;
int mProgram;
private final String vertexShaderCode =
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = vPosition;" +
"}";

private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float triangleCoords[] = { // in counterclockwise order:
0.0f, 0.622008459f, 0.0f, // top
0f, 0.311004243f, 0.0f, // bottom left
0f, 0.311004243f, 0.0f // bottom right
};

// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };



public Triangle() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
triangleCoords.length * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());

// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(triangleCoords);
// set the buffer to read the first coordinate
vertexBuffer.position(0);

int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
int 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
}

public void draw() {
int vertexStride = 10;
int vertexCount = 10;
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);

// get handle to vertex shader's vPosition member
int mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);

// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);

// get handle to fragment shader's vColor member
int mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);

// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}

我还在 list 文件中指定:

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

编辑:明确地说,我根本没有收到任何错误。我的屏幕是黑色的,而且一直是黑色。我已经使用了一些打印语句并且我已经确认正在调用 Triangle.draw() 。所以我的猜测是绘图实现是错误的,但我遵循了文档,所以可能是其他原因。感谢您的帮助。

最佳答案

我看到两个主要问题。首先,你的三角坐标:

static float triangleCoords[] = {   // in counterclockwise order:
0.0f, 0.622008459f, 0.0f, // top
0f, 0.311004243f, 0.0f, // bottom left
0f, 0.311004243f, 0.0f // bottom right
};

第二个和第三个顶点的坐标相同。具有两个相同顶点的三角形是退化的,不会渲染任何像素。

然后这些值看起来有问题:

int vertexStride = 10;
int vertexCount = 10;

每个顶点有 3 个 float ,所以 vertexStride 应该是 3 * sizeof(float),也就是 12。我只看到 3 个顶点,所以 vertexCount 应该是 3。

关于java - 尝试使用 Android 显示 openGL 图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216075/

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