I've found andengine sample with blur effect and it works fine, i found that i can use ShaderProgram class and in PreDraw of the Sprite use some shaders (setShaders).
Can i use the same class ShaderProgramm and use shader without attach to sprite ?
For example in onDraw use Shader that only draw a grenn triangle on scene (like in android opengl example). Is there simple way to use ShaderProgram in this way (how can i attach shaders out of PreDraw of Sprite) ?
I've tried smth like this:
BrushShaderProgram bs = BrushShaderProgram.getInstance();
bs.link(pGLState);
GLES20.glUseProgram(bs.getId());
GLES20.glVertexAttribPointer(bs.maPositionHandle, 3, GLES20.GL_FLOAT, false, 12, triangleVB);
GLES20.glEnableVertexAttribArray(bs.maPositionHandle);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
shader is simple:
public static final String VERTEXSHADER =
"attribute vec4 vPosition; \n" +
"void main(){ \n" +
" gl_Position = vPosition; \n" +
"} \n";
public static final String FRAGMENTSHADER = "precision mediump float; \n" +
"void main(){ \n" +
" gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" +
"} \n";
Why not create a Triangle class, just like the Rectangle class?
Related
I am trying to add the translation to a simple example of drawing a triangle using OpenGL ES 2.0 for Android. I have created translation matrix with 0 translation in all directions which should be the identity matrix. When I remove the multiplication from vertex shader triangle renders fine. However when I try to add some transformation(even identity) nothing is rendered. I think it is something about sending matrix data to the shader. GL.UniformMatrix 4 only takes float as attributes and I cant convert Matrix4 to float array. At the end I desperately created float array for identity matrix and passed it using GL.UniformMAtrix4 however nothing is rendered again.
Can anyone with the experience help me out please.
Matrix4 uvMat = Matrix4.CreateTranslation(0.0f,0.0f,0.0f);
// Vertex and fragment shaders
string vertexShaderSrc = "attribute vec4 vPosition; \n" +
"uniform mat4 uvMat; \n" +
"void main() \n" +
"{ \n" +
" gl_Position = uvMat*vPosition; \n" +
"} \n";
string fragmentShaderSrc = "precision mediump float;\n" +
"void main() \n" +
"{ \n" +
" gl_FragColor = vec4 (1.0, 0.0, 0.0, 1.0); \n" +
"} \n";
int vertexShader = LoadShader (All.VertexShader, vertexShaderSrc );
int fragmentShader = LoadShader (All.FragmentShader, fragmentShaderSrc );
program = GL.CreateProgram();
if (program == 0)
throw new InvalidOperationException ("Unable to create program");
GL.AttachShader (program, vertexShader);
GL.AttachShader (program, fragmentShader);
GL.BindAttribLocation (program, 0, "vPosition");
GL.LinkProgram (program);
float[] matTrans = new float[] {1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
int uniMatLoc = GL.GetUniformLocation (program, "uvMat");
Console.WriteLine (uniMatLoc.ToString());
GL.UniformMatrix4 (uniMatLoc, 16, false, matTrans);
The second argument to UniformMatrix4 is the number of matrices, not the number of elements in the matrix. So the call should be:
GL.UniformMatrix4(uniMatLoc, 1, false, matTrans);
I want to change a pixel color of the Bitmap with OpenGLES, by picking it with e.g. coordinates. What do I have to do? Any advices?
fragmentShader = riGraphicTools.loadShader(GLES20.GL_FRAGMENT_SHADER, riGraphicTools.test_Image);
riGraphicTools.sp_Image = GLES20.glCreateProgram();
GLES20.glAttachShader(riGraphicTools.sp_Image, fragmentShader);
public static final String test_Image =
"precision mediump float;" +
"varying vec2 v_texCoord;" +
"uniform sampler2D s_texture;" +
"void main() {" +
" gl_FragColor = texture2D( s_texture, v_texCoord );" +
"}";
i don't think ES 2.0 supports writeable textures, so instead of modifying sp_image on the fly, just render to texture with an FBO and use that texture in place of your original sp_image.
I'm rendering a libgdx mesh using shader with alpha color less than 1.0.
When rendering the first frame, the alpha value set in shader is being ignored and rendered as 1.0. All following rendering frames are fine.
The same happened to me in previous project drawing lines and shapes using glDrawArrays and I haven't found the solution yet.
Code in libgdx render() loop:
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
Gdx.gl20.glEnable(GL20.GL_BLEND);
MeshManager_colorFill.meshShader.begin();
MeshManager_colorFill.meshShader.setUniformMatrix("u_worldView", Snappr.camMatrix);
meshArray_colorFill.render(MeshManager_colorFill.meshShader, GL20.GL_TRIANGLE_FAN, 0, meshArray_colorFill.get(i).getNumVertices());
MeshManager_colorFill.meshShader.end();
Gdx.gl20.glDisable(GL20.GL_BLEND);
My shader (compiled in create(){}):
public static final String meshVertexShader_colorFill =
"attribute vec2 a_position; \n" +
"uniform mat4 u_worldView;\n" +
"void main() \n" +
"{ \n" +
" gl_Position = u_worldView * vec4(a_position.xy, 0, 1); \n" +
"} \n" ;
public static final String meshFragmentShader_colorFill =
"precision mediump float;\n" +
"void main() \n" +
"{ \n" +
" gl_FragColor = vec4(1,1,1,0.2);\n" +
"}";
How do I render the very first frame as it should be?
Thanks
glBlendFunc in create() does the trick, specifically: "Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, L20.GL_ONE_MINUS_SRC_ALPHA);" I'm using SpriteBatch to render a font, right after the mesh. glBlendFunc is contained internally within the SpriteBatch, looks like that's why all the other frames were fine.
I found that ModelBatch.begin(...) will disable Blending by calling Gdx.gl.glDisable(GL20.GL_BLEND). So make sure to enable blending AFTER calling ModelBatch.begin().
modelBatch.begin(camera); // resets Blending to false
// enable Blending
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
// draw mesh
mesh.render(shader, GL20.GL_TRIANGLES);
modelBatch.end();
Source https://stackoverflow.com/a/66820414/2413469
This vertex shader code works on every device except for the Galaxy Note 2.
gl_Position = uMVPMatrix * vPosition;
where if I reverse the matrix multiplication to:
gl_Position = vPosition * uMVPMatrix; I can actually get things to appear.
Unfortunately, the reverse would require me to completely rewrite my transformations library.
Does anyone have any insight on what could be causing this, is this an opengl driver error with the device?
Shader code
private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"attribute vec2 a_TexCoordinate;" +
"varying vec2 v_TexCoordinate;" +
"void main() {" +
// the matrix must be included as a modifier of gl_Position
"v_TexCoordinate = a_TexCoordinate;" +
"gl_Position = uMVPMatrix * vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform sampler2D u_Texture;" +
"varying vec2 v_TexCoordinate;" +
"void main() {" +
" gl_FragColor = texture2D(u_Texture, v_TexCoordinate);" +
//" gl_FragColor = vec4(v_TexCoordinate, 0, 1);" +
"}";
This is not about only a galaxy note 2 platform.
This is a mathematical question. Because both glsl/hlsl uses column major order,
It is a right way to multiply MATRIX x VECTOR
or
you can transpose the matrix using a option in
glUniformMatrix4fv( h_Uniforms[UNIFORMS_PROJECTION], 1, GL_FALSE or GL_TRUE, g_proxtrans.s);
Apparently, It can be a problem not to call this function with the option ( GL_TRUE is to use transposing ) each every frame just try .
In My application i have to use the Android Camera, and OpenGLES.
I have also have to give the effect to the Camera Vision with two files called one.vsh and one.fsh But i dont know how to implement that file in OpenGLES.
Even i also dont know how to implement android camera to work with OPENGLES to do effect with that two files.
Please help me for this.
Thanks.
Well, I don't have a test about Android camera to use such effect on it.
But ofcourse, you can use the shader file in the onSurfaceCreated method as like below:
//
// Initialize the shader and program object
//
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
String vShaderStr = "uniform mat4 u_mvpMatrix; \n"
+ "attribute vec4 a_position; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_Position = u_mvpMatrix * a_position; \n"
+ "} \n";
String fShaderStr = "precision mediump float; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n"
+ "} \n";
// Load the shaders and get a linked program object
mProgramObject = ESShader.loadProgram(vShaderStr, fShaderStr);
// Get the attribute locations
mPositionLoc = GLES20.glGetAttribLocation(mProgramObject, "position");
// Get the uniform locations
mMVPLoc = GLES20.glGetUniformLocation(mProgramObject, "u_mvpMatrix");
// Generate the vertex data
mCube.genCube(1.0f);
// Starting rotation angle for the cube
mAngle = 45.0f;
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
Just replace the String that you want to use for vertex shader and fragment shader.
Hope this helps.