Get eye position coordinates in OpenGL ES - java

The 3D application has a static camera:
float eyeX = 0.0f;
float eyeY = 0.0f;
float eyeZ = 0.0f;
Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ,
0f, 0f, -4f, 0f, 1.0f, 0.0f)
Then this vector is used for the eye coordinates in the shader?:
const vec4 eyePos = vec4(0.0, 0.0, 0.0, 0.0);
Or need additional transformations?
Note: Read the post What exactly are eye space coordinates? but I still have doubts because my fog shader is not working. In this shader, the distance from the observer to the object is calculated:
uniform mat4 u_vMatrix;
in vec4 a_position;
out float v_eyeDist;
const vec4 eyePos = vec4(0.0, 0.0, 0.0, 0.0);
...
void main() {
...
vec4 viewPos = u_vMatrix * a_position;
v_eyeDist = sqrt(pow((viewPos.x - eyePos.x), 2.0) +
pow((viewPos.y - eyePos.y), 2.0) +
pow((viewPos.z - eyePos.z), 2.0));
...
}
Thanks in advance!
Solution: On the advice Rabbid76, I used length() function, as well as a model-view matrix.
uniform mat4 u_mvMatrix; // model-view matrix
in vec4 a_position;
out float v_eyeDist;
...
void main() {
...
vec4 viewPos = u_mvMatrix * a_position;
v_eyeDist = length(viewPos);
...
}

The view matrix transforms from world space to view space. The view space is the local system which is defined by the point of view onto the scene. The position of the view, the line of sight and the upwards direction of the view, define a coordinate system relative to the world coordinate system.
The origin of the view space is the "eye" position, thus in view space the "eye" position is at (0, 0, 0).
In glsl the distance of to points can be computed by the built-in function distance. It is sufficient to compute the Euclidean distance of the componets x, y, z (Cartesian coordinates), since the w components (Homogeneous coordinates) is 1 for both vectors. e.g.:
v_eyeDist = distance(viewPos.xyz, eyePos.xyz);
Since the the point of view (the position of the camera) is (0, 0, 0) in viespace, it is sufficient to compute the length of the view vector, to compute the distance. The distance of a point to the origin of the coordinate system is the length of the vector to the point. In glsl this can be computed by the built-in function length. In this case it is important, to compute the length of the components x, y, z, and to exclude the w component. Including the w component would lead to wrong results:
v_eyeDist = length(viewPos.xyz);

Related

Android OpenGL ES 2.0, Compute Normal for each triangle

I need to compute Normals for each Triangle Face (not for each vertex) using Opengl ES 2.0 in Android. But I can't pass attribute in the Fragment Shader directly.
Found one solution: Repeat vertices for each Triangle, and pass Triangle face Normal as an Attribute in Vertex Shader.
But I don't want to duplicate the vertices. I am drawing triangle using Vertex Indices.
So, a vertex is shared by more than one triangle, then how should I compute Triangle face Normals.
p.s. I am a newbie in opengl.
The easiest solution is just to duplicate vertices. The vertex shader is very rarely a bottleneck. I do not know your specific needs, however, there are cirumstances when duplicating a vertex is not a good solution. For example, if the mesh is skinned and animated that means a lot of computation happens in the vertex shader. Another case is when the mesh is animated in some weird way in the vertex shader and you have to recompute the normals. Clearly, you cannot compute per face normals in the vertex shader. You could do that in the geometry shader, but we do not have one in OpenGL ES 2.0. However, there is a simple solution - compute normals in fragment shader! So, if duplication of vertices does not work for you, here is the solution:
We will need an OpenGL extension - standard_derivatives, which is widely supported, but you will still need to check if it is supported on the device before running the code. To enable the extension, you will have to add the following line to the fragment shader before it's code:
#extension GL_OES_standard_derivatives : enable
We will need a varying variable for the vertex position in the world coordinates. It should be computed in the vertex shader and how it is done depends much on your shader. It is used for many needs, so you may already compute it in your vertex shader. So let's assume, that we have this line in the fragment shader:
varying vec3 positionWorld;
We will need a view matrix of the camera. It is possible that you are already passing one to the fragment shader. Let's assume that we have this uniform in the fragment shader:
uniform mat4 viewMatrix;
Now, we are going to compute the normal. First, we compute a normal in view space and then transform into the worldspace. To compute normal in the viewspace, we use derivative functions:
vec3 normalViewSpace = normalize(cross(dFdx(positionWorldSpace), dFdy(positionWorldSpace)));
Here a derivative of position is taken with respect to x and y coordinate in screen space. That means that we have two vectors that are in lay in the surface plane. To get normal to the surface we do a cross product. Sure, the result is not a unit vector, so we need also to normalize it.
The last step is to compute normal in the worldspace. View matrix applies a transformation from world space to view space. One could think that we need to compute the inverse of it since we need to go from view space to world space, but because view matrix is orthonormal, the transpose of that matrix is also its inverse, so the code will be:
vec3 normalWorldSpace = (vec4(normalViewSpace, 0.0) * viewMatrix).xyz;
To make life easier, we can wrap everything into a function:
vec3 ReconstructNormal(vec3 positionWorldSpace)
{
vec3 normalViewSpace = normalize(cross(dFdx(positionWorldSpace), dFdy(positionWorldSpace)));
vec3 normalWorldSpace = (vec4(normalViewSpace, 0.0) * viewMatrix).xyz;
return normalWorldSpace;
}
Now we have a reconstructed normal in world space. Below, just a simple example, why this can be very useful. Note, that since it uses WebGL, it is also pretty much OpenGL ES 2.0 compatible.
var container;
var camera, scene, renderer;
var mesh;
var uniforms;
var clock = new THREE.Clock();
init();
animate();
function init() {
container = document.getElementById('container');
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.z = 0.6;
camera.position.y = 0.2;
camera.rotation.x = -0.45;
scene = new THREE.Scene();
var boxGeometry = new THREE.PlaneGeometry(0.75, 0.75, 32, 32);
var heightMap = THREE.ImageUtils.loadTexture("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAeeSURBVFhHTdfFq1VfFAfwfbzP7q5ndwcm2IqKgQoiOhQFQXAgDpxecObAP0ARdGJMdGDhwAILO7C7uzvv73wWHPltuLx7z9l7rfWNtc552ZQpUypt27ZN7dq1S/9f7969S1evXk116tRJtWrVSj9//kzfv39PX79+jfvdu3dPpVIp3b9/P3Xo0CH1798/tWrVKjVs2DBt3749vX//PnXr1i3VqFEj9jVq1Ci9efMm3blzJ7Vp0yY1adIk/fr1K2Xz5s2r2CjRy5cv42/r1q3Tnz9/0smTJ6MQh+vVq5du3LgRwSSsrq5OnTp1Sq9fv05fvnyJ38OHD0+7du1K+/bti8Jq166dfvz4EQU7v2TJkoh35cqVADN37txU1bJly0hm+VskaNasWXr+/HkEUe2AAQNSnz590ufPn1OLFi3Sx48f0+3btyO5fUePHo2CJ0yYkJo2bRpgqqqqIq4lzsaNG/8xaCkqGJDYjbdv36aaNWt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: 0.0 }, u_heightMap: {type: "t",value:heightMap} };
var material = new THREE.ShaderMaterial({
uniforms: uniforms,
side: THREE.DoubleSide,
wireframe: false,
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragment_shader').textContent
});
mesh = new THREE.Mesh(boxGeometry, material);
mesh.rotation.x = 3.14 / 2.0;
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0x000000, 1 );
container.appendChild(renderer.domElement);
onWindowResize();
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize(event) {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
var delta = clock.getDelta();
uniforms.u_time.value += delta;
//mesh.rotation.z += delta * 0.5;
renderer.render(scene, camera);
}
body { margin: 0px; overflow: hidden; }
<script src="https://threejs.org/build/three.min.js"></script>
<div id="container"></div>
<script id="fragment_shader" type="x-shader/x-fragment">
#extension GL_OES_standard_derivatives : enable
varying vec3 positionWorld; // position of vertex in world coordinates
vec3 ReconstructNormal(vec3 positionWorldSpace)
{
vec3 normalViewSpace = normalize(cross(dFdx(positionWorldSpace), dFdy(positionWorldSpace)));
vec3 normalWorldSpace = (vec4(normalViewSpace, 0.0) * viewMatrix).xyz;
return normalWorldSpace;
}
// Just some example of using a normal. Here we do a really simple shading
void main( void )
{
vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0));
vec3 normal = ReconstructNormal(positionWorld);
float diffuse = max(dot(lightDir, normal), 0.0);
vec3 albedo = vec3(0.2, 0.4, 0.7);
gl_FragColor = vec4(albedo * diffuse, 1.0);
}
</script>
<script id="vertexShader" type="x-shader/x-vertex">
uniform lowp sampler2D u_heightMap;
uniform float u_time;
varying vec3 positionWorld;
// Example of vertex shader that moves vertices
void main()
{
vec3 pos = position;
vec2 offset1 = vec2(1.0, 0.5) * u_time * 0.01;
vec2 offset2 = vec2(0.5, 1.0) * u_time * 0.01;
float hight1 = texture2D(u_heightMap, uv + offset1).r * 0.02;
float hight2 = texture2D(u_heightMap, uv + offset2).r * 0.02;
pos.z += hight1 + hight2;
vec4 mvPosition = modelViewMatrix * vec4( pos, 1.0 );
positionWorld = mvPosition.xyz;
gl_Position = projectionMatrix * mvPosition;
}
</script>

How to fixate the camera on the player, so when the player moves the camera will follow

I am trying to make a OpenGL game in java in which you are a spaceship and you fly around in 3D space, the aim is to eliminate all of the enemy spaceships in the level. However, I cannot seem to figure out how to make the camera fixate/follow the player. I want to do this so that i can move the player (AKA: spacehip) around using user input and the camera will follow so that the player doesn't go out of sight (off the screen).
NOTE: I'm using modern OpenGL. (Shaders ...), not the fixed function pipeline.
(i'll explain in general code, not Java, but the syntax is easy to translate)
Assuming you're using transformation with Matrix
all the objects in the scene are rendered By:
B * M * P,
while:
B = Base (or View/Camera) matrix
M = Model (or objects, like the enemies or the player spaceship) matrix
P = Projection matrix
in the Update cycle, which comes before the rendering cycle, right after you update the player's spaceship Model matrix, change the Base matrix accordingly, for example to put the camera behind the spaceship first find the heading vector of the spaceship and the up vector of the spaceship :
Vector3 vHeading = shipMatrix * Vector3(0.0,0.0,1.0);
Vector3 vShipUp = shipMatrix * Vector3(0.0,1.0,0.0);
then create the camera matrix placed behind the spaceship and looking at the spaceship using a LookAt calculations:
/// set the offsets between the camera and the spaceship ///
float distCameraToShip = 2.0;
float cameraElevation = 2.0;
// find the ship position ///
Vector3 vShipTranslation = shipMatrix.GetTranslation();
/// or Vector3 vShipTranslation = shipMatrix * Vector3(0.0,0.0,0.0); ///
/// calculate the camera transformation ///
Vector3 vCameraPos = vShipTranslation - vHeading * distCameraToShip + vShipUp * cameraElevation;
Matrix4x4 cameraMatrix = LookAt(vCameraPos, vShipTranslation, vShipUp);
a LookAt Implementation:
CMatrix4x4 LookAt(Vector3 vEye, Vector3 vObject, Vector3 vUp)
{
Vector3 n = normalize(vEye - vObject);
Vector3 u = cross(vUp , n);
Vector3 v = cross(n , u);
float m[16] = { u[0], v[0], n[0], 0.0f,
u[1], v[1], n[1], 0.0f,
u[2], v[2], n[2], 0.0f,
(u * -1.0 * vEye) ,
(v * -1.0 * vEye) ,
(n * -1.0 * vEye),
1.0f };
return m;
}
you use this camera matrix for all the rendering cycle and pass it to the shader ex:
////// Vertex shader ///
/// it is recommended to do the multiplication on the CPU and pass the ModelViewMatrix to the shader, here is just to example ///
uniform mat4 u_BaseMatrix;
uniform mat4 u_ModelMatrix;
uniform mat4 u_ProjectionMatrix;
in vec3 a_VerAttrib;
void main()
{
gl_Position = u_ProjectionMatrix * u_BaseMatrix * u_ModelMatrix * vec4(a_VertAttrib, 1.0);
}
now you can start manipulating the camera and give it all kinds of cool interpolations, like set the distance between the camera and the ship according to the ship's speed :
float distCameraToShip = 2.0 + pow(shipSpeed,2.0) * 0.1;
you can also use time smooth filter to give it a cool following effect:
/// dt = time diff between updating cycles, or 1/FrameRate ///
float ct = 1.0 / (1.0 + dt);
cameraMatrix = cameraMatrix + (previousCameraMatrix - cameraMatrix) * ct;
In order to keep the camera fixed of the player you will need to specify what what yaw and pitch (rotations) you would like applied to the camera and a distance away from the player then you would computer they distances to translate the camera so that you see the player from that angle.
Here is a brilliant tutorial on exactly what you are asking:
https://youtu.be/PoxDDZmctnU

LWJGL change line's color between vertices

I'm using java and LWJGL/openGL to create some graphics. For rendering I use the following:
Constructor of RawModel:
public RawModel(int vaoID, int vertexCount, String name){
this.vaoID = vaoID;
this.vertexCount = vertexCount;
this.name = name;
}
Renderer:
public void render(Entity entity, StaticShader shader){
RawModel rawModel = entity.getRaw(active);
GL30.glBindVertexArray(rawModel.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(3);
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(),
entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
shader.loadTransformationMatrix(transformationMatrix);
GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(3);
GL30.glBindVertexArray(0);
}
I'm using GL11.GL_TRIANGLES cuz that's how I can make models' lines show up instead of faces. But when I set color for a vertex, it just colors surrounding lines to the color set, in some cases all of it's lines just take the color of surrounding vertices. How could I make it so that it kind of combines those 2 colors depending on the distance of each vertex and the colors?
Fragment shader:
#version 400 core
in vec3 colour;
in vec2 pass_textureCoords;
out vec4 out_Color;
uniform sampler2D textureSampler;
void main(void){
vec4 textureColour = texture(textureSampler, pass_textureCoords);
//out_Color = texture(textureSampler, pass_textureCoords);
out_Color = vec4(colour, 1.0);
}
Vertex shader:
#version 400 core
in vec3 position;
in vec2 textureCoords;
in int selected;
out vec3 colour;
out vec2 pass_textureCoords;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
void main(void){
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0);
pass_textureCoords = textureCoords;
if(selected == 1){
colour = vec3(200, 200, 200);
}
else{
colour = vec3(0, 0, 0);
}
gl_BackColor = vec4(colour, 1.0);
}
I'm using GL11.GL_TRIANGLES cuz that's how I can make models' lines show up instead of faces.
Well, GL_TRIANGLES is for rendering triangles, which are faces. If you only want the models' lines, you can use one of the line drawing modes (GL_LINES, GL_LINE_LOOP, GL_LINE_STRIP etc).
However, a better way is to enable
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
, which makes only the outline of the triangles show up.
You can switch it off again with
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
How could I make it so that it kind of combines those 2 colors depending on the distance of each vertex and the colors?
I'm not sure what you mean with this; by default values passed from the vertex shader to the fragment shader are already interpolated across the mesh; so the color a fragment receives already depends on all the vertices' color and distance.
Edit:
In the vertex shader:
if(selected == 1){
colour = vec3(200, 200, 200);
}
I assume you want to assign an RGB value of (200, 200, 200), which is a very light white. However, OpenGL uses floating-point color components in the range 0.0 to 1.0. Values that go above or below this range are clipped. The value of colour is now interpolated across the fragments, which will receive values with components far higher than 1.0. These will be clipped to 1.0, meaning all your fragments appear white.
So, in order to solve this issue, you have to instead use something like
colour = vec3(0.8, 0.8, 0.8);

Why do my lighting calculations cause colors to jump to black?

I have a scene containing a map generated from a heightmap. I can move this map around on the X and Y axis, where the map gets moved upwards or downwards such that the map geometry intersects the origin. There is a single light that hovers over the map, always 5 units above the height of the geometry at its x and y coordinate.
When the camera is located close to the origin of the scene, all the lighting behaves fine. The more I move away from it, however, the more triangles jump first quickly to bright white, after which they become black almost instantly. I have not been able to figure out what is causing this.
Here is an overview of the structure of the scene graph:
Container instances do not change the openGL state
The shader node activates the shader pair shown below.
The problem is also present when using the fixed function pipeline.
The scene is translated and rotated prior to rendering the scene graph
I'm fairly certain the problem lies with the construction of the scene, but I'm not entirely certain.
Here are some screenshots of the effects. First one where the light is close to the camera, and where the camera is located some distance away from the scene origin:
Note the light being shown as a sphere, marked by the red circle. Second, one where the light is away from the camera:
I am also drawing the normals for reference. The glow visible in this picture is always present, no matter where the light is located relative to the camera.
Here are my shaders. I'm fairly certain they work as they're supposed to, because they worked properly in ShaderMaker:
Vertex shader:
varying vec3 normal;
varying vec3 position;
void main( void )
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
normal = gl_NormalMatrix * gl_Normal;
position = ( gl_ModelViewMatrix * gl_Vertex ).xyz;
}
Fragment shader:
varying vec3 normal;
varying vec3 position;
vec4 lightSource(vec3 norm, vec3 view, gl_LightSourceParameters light)
{
vec3 lightVector = normalize(light.position.xyz - view);
vec3 reflection = normalize(lightVector - view.xyz);
float diffuseFactor = max(0, dot(norm, lightVector));
float specularDot = max(0, dot(norm, reflection));
float specularFactor = pow(specularDot, gl_FrontMaterial.shininess);
return
gl_FrontMaterial.ambient * light.ambient +
gl_FrontMaterial.diffuse * light.diffuse * diffuseFactor +
gl_FrontMaterial.specular * light.specular * specularFactor;
}
vec4 lighting()
{
// normal might be damaged by linear interpolation.
vec3 norm = normalize(normal);
return
gl_FrontMaterial.emission +
gl_FrontMaterial.ambient * gl_LightModel.ambient +
lightSource(norm, position, gl_LightSource[0]);
}
void main()
{
gl_FragColor = lighting();
}
The solution to the problem was that I was applying glFrustrum() on the modelView matrix. When I set the glMatrixMode to the projection matrix before calling glFrustrum instead, the scene rendered correctly.

LibGdx shader "no uniform with name..." in phong shader

I'm very new to OpenGL and LibGdx. I started with these tutorials but wanted to apply a phong texture. I've tried to merge a number of examples but am having issues.
I've got a sphere and spinning cube on the center of the screen. I've still got hundreds of things to work out but for the moment, I don't understand why LibGdx is reporting that my uniform material can't be found...
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.IllegalArgumentException: no uniform with name 'uMvpMatrix' in shader
Pixel Shader
I don't believe the Fragment shader is relevant but it's at the bottom in case.
#version 120
uniform mat4 uMvpMatrix;
varying vec3 diffuseColor;
// the diffuse Phong lighting computed in the vertex shader
varying vec3 specularColor;
// the specular Phong lighting computed in the vertex shader
varying vec4 texCoords; // the texture coordinates
void main()
{
vec3 normalDirection =
normalize(gl_NormalMatrix * gl_Normal);
vec3 viewDirection =
-normalize(vec3(gl_ModelViewMatrix * gl_Vertex));
vec3 lightDirection;
float attenuation;
if (0.0 == gl_LightSource[0].position.w)
// directional light?
{
attenuation = 1.0; // no attenuation
lightDirection =
normalize(vec3(gl_LightSource[0].position));
}
else // point light or spotlight (or other kind of light)
{
vec3 vertexToLightSource =
vec3(gl_LightSource[0].position
- gl_ModelViewMatrix * gl_Vertex);
float distance = length(vertexToLightSource);
attenuation = 1.0 / distance; // linear attenuation
lightDirection = normalize(vertexToLightSource);
if (gl_LightSource[0].spotCutoff <= 90.0) // spotlight?
{
float clampedCosine = max(0.0, dot(-lightDirection,
gl_LightSource[0].spotDirection));
if (clampedCosine < gl_LightSource[0].spotCosCutoff)
// outside of spotlight cone?
{
attenuation = 0.0;
}
else
{
attenuation = attenuation * pow(clampedCosine,
gl_LightSource[0].spotExponent);
}
}
}
vec3 ambientLighting = vec3(gl_LightModel.ambient);
// without material color!
vec3 diffuseReflection = attenuation
* vec3(gl_LightSource[0].diffuse)
* max(0.0, dot(normalDirection, lightDirection));
// without material color!
vec3 specularReflection;
if (dot(normalDirection, lightDirection) < 0.0)
// light source on the wrong side?
{
specularReflection = vec3(0.0, 0.0, 0.0);
// no specular reflection
}
else // light source on the right side
{
specularReflection = attenuation
* vec3(gl_LightSource[0].specular)
* vec3(gl_FrontMaterial.specular)
* pow(max(0.0, dot(reflect(-lightDirection,
normalDirection), viewDirection)),
gl_FrontMaterial.shininess);
}
diffuseColor = ambientLighting + diffuseReflection;
specularColor = specularReflection;
texCoords = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
Setup
shader = new ShaderProgram(vertexShader, fragmentShader);
mesh = Shapes.genCube();
mesh.getVertexAttribute(Usage.Position).alias = "a_position";
Render
...
float aspect = Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight();
projection.setToProjection(1.0f, 20.0f, 60.0f, aspect);
view.idt().trn(0, 0, -2.0f);
model.setToRotation(axis, angle);
combined.set(projection).mul(view).mul(model);
Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
shader.begin();
shader.setUniformMatrix("uMvpMatrix", combined);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
Stack Trace
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: java.lang.IllegalArgumentException: no uniform with name 'uMvpMatrix' in shader
at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:283)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:539)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:527)
at com.overshare.document.Views.Test.onRender(Test.java:150)
...
Fragment Shader
#ifdef GL_ES
precision mediump float;
#endif
precision mediump float;
varying vec4 v_Color;
void main()
{
gl_FragColor = v_Color;
}
Can someone please tell me what I'm missing?
I ran into something similar. I think because your shader doesn't use the "uMvpMatrix" uniform, it declaration gets optimized out, and so its "not there" when you go to set it. If you change your shader to reference the matrix in some way, you should get farther.
See (indirectly)
Do (Unused) GLSL uniforms/in/out Contribute to Register Pressure?
I believe there are ways of developing and compiling shaders offline, so for a complex shader it may make sense to develop it outside of Libgdx (hopefully you'd get better error messages). Libgdx is just passing the giant string on to the lower layers, it doesn't do much with the shader itself, so there shouldn't be compatibility issues.
Also, problem could be in precision specifier. On device(Nexus S) the next uniform defining will throw the same error:
uniform float yShift;
Using precision specifier solves the problem:
uniform lowp float yShift;
LibGDX allows to check shader compilation and get error log:
if(!shader.isCompiled()){
String log = shader.getLog();
}
Finally, there's flag to ignore shader errors:
ShaderProgram.pedantic = false;

Categories

Resources