You need to add a "varying" parameter for position and have your vertex shader set it so that the fragment shader can use it.
Vertex shader:
Code:
attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec3 v_vPosition; // <- added
//
void main() {
v_vPosition = in_Position; // <- added
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION]
* vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);
v_vColour = in_Colour;
v_vTexcoord = in_TextureCoord;
}
Fragment shader:
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec3 v_vPosition; // <- added
//
void main() {
// (v_vPosition will contain XYZ of the current pixel)
vec4 col = v_vColour * texture2D(gm_BaseTexture, v_vTexcoord);
col.a *= float((v_vPosition.y % 10.0) < 5.0); // make every other stripe transparent, for example
gl_FragColor = col;
}
This is my current code so far:
Code:
//Most of the code is currently commented out to test just the reflection bit, I removed it for easier reading
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec3 v_vNormal;
varying vec3 v_vPosition;
varying vec2 fragCoord; //This is actually in_Position.xy, because someone in the comments mentioned that is the same as gl_fragCoord
uniform vec3 skyColour;
uniform vec3 sunPos;
uniform vec3 cameraPos;
uniform sampler2D normalMap;
uniform sampler2D reflect_tex;
void main()
{
vec2 fragCoordMini = vec2(fragCoord.x/1280.0, fragCoord.y/720.0);
vec2 fragCoordFlipped = vec2(fragCoordMini.x, 0.5-(fragCoordMini.y-0.5));
vec4 col = texture2D(reflect_tex, fragCoordFlipped);
gl_FragColor = col;
}
I was hoping to do this:
(it is time stamped to his demonstration of how it works).
He does this long process of using all sorts of data he calculated in his engine, however someone in the comments mentioned:
"gl_FragCoord gives the position on the screen in terms of pixels, so the coordinate system for that goes from (0, 0) to (displayWidth, displayHeight). To use this we would still need to divide by the display width and height."
I could not find a gl_fragCoord function, but xor's tutorial said I could do it with:
"You will need to create a varying vec2 called "fragCoord". Then set it in the vertex shader to "in_Position.xy""
However I have tried this and I have an abomination of a reflection: