Can't link compiled shaders to program OpenGL ES 2.0 Android - android

I have a problem with linking shaders to program in OpenGL ES 2.0.
Here's my code
package pl.projekcik;
import android.content.Context;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.logging.Logger;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import util.LoggerConfig;
import util.ShaderHelper;
import util.TextResourceReader;
import static android.opengl.GLES20.*;
import static android.opengl.GLUtils.*;
import static android.opengl.Matrix.*;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
public class ProjekcikRenderer implements Renderer {
private static final int POSITION_COMPONENT_COUNT = 3;
private static final int BYTES_PER_FLOAT = 4;
private final FloatBuffer vertexData;
private final Context context;
private int program;
private static final String U_COLOR = "u_COLOR";
private int uColorLocation;
private static final String A_POSITION = "a_Position";
private int aPositionLocation;
private static final String TAG = "ProjekcikRenderer";
public ProjekcikRenderer(Context context){
this.context = context;
float[] tableVertices = {
0f, 0f, 0f,
1f, 1f, 0f,
0f, 1f, 0f,
0f, 0f, 0f,
1f, 0f, 0f,
1, 1f, 0f
};
vertexData = ByteBuffer
.allocateDirect(tableVertices.length * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
vertexData.put(tableVertices);
}
#Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config){
glClearColor(1.0f, 1.0f, 0.5f, 0.0f);
String vertexShaderSource = null;
String fragmentShaderSource = null;
try{
vertexShaderSource = TextResourceReader.readTextFileFromResource(context, R.raw.simple_vertex_shader);
fragmentShaderSource = TextResourceReader.readTextFileFromResource(context, R.raw.simple_fragment_shader);
} catch (IOException e){
throw new RuntimeException("IOException", e);
} catch (Exception e){
throw new RuntimeException("Other Exception", e);
}
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
Log.d(TAG, "CREATING VERTEX SHADER");
String s = new String(Integer.toString(vertexShader));
if(vertexShader == 0){
if(LoggerConfig.ON){
Log.w(TAG, "Could not create new VERTEX shader");
}
return;
} else {
if(LoggerConfig.ON){
Log.w(TAG, s);
}
}
GLES20.glShaderSource(vertexShader, vertexShaderSource);
GLES20.glCompileShader(vertexShader);
final int[] compileStatus = new int[1];
GLES20.glGetShaderiv(vertexShader, GL_COMPILE_STATUS, compileStatus, 0);
if(LoggerConfig.ON){
Log.v(TAG, "Results of compiling source:" + "\n" + vertexShaderSource + "\n" + glGetShaderInfoLog(vertexShader));
}
if(compileStatus[0] == 0){
glDeleteShader(vertexShader);
if(LoggerConfig.ON){
Log.w(TAG, "Compilation of shader failed");
}
return;
} else {
if(LoggerConfig.ON){
Log.v(TAG, "VERTEX SHADER COMPILED");
}
}
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
Log.d(TAG, "CREATING FRAGMENT SHADER");
s = new String(Integer.toString(fragmentShader));
if(fragmentShader == 0){
if(LoggerConfig.ON){
Log.w(TAG, "Could not create new FRAGMENT shader");
}
return;
} else {
if(LoggerConfig.ON){
Log.w(TAG, s);
}
}
GLES20.glShaderSource(fragmentShader, fragmentShaderSource);
GLES20.glCompileShader(vertexShader);
GLES20.glGetShaderiv(vertexShader, GL_COMPILE_STATUS, compileStatus, 0);
if(LoggerConfig.ON){
Log.v(TAG, "Results of compiling source:" + "\n" + fragmentShaderSource + "\n" + glGetShaderInfoLog(fragmentShader));
}
if(compileStatus[0] == 0){
glDeleteShader(fragmentShader);
if(LoggerConfig.ON){
Log.w(TAG, "Compilation of shader failed");
}
return;
} else {
if(LoggerConfig.ON){
Log.v(TAG, "FRAGMENT SHADER COMPILED");
}
}
program = GLES20.glCreateProgram();
s = new String(Integer.toString(program));
Log.v("PROGRAM ID: ", s);
if(program == 0){
if(LoggerConfig.ON){
Log.w(TAG, "Could not create new program");
}
return;
}
try{
GLES20.glAttachShader(program, vertexShader);
GLES20.glAttachShader(program, fragmentShader);
} catch (Exception e){
Log.w("ATTACH", "COULD NOT ATTACH 1 OR MORE SHADERS");
}
try{
GLES20.glLinkProgram(program);
} catch (Exception e){
Log.w("LINK", "Link program failed");
}
final int[] linkStatus = new int[1];
glGetProgramiv(program, GL_LINK_STATUS, linkStatus, 0);
if(LoggerConfig.ON){
Log.v(TAG, "Results of linking program:\n" + glGetProgramInfoLog(program));
}
if(linkStatus[0] == 0){
glDeleteProgram(program);
if(LoggerConfig.ON){
Log.w(TAG, "Linking of program failed");
}
return;
}
if(LoggerConfig.ON){
validateProgram(program);
}
glUseProgram(program);
uColorLocation = glGetUniformLocation(program, U_COLOR);
aPositionLocation = glGetAttribLocation(program, A_POSITION);
vertexData.position(0);
glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT, false, 0, vertexData);
glEnableVertexAttribArray(aPositionLocation);
}
#Override
public void onSurfaceChanged(GL10 glUnused, int width, int height){
glViewport(0, 0, width, height);
}
#Override
public void onDrawFrame(GL10 glUnused){
glClear(GL_COLOR_BUFFER_BIT);
glUniform4f(uColorLocation, 0.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
public static boolean validateProgram(int programObjectId){
glValidateProgram(programObjectId);
final int[] validateStatus = new int[1];
glGetProgramiv(programObjectId, GL_VALIDATE_STATUS, validateStatus, 0);
Log.v(TAG, "Results of validating program: " + validateStatus[0] + "\nLog: " + glGetProgramInfoLog(programObjectId));
return validateStatus[0] != 0;
}
}
The problem is, after succesfully compiling shaders I'm trying to attach my shaders to my program. It looks like ot's attaching shaders, but linking doesn't. Here's log of from this operation:
08-12 11:24:10.330 23044-23044/pl.projekcik D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
08-12 11:24:10.330 23044-23044/pl.projekcik D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
08-12 11:24:10.340 23044-23044/pl.projekcik D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
08-12 11:24:10.350 23044-23044/pl.projekcik D/OpenGLRenderer﹕ Enabling debug mode 0
08-12 11:24:10.420 23044-23057/pl.projekcik D/ProjekcikRenderer﹕ CREATING VERTEX SHADER
08-12 11:24:10.420 23044-23057/pl.projekcik W/ProjekcikRenderer﹕ 1
08-12 11:24:10.420 23044-23057/pl.projekcik V/ProjekcikRenderer﹕ Results of compiling source:
attribute highp vec4 a_Position;
void main(){
gl_Position = a_Position;
}
08-12 11:24:10.420 23044-23057/pl.projekcik V/ProjekcikRenderer﹕ VERTEX SHADER COMPILED
08-12 11:24:10.420 23044-23057/pl.projekcik D/ProjekcikRenderer﹕ CREATING FRAGMENT SHADER
08-12 11:24:10.420 23044-23057/pl.projekcik W/ProjekcikRenderer﹕ 2
08-12 11:24:10.420 23044-23057/pl.projekcik V/ProjekcikRenderer﹕ Results of compiling source:
precision mediump float;
uniform vec4 u_Color;
void main(){
gl_FragColor = u_Color;
}
08-12 11:24:10.420 23044-23057/pl.projekcik V/ProjekcikRenderer﹕ FRAGMENT SHADER COMPILED
08-12 11:24:10.420 23044-23057/pl.projekcik V/PROGRAM ID:﹕ 3
08-12 11:24:10.420 23044-23057/pl.projekcik V/ProjekcikRenderer﹕ Results of linking program:
L0101 All attached shaders must be compiled prior to linking
08-12 11:24:10.420 23044-23057/pl.projekcik W/ProjekcikRenderer﹕ Linking of program failed
08-12 11:24:10.430 23044-23046/pl.projekcik D/dalvikvm﹕ GC_CONCURRENT freed 270K, 8% free 6260K/6791K, paused 17ms+4ms, total 81ms
Any help would be greatly appreciated

Your problem is here:
GLES20.glShaderSource(fragmentShader, fragmentShaderSource);
GLES20.glCompileShader(vertexShader);
GLES20.glGetShaderiv(vertexShader, GL_COMPILE_STATUS, compileStatus, 0);
You're compiling the vertexShader instead of the fragmentShader, so the vertexShader is getting compiled twice, and the fragmentShader isn't getting compiled at all.

Related

How to read data from specific buffer with glreadpixels based on GLES30 on Android

As I understood, from GLES30 there is no more gl_FragColor buffer (I saw it HERE)
Since I can't read a "Special Variables ", how can I read an "out" buffer?
This is my code:
private static final String FRAGMENT_SHADER =
"#version 300 es\n"+
"#extension GL_OES_EGL_image_external_essl3 : require\n" +
"precision mediump float;\n" + // highp here doesn't seem to matter
"in vec2 vTextureCoord;\n" +
"uniform sampler2D sTexture;\n" +
"out vec4 fragColor ;\n" +
"void main() {\n" +
" vec4 tc = texture(sTexture, vTextureCoord);\n" +
" fragColor.r = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;\n" +
" fragColor.g = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;\n" +
" fragColor.b = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;\n" +
"}\n";
Here I tried to read the data:
ByteBuffer mPixelBuf = ByteBuffer.allocateDirect(mWidth * mHeight * 4);
mPixelBuf.order(ByteOrder.LITTLE_ENDIAN);
GLES30.glReadPixels(startX, startY, frameWidth, frameHeight, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, mPixelBuf);
There are no gl errors in the code.
The output mPixelBuf only zeroes.
How can I make sure that fragColor is reading?
Thanks
Update1- Full Texture Render Code:
package com.MES.YOtm.AnalyzingAdapter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.graphics.SurfaceTexture;
import android.opengl.GLES11Ext;
import android.opengl.GLES30;
import android.opengl.Matrix;
import android.util.Log;
/**
* Code for rendering a texture onto a surface using OpenGL ES 2.0.
*/
public class STextureRender {
private static final String TAG = "Myopengl";
private int zoom;
private static final int FLOAT_SIZE_BYTES = 4;
private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0;
private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
private final float[] mTriangleVerticesData = {
// X, Y, Z, U, V
-1.0f, -1.0f, 0, 0.f, 0.f,
1.0f, -1.0f, 0, 1.f, 0.f,
-1.0f, 1.0f, 0, 0.f, 1.f,
1.0f, 1.0f, 0, 1.f, 1.f,
};
private FloatBuffer mTriangleVertices;
private static final String VERTEX_SHADER =
"#version 300 es\n"+
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uSTMatrix;\n" +
"in vec4 aPosition;\n" +
"in vec4 aTextureCoord;\n" +
"out vec2 vTextureCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" +
"}\n";
//Smapler2D
private static final String FRAGMENT_SHADER =
"#version 300 es\n"+
"#extension GL_OES_EGL_image_external_essl3 : require\n" +
"precision mediump float;\n" + // highp here doesn't seem to matter
"in vec2 vTextureCoord;\n" +
"uniform mediump sampler2D sTexture;\n" +
"layout(location = 0) out mediump vec4 fragColor ;\n" +
"void main() {\n" +
" vec4 tc = texture(sTexture, vTextureCoord);\n" +
" fragColor.r = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;\n" +
" fragColor.g = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;\n" +
" fragColor.b = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;\n" +
"}\n";
private float[] mMVPMatrix = new float[16];
private float[] mSTMatrix = new float[16];
private int mProgram;
private int mTextureID = -12345;
private int muMVPMatrixHandle;
private int muSTMatrixHandle;
private int maPositionHandle;
private int maTextureHandle;
public STextureRender(int _zoom) {
Log.v("My Error", "Start STextureRender constructor");
try
{
mTriangleVertices = ByteBuffer.allocateDirect(
mTriangleVerticesData.length * FLOAT_SIZE_BYTES)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mTriangleVertices.put(mTriangleVerticesData).position(0);
Matrix.setIdentityM(mSTMatrix, 0);
zoom = _zoom;
}
catch(Exception ex)
{
Log.v("My Error", "STextureRender Error = " + ex.toString());
}
Log.v("My Error", "End STextureRender constructor");
}
public int getTextureId() {
return mTextureID;
}
/**
* Draws the external texture in SurfaceTexture onto the current EGL surface.
*/
public void drawFrame(SurfaceTexture st, boolean invert) {
checkGlError("onDrawFrame start");
try
{
st.getTransformMatrix(mSTMatrix);
if (invert) {
mSTMatrix[5] = -mSTMatrix[5];
mSTMatrix[13] = 1.0f - mSTMatrix[13];
}
GLES30.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
GLES30.glUseProgram(mProgram);
checkGlError("glUseProgram");
GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
GLES30.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
GLES30.glVertexAttribPointer(maPositionHandle, 3, GLES30.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maPosition");
GLES30.glEnableVertexAttribArray(maPositionHandle);
checkGlError("glEnableVertexAttribArray maPositionHandle");
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
GLES30.glVertexAttribPointer(maTextureHandle, 2, GLES30.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maTextureHandle");
GLES30.glEnableVertexAttribArray(maTextureHandle);
checkGlError("glEnableVertexAttribArray maTextureHandle");
Matrix.setIdentityM(mMVPMatrix, 0);
GLES30.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES30.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
checkGlError("glDrawArrays");
}
catch(Exception ex)
{
Log.v("My Error", "drawFrame Error = " + ex.toString());
}
}
/**
* Initializes GL state. Call this after the EGL surface has been created and made current.
*/
public void surfaceCreated() {
try
{
mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
if (mProgram == 0) {
throw new RuntimeException("failed creating program");
}
maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");
checkGlError("glGetAttribLocation aPosition");
if (maPositionHandle == -1) {
throw new RuntimeException("Could not get attrib location for aPosition");
}
maTextureHandle = GLES30.glGetAttribLocation(mProgram, "aTextureCoord");
checkGlError("glGetAttribLocation aTextureCoord");
if (maTextureHandle == -1) {
throw new RuntimeException("Could not get attrib location for aTextureCoord");
}
muMVPMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");
checkGlError("glGetUniformLocation uMVPMatrix");
if (muMVPMatrixHandle == -1) {
throw new RuntimeException("Could not get attrib location for uMVPMatrix");
}
muSTMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uSTMatrix");
checkGlError("glGetUniformLocation uSTMatrix");
if (muSTMatrixHandle == -1) {
throw new RuntimeException("Could not get attrib location for uSTMatrix");
}
int[] textures = new int[1];
GLES30.glGenTextures(1, textures, 0);
mTextureID = textures[0];
GLES30.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
checkGlError("glBindTexture mTextureID");
GLES30.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES30.GL_TEXTURE_MIN_FILTER,
GLES30.GL_NEAREST);
GLES30.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES30.GL_TEXTURE_MAG_FILTER,
GLES30.GL_LINEAR);
GLES30.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES30.GL_TEXTURE_WRAP_S,
GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES30.GL_TEXTURE_WRAP_T,
GLES30.GL_CLAMP_TO_EDGE);
checkGlError("glTexParameter");
}
catch(Exception ex)
{
Log.v("My Error", "surfaceCreated Error = " + ex.toString());
}
}
/**
* Replaces the fragment shader. Pass in null to reset to default.
*/
public void changementShader(String fragmentShader) {
try
{
if (fragmentShader == null) {
fragmentShader = FRAGMENT_SHADER;
}
GLES30.glDeleteProgram(mProgram);
mProgram = createProgram(VERTEX_SHADER, fragmentShader);
if (mProgram == 0) {
Log.v("My Error", "failed creating program");
throw new RuntimeException("failed creating program");
}
}
catch(Exception ex)
{
Log.v("My Error", " changementShader Error = " + ex.toString());
}
}
private int loadShader(int shaderType, String source) {
try
{
int shader = GLES30.glCreateShader(shaderType);
checkGlError("glCreateShader type=" + shaderType);
GLES30.glShaderSource(shader, source);
GLES30.glCompileShader(shader);
int[] compiled = new int[1];
GLES30.glGetShaderiv(shader, GLES30.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
Log.e(TAG, "Could not compile shader " + shaderType + ":");
Log.e(TAG, " " + GLES30.glGetShaderInfoLog(shader));
GLES30.glDeleteShader(shader);
shader = 0;
}
return shader;
}
catch(Exception ex)
{
Log.v("My Error", "loadShader Error = " + ex.toString());
return 0;
}
}
private int createProgram(String vertexSource, String fragmentSource) {
try
{
int vertexShader = loadShader(GLES30.GL_VERTEX_SHADER, vertexSource);
if (vertexShader == 0) {
return 0;
}
int pixelShader = loadShader(GLES30.GL_FRAGMENT_SHADER, fragmentSource);
if (pixelShader == 0) {
return 0;
}
int program = GLES30.glCreateProgram();
checkGlError("glCreateProgram");
if (program == 0) {
Log.e(TAG, "Could not create program");
}
GLES30.glAttachShader(program, vertexShader);
checkGlError("glAttachShader");
GLES30.glAttachShader(program, pixelShader);
checkGlError("glAttachShader");
GLES30.glLinkProgram(program);
int[] linkStatus = new int[1];
GLES30.glGetProgramiv(program, GLES30.GL_LINK_STATUS, linkStatus, 0);
if (linkStatus[0] != GLES30.GL_TRUE) {
Log.e(TAG, "Could not link program: ");
Log.e(TAG, GLES30.glGetProgramInfoLog(program));
GLES30.glDeleteProgram(program);
program = 0;
}
return program;
}
catch(Exception ex)
{
Log.v("My Error", "createProgram Error = " + ex.toString());
return 0;
}
}
public void checkGlError(String op) {
int error;
while ((error = GLES30.glGetError()) != GLES30.GL_NO_ERROR) {
Log.e(TAG, op + ": glError " + error);
throw new RuntimeException(op + ": glError " + error);
}
}
}
Your shader code is totally irrelevant to what glReadPixels reads, and it has nothing to do with special variable names. It reads from the currently bound read framebuffer; i.e. glReadPixels in ES 3.0 works in exactly the same way as it used to work in ES 2.0.
The only exception is multiple render target support, but that's not relevant in this case.
How can I make sure that fragColor is reading?
glClearColor(some interesting color)
glClear(COLOR_BUFFER_BIT)
glReadPixels()
assert color == some interesting color
What I have learned is that your GLES30.glReadPixels call needs to be done before eglSwapBuffers because glReadBuffer is initially set to GL_BACK in double-buffered configurations according to document of glReadBuffer. Once eglSwapBuffers calls glReadPixels reads nothing back to main memory.

OpenGL ES 2.0 can't compile shaders Android

I have a problem with shader compiler in OpenGL ES 2.0. Everytime I'm trying to compile shader, it won't compile and I don't really know why. I can create shader, compiling gives me error.
Here's code of my Renderer class
import android.content.Context;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.logging.Logger;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import util.LoggerConfig;
import util.ShaderHelper;
import util.TextResourceReader;
import static android.opengl.GLES20.*;
import static android.opengl.GLUtils.*;
import static android.opengl.Matrix.*;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
public class ProjekcikRenderer implements Renderer {
private static final int POSITION_COMPONENT_COUNT = 2;
private static final int BYTES_PER_FLOAT = 4;
private final FloatBuffer vertexData;
private final Context context;
private int program;
private static final String U_COLOR = "u_COLOR";
private int uColorLocation;
private static final String A_POSITION = "a_Position";
private int aPositionLocation;
private static final String TAG = "ProjekcikRenderer";
public ProjekcikRenderer(Context context){
this.context = context;
float[] tableVertices = {
0f, 0f,
1f, 1f,
0f, 1f,
0f, 0f,
1f, 0f,
1, 1f
};
vertexData = ByteBuffer
.allocateDirect(tableVertices.length * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
vertexData.put(tableVertices);
}
#Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config){
glClearColor(1.0f, 1.0f, 0.5f, 0.0f);
String vertexShaderSource = null;
String fragmentShaderSource = null;
try{
vertexShaderSource = TextResourceReader.readTextFileFromResource(context, R.raw.simple_vertex_shader);
fragmentShaderSource = TextResourceReader.readTextFileFromResource(context, R.raw.simple_fragment_shader);
} catch (IOException e){
throw new RuntimeException("IOException", e);
} catch (Exception e){
throw new RuntimeException("Other Exception", e);
}
//int vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
Log.d(TAG, "CREATING VERTEX SHADER");
String s = new String(Integer.toString(vertexShader));
if(vertexShader == 0){
if(LoggerConfig.ON){
Log.w(TAG, "Could not create new VERTEX shader");
}
return;
} else {
if(LoggerConfig.ON){
Log.w(TAG, s);
}
}
glShaderSource(vertexShader, vertexShaderSource);
final int[] compileStatus = new int[1];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, compileStatus, 0);
if(LoggerConfig.ON){
Log.v(TAG, "Results of compiling source:" + "\n" + vertexShaderSource + "\n" + glGetShaderInfoLog(vertexShader));
}
if(compileStatus[0] == 0){
glDeleteShader(vertexShader);
if(LoggerConfig.ON){
Log.w(TAG, "Compilation of shader failed");
}
return;
}
int fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
program = linkProgram(vertexShader, fragmentShader);
if(LoggerConfig.ON){
validateProgram(program);
}
glUseProgram(program);
uColorLocation = glGetUniformLocation(program, U_COLOR);
aPositionLocation = glGetAttribLocation(program, A_POSITION);
vertexData.position(0);
glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT, false, 0, vertexData);
glEnableVertexAttribArray(aPositionLocation);
}
#Override
public void onSurfaceChanged(GL10 glUnused, int width, int height){
glViewport(0, 0, width, height);
}
#Override
public void onDrawFrame(GL10 glUnused){
glClear(GL_COLOR_BUFFER_BIT);
glUniform4f(uColorLocation, 0.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
public static int compileVertexShader(String shaderCode){
return compileShader(GL_VERTEX_SHADER, shaderCode);
}
public static int compileFragmentShader(String shaderCode){
return compileShader(GL_FRAGMENT_SHADER, shaderCode);
}
private static int compileShader(int type, String shaderCode){
final int shaderObjectId = glCreateShader(type);
String s = new String(Integer.toString(type));
if(shaderObjectId == 0){
if(LoggerConfig.ON){
Log.w(TAG, "Could not create new shader");
}
return 0;
} else {
if(LoggerConfig.ON){
Log.w(TAG, s);
}
}
glShaderSource(shaderObjectId, shaderCode);
final int[] compileStatus = new int[1];
glGetShaderiv(shaderObjectId, GL_COMPILE_STATUS, compileStatus, 0);
if(LoggerConfig.ON){
Log.v(TAG, "Results of compiling source:" + "\n" + shaderCode + "\n" + glGetShaderInfoLog(shaderObjectId));
}
if(compileStatus[0] == 0){
glDeleteShader(shaderObjectId);
if(LoggerConfig.ON){
Log.w(TAG, "Compilation of shader failed");
}
return 0;
}
return shaderObjectId;
}
public static int linkProgram (int vertexShaderId, int fragmentShaderId){
final int programObjectId = 0;
glAttachShader(programObjectId, vertexShaderId);
glAttachShader(programObjectId, fragmentShaderId);
glLinkProgram(programObjectId);
if(programObjectId == 0){
if(LoggerConfig.ON){
Log.w(TAG, "Could not create new program");
}
return 0;
}
final int[] linkStatus = new int[1];
glGetProgramiv(programObjectId, GL_LINK_STATUS, linkStatus, 0);
if(LoggerConfig.ON){
Log.v(TAG, "Results of linking program:\n" + glGetProgramInfoLog(programObjectId));
}
if(linkStatus[0] == 0){
glDeleteProgram(programObjectId);
if(LoggerConfig.ON){
Log.w(TAG, "Linking of program failed");
}
return 0;
}
return programObjectId;
}
public static boolean validateProgram(int programObjectId){
glValidateProgram(programObjectId);
final int[] validateStatus = new int[1];
glGetProgramiv(programObjectId, GL_VALIDATE_STATUS, validateStatus, 0);
Log.v(TAG, "Results of validating program: " + validateStatus[0] + "\nLog: " + glGetProgramInfoLog(programObjectId));
return validateStatus[0] != 0;
}
}
and here's my vertex shader
attribute vec4 a_Position;
void main(){
gl_Position = a_Position;
}
And logs
08-12 09:26:46.350 1281-1281/pl.projekcik D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
08-12 09:26:46.350 1281-1281/pl.projekcik D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
08-12 09:26:46.350 1281-1281/pl.projekcik D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
08-12 09:26:46.370 1281-1281/pl.projekcik D/OpenGLRenderer﹕ Enabling debug mode 0
08-12 09:26:46.400 1281-1308/pl.projekcik D/ProjekcikRenderer﹕ CREATING VERTEX SHADER
08-12 09:26:46.400 1281-1308/pl.projekcik W/ProjekcikRenderer﹕ 1
08-12 09:26:46.400 1281-1308/pl.projekcik V/ProjekcikRenderer﹕ Results of compiling source:
attribute vec4 a_Position;
void main(){
gl_Position = a_Position;
}
08-12 09:26:46.400 1281-1308/pl.projekcik W/ProjekcikRenderer﹕ Compilation of shader failed
08-12 09:26:46.430 1281-1283/pl.projekcik D/dalvikvm﹕ GC_CONCURRENT freed 225K, 8% free 6284K/6791K, paused 12ms+5ms, total 44ms
Any help would be greatly appreciated. I was using Kevin Brothael's book OpenGL ES 2 for Android, since it's my first time with OpenGL.
EDIT I'm using Android 4.1.1 on Prestigio PMP7880D3G with some "Quad Core GPU". But almost same code (functions used for shader compiling were in additional class ShaderHelper) code didn't work on Android 5.0.2 running on Xperia Z1 Compact.
You're missing a call to GLES20.glCompileShader(vertexShader) after you call glShaderSource()
Try to fix your code here:
glShaderSource(vertexShader, vertexShaderSource);
glCompileShader(vertexShader); // <-- this line is missing.
// Now see if the compilation worked.
int[] compiled = new int[1];
glGetShaderiv(vertexShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0)
{
// Handle errors with shader compilation.
}
Here is a code stub I use to generate programs in OpenGL-ES 2.0:
private int loadShader(String strSource, int iType)
{
int[] compiled = new int[1];
int iShader = GLES20.glCreateShader(iType);
GLES20.glShaderSource(iShader, strSource);
GLES20.glCompileShader(iShader);
GLES20.glGetShaderiv(iShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
Log.d("Load Shader Failed", "Compilation\n" + GLES20.glGetShaderInfoLog(iShader));
return 0;
}
return iShader;
}
public int loadProgram(String strVSource, String strFSource)
{
int iVShader;
int iFShader;
int iProgId;
int[] link = new int[1];
iVShader = loadShader(strVSource, GLES20.GL_VERTEX_SHADER);
if (iVShader == 0)
{
Log.d("Load Program", "Vertex Shader Failed");
return 0;
}
iFShader = loadShader(strFSource, GLES20.GL_FRAGMENT_SHADER);
if(iFShader == 0)
{
Log.d("Load Program", "Fragment Shader Failed");
return 0;
}
iProgId = GLES20.glCreateProgram();
GLES20.glAttachShader(iProgId, iVShader);
GLES20.glAttachShader(iProgId, iFShader);
GLES20.glLinkProgram(iProgId);
GLES20.glGetProgramiv(iProgId, GLES20.GL_LINK_STATUS, link, 0);
if (link[0] <= 0) {
Log.d("Load Program", "Linking Failed");
return 0;
}
GLES20.glDeleteShader(iVShader);
GLES20.glDeleteShader(iFShader);
return iProgId;
}
First off, are you sure that the rendering context is set up for ES 2.0? If you're using a GLSurfaceView, it will default to ES 1.1 unless something else is requested, but that part of the code isn't included here (although it does seem to be set up right based on the log).
Secondly, contrary to GLSL shaders for desktop OpenGL, variables in shaders in GL ES need to specify the precision. So where you have attribute vec4 a_Position; you need to specify it as e.g. attribute highp vec4 a_Position;. This should hopefully be enough for this particular simple vertex shader to compile.

Bitmap texture does not show up in framebuffer surface on Android

I'm trying to use the new EffectFactory/Effect to add effects to images off screen (i.e. framebuffer). I've looked at the HelloEffects.java example provided in the SDK and I've tried it out and it works. Except it obviously uses a GLSurfaceView and that isn't what I want.
So I've taken tests/effect/src/android/effect/cts/GLEnv.java to setup the EGL stuff and I've also grabbed TextureRenderer.java and GLToolbox from the HelloEffects example. Mashed them all up and I've got the code below.
(On a side note, I have also tried tests/media/src/android/media/cts/OutputSurface.java to setup the EGL stuff and I got the exact same result.)
When I run it, the image I get back is just uniformly blue. This corresponds to the glClear I did with the colour blue. This proves at least to some degree that pixels are being rendered to the framebuffer, glReadPixels is seeing those pixels and the bitmap output is working.
But why is the texture not showing up? Neither the original nor the effect-applied texture shows up. No GL errors are detected either.
I've trimmed down the code to a single file working example that can be copied/pasted into Eclipse and will run. Obviously modify the input and output image paths per your needs.
Tested on a Nexus 10 / Android 4.3 as well as the Emulator. Same results.
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import android.media.effect.Effect;
import android.media.effect.EffectContext;
import android.media.effect.EffectFactory;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
public class MainActivity extends Activity
{
private int[] mTextures = new int[2];
private EffectContext mEffectContext;
private Effect mEffect;
private TextureRenderer mTexRenderer = new TextureRenderer();
private int mImageWidth;
private int mImageHeight;
final static String imageFileOut = "/data/local/out.png";
final static String imageFileIn = "/data/local/lol.png";
private GLEnv mEnv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEnv = new GLEnv();
mEnv.makeCurrent();
mEffectContext = EffectContext.createWithCurrentGlContext();
mTexRenderer.init();
loadTextures();
initAndapplyEffect();
renderResult();
saveBitmap();
}
void saveBitmap()
{
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
ByteBuffer pixelBuffer = ByteBuffer.allocateDirect(mImageWidth * mImageHeight * 4).order(ByteOrder.nativeOrder());
GLES20.glReadPixels(0, 0, mImageWidth, mImageHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
mEnv.checkForEGLErrors("store Pixels");
Bitmap bitmap = Bitmap.createBitmap(mImageWidth, mImageHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(pixelBuffer);
try
{
FileOutputStream fos = new FileOutputStream(imageFileOut);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) { e.printStackTrace(); }
}
private void initAndapplyEffect()
{
EffectFactory effectFactory = mEffectContext.getFactory();
if (mEffect != null)
{
mEffect.release();
}
mEffect = effectFactory.createEffect(EffectFactory.EFFECT_BRIGHTNESS);
mEffect.setParameter("brightness", 2.0f);
mEffect.apply(mTextures[0], mImageWidth, mImageHeight, mTextures[1]);
}
private int loadTextures()
{
// Generate textures
GLES20.glGenTextures(2, mTextures, 0);
// Load input bitmap
Bitmap bitmap = BitmapFactory.decodeFile(imageFileIn);
mImageWidth = bitmap.getWidth();
mImageHeight = bitmap.getHeight();
mTexRenderer.updateTextureSize(mImageWidth, mImageHeight);
// Upload to texture
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Set texture parameters
GLToolbox.initTexParams();
return mTextures[0];
}
private void renderResult()
{
mTexRenderer.renderTexture(mTextures[1]);
//mTexRenderer.renderTexture(mTextures[0]);
}
public class GLEnv {
private EGLContext mEGLContext;
private EGLSurface mEGLSurface;
private EGLDisplay mEGLDisplay;
private EGLConfig mEGLConfig;
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
private static final int EGL_OPENGL_ES2_BIT = 0x0004;
public GLEnv() {
EGL10 egl = (EGL10)EGLContext.getEGL();
mEGLDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
checkForEGLErrors("eglGetDisplay");
int[] version = new int[2];
egl.eglInitialize(mEGLDisplay, version);
int[] configSpec = {
EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
EGL10.EGL_RED_SIZE, 8,
EGL10.EGL_GREEN_SIZE, 8,
EGL10.EGL_BLUE_SIZE, 8,
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
egl.eglChooseConfig(mEGLDisplay, configSpec, configs, 1, num_config);
checkForEGLErrors("eglChooseConfig");
if (num_config[0] < 1) {
throw new RuntimeException("Could not find a suitable config for EGL context!");
}
mEGLConfig = configs[0];
int[] attribs = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
mEGLContext = egl.eglCreateContext(mEGLDisplay, mEGLConfig, EGL10.EGL_NO_CONTEXT, attribs);
checkForEGLErrors("eglCreateContext");
int[] surfaceSize = { EGL10.EGL_WIDTH, 1920, EGL10.EGL_HEIGHT, 1080, EGL10.EGL_NONE };
mEGLSurface = egl.eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, surfaceSize);
checkForEGLErrors("eglCreatePbufferSurface");
}
public void makeCurrent() {
EGL10 egl = (EGL10)EGLContext.getEGL();
egl.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext);
checkForEGLErrors("eglMakeCurrent");
}
public void checkForEGLErrors(String operation) {
EGL10 egl = (EGL10)EGLContext.getEGL();
int error = egl.eglGetError();
if (error != EGL10.EGL_SUCCESS) {
throw new RuntimeException("Operation '" + operation + "' caused EGL error: " + error);
}
}
}
private static final float[] TEX_VERTICES = {
0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
};
private static final float[] POS_VERTICES = {
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f
};
public class TextureRenderer {
private int mProgram;
private int mTexSamplerHandle;
private int mTexCoordHandle;
private int mPosCoordHandle;
private FloatBuffer mTexVertices;
private FloatBuffer mPosVertices;
private int mViewWidth;
private int mViewHeight;
private int mTexWidth;
private int mTexHeight;
private static final String VERTEX_SHADER =
"attribute vec4 a_position;\n" +
"attribute vec2 a_texcoord;\n" +
"varying vec2 v_texcoord;\n" +
"void main() {\n" +
" gl_Position = a_position;\n" +
" v_texcoord = a_texcoord;\n" +
"}\n";
private static final String FRAGMENT_SHADER =
"precision mediump float;\n" +
"uniform sampler2D tex_sampler;\n" +
"varying vec2 v_texcoord;\n" +
"void main() {\n" +
" gl_FragColor = texture2D(tex_sampler, v_texcoord);\n" +
"}\n";
private static final int FLOAT_SIZE_BYTES = 4;
public void init() {
// Create program
mProgram = GLToolbox.createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
// Bind attributes and uniforms
mTexSamplerHandle = GLES20.glGetUniformLocation(mProgram,
"tex_sampler");
mTexCoordHandle = GLES20.glGetAttribLocation(mProgram, "a_texcoord");
mPosCoordHandle = GLES20.glGetAttribLocation(mProgram, "a_position");
// Setup coordinate buffers
mTexVertices = ByteBuffer.allocateDirect(
TEX_VERTICES.length * FLOAT_SIZE_BYTES)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mTexVertices.put(TEX_VERTICES).position(0);
mPosVertices = ByteBuffer.allocateDirect(
POS_VERTICES.length * FLOAT_SIZE_BYTES)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mPosVertices.put(POS_VERTICES).position(0);
}
public void tearDown() {
GLES20.glDeleteProgram(mProgram);
}
public void updateTextureSize(int texWidth, int texHeight) {
mTexWidth = texWidth;
mTexHeight = texHeight;
computeOutputVertices();
}
public void updateViewSize(int viewWidth, int viewHeight) {
mViewWidth = viewWidth;
mViewHeight = viewHeight;
computeOutputVertices();
}
public void renderTexture(int texId) {
// Bind default FBO
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
// Use our shader program
GLES20.glUseProgram(mProgram);
GLToolbox.checkGlError("glUseProgram");
// Set viewport
GLES20.glViewport(0, 0, mViewWidth, mViewHeight);
GLToolbox.checkGlError("glViewport");
// Disable blending
GLES20.glDisable(GLES20.GL_BLEND);
// Set the vertex attributes
GLES20.glVertexAttribPointer(mTexCoordHandle, 2, GLES20.GL_FLOAT, false,
0, mTexVertices);
GLES20.glEnableVertexAttribArray(mTexCoordHandle);
GLES20.glVertexAttribPointer(mPosCoordHandle, 2, GLES20.GL_FLOAT, false,
0, mPosVertices);
GLES20.glEnableVertexAttribArray(mPosCoordHandle);
GLToolbox.checkGlError("vertex attribute setup");
// Set the input texture
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLToolbox.checkGlError("glActiveTexture");
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
GLToolbox.checkGlError("glBindTexture");
GLES20.glUniform1i(mTexSamplerHandle, 0);
// Draw
GLES20.glClearColor(0.0f, 0.0f, 0.5f, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}
private void computeOutputVertices() {
if (mPosVertices != null) {
float imgAspectRatio = mTexWidth / (float)mTexHeight;
float viewAspectRatio = mViewWidth / (float)mViewHeight;
float relativeAspectRatio = viewAspectRatio / imgAspectRatio;
float x0, y0, x1, y1;
if (relativeAspectRatio > 1.0f) {
x0 = -1.0f / relativeAspectRatio;
y0 = -1.0f;
x1 = 1.0f / relativeAspectRatio;
y1 = 1.0f;
} else {
x0 = -1.0f;
y0 = -relativeAspectRatio;
x1 = 1.0f;
y1 = relativeAspectRatio;
}
float[] coords = new float[] { x0, y0, x1, y0, x0, y1, x1, y1 };
mPosVertices.put(coords).position(0);
}
}
}
public static class GLToolbox {
public static int loadShader(int shaderType, String source) {
int shader = GLES20.glCreateShader(shaderType);
if (shader != 0) {
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
int[] compiled = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
String info = GLES20.glGetShaderInfoLog(shader);
GLES20.glDeleteShader(shader);
shader = 0;
throw new RuntimeException("Could not compile shader " +
shaderType + ":" + info);
}
}
return shader;
}
public static int createProgram(String vertexSource,
String fragmentSource) {
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
if (vertexShader == 0) {
return 0;
}
int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
if (pixelShader == 0) {
return 0;
}
int program = GLES20.glCreateProgram();
if (program != 0) {
GLES20.glAttachShader(program, vertexShader);
checkGlError("glAttachShader");
GLES20.glAttachShader(program, pixelShader);
checkGlError("glAttachShader");
GLES20.glLinkProgram(program);
int[] linkStatus = new int[1];
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus,
0);
if (linkStatus[0] != GLES20.GL_TRUE) {
String info = GLES20.glGetProgramInfoLog(program);
GLES20.glDeleteProgram(program);
program = 0;
throw new RuntimeException("Could not link program: " + info);
}
}
return program;
}
public static void checkGlError(String op) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
throw new RuntimeException(op + ": glError " + error);
}
}
public static void initTexParams() {
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
GLES20.GL_CLAMP_TO_EDGE);
}
}
}
So I finally figured it out myself after over clocking quite a few of the little grey cells.
The issue is with computeOutputVertices(); This might have worked with a GLSurfaceView, but for some reason, whatever its doing is not compatible with a PBuffer. Just comment that line out and it works beautifully.

Android OpenGL ES 2.0 textures

I've been trying to get into OpenGL ES world and everything have been fine(following book "OpenGL ES 2.0 Programming Guide" which is great!) until now. I've tried adding textures to primitive I've drawn, which I succeed in using previous versions of OpenGL ES and with WebGL.
I can perfectly draw texture if I place the "texture" inside Java code like this:
pixelBuffer.put(new byte[]{
0, 0, Byte.MAX_VALUE,
0, Byte.MAX_VALUE, 0,
Byte.MAX_VALUE, 0, 0,
0, 0, 0, 0});
but whenever I try to load the texture from external file, it just appears as black. Below is code I am using to load the texture and use it.
My Activity:
glSurface = new GLSurfaceView(this);
glSurface.setEGLContextClientVersion(2);
glSurface.setDebugFlags(GLSurfaceView.DEBUG_LOG_GL_CALLS | GLSurfaceView.DEBUG_CHECK_GL_ERROR);
this.setContentView(glSurface);
glSurface.setRenderer(new TriangleRenderer(this));
ShaderLoader:
public static int loadShader(int shaderType, String shaderSource) {
int shaderHandle = glCreateShader(shaderType);
glShaderSource(shaderHandle, shaderSource);
glCompileShader(shaderHandle);
int[] buffer = new int[1];
glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, buffer, 0);
if(buffer[0] == GLES20.GL_FALSE) {
Log.e("ShaderHelper", glGetShaderInfoLog(shaderHandle));
glDeleteShader(shaderHandle);
return -1;
}
return shaderHandle;
}
public static int loadProgram(String vertexShader, String fragmentShader) {
int programHandle = glCreateProgram();
glAttachShader(programHandle, loadShader(GL_VERTEX_SHADER, vertexShader));
glAttachShader(programHandle, loadShader(GL_FRAGMENT_SHADER, fragmentShader));
glLinkProgram(programHandle);
int[] buffer = new int[1];
glGetProgramiv(programHandle, GL_LINK_STATUS, buffer, 0);
if(buffer[0] == GL_FALSE) {
Log.e("ShaderHelper", glGetProgramInfoLog(programHandle));
glDeleteProgram(programHandle);
return -1;
}
return programHandle;
}
TextReader:
public static String readResource(Resources resources, int id) {
StringBuilder content = new StringBuilder(128);
BufferedReader br = new BufferedReader(new InputStreamReader(resources.openRawResource(id)));
String line = null;
try {
while((line = br.readLine()) != null) {
content.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Log.v("Readed text:", content.toString());
return content.toString();
}
Renderer:
private int shaderProgram;
private int vertexBufferPointer;
private int colorBufferPointer;
private int textureBufferPointer;
private Context context;
private float[] vertices = {-1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f};
private float[] colors = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f
};
private float[] textureVertices = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f
};
private int aVerPos, aTexPos, aVerCol;
private int uSamp;
private int texture;
private Bitmap textureBitmap;
public TriangleRenderer(Context context) {
this.context = context;
}
#Override
public void onDrawFrame(GL10 gl) {
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(uSamp, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferPointer);
glVertexAttribPointer(aVerPos, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, colorBufferPointer);
glVertexAttribPointer(aVerCol, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, textureBufferPointer);
glVertexAttribPointer(aTexPos, 2, GL_FLOAT, false, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
checkGLError("test");
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
glViewport(0, 0, width, height);
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
initBuffers();
initShaders();
initTextures();
}
private void initTextures() {
textureBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
Log.v("Bitmap inafo:", textureBitmap.getWidth() + ", " + textureBitmap.getHeight());
int[] buffer = new int[1];
glGenTextures(1, buffer, 0);
texture = buffer[0];
Log.v("Texture", "Texture is at " + texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, GL_TRUE);
GLUtils.texImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureBitmap, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
private void initShaders() {
shaderProgram = ShaderLoader.loadProgram(TextReader.readResource(context.getResources(), R.raw.vshader), TextReader.readResource(context.getResources(), R.raw.fshader));
Log.v("Shader program", shaderProgram + " is the id of shader program. :)");
glUseProgram(shaderProgram);
aVerPos = glGetAttribLocation(shaderProgram, "aVerPos");
if(aVerPos == -1) {
Log.e("Shader program", "Cudn't find aVerPos");
} else {
Log.v("Shader program", "Found vPosition # " + aVerPos);
}
glEnableVertexAttribArray(aVerPos);
aVerCol = glGetAttribLocation(shaderProgram, "aVerCol");
if(aVerCol == -1) {
Log.e("Error", "Couldn't find aVColor");
} else {
Log.v("Success", "aVColor is at " + aVerCol + " :-3");
}
glEnableVertexAttribArray(aVerCol);
aTexPos = glGetAttribLocation(shaderProgram, "aTexPos");
if(aTexPos == -1) {
Log.e("Error", "Failed 2 find aTexPos");
} else {
Log.v("Succeed", "Succesfully located aTexPos # " + aTexPos);
}
glEnableVertexAttribArray(aTexPos);
uSamp = glGetUniformLocation(shaderProgram, "uSampler");
if(uSamp == -1) {
Log.e("Error", "Couldn't finda uSampler " + uSamp);
} else {
Log.v("Succeed", "uSampler is # " + uSamp + " :3");
}
}
private void initBuffers() {
vertexBufferPointer = initFloatBuffer(vertices);
colorBufferPointer = initFloatBuffer(colors);
textureBufferPointer = initFloatBuffer(textureVertices);
}
private int initFloatBuffer(float[] data) {
int[] buffer = new int[1];
glGenBuffers(1, buffer, 0);
int pointer = buffer[0];
if(pointer == -1) {
Log.e("Error", "Couldn't create buffer");
} else {
Log.v("Success", "Succesfully created buffer to " + pointer);
}
glBindBuffer(GL_ARRAY_BUFFER, pointer);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4); //one float size is 4 bytes
byteBuffer.order(ByteOrder.nativeOrder()); //byte order must be native
FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
floatBuffer.put(data);
floatBuffer.flip();
glBufferData(GL_ARRAY_BUFFER, data.length * 4, floatBuffer, GL_STATIC_DRAW);
return pointer;
}
private void checkGLError(String op) {
int error = glGetError();
if(error != GL_NO_ERROR) {
Log.e("Error", op + "'s errorcode:" + Integer.toHexString(error));
}
}
and ofcourse
Vertex Shader:
attribute vec3 aVerPos;
attribute vec3 aVerCol;
attribute vec2 aTexPos;
varying vec3 vVerCol;
varying vec2 vTexPos;
void main(void) {
vTexPos = aTexPos;
vVerCol = aVerCol;
gl_Position = vec4(aVerPos, 1.0);
}
Fragment Shader:
precision mediump float;
varying vec3 vVerCol;
varying vec2 vTexPos;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTexPos);
}
and result of executing that code is black screen and my custom logs
03-24 18:11:44.933: D/libEGL(4805): loaded /system/lib/egl/libGLES_android.so
03-24 18:11:44.948: D/libEGL(4805): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
03-24 18:11:44.956: D/libEGL(4805): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
03-24 18:11:44.956: D/libEGL(4805): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
03-24 18:11:45.066: D/dalvikvm(4805): Note: class Landroid/opengl/GLWrapperBase; has 250 unimplemented (abstract) methods
03-24 18:11:45.073: V/GLSurfaceView(4805): glGetString(7937) returns PowerVR SGX 540;
03-24 18:11:45.073: V/Success(4805): Succesfully created buffer to 70001
03-24 18:11:45.073: V/Success(4805): Succesfully created buffer to 140002
03-24 18:11:45.073: V/Success(4805): Succesfully created buffer to 210003
03-24 18:11:45.081: V/Readed text:(4805): attribute vec3 aVerPos;attribute vec3 aVerCol;attribute vec2 aTexPos;varying vec3 vVerCol;varying vec2 vTexPos;void main(void) { vTexPos = aTexPos; vVerCol = aVerCol; gl_Position = vec4(aVerPos, 1.0);}
03-24 18:11:45.081: V/Readed text:(4805): precision mediump float;varying vec3 vVerCol;varying vec2 vTexPos;uniform sampler2D uSampler;void main(void) { gl_FragColor = texture2D(uSampler, vTexPos);}
03-24 18:11:45.097: V/Shader program(4805): 70001 is the id of shader program. :)
03-24 18:11:45.097: V/Shader program(4805): Found vPosition # 2
03-24 18:11:45.105: V/Success(4805): aVColor is at 1 :-3
03-24 18:11:45.105: V/Succeed(4805): Succesfully located aTexPos # 0
03-24 18:11:45.105: V/Succeed(4805): uSampler is # 1 :3
03-24 18:11:45.105: V/Bitmap inafo:(4805): 96, 96
03-24 18:11:45.105: V/Texture(4805): Texture is at 70001
I've also tried using legen... wait for it... dary nehe texture but didn't show up either
Okay.. out of stupidity I only added the image into drawable-hdpi.. This happened to me before but I didn't learn from it. Problem was solved by moving the picture to /raw folder

Using SurfaceTexture in Android

I want to play a video into an OpenGL texture on XOOM using Android 3.0.
I have come across SurfaceTexture in the goole developer docs which has been added in API 11
http://developer.android.com/reference/android/graphics/SurfaceTexture.html
The documentation talks about using GL_TEXTURE_EXTERNAL_OES instead of GL_TEXTURE_2D, I cannot find any reference to this define in the latest android-ndk-r5c, it's not defined in in the ndk and it only goes up to platform-9, so I guess I would need platform-11 and there appears to be no android.opengl.GLES20Ext which I guess is where it would reside on the java side.
I have the latest of all released android tools from google and I cannot find any extra things from NVidia or Motorola on their developer sites.
Do anyone have a working example of using SurfaceTexture to either put the camera image or video onto an OpenGL texture? and/or know what I am missing to be able to use this new functionality?
A demo locate https://github.com/crossle/MediaPlayerSurface
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.Log;
import android.view.Surface;
class VideoSurfaceView extends GLSurfaceView {
VideoRender mRenderer;
private MediaPlayer mMediaPlayer = null;
public VideoSurfaceView(Context context, MediaPlayer mp) {
super(context);
setEGLContextClientVersion(2);
mMediaPlayer = mp;
mRenderer = new VideoRender(context);
setRenderer(mRenderer);
}
#Override
public void onResume() {
queueEvent(new Runnable(){
public void run() {
mRenderer.setMediaPlayer(mMediaPlayer);
}});
super.onResume();
}
private static class VideoRender
implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener {
private static String TAG = "VideoRender";
private static final int FLOAT_SIZE_BYTES = 4;
private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0;
private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
private final float[] mTriangleVerticesData = {
// X, Y, Z, U, V
-1.0f, -1.0f, 0, 0.f, 0.f,
1.0f, -1.0f, 0, 1.f, 0.f,
-1.0f, 1.0f, 0, 0.f, 1.f,
1.0f, 1.0f, 0, 1.f, 1.f,
};
private FloatBuffer mTriangleVertices;
private final String mVertexShader =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uSTMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" +
"}\n";
private final String mFragmentShader =
"#extension GL_OES_EGL_image_external : require\n" +
"precision mediump float;\n" +
"varying vec2 vTextureCoord;\n" +
"uniform samplerExternalOES sTexture;\n" +
"void main() {\n" +
" gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
"}\n";
private float[] mMVPMatrix = new float[16];
private float[] mSTMatrix = new float[16];
private int mProgram;
private int mTextureID;
private int muMVPMatrixHandle;
private int muSTMatrixHandle;
private int maPositionHandle;
private int maTextureHandle;
private SurfaceTexture mSurface;
private boolean updateSurface = false;
private static int GL_TEXTURE_EXTERNAL_OES = 0x8D65;
private MediaPlayer mMediaPlayer;
public VideoRender(Context context) {
mTriangleVertices = ByteBuffer.allocateDirect(
mTriangleVerticesData.length * FLOAT_SIZE_BYTES)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mTriangleVertices.put(mTriangleVerticesData).position(0);
Matrix.setIdentityM(mSTMatrix, 0);
}
public void setMediaPlayer(MediaPlayer player) {
mMediaPlayer = player;
}
public void onDrawFrame(GL10 glUnused) {
synchronized(this) {
if (updateSurface) {
mSurface.updateTexImage();
mSurface.getTransformMatrix(mSTMatrix);
updateSurface = false;
}
}
GLES20.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUseProgram(mProgram);
checkGlError("glUseProgram");
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maPosition");
GLES20.glEnableVertexAttribArray(maPositionHandle);
checkGlError("glEnableVertexAttribArray maPositionHandle");
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maTextureHandle");
GLES20.glEnableVertexAttribArray(maTextureHandle);
checkGlError("glEnableVertexAttribArray maTextureHandle");
Matrix.setIdentityM(mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
checkGlError("glDrawArrays");
GLES20.glFinish();
}
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
}
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
mProgram = createProgram(mVertexShader, mFragmentShader);
if (mProgram == 0) {
return;
}
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
checkGlError("glGetAttribLocation aPosition");
if (maPositionHandle == -1) {
throw new RuntimeException("Could not get attrib location for aPosition");
}
maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
checkGlError("glGetAttribLocation aTextureCoord");
if (maTextureHandle == -1) {
throw new RuntimeException("Could not get attrib location for aTextureCoord");
}
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
checkGlError("glGetUniformLocation uMVPMatrix");
if (muMVPMatrixHandle == -1) {
throw new RuntimeException("Could not get attrib location for uMVPMatrix");
}
muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
checkGlError("glGetUniformLocation uSTMatrix");
if (muSTMatrixHandle == -1) {
throw new RuntimeException("Could not get attrib location for uSTMatrix");
}
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
mTextureID = textures[0];
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);
checkGlError("glBindTexture mTextureID");
GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_NEAREST);
GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
/*
* Create the SurfaceTexture that will feed this textureID,
* and pass it to the MediaPlayer
*/
mSurface = new SurfaceTexture(mTextureID);
mSurface.setOnFrameAvailableListener(this);
Surface surface = new Surface(mSurface);
mMediaPlayer.setSurface(surface);
surface.release();
try {
mMediaPlayer.prepare();
} catch (IOException t) {
Log.e(TAG, "media player prepare failed");
}
synchronized(this) {
updateSurface = false;
}
mMediaPlayer.start();
}
synchronized public void onFrameAvailable(SurfaceTexture surface) {
updateSurface = true;
}
private int loadShader(int shaderType, String source) {
int shader = GLES20.glCreateShader(shaderType);
if (shader != 0) {
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
int[] compiled = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
Log.e(TAG, "Could not compile shader " + shaderType + ":");
Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
GLES20.glDeleteShader(shader);
shader = 0;
}
}
return shader;
}
private int createProgram(String vertexSource, String fragmentSource) {
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
if (vertexShader == 0) {
return 0;
}
int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
if (pixelShader == 0) {
return 0;
}
int program = GLES20.glCreateProgram();
if (program != 0) {
GLES20.glAttachShader(program, vertexShader);
checkGlError("glAttachShader");
GLES20.glAttachShader(program, pixelShader);
checkGlError("glAttachShader");
GLES20.glLinkProgram(program);
int[] linkStatus = new int[1];
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
if (linkStatus[0] != GLES20.GL_TRUE) {
Log.e(TAG, "Could not link program: ");
Log.e(TAG, GLES20.glGetProgramInfoLog(program));
GLES20.glDeleteProgram(program);
program = 0;
}
}
return program;
}
private void checkGlError(String op) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
Log.e(TAG, op + ": glError " + error);
throw new RuntimeException(op + ": glError " + error);
}
}
} // End of class VideoRender.
} // End of class VideoSurfaceView.
Example code. This creates a new external texture suitable for use in a SurfaceTexture, then wraps it in said SurfaceTexture and passes it to the camera as a surface to write the preview into.
int[] textures = new int[1];
// generate one texture pointer and bind it as an external texture.
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textures[0]);
// No mip-mapping with camera source.
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
// Clamp to edge is only option.
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
int texture_id = textures[0];
SurfaceTexture mTexture = new SurfaceTexture(texture_id);
mTexture.setOnFrameAvailableListener(this);
Camera cam = Camera.open();
cam.setPreviewTexture(mTexture);
Note that if you render this object, you'll need to be careful: It's NOT a 2D texture, so it needs special treatment in the shader.
If using API level 11 to 14 you can just define GL_TEXTURE_EXTERNAL_OES yourself by placing
private static final int GL_TEXTURE_EXTERNAL_OES = 0x8D65;
in your code. This seems to work just fine for me.
NVIDIA has a full and working sample in their TEGRA Android Developer pack. The sample is written pure Java and runs in Standard Eclipse+Android SDK - so you just need to install the samples.
The name of the project is surfacetexture (or something like it). It works nice!
Hopes it helps you.

Categories

Resources