What is differences between glVertexAttribPointer and glVertexAttrib1f - opengl-es-2.0

In opengl es 2.0 when I wanted to change the attribute named "a_degree" in vertex shader at first I used glVertexAttribPointer and glEnableVertexAttribArray with true parameters but the behaviour is totally different when I used glVertexAttrib1f why?
here is my shaders code:
const char* CGraphic::VERTEX_SHADER_SOURCE =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"attribute vec4 a_color; \n"
"attribute float a_degree; \n"
"varying lowp vec4 v_color; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" float radianS = a_degree* "
" (3.14159265/180.0); \n"
" float s = sin(radianS); \n"
" float c = cos(radianS); \n"
" mat4 mvpMatrix=mat4( \n"
" c,-s,0,0, "
" s,c,0,0, "
" 0,0,1,0, "
" 0,0,0,1); \n"
" v_color = a_color; \n"
" gl_Position = a_position*mvpMatrix; \n"
" v_texCoord = a_texCoord; \n"
"} \n";
const char* CGraphic::FRAGMENT_SHADER_SOURCE =
"precision mediump float; \n"
" \n"
"varying vec4 vColor; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_texture; \n"
" \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D( s_texture, v_texCoord );\n"
"} \n";
use with:
glEnableVertexAttribArray ( m_shaderData.rotateLoc );
glVertexAttribPointer ( m_shaderData.rotateLoc, 1, GL_FLOAT,
GL_FALSE, 0, &degree );
vs
glVertexAttrib1f(m_shaderData.rotateLoc,degree);
In fact glVertexAttrib1f work fine in this situation and my texture rotate correctly but with glVertexAttribPointer just one point of the texture rotate that isn't my desire.

glVertexAttrib allows you to specify a fixed value for the attribute.
In contrast, glVertexAttribPointer when enabled via glEnableVertexAttribArray allows you to specify a unique value for each vertex.
Read more here:
https://www.khronos.org/opengles/sdk/docs/man/xhtml/glVertexAttrib.xml
https://www.khronos.org/opengles/sdk/docs/man/xhtml/glVertexAttribPointer.xml
So, if you are drawing a triangle with multiple points, you would need to specify a separate degree for each point when using glVertexAttribPointer. Thus, degree would need to be a float[], while it looks like you're only specifying a single value right now as a float.
Most likely the values after degree in memory are zeros, which is why the other points are not rotating.
If you want the value to be the same, you CAN use glVertexAttrib. If you're never going to specify it per vertex, using a uniform value is likely better.

Related

No uniform with name in shader exclusively on Raspberry Pi

Similar Questions:
No uniform with name in shader ,
Fragment shader: No uniform with name in shader
LibGDX: libgdx.badlogicgames.com
LibOnPi: www.habitualcoder.com/?page_id=257
I am attempting to run LibGDX on a Raspberry Pi with little luck. After some trial and error I eventually got it to start throwing the error "no uniform with name 'mvp' in shader". The problem is much like the similar questions, however in my situation it seems to me that 'mvp' is actually being used by the shader to set the positions.
The really strange part is that it runs on the PC (Windows 7 x64 in Eclipse ADT) just fine, but not on the Pi. Does the pi handle shaders differently, if not, what is causing this error to be thrown exclusively on the pi?
Vertex_Shader =
"attribute vec3 a_position; \n"
+ "attribute vec4 a_color; \n"
+ "attribute vec2 a_texCoords; \n"
+ "uniform mat4 mvp; \n"
+ "varying vec4 v_color; \n" + "varying vec2 tCoord; \n"
+ "void main() { \n"
+ " v_color = a_color; \n"
+ " tCoord = a_texCoords; \n"
+ " gl_Position = mvp * vec4(a_position, 1f); \n"
+ "}";
Fragment_Shader =
"precision mediump float; \n"
+ "uniform sampler2D u_texture; \n"
+ "uniform int texture_Enabled; \n"
+ "varying vec4 v_color; \n"
+ "varying vec2 tCoord; \n"
+ "void main() { \n"
+ " vec4 texColor = texture2D(u_texture, tCoord); \n"
+ " gl_FragColor = ((texture_Enabled == 1)?texColor:v_color); \n"
+ "}";
...
shader = new ShaderProgram(Vertex_Shader, Fragment_Shader);
...
shader.setUniformMatrix("mvp", camera.combined);
I also noticed this question:
c++ OpenGL glGetUniformLocation for Sampler2D returns -1 on Raspberry PI but works on Windows
which is quite similar, however implementing the proposed solution of putting the "#version 150" at the top of the shader just broke it on the PC too. (stated that there was no uniform with name 'mvp')
EDIT:
1 - Added Fragment Shader at request of keaukraine
2 - Fix found by Keaukraine and ArttuPeltonen. Raspberry Pi requires a version number in the shader. OpenGl-ES 2.0 uses version 100
Answer provided by keaukraine and ArttuPeltonen
Raspberry Pi requires a version number in the shader. OpenGl-ES 2.0 uses version 100. It did not work when I initially tried it due to forgetting to add whitespace. "#version 100attribute..." is not the same as "#version 100\nattribute"
Example Final Shader:
Vertex_Shader =
"#version 100\n"
+ "attribute vec3 a_position; \n"
+ "attribute vec4 a_color; \n"
+ "attribute vec2 a_texCoords; \n"
+ "uniform mat4 mvp; \n"
+ "varying vec4 v_color; \n" + "varying vec2 tCoord; \n"
+ "void main() { \n"
+ " v_color = a_color; \n"
+ " tCoord = a_texCoords; \n"
+ " gl_Position = mvp * vec4(a_position, 1f); \n"
+ "}";
Thank you both.

GPUImage and Shader Compile Errors

I'm implementing a custom filter creating two strings one as a vertex shader and the other one as a fragment one.
I'm in the early stage and I was just trying to compile and run a program with custom shaders just copying the one used in GPUImage.
Here the shaders:
NSString *const CustomShaderV = SHADER_STRING
(
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
attribute vec4 inputTextureCoordinate2;
varying vec2 textureCoordinate;
varying vec2 textureCoordinate2;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
textureCoordinate2 = inputTextureCoordinate2.xy;
}
);
NSString *const CustomShaderF = SHADER_STRING
(
varying highp vec2 textureCoordinate;
varying highp vec2 textureCoordinate2;
uniform sampler2D inputImageTexture;
uniform sampler2D inputImageTexture2;
uniform highp float bVal;
uniform highp float hVal;
uniform highp float sVal;
void main()
{
lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
lowp vec4 alphaColor = texture2D(inputImageTexture2, textureCoordinate2);
lowp vec4 outputColor;
outputColor = textureColor;
gl_FragColor = outputColor;
}
);
However whenever I initialize a filter with such shaders my app crash with this log.
2013-11-24 17:58:40.243[865:60b] Shader compile log:
ERROR: 0:1: 'CustomShaderV' : syntax error syntax error
2013-11-24 17:58:40.245[865:60b] Failed to compile vertex shader
2013-11-24 17:58:40.248[865:60b] Shader compile log:
ERROR: 0:1: 'CustomShaderF' : syntax error syntax error
2013-11-24 17:58:40.249[865:60b] Failed to compile fragment shader
2013-11-24 17:58:40.250[865:60b] Program link log: ERROR: One or more attached shaders not successfully compiled
2013-11-24 17:58:40.252[865:60b] Fragment shader compile log: (null)
2013-11-24 17:58:40.253[865:60b] Vertex shader compile log: (null)
The shaders are correct to me since are just cut and copy of other working shaders included in the GPUImage projects.
The call inside the code to the two shaders above is as following:
customFilter = [[GPUImageFilter alloc] initWithVertexShaderFromString:#"CustomShaderV" fragmentShaderFromString:#"CustomShaderF"];
My goal is to blend two videos that's why two textures are present in the shaders.
Based on the comment by OP,
here is the call: customFilter = [[GPUImageFilter alloc] initWithVertexShaderFromString:#"CustomShaderV" fragmentShaderFromString:#"CustomShaderF"];
the error was that the variable name was quoted and being used as a string. Fix was:
Change that to: [[GPUImageFilter alloc] initWithVertexShaderFromString:CustomShaderV fragmentShaderFromString:CustomShaderF];

Switching between textures in OpenGL ES 2

I'm trying to figure out how to put different textures into different texture units and choose which texture to draw with. I have the following code in my onDrawFrame() method
int[] texture = new int[7];
texture[0] =TextureHelper.loadTexture(mActivityContext,R.drawable.texture1);
texture[1] =TextureHelper.loadTexture(mActivityContext,R.drawable.texture2);
texture[2] =TextureHelper.loadTexture(mActivityContext,R.drawable.texture3);
texture[3] =TextureHelper.loadTexture(mActivityContext,R.drawable.texture4);
texture[4] =TextureHelper.loadTexture(mActivityContext,R.drawable.texture5);
texture[5] =TextureHelper.loadTexture(mActivityContext,R.drawable.texture6);
texture[6] =TextureHelper.loadTexture(mActivityContext,R.drawable.texture7);
for (int i = 0; i < 7; i ++) {
GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[i]);
GLES20.glUniform1i(mTextureUniformHandle, i);
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, -0.60f + 0.2f * i, 0.0f, 0.0f);
draw();
}
What this is supposed to do is load seven different textures into separate texture units and draw cubes, each cube with a different texture. However, what ends up happening is that all of the cubes end up being drawn with the first texture.
It works correctly if I change GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i) to GLES20.glActiveTexture(GLES20.GL_TEXTURE0) and GLES20.glUniform1i(mTextureUniformHandle, i) to GLES20.glUniform1i(mTextureUniformHandle, 0), but that just uses a single texture unit and replaces the texture in that unit every time, which is not what I want to do.
What am I doing wrong?
Thanks in advance.
EDIT:
Vertex shader:
"uniform mat4 u_MVPMatrix;" + // A constant representing the
// combined
// model/view/projection matrix.
"uniform mat4 u_MVMatrix;" + // A constant representing the
// combined model/view matrix.
"attribute vec4 a_Position;" + // Per-vertex position
// information we will pass in.
"attribute vec4 a_Color;" + // Per-vertex color information we
// will pass in.
"attribute vec2 a_TexCoordinate;" + // Per-vertex texture
// coordinate information we
// will pass in.
"varying vec3 v_Position;" + // This will be passed into the
// fragment shader.
"varying vec4 v_Color;" + // This will be passed into the
// fragment shader.
"varying vec2 v_TexCoordinate;" + // This will be passed into
// the fragment shader.
// The entry point for our vertex shader.
"void main()" + "{" +
// Transform the vertex into eye space.
"v_Position = vec3(u_MVMatrix * a_Position);" +
// Pass through the color.
"v_Color = a_Color;" +
// Pass through the texture coordinate.
"v_TexCoordinate = a_TexCoordinate;" +
// gl_Position is a special variable used to store the final
// position.
// Multiply the vertex by the matrix to get the final point in
// normalized screen coordinates.
"gl_Position = u_MVPMatrix * a_Position;" + "} ";
Fragment shader:
"precision mediump float;" + // Set the default precision to medium. We don't need as high of a
// precision in the fragment shader.
"uniform sampler2D u_Texture;" + // The input texture.
"varying vec3 v_Position;" + // Interpolated position for this fragment.
"varying vec4 v_Color;" + // This is the color from the vertex shader interpolated across the
// triangle per fragment.
"varying vec2 v_TexCoordinate;" + // Interpolated texture coordinate per fragment.
// The entry point for our fragment shader.
"void main()" +
"{" +
// Multiply the color by the diffuse illumination level and texture value to get final output color.
"gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));" +
"}";
draw() method:
public void draw() {
// Pass in the position information
mCubePositions.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 0, mCubePositions);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
mCubeColors.position(0);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 0, mCubeColors);
GLES20.glEnableVertexAttribArray(mColorHandle);
// Pass in the texture coordinate information
mCubeTextureCoordinates.position(0);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0, mCubeTextureCoordinates);
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
// This multiplies the view matrix by the model matrix, and stores the
// result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
// Pass in the modelview matrix.
GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);
// This multiplies the modelview matrix by the projection matrix, and
// stores the result in the MVP matrix
// (which now contains model * view * projection).
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
// Pass in the combined matrix.
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
// Draw the cube.
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
}
Assigning mTextureUniformHandle :
mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture");
Lately I've been searching for multiple textures in fragment shader and came across this Binding textures to samplers
from which I got the following to work:
In onSurfaceCreated or onSurfaceChanged:
Load shaders (attach and link) and get uniform locations for sampler2D (and other variables):
normalMapLoc = GLES20.glGetUniformLocation(shaderProgram, "normalMap");
shadowMapLoc = GLES20.glGetUniformLocation(shaderProgram, "shadowMap");
Load textures:
GLES20.glGenTextures(2, textures, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GL10.GL_TEXTURE_COORD_ARRAY, textures[1]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, mColorBuffer);
GLES20.glUniform1i(normalMapLoc, 0); // Texture unit 0 is for normal images.
GLES20.glUniform1i(shadowMapLoc, 1); // Texture unit 1 is for shadow maps.
In onDrawFrame:
GLES20.glClearColor(0f, 0f, 0f, 0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// pass variables to the fragment shader
...
// get handle to vertex shader's Position member, etcetera
int mPositionHandle = GLES20.glGetAttribLocation(shaderProgram, "vPosition");
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, mVertexBuffer);
GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, 4, GLES20.GL_UNSIGNED_SHORT, mIndexBuffer);
and finally the fragment shader looks like this (only relevant portion of code):
uniform sampler2D normalMap, shadowMap;
varying vec2 pos;
void main() {
vec4 color = texture2D(normalMap, pos);
vec4 shadow = texture2D(shadowMap, pos);
// do stuff with the colors
...
gl_FragColor = ...;
}
This way i was finally able to access both textures !
Hope this helps.

OpenGL ES 2 squeeze distortion filter like photobooth app

I'm tryng to code a squeeze effect like the one in photobooth ios or osx app; I'm new to shaders, but I was able to implement GPUImage library and tweek some shaders; however all that I was able to obtain is a spherical distortion, and the final result is a bit different from what I would like to achieve.
here is some code I modded from #import "GPUImageBulgeDistortionFilter.h"
in particular I was using this code
NSString *const kGPUImageBulgeDistortionFragmentShaderString = SHADER_STRING
(
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform highp vec2 center;
uniform highp float radius;
uniform highp float scale;
void main()
{
highp vec2 textureCoordinateToUse = textureCoordinate;
highp float dist = distance(center, textureCoordinate);
highp float PI = 3.14159265358979323846264;//stone added
textureCoordinateToUse -= center;
textureCoordinateToUse.x = 0.5+textureCoordinateToUse.x*cos(textureCoordinateToUse.x*PI/radius)*scale;
textureCoordinateToUse.y = 0.5 + textureCoordinateToUse.y*cos(textureCoordinateToUse.y*PI/radius)*scale;
gl_FragColor = texture2D(inputImageTexture, textureCoordinateToUse );
}
);
This code is using cos and sin and PI so it's definitely in the spherical range; any hint to make it more planar, with a tiny unstretched part in the middle will be a great help !

OpenglES 2.0 GL_POINTS disappear

I am using gl_points as sprites, when points reach screen boundary they disappear, problem is that my points are larger than 1 pixel, so they disappear when half of point beaks trough screen bound, i am assuming that there is some kind of culling turned on to remove points that are offscreen, question is how to turn it off.
GLES20Renderer.programLight = GLES20.glCreateProgram();
int vertexShaderLight = GLES20Renderer.loadShader(GLES20.GL_VERTEX_SHADER, GLES20Renderer.vertexShaderCodeLight);
int fragmentShaderLight = GLES20Renderer.loadShader(GLES20.GL_FRAGMENT_SHADER, GLES20Renderer.fragmentShaderCodeLight);
GLES20.glAttachShader(GLES20Renderer.programLight, vertexShaderLight);
GLES20.glAttachShader(GLES20Renderer.programLight, fragmentShaderLight);
GLES20.glLinkProgram(GLES20Renderer.programLight);
uPLocationLight = GLES20.glGetUniformLocation(GLES20Renderer.programLight, "uP");
uVPositionLocationLight = GLES20.glGetUniformLocation(GLES20Renderer.programLight, "uVPosition");
GLES20.glUseProgram(GLES20Renderer.programLight);
GLES20.glUniform4f(uVPositionLocationLight, LightPosInEyeSpace[0], LightPosInEyeSpace[1], LightPosInEyeSpace[2], LightPosInEyeSpace[3]);
GLES20.glUniformMatrix4fv(uPLocationLight, 1, false, ProjectionMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
private static final String vertexShaderCodeLight =
"uniform vec4 uVPosition; \n"
+ "uniform mat4 uP; \n"
+ "void main(){ \n"
+ " gl_PointSize = 15.0; \n"
+ " gl_Position = uP * uVPosition; \n"
+ "} \n";
private static final String fragmentShaderCodeLight =
"#ifdef GL_FRAGMENT_PRECISION_HIGH \n"
+ "precision highp float; \n"
+ "#else \n"
+ "precision mediump float; \n"
+ "#endif \n"
+ "void main(){ \n"
+ " gl_FragColor = vec4(1.0,1.0,1.0,1.0); \n"
+ "} \n";
GL_POINTS are clipped at the center. This is just a limitation of GL_POINTS. If you can't live with this just use regular quads (assuming performance can deal with it)
Increasing viewport size helps, or you can render it to texture and zoom :)