Jump to content

  • Log In with Google      Sign In   
  • Create Account

I CAN DO

Member Since 23 Mar 2005
Offline Last Active Dec 08 2013 10:13 AM

Topics I've Started

GLSL BumpMap Math error

02 December 2013 - 10:17 AM

I have a problem with OpenGL and GLSL to implement Bump Map.

Vertex shader:

#version 330 core



layout (std140) uniform Matrices {



mat4 projViewModelMatrix;

mat3 normalMatrix;

mat4 modelViewMatrix;

};



uniform mat4 View;

uniform vec3 LightPosition;

uniform vec3 CameraPosition;



in vec3 position;

in vec3 normal;

in vec2 texCoord;



out vec2 TexCoord;

out vec3 Normal;



// BUMP MAPPING

/////////////////////////////////////

out vec3 lightVec;

out vec3 eyeVec;

in vec3 vTangent;

in vec3 bTangent;







void main()

{

Normal = normalize(normalMatrix * normal);

gl_Position = projViewModelMatrix * vec4(position, 1.0);

TexCoord = vec2(texCoord);



// BUMP MAPPING

vec3 n = normalize(normalMatrix * normal);

vec3 t = normalize(normalMatrix * vTangent);

vec3 b = normalize(normalMatrix * bTangent);



vec3 vVertex = vec3(modelViewMatrix * vec4(position, 1.0));

// vec3 tmpVec = LightPosition.xyz - vVertex.xyz;

vec3 tmpVec = (View * vec4(LightPosition, 1.0)).xyz - vVertex.xyz;



lightVec.x = dot(tmpVec, t);

lightVec.y = dot(tmpVec, b);

lightVec.z = dot(tmpVec, n);



tmpVec = -vVertex;





eyeVec.x = dot(tmpVec, t);

eyeVec.y = dot(tmpVec, b);

eyeVec.z = dot(tmpVec, n);





}

FragmentShader

#version 330
layout (std140) uniform Material {

vec4 diffuse;

vec4 ambient;

vec4 specular;

vec4 emissive;

float shininess;

int texCount;

};



uniform sampler2D texUnit;



in vec3 Normal;

in vec2 TexCoord;



out vec4 outputF;



//BUMP MAPPING ///////////////////////////////////////////



in vec3 lightVec;

in vec3 eyeVec;



uniform sampler2D normalMap;



uniform vec4 diffuseLight;

uniform vec4 ambientLight;

uniform vec4 specularLight;







main()

{

vec3 N2 = normalize(texture(normalMap, TexCoord).xyz * 2.0 - 1.0);

vec3 L2 = normalize(lightVec);

vec3 V2 = normalize(eyeVec);

vec3 R2 = normalize(-reflect(L2, N2));



float NdotL = max(0.0, dot(N2, L2));

float RdotV = max(0.0, dot(R2, V2));



// Compute final colours.

vec4 ambient = ambientLight * ambient;

vec4 diffuse = diffuseLight * diffuse * NdotL;

vec4 specular2 = specularLight * specular * (pow(RdotV, shininess) * 1);



vec4 base = texture2D(texUnit, TexCoord);



// Final colour.

outputF = base * ambient + base * diffuse + specular2;

}

There is some error there? I feel that the calculation of the direction of the light is wrong. Could it be?

 

The line:

vec3 tmpVec = (View * vec4(LightPosition, 1.0)).xyz - vVertex.xyz;

is OK?

the line:

tmpVec = -vVertex;

 

OK? or I need change to:

tmpVec = (View * vec4(CameraPosition, 1.0)).xyz - vVertex.xyz;

 

or

tmpVec = (CameraPosition).xyz - vVertex.xyz;

 

Please help me.

Thank you, best regards.

 

Shadow Map too big

30 April 2013 - 04:25 PM

All is OK in the shader map texture, but the is scaled (too big).

The coord texture:

mat4 bias = mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0);

ShadowCoord = bias * projMatrix * viewModelMatrix * vec4(position,1);

Why in another example the textureMap coord is:

ShadowCoord = bias * projMatrix * viewModelMatrix * ViewMatrixInverse;

And another:

ShadowCoord = bias * projMatrix * viewModelMatrix * ModelMatrixRenderObject * vec4(position,1);

Please, does anyone can explain the differences? and Why do my Texture Map is scaled?

Thank you, best regards.


Particle System 1 pass problems

26 April 2013 - 12:19 PM

Im working in Particle System Class from this tutorial Particles - Anton's OpenGL 4 Wiki - Dr Anton Gerdelan

 

Code:

 

//Vertex Shader 

// shader to update a particle system based on a simple kinematics function
#version 330

in vec3 v; // initial velocity
in float tZero; // start time

uniform mat4 projViewModelMatrix;

uniform vec3 emitterPos_wor; // emitter position in world coordinates
uniform float T; // system time T in seconds

out float opacity;

void main() {
// work out how many seconds into our particle's life-time we are (after its starting time)
float t = T - tZero;
vec3 p;
// gradually make particle more transparent over its life-time
opacity = 1 - (t / 3) - 0.2;

// particle stays put until it has reached its birth second
if (t > 0) {
// gravity
vec3 a = vec3(0,-10,0);
// this is a standard kinematics equation of motion with velocity (from VBO) and acceleration (gravity)
p = emitterPos_wor + v * t + 0.5 * a * t * t;
} else {
p =  emitterPos_wor;
}
gl_Position = projViewModelMatrix * vec4(p, 1);
}


// Pixel shader

// shader to render simple particle system's points
#version 330

uniform sampler2D textureMap; // I used a texture for my particles
out vec4 fragColour;

uniform vec4 Color;

in float opacity;

void main() {
vec4 texcol = texture2D(textureMap, gl_PointCoord); // using point uv coordinates which are pre-defined over the point
fragColour = vec4(1-opacity,1-opacity,1-opacity,1-opacity) * texcol * Color; // bright blue!
}

/////// CPU

bool ParticleSystem::init(vec3 Position){

std::vector<vec3> Velocidad;
std::vector<float> Life;

for ( int i = 0; i < MAX_PARTICLES; i++ ) {
Velocidad.push_back(vec3(0,-1,0));
}

for ( int i = 0; i < MAX_PARTICLES; i++ ) {
Life.push_back(0.001f * (float)(i));
}


glGenVertexArrays( 1, &m_VAO );
glBindVertexArray( m_VAO );

glGenBuffers(ARRAY_SIZE_IN_ELEMENTS(m_Buffers), m_Buffers);

glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VEL_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(Velocidad[0]) * Velocidad.size(), &Velocidad[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);    

glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[LIF_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(Life[0]) * Life.size(), &Life[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, 0);    

glBindVertexArray(0);

return true;
}

/////////////////// FINAL RENDER 

bool ParticleSystem::render(){

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,ModeloOrtho.getTextureFromID(24));

glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
glEnable(GL_POINT_SPRITE);
glPointSize(150.0f);
shaderParticle.setUniform("projViewModelMatrix",vsml->get(VSMathLib::PROJ_VIEW_MODEL));
shaderParticle.setUniform("emitterPos_wor",ParticleInit);
shaderParticle.setUniform("T",ParticleTime);
shaderParticle.setUniform("Color",ColorParticle);

glUseProgram(shaderParticle.getProgramIndex()); 

glBindVertexArray(m_VAO);
glDrawElements(GL_POINTS, 0, GL_UNSIGNED_INT, 0); 
glBindVertexArray(0);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,0);
return true;
}

 

 

The problem is that nothing happens.

And if I change this line: glDrawElements (GL_POINTS, MAX_PARTICLES, GL_UNSIGNED_INT, 0); Crash the system.

What am I doing wrong?

Thank you, best regards.


Cubemap error

20 February 2010 - 06:39 AM

Hello friends, I have the following problem to solve. My scene consists of a 3D object that has a dynamic cubemap. When I work with glBegin() glEnd() everything works fine but when I work with VBO, the Cubemap not draw. glUseProgram (ProgramShader [j]); glEnableClientState (GL_VERTEX_ARRAY); glEnableClientState (GL_NORMAL_ARRAY); glEnableClientState (GL_TEXTURE_COORD_ARRAY); glClientActiveTextureARB (GL_TEXTURE0_ARB); glBindBufferARB (GL_ARRAY_BUFFER_ARB, BufferTextur [j]); glTexCoordPointer (2 GL_FLOAT, 0, 0); glEnableClientState (GL_TEXTURE_COORD_ARRAY); glActiveTextureARB (GL_TEXTURE0_ARB); glBindTexture (GL_TEXTURE_CUBE_MAP_ARB, DynTexture); glEnable (GL_TEXTURE_CUBE_MAP_ARB); glClientActiveTextureARB (GL_TEXTURE1_ARB); glBindBufferARB (GL_ARRAY_BUFFER_ARB, BufferTextur [j]); glEnableClientState (GL_TEXTURE_COORD_ARRAY); glTexCoordPointer (2 GL_FLOAT, 0, 0); glActiveTextureARB (GL_TEXTURE1_ARB); glEnable (GL_TEXTURE_2D); glBindTexture (GL_TEXTURE_2D, NumeroDeTextura[M[j]->faces [0]. material].texID); glEnable (GL_TEXTURE_2D); glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferVertex[j]); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, BufferIndexs[j]); glVertexPointer(3, GL_FLOAT, 0, 0); glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferNormal[j]); glNormalPointer(GL_FLOAT, 0, 0); glDrawElements(GL_TRIANGLES,3*M[j]->nfaces,GL_UNSIGNED_SHORT,0); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glClientActiveTextureARB (GL_TEXTURE1_ARB); glDisableClientState (GL_TEXTURE_COORD_ARRAY); glClientActiveTextureARB (GL_TEXTURE0_ARB); glDisableClientState (GL_TEXTURE_COORD_ARRAY); glActiveTextureARB (GL_TEXTURE0_ARB); glDisable (GL_TEXTURE_CUBE_MAP_ARB); glDisable (GL_TEXTURE_2D); glActiveTextureARB (GL_TEXTURE1_ARB); glDisable (GL_TEXTURE_2D); glUseProgram (0); What could be wrong?

Free 3D model

17 December 2009 - 11:21 PM

Hi friends, I need a City low poly texturized 3D model. It's for a demo game. It's a car game and non comercial. Quién puede ayudarme?

PARTNERS