Reputation: 347
I'm having trouble getting a triangle to render on an Android device using OpenGL ES 3.0.
I'm using Google Nexus 5 for testing. Below is my code - but all it renders is a blue screen (which is the clear color I have specified). I expect to see a red triangle but it isn't appearing.
Can someone please point out the flaw in my code? The shaders compile and link successfully.
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");
}
}
}
My manifest is below:
<?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>
Upvotes: 0
Views: 1143
Reputation: 347
Thanks to Reto Koradi and Colonel Thirty Two for finding two important flaws with my code. The last flaw I found was that I needed to specify native ordering of the byte buffer obtained from glMapBufferRange before writing to it.
ByteBuffer b = (ByteBuffer)glMapBufferRange(GL_ARRAY_BUFFER, 0, (int)(vertexSize*3), GL_MAP_WRITE_BIT);
b.order(ByteOrder.nativeOrder());
Upvotes: 1
Reputation: 54572
You enable backface culling in onSurfaceCreated()
:
glEnable(GL_CULL_FACE);
The default winding order for front faces in OpenGL is counter-clockwise. This means that if you enable backface culling, all triangles with clockwise winding are eliminated. So the triangle in your code will not be rendered because it has clockwise winding:
(1f, -1f, 0f)
(-1f, -1f, 0f)
(-1f, 1f, 0f)
In ASCII art, the triangle looks like this, with vertices numbered from 0 to 2:
2
| \
| \
| \
| \
1-----0
You can tell that the winding is clockwise if you follow the vertices in index order.
Upvotes: 2