Skip to content

Commit

Permalink
Added directional and Spotlight and earthlight effect.
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoMorera committed May 24, 2021
1 parent 4a455c6 commit 37cc35d
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 61 deletions.
66 changes: 34 additions & 32 deletions Geometry/Light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,38 @@ void Light::setLightAngle(float angle) {

void Light::LightsToGPU(QGLShaderProgram *program, int i) {
//declaramos un vector de identificadores
struct lights_Id
{
GLuint position;
GLuint iA;
GLuint iD;
GLuint iS;
GLuint coeficients;
GLuint type;
GLuint direction;
GLuint angle;
};
lights_Id lights_IdVect[MAX];

//obtenemos los identificadores de la GPU
lights_IdVect[i].position=program->uniformLocation(QString("lights[%1].position_g").arg( i ));
lights_IdVect[i].iA=program->uniformLocation(QString("lights[%1].iA_g").arg( i ));
lights_IdVect[i].iD=program->uniformLocation(QString("lights[%1].iD_g").arg( i ));
lights_IdVect[i].iS=program->uniformLocation(QString("lights[%1].iS_g").arg( i ));
lights_IdVect[i].coeficients=program->uniformLocation(QString("lights[%1].coeficients_g").arg( i ));
lights_IdVect[i].type=program->uniformLocation(QString("lights[%1].type_g").arg( i ));
lights_IdVect[i].direction=program->uniformLocation(QString("lights[%1].direction_g").arg( i ));
lights_IdVect[i].angle=program->uniformLocation(QString("lights[%1].angle_g").arg( i ));

//Bind de las zonas de memoria que correspondan
glUniform1f(lights_IdVect[i].type,type_);
glUniform4fv(lights_IdVect[i].position,1,position_);
glUniform3fv(lights_IdVect[i].iA,1,iA_);
glUniform3fv(lights_IdVect[i].iD,1,iD_);
glUniform3fv(lights_IdVect[i].iS,1,iS_);
glUniform3fv(lights_IdVect[i].coeficients,1,coeficients_);
glUniform4fv(lights_IdVect[i].direction,1,direction_);
glUniform1f(lights_IdVect[i].angle,angle_);
struct lights_Id
{
GLuint position;
GLuint iA;
GLuint iD;
GLuint iS;
GLuint coeficients;
GLuint type;
GLuint direction;
GLuint angle;
};
lights_Id lights_IdVect[MAX];

//obtenemos los identificadores de la GPU
lights_IdVect[i].position=program->uniformLocation(QString("lights[%1].position_g").arg( i ));
lights_IdVect[i].iA=program->uniformLocation(QString("lights[%1].iA_g").arg( i ));
lights_IdVect[i].iD=program->uniformLocation(QString("lights[%1].iD_g").arg( i ));
lights_IdVect[i].iS=program->uniformLocation(QString("lights[%1].iS_g").arg( i ));
lights_IdVect[i].coeficients=program->uniformLocation(QString("lights[%1].coeficients_g").arg( i ));
lights_IdVect[i].type=program->uniformLocation(QString("lights[%1].type_g").arg( i ));
lights_IdVect[i].direction=program->uniformLocation(QString("lights[%1].direction_g").arg( i ));
lights_IdVect[i].angle=program->uniformLocation(QString("lights[%1].angle_g").arg( i ));

//Bind de las zonas de memoria que correspondan

glUniform1i(lights_IdVect[i].type,type_);
glUniform4fv(lights_IdVect[i].position,1,position_);
glUniform3fv(lights_IdVect[i].iA,1,iA_);
glUniform3fv(lights_IdVect[i].iD,1,iD_);
glUniform3fv(lights_IdVect[i].iS,1,iS_);
glUniform3fv(lights_IdVect[i].coeficients,1,coeficients_);
glUniform4fv(lights_IdVect[i].direction,1,direction_);
glUniform1f(lights_IdVect[i].angle,angle_);
qDebug() << "Angle" << this -> angle_;
}
25 changes: 18 additions & 7 deletions Renders/GLWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ void GLWidget::initializeGL() {
glEnable(GL_DOUBLE);

initShadersGPU();
program = type_shaders[2];
program = type_shaders[6];
//type_shaders[0]->link();
type_shaders[2]->bind();
type_shaders[6]->bind();
// Creacio d'una Light per apoder modificar el seus valors amb la interficie
auto l = make_shared<Light>(Puntual);
//auto l1 = make_shared<Light>(Puntual);
auto l1 = make_shared<Light>(Puntual);
//realizamos los cambios pertinentes respecto al constructor base
//auto l2 = make_shared<Light>(Direccional, vec4(1,5,10,0), vec3(0.2,0.2,0.2), vec3(0.8,0.8,0.8), vec3(1,1,1), vec3(0,0,1), vec4(10,5.0,1.0), 35.0);
//auto l3 = make_shared<Light>(Spot, vec4(1,5,10,0), vec3(0.2,0.2,0.2), vec3(0.8,0.8,0.8), vec3(1,1,1), vec3(0,0,1), vec4(10,5.0,1.0), 30.0);
scene->addLight(l);
auto l2 = make_shared<Light>(Direccional, vec4(1,5,1,0), vec3(0.2,0.2,0.2), vec3(0.8,0.8,0.8), vec3(1,1,1), vec3(0,0,1), vec4(10,5.0,1.0), 35.0);
auto l3 = make_shared<Light>(Spot, vec4(-10,5,-10,0), vec3(0.2,0.2,0.2), vec3(0.8,0.8,0.8), vec3(1,1,1), vec3(0,0,1), vec4(5,5.0,1.0), 25.0);
//scene->addLight(l);
//scene->addLight(l1);
//scene->addLight(l2);
//scene->addLight(l3);
scene->addLight(l3);
scene->setAmbientToGPU(program);
scene->lightsToGPU(program);
scene->camera->init(this->size().width(), this->size().height(), scene->capsaMinima);
Expand Down Expand Up @@ -98,6 +98,7 @@ void GLWidget::initShadersGPU(){
initShader("://resources/vshader_toon.glsl", "://resources/fshader_toon.glsl",3);
initShader("://resources/vshader_phong_texture.glsl", "://resources/fshader_phong_texture.glsl",4);
initShader("://resources/vshader_phong_texture_indirect.glsl", "://resources/fshader_phong_texture_indirect.glsl",5);
initShader("://resources/vshader_phong.glsl", "://resources/fshaderNacho.glsl",6);

}

Expand Down Expand Up @@ -248,6 +249,16 @@ void GLWidget::activaIndirecte() {
scene->toGPU(program);
updateShaderTexture();

}
void GLWidget::activaNachoShader() {
//A implementar a la fase 2 de la practica 2
qDebug()<<"Estic a Phong Tex Indirecte";
program = type_shaders[6];
program->link();
program->bind();
scene->toGPU(program);
updateShaderTexture();

}
void GLWidget::activaBackground() {
//A implementar a la fase 2 de la practica 2
Expand Down
3 changes: 2 additions & 1 deletion Renders/GLWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public slots:
void activaToonShader();
void activaPhongShader();
void activaGouraudShader();
void activaNachoShader();

void activaPhongTex();
void activaIndirecte();
Expand Down Expand Up @@ -97,7 +98,7 @@ public slots:
shared_ptr<QGLShaderProgram> program; // Per ars nomes es té un parell vertex-fragment
// Cal guardar-ne més d'un en la primera fase.

shared_ptr<QGLShaderProgram> type_shaders[6];
shared_ptr<QGLShaderProgram> type_shaders[7];
void initShader(const char* vertexShaderFile, const char* fragmentShaderFile, int type_shader);
void initShadersGPU();
void updateShader();
Expand Down
1 change: 1 addition & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<file>resources/fshader_phong_texture_indirect.glsl</file>
<file>resources/vshader_phong_texture_indirect.glsl</file>
<file>resources/fshaderNacho.glsl</file>
<file>resources/fshaderNacho_copy.glsl</file>
</qresource>
</RCC>
66 changes: 45 additions & 21 deletions resources/fshaderNacho.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void main() {
vec4 N = normalize(normal);
vec4 V = normalize(obs - position);
vec4 L;
vec4 L_d;
vec4 H;

float attenuation;
Expand All @@ -54,7 +55,7 @@ void main() {
for (int i = 0; i < numl; i++) {

// If the light is Puntual (at the moment we only have Puntual Lights)
if (lights[i].type_g == 3) {
if (lights[i].type_g==0) {
L = normalize(lights[i].position_g - position);
distance = length(L);

Expand All @@ -79,7 +80,7 @@ void main() {

}
//If the light is Directional
else if(lights[i].type_g == 4) {
else if(lights[i].type_g==1) {
L = normalize(lights[i].direction_g);

// Calculate attenuation
Expand All @@ -95,30 +96,38 @@ void main() {
aux_color += (diffuse + specular) / attenuation + ambient;
//aux_color += diffuse + specular + ambient;
}
//If the light is Spotlight
else {
L = normalize(-lights[i].direction_g);
V = normalize(lights[i].position_g - position);
//If the light is Spotlight
else{

L_d = normalize(-lights[i].direction_g);
L = normalize(lights[i].position_g - position);
distance = length(V);

float theta = dot(L, V);
// Calculate Phong direct iluminations
H = normalize(L + V);


if(theta > lights[i].angle_g)//if (cos((10.0/2)(2.03.141592)/360) < theta)//
float theta = dot(V,L);
if(theta > cos(lights[i].angle_g))
{


// Calculate attenuation
attenuation = (lights[i].coeficients_g.x * pow(distance, 2.0) + lights[i].coeficients_g.y * distance + lights[i].coeficients_g.z);
// Calculate attenuation
if (attenuation <= 1.0) {
attenuation = 1.0;
}
else {
attenuation = 1.0/attenuation;
attenuation = dot(L_d,L)/attenuation;
}


// Calculate Phong direct iluminations
H = normalize(L + V);

vec4 norm = N;
vec4 lightDir = normalize(lights[i].position_g- position);

diffuse = lights[i].iD_g * material.diffuse * max(dot(L, N), 0.0);
specular = lights[i].iS_g * material.especular * pow(max(dot(N, H), 0.0), material.shineness);
ambient = lights[i].iA_g * material.ambient;
Expand All @@ -128,16 +137,32 @@ void main() {
} else { // The point is outside the cone of light from the spotlight.
aux_color += vec3(0.0); // The light will add no color to the point.
}
}
// Calculate Phong direct iluminations
H = normalize(L + V);

diffuse = lights[i].iD_g * material.diffuse * max(dot(L, N), 0.0);
specular = lights[i].iS_g * material.especular * pow(max(dot(N, H), 0.0), material.shineness);
ambient = lights[i].iA_g * material.ambient;
// Add it to the output color with the correspondent attenuation
aux_color += (diffuse + specular) / attenuation + ambient;
//aux_color += diffuse + specular + ambient;
}/*
//If we want the "earthlight" effect
else {
vec4 lightDir = normalize(lights[i].position_g- position);
float diff = max(dot(N, lightDir),0.0);
diffuse = lights[i].iD_g * material.diffuse * diff;
vec4 viewDir = V;
vec4 reflectDir = reflect(-lightDir,N);
float spec = pow(max(dot(viewDir,reflectDir),0.0),material.shineness);
specular = lights[i].iS_g * material.especular * spec;
float theta = dot(lightDir, normalize(-lights[i].direction_g));
float distance = length(lights[i].position_g-position);
float attenuation = (lights[i].coeficients_g.x * pow(distance, 2.0) + lights[i].coeficients_g.y * distance + lights[i].coeficients_g.z);
attenuation = 1.0f/attenuation;
ambient = lights[i].iA_g * material.ambient;
// Add it to the output color with the correspondent attenuation
aux_color += (diffuse + specular) * attenuation + ambient;
//aux_color += diffuse + specular + ambient;
}*/
//}
}

color = vec4(aux_color, material.alpha);
Expand All @@ -154,4 +179,3 @@ void main() {
//color = vec4(material.alpha,1.0,1.0,1.0);

}

Loading

0 comments on commit 37cc35d

Please sign in to comment.