PDA

View Full Version : Guess: Shader problem?



agnfolie
02-04-2013, 07:56 AM
=======/////Solved//////=========

precision mediump float;
uniform sampler2D u_Texture;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec2 v_TexCoordinate;



void main()
{
vec3 LightPos = vec3(0,100,0);
vec3 lightVector = normalize(LightPos);
float diffuse = max(dot(v_Normal, lightVector), 0.1);
diffuse = diffuse + 0.3;
if(diffuse > 1.0)
diffuse = 1.0;


gl_FragColor = (diffuse * texture2D(u_Texture, v_TexCoordinate));
}
Does the trick for me.


So I am quite new to this stuff but it is so fun! Im currently coding for the Android platform thus the code is in Java.

Ive just got a grasp of how to create a HeightMap from a alpha level encoded image. While rendering the "map" and rotating the camera around (0,0,0) beginning at (60,10,60) I get these shadows. Since my lightsource is allways at(40,50,40) it feels a bit strange to me.

Apparently I can't post pictures yet but ill post them as soon as my activation email comes through.

Description of problem:

Lets say I have a plane centered in (0,0,0).
When the camera rotates around the plane clockwise I get a shadow entering from the right. After a while the entire plane is shaded and then it starts to be illuminated from the right and repeat. It is bound to the rotation of the camera. It allways starts to Illuminate at the same point.

my fragment shader:

precision mediump float;
uniform sampler2D u_Texture;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec2 v_TexCoordinate;



void main()
{
vec3 LightPos = vec3(0,100,0);
float distance = length(LightPos - v_Position);
vec3 lightVector = normalize(LightPos - v_Position);
float diffuse = max(dot(v_Normal, lightVector), 0.1);
diffuse = diffuse + 0.3;
if(diffuse > 1.0)
diffuse = 1.0;


gl_FragColor = (diffuse * texture2D(u_Texture, v_TexCoordinate));
}

my vertex shader:

uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
attribute vec4 a_Position;
attribute vec3 a_Normal;
attribute vec2 a_TexCoordinate;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec2 v_TexCoordinate;
void main()
{
v_Position = vec3(u_MVMatrix * a_Position);
v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
v_TexCoordinate = a_TexCoordinate;
gl_Position = u_MVPMatrix * a_Position;
}

on creation of the scene:


Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 1, 200);
Matrix.setLookAtM(mVMatrix, 0, 60, 10f,60f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
where mProjMatrix is the projection Matrix
mVMatrix is the View Matrix

on frame render:


GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
curTime = System.nanoTime();
dt = 1000000000/(curTime-lastTime);

Matrix.rotateM(mVMatrix, 0, dt*SLOW_FACTOR, 0, 1, 0);

heightMap.render(mVMatrix, mProjMatrix);

lastTime = curTime;

and lastly the render function for the HeightMap:


void render(float[] viewMatrix,float[] projectionMatrix) {



if (vbo[0] > 0 && ibo[0] > 0 && readyToBeDrawn) {

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureDataHandle);
GLES20.glUniform1i(TextureUniformHandle, 0);


GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[0]);

// Bind Attributes
GLES20.glVertexAttribPointer(positionShaderHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT, false,
STRIDE* BYTES_PER_FLOAT, 0);
GLES20.glEnableVertexAttribArray(positionShaderHan dle);

GLES20.glVertexAttribPointer(normalShaderHandle, NORMAL_DATA_SIZE, GLES20.GL_FLOAT, false,
STRIDE* BYTES_PER_FLOAT, (POSITION_DATA_SIZE) * BYTES_PER_FLOAT);
GLES20.glEnableVertexAttribArray(normalShaderHandl e);

GLES20.glVertexAttribPointer(textureShaderHandle, TEXTURE_DATA_SIZE, GLES20.GL_FLOAT, false,
STRIDE* BYTES_PER_FLOAT, (POSITION_DATA_SIZE+NORMAL_DATA_SIZE) * BYTES_PER_FLOAT);
GLES20.glEnableVertexAttribArray(textureShaderHand le);



// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(modelViewMatrix , 0, viewMatrix, 0, modelMatrix, 0);

// Pass in the modelview matrix.
GLES20.glUniformMatrix4fv(MVMatrixHandle, 1, false, modelViewMatrix, 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(modelViewProjectionMatrix, 0, projectionMatrix, 0, modelViewMatrix, 0);

// Pass in the combined matrix.
GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, modelViewProjectionMatrix, 0);
// Draw
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER , ibo[0]);
GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, indexCount, GLES20.GL_UNSIGNED_SHORT, 0);

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER , 0);
GLUtilities.checkGlError("onRender");
}
}