gpt4 book ai didi

android - 无法让 opengl es 2.0 渲染三角形

转载 作者:行者123 更新时间:2023-11-28 05:23:33 24 4
gpt4 key购买 nike

我一直在尝试学习 opengl,我似乎无法得到一个三角形来渲染我的代码应该工作它将渲染我设置的 clearColor 但不是三角形,我的顶点和 fragment 着色器没有返回错误任何人都可以看到有问题谢谢

public class MainActivity extends Activity implements GLSurfaceView.Renderer
{
public static int POSITION_ATTRIBUTE = 0;
public int program;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

GLSurfaceView glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0);
glSurfaceView.setRenderer(this);
setContentView(glSurfaceView);
}

@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config)
{
// Vertex Shader Source Code
String vertexShaderSource = "" +
"attribute vec2 position; \n" +
"\n" +
"void main() \n" +
"{ \n" +
" gl_Position = vec4(position.x, position.y, 0.0, 1.0); \n" +
"} \n" +
"\n";

String fragmentShaderSource = "" +
"\n" +
"\n" +
"void main() \n" +
"{ \n" +
" gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); \n" +
"} \n" +
"\n";

// Write and compile vertex shader
int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexShader, vertexShaderSource);
GLES20.glCompileShader(vertexShader);
Log.i("Vertex Shader", "Compile Log: " + GLES20.glGetShaderInfoLog(vertexShader));

// Write and compile fragment shader
int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmentShader, fragmentShaderSource);
GLES20.glCompileShader(fragmentShader);
Log.i("Fragment Shader", "Compile Log: " + GLES20.glGetShaderInfoLog(fragmentShader));

// Link the shaders into a program object
program = GLES20.glCreateProgram();
GLES20.glAttachShader(program, vertexShader);
GLES20.glAttachShader(program, fragmentShader);
// Links the variables
GLES20.glBindAttribLocation(program, POSITION_ATTRIBUTE, "position");
GLES20.glLinkProgram(program);
Log.i("Link", "Link Log: " + GLES20.glGetProgramInfoLog(program));

GLES20.glClearColor(0.8f, 0.3f, 0.2f, 1.0f);
GLES20.glEnableVertexAttribArray(POSITION_ATTRIBUTE);
}

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

}

@Override
public void onDrawFrame(GL10 unused)
{
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

// Give OpenGl a triangle to render
float[] geometry = new float[]{
-0.2f, 0.3f,
0.4f, 0.5f,
0.1f, 0.1f,
};

ByteBuffer geometryByteBuffer = ByteBuffer.allocateDirect(geometry.length * 4);
geometryByteBuffer.order(ByteOrder.nativeOrder());
FloatBuffer geometryBuffer = geometryByteBuffer.asFloatBuffer();
geometryBuffer.put(geometry);
geometryBuffer.rewind();
GLES20.glVertexAttribPointer(POSITION_ATTRIBUTE, 2, GLES20.GL_FLOAT, false, 0, geometryBuffer);

GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, geometry.length / 2);
}

最佳答案

问题是我忘记了 GLES20 .gl_Useprogram(program);

关于android - 无法让 opengl es 2.0 渲染三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40985042/

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