gpt4 book ai didi

java - 使用 OpenGL ES 3.0 渲染三角形

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

我在使用 OpenGL ES 3.0 在 Android 设备上渲染三角形时遇到问题。

我使用 Google Nexus 5 进行测试。下面是我的代码 - 但它呈现的只是蓝屏(这是我指定的清晰颜色)。我希望看到一个红色三角形,但它没有出现。

有人可以指出我的代码中的缺陷吗?着色器编译并链接成功。

public class MainActivity extends Activity
{

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new TestSurfaceView(this));
}

public boolean onCreateOptionsMenu(Menu menu)
{
return false;
}

public boolean onOptionsItemSelected(MenuItem item)
{
return false;
}

private class TestSurfaceView extends GLSurfaceView implements Renderer
{
private final String vertexShaderCode =
"#version 300 es\n\n"+
"in vec3 inPosition;\n" +
"void main()\n" +
"{\n" +
" gl_Position = vec4(inPosition.xyz, 1.0);\n" +
"} \n";

private final String fragmentShaderCode =
"#version 300 es\n\n"+
"out vec4 outColor;\n"+
"void main()\n" +
"{\n" +
" outColor = vec4(1.0, 0.0, 0.0, 1.0); \n" +
"} \n";

public TestSurfaceView(Context context)
{
super(context);
super.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
setEGLContextClientVersion(3);
setRenderer(this);
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}

public void onDrawFrame(GL10 gl)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1);
}

public void onSurfaceChanged(GL10 gl, int width, int height)
{
glViewport(0, 0, width, height);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
// States

glClearColor(0f, 0f, 1f, 0f); // Set background to blue
glEnable(GL_DEPTH_TEST); // Enable Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do //TODO check
glEnable(GL_BLEND); // Enable Blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // The Type Of Blending To Do (for translucency)

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glActiveTexture(0);

// Shaders

int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, vertexShaderCode);
glCompileShader(vertexShader);
System.out.println("Vertex shader compile log:\n"+glGetShaderInfoLog(vertexShader));

int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, fragmentShaderCode);
glCompileShader(fragmentShader);
System.out.println("Fragment shader compile log:\n"+glGetShaderInfoLog(vertexShader));

int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);

glBindAttribLocation(shaderProgram, 0, "inPosition");

glLinkProgram(shaderProgram);
glValidateProgram(shaderProgram);
System.out.println("Shader program validate log:\n"+glGetProgramInfoLog(shaderProgram));
glUseProgram(shaderProgram);

// VAO

float floatSize = Float.SIZE/8;
float vertexSize = 3*floatSize;

int[] arrays = new int[1];
glGenVertexArrays(1, arrays, 0);
int modelVao = arrays[0];
glBindVertexArray(modelVao);

int[] buffers = new int[3];
glGenBuffers(3, buffers, 0);

int modelVertexVbo = buffers[0];
glBindBuffer(GL_ARRAY_BUFFER, modelVertexVbo);

glBufferData(GL_ARRAY_BUFFER, (int)(vertexSize*3f), null, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// Triangle

glBindBuffer(GL_ARRAY_BUFFER, modelVertexVbo);
ByteBuffer b = (ByteBuffer)glMapBufferRange(GL_ARRAY_BUFFER, 0, (int)(vertexSize*3), GL_MAP_WRITE_BIT);

b.putFloat(-1f);
b.putFloat(1f);
b.putFloat(0f);

b.putFloat(-1f);
b.putFloat(-1f);
b.putFloat(0f);

b.putFloat(1f);
b.putFloat(-1f);
b.putFloat(0f);

b.flip();
glUnmapBuffer(GL_ARRAY_BUFFER);
System.out.println("DONE");
}
}
}

我的 list 如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />
<uses-feature android:glEsVersion="0x00030000" android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

最佳答案

您可以在 onSurfaceCreated() 中启用背面剔除:

glEnable(GL_CULL_FACE);

OpenGL 中正面的默认缠绕顺序是逆时针。这意味着,如果启用背面剔除,所有顺时针缠绕的三角形都会被消除。因此代码中的三角形不会被渲染,因为它是顺时针缠绕的:

(1f, -1f, 0f)
(-1f, -1f, 0f)
(-1f, 1f, 0f)

在 ASCII 艺术中,三角形看起来像这样,顶点编号为 0 到 2:

2
| \
| \
| \
| \
1-----0

如果按照索引顺序跟踪顶点,您可以看出缠绕是顺时针的。

关于java - 使用 OpenGL ES 3.0 渲染三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24771153/

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