OpenGL ES3: no matching overloaded function found for packUnorm2x16 - opengl-es

I am trying to pack 2 float values into 1 uint value using packUnorm2x16 method of OpenGL ES3 GLSL. But the compilation of the shader fails with 'packUnorm2x16': no matching overloaded function found error.
This is my fragment shader:
varying highp vec2 vDisplacement;
void main() {
gl_FragColor = vec4(packUnorm2x16(vDisplacement), vec3(0.0));
}
I am trying to render a result to a GL_R32UI texture.

packUnorm2x16 is supported since OpenGL ES 3.0. You've to add the version qualifier #version 300 es to the first lien of the fragment shader:
#version 300 es
varying highp vec2 vDisplacement;
void main() {
gl_FragColor = vec4(packUnorm2x16(vDisplacement), vec3(0.0));
}

Related

GLSL : Use of undeclared identifier 'gl_FragData'

I'm debugging a really big project shading a tree, in that project the author used glsl file to shader the tree, but I have trouble compiling the glsl file:
Here is the error log:
compile: code/trees/TreeRender/Shader/Graph40.vert.glsl
ERROR: 0:4: '' : syntax error: #version
ERROR: 0:15: 'layout' : syntax error: syntax error
the Graph40.vert.glsl:
#version 120 core //the original file use version 400
#define VERT_POSITION 0
#define VERT_NORMAL 1
#define VERT_COLOR 2
#define VERT_TEXTURE 3
uniform mat4x4 matModel;
uniform mat4x4 matView;
uniform mat4x4 matProjection;
layout(location = VERT_POSITION) in vec4 Position;
layout(location = VERT_NORMAL) in vec4 Normal;
layout(location = VERT_COLOR) in vec4 Color;
layout(location = VERT_TEXTURE) in vec4 Texture;
out vec4 VertPosition;
out vec4 VertNormal;
out vec4 VertColor;
out vec4 VertTexture;
void main()
{
VertPosition = Position;
VertNormal = Normal;
VertColor = Color;
VertTexture = Texture;
gl_Position = matProjection * matView * matModel * vec4(Position.xyz, 1);
}
Another error log:
compile: code/trees/TreeRender/Shader/Default.vert.glsl
ERROR: 0:11: 'matModel' : syntax error: syntax error
The Default.vert.glsl:
#define VERT_POSITION 0
#define VERT_NORMAL 1
#define VERT_COLOR 2
#define VERT_TEXTURE 3
uniform mat4x4 matModel;
uniform mat4x4 matView;
uniform mat4x4 matProjection;
layout(location = VERT_POSITION) in vec4 Position;
layout(location = VERT_NORMAL) in vec4 Normal;
layout(location = VERT_COLOR) in vec4 Color;
layout(location = VERT_TEXTURE) in vec4 Texture;
out vec4 VertPosition;
out vec4 VertNormal;
out vec4 VertColor;
out vec4 VertTexture;
void main()
{
VertPosition = Position;
VertNormal = Normal;
VertColor = Color;
VertTexture = Texture;
gl_Position = matProjection * matView * matModel * vec4(Position.xyz, 1);
}
I tried to google the error, but found no feasible solution.
I use mac osx, xcode 7.0, the OpenGL and glut are all default versions. Glew version is 1.13.0.
Is that because of the version not match with the original version that the author used? Because I checked the original version, he used GLEW 1.9.0 and GLUT 3.7.6.
/////update//////
The original glsl files have:
#version 400 core
but there will be an error :
ERROR: 0:4: '' : version '400' is not supported
ERROR: 0:4: '' : syntax error: #version
so I commented that line. But other errors still there.
I checked my OpenGL version, using OpenGL Extension viewer, it's 4.1 in my mac, but versions older than that are also supported and should work too. But when I change to #version 410 core, it has the same error, saying that 410 is not supported.
///////////update////////////
It turned out that the version mac supported is NOT the version my context using. I printed in my code using GL_VERSION, it's 2.1 I'm using. Now I have changed into 4.1, according to [this][1]. But there is still errors:
trees/TreeRender/Shader/DefaultDepth.frag.glsl
helloERROR: 0:20: Use of undeclared identifier 'gl_FragData'
the DefaultLight.frag.glsl:
#version 400 core
in vec4 VertPosition;
in vec4 VertNormal;
in vec4 VertColor;
in vec4 VertTexture;
uniform vec3 lightPos;
void main()
{
float moment1 = gl_FragCoord.z;
float moment2 = moment1 * moment1;
gl_FragData[0] = vec4(moment1, moment2, 0.0, 1.0);
}
Version 120 does not support core profiles (only OpenGL 3.2+)
Layout quallifiers are also only available in OpenGL 3.2+

How to use a 3x3 homography matrix in OpenGL ES shaders?

I have a 3x3 homography matrix that works correctly with OpenCV's warpPerspective, but I need to do the warping on GPU for performance reasons. What is the best approach? I tried multiplying in the vertex shader to get the texture coordinates and then render a quad, but I get strange distortions. I'm not sure if it's the interpolation not working as I expect. Attaching output for comparison (it involves two different, but close enough shots).
Absolute difference of warp and other image from GPU:
Composite of warp and other image in OpenCV:
EDIT:
Following are my shaders: the task is image rectification (making epilines become scanlines) + absolute difference.
// Vertex Shader
static const char* warpVS = STRINGIFY
(
uniform highp mat3 homography1;
uniform highp mat3 homography2;
uniform highp int width;
uniform highp int height;
attribute highp vec2 position;
varying highp vec2 refTexCoords;
varying highp vec2 curTexCoords;
highp vec2 convertToTexture(highp vec3 pixelCoords) {
pixelCoords /= pixelCoords.z; // need to project
pixelCoords /= vec3(float(width), float(height), 1.0);
pixelCoords.y = 1.0 - pixelCoords.y; // origin is in bottom left corner for textures
return pixelCoords.xy;
}
void main(void)
{
gl_Position = vec4(position / vec2(float(width) / 2.0, float(height) / 2.0) - vec2(1.0), 0.0, 1.0);
gl_Position.y = -gl_Position.y;
highp vec3 initialCoords = vec3(position, 1.0);
refTexCoords = convertToTexture(homography1 * initialCoords);
curTexCoords = convertToTexture(homography2 * initialCoords);
}
);
// Fragment Shader
static const char* warpFS = STRINGIFY
(
varying highp vec2 refTexCoords;
varying highp vec2 curTexCoords;
uniform mediump sampler2D refTex;
uniform mediump sampler2D curTex;
uniform mediump sampler2D maskTex;
void main(void)
{
if (texture2D(maskTex, refTexCoords).r == 0.0) {
discard;
}
if (any(bvec4(curTexCoords[0] < 0.0, curTexCoords[1] < 0.0, curTexCoords[0] > 1.0, curTexCoords[1] > 1.0))) {
discard;
}
mediump vec4 referenceColor = texture2D(refTex, refTexCoords);
mediump vec4 currentColor = texture2D(curTex, curTexCoords);
gl_FragColor = vec4(abs(referenceColor.r - currentColor.r), 1.0, 0.0, 1.0);
}
);
I think you just need to do the projection per pixel. Make refTexCoords and curTexCoords at least vec3, then do the /z in the pixel shader before texture lookup. Even better use the textureProj GLSL instruction.
You want to do everything that is linear in the vertex shader, but things like projection need to be done in the fragment shader per pixel.
This link might help with some background: http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/

Simple channel offset shader in GL ES 2.0

Could someone point me in the right direction to creating an effect similar to this that would run on GL ES 2.0?
.vert
uniform vec2 uAberrationOffset;
void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}
.frag
uniform sampler2DRect baseTex;
uniform vec2 uAberrationOffset;
void main() {
vec4 coords = gl_TexCoord[0];
// baseTex is FBO of screen (1280x800 -> non-square)
// offset red
vec4 fbo1 = texture2DRect(baseTex, coords.xy - uAberrationOffset);
// keep green where it is
vec4 fbo2 = texture2DRect(baseTex, coords.xy);
// offset blue
vec4 fbo3 = texture2DRect(baseTex, coords.xy + uAberrationOffset);
// FBO channels mixed (incl. offsets)
vec4 colFinal = vec4(fbo1.r, fbo2.g, fbo3.b, 1.);
// Output final pixel color
gl_FragColor = colFinal;
}
The following baby steps will allow you to port these shaders to ES 2.0.
Do not use old-school uniforms: gl_ProjectionMatrix and gl_ModelViewMatrix. Replace these with user-defined uniforms.
sampler2DRect and texture2DRect are not supported in ES but you can use a normal sampler and texture2D call for this effect.
gl_MultiTexCoord0 and gl_Vertex are old-school attributes. You'll need to replace them with user-defined vertex attributes.

OpenGLES 2.0 set vertex colors

I am creating a drawing app and need to change the colors periodically. So, one point might be green, another red.
I'm trying to do it as follows:-
program
glBindAttribLocation(_program, ATTRIB_COLOR, "color");
vertex shader
attribute vec4 position;
attribute float size;
attribute vec4 color;
varying vec4 fragColor;
void main()
{
gl_Position = position;
gl_PointSize = 30.0;
fragColor = color;
}
Fragment shader
precision mediump float;
varying vec4 fragColor;
void main() {
gl_FragColor = fragColor;
}
The problem is, the color varies depending upon where the point is positioned on the screen. If I set red as the color of the attribute I need it to be pure red wherever the point appears on screen.
The problem was not related to the above code. I had misaligned the attribute data being sent to the shader by the program.

Do WebGL fragment shaders support outerProduct?

When compiling this WebGL fragment shader in both Chrome 22 and Firefox 15:
precision mediump float;
uniform vec2 u_resolution;
uniform sampler2D u_tex;
void main() {
vec2 texCoord = gl_FragCoord.xy / u_resolution;
vec4 floatColor = texture2D(u_tex, texCoord);
mat3 outerMat = outerProduct(floatColor.rgb,floatColor.rgb);
gl_FragColor = vec4(outerMat[0], 1);
}
I'm getting this error:
ERROR: 0:8: 'outerProduct' : no matching overloaded function found
ERROR: 0:8: '=' : cannot convert from 'const mediump float' to '3X3 matrix of float'
The OpenGL ES 2.0 GLSL spec indicates that mat3 outerProduct(vec3,vec3) is supported, and the WebGL spec says that it accepts ES shaders, so I'm not sure what's going wrong. Is outerProduct not supported in WebGL fragment shaders, or am I doing something wrong?
There is no outerProduct function in OpenGL ES 2.0 GLSL. Where did you read that there was? (or am I missing it?)
Here's the spec
http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf
Try mat3 outerMat = outerProduct(vec3(floatColor.rgb),vec3(floatColor.rgb));
To expand, maybe giving it an explicit type is better than using .rgb.
it is now available with webGL2.
Check your system here: https://webglreport.com/?v=2

Resources