Last active
May 15, 2020 12:00
-
-
Save Raffaele3D/4a614a9d6502b35ff26888bbaaf7f916 to your computer and use it in GitHub Desktop.
Perspective camera/Vertex Shader/Fragment Shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
**************************************************** | |
********PERSPECTIVE CAMERA************************** | |
**************************************************** | |
private void Render () | |
{ | |
if (!loaded) | |
return; | |
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); | |
#region CAMERA | |
//***************** CAMERA | |
//************************ | |
if (CameraMode == "Perspective") | |
{ | |
updateCameraTextBoxes(); | |
//GL.UseProgram(colorProgram.id); //WHEN I ENABLE THIS LINE THE MODEL DISAPPEAR FROM THE SCEEN | |
// Use cooresponding shader when setting uniforms/drawing objects | |
int lightPosLoc = GL.GetUniformLocation(colorProgram.id, "light.position"); | |
int viewPosLoc = GL.GetUniformLocation(colorProgram.id, "viewPos"); | |
GL.Uniform3(lightPosLoc, lightPos.X, lightPos.Y, lightPos.Z); | |
GL.Uniform3(viewPosLoc, CameraWalkThrough_StartPos.X, CameraWalkThrough_StartPos.Y, CameraWalkThrough_StartPos.Z); | |
// Set lights properties | |
GL.Uniform3(GL.GetUniformLocation(colorProgram.id, "light.ambient"), ref lightAmbient); | |
GL.Uniform3(GL.GetUniformLocation(colorProgram.id, "light.diffuse"), ref lightDiffuse); | |
GL.Uniform3(GL.GetUniformLocation(colorProgram.id, "light.specular"), ref lightSpecular); | |
GL.Uniform1(GL.GetUniformLocation(colorProgram.id, "light.constant"), lightConstant); | |
GL.Uniform1(GL.GetUniformLocation(colorProgram.id, "light.linear"), lightLinear); | |
GL.Uniform1(GL.GetUniformLocation(colorProgram.id, "light.quadratic"), lightQuadratic); | |
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(fov * degToRad, (float)glControl1.Width / (float)glControl1.Height, nearCuttingPlane, farCuttingPlane); | |
Matrix4 view = CameraLookAt.GetViewMatrix_OrbitSelected(CameraMode, targetDistance); | |
ViewProjectionMatrix = view * projection; | |
// Transformation matrices | |
GL.ProgramUniformMatrix4(GL.GetUniformLocation(colorProgram.id, "projection"), 1, false, ref projection); | |
GL.ProgramUniformMatrix4(GL.GetUniformLocation(colorProgram.id, "view"), 1, false, ref view); | |
// Draw the loaded model | |
Matrix4 model = new Matrix4 (new Vector4 (1,0,0,0), new Vector4(0, 1, 0, 0), new Vector4(0, 0, 1, 0), new Vector4(0, 0, 0, 1)); | |
GL.ProgramUniformMatrix4(GL.GetUniformLocation(colorProgram.id, "model"), 1, false, ref model); | |
ourModel.Draw(colorProgram.id); //HERE I DRAW THE MODEL, BUT IF I ENABLE THE COMMAND LINE GL.UseProgram(colorProgram.id) THE MODEL DISAPPEAR FROM THE SCEEN!!! | |
} | |
GL.MatrixMode(MatrixMode.Projection); | |
GL.LoadIdentity(); | |
GL.LoadMatrix(ref ViewProjectionMatrix); | |
#endregion | |
statistics(); | |
Grid.Render_XY(Grid_xSpacing, Grid_ySpacing, Grid_size, Grid_drawGrid_XY); | |
Grid.Render_XZ(Grid_xSpacing, Grid_zSpacing, Grid_size, Grid_drawGrid_XZ); | |
draw_Geometries(); | |
drawGeometries_Style(); | |
glControl1.SwapBuffers(); | |
} | |
**************************************************** | |
********VERTEX SHADER******************************* | |
**************************************************** | |
#version 330 core | |
layout (location = 0) in vec3 position; | |
layout (location = 1) in vec3 normal; | |
layout (location = 2) in vec2 texCoords; | |
out vec3 Normal; | |
out vec3 FragPos; | |
out vec2 TexCoords; | |
uniform mat4 model; | |
uniform mat4 view; | |
uniform mat4 projection; | |
void main() | |
{ | |
gl_Position = projection * view * model * vec4(position, 1.0f); | |
FragPos = vec3(model * vec4(position, 1.0f)); | |
Normal = mat3(transpose(inverse(model))) * normal; | |
TexCoords = texCoords; | |
} | |
**************************************************** | |
********FRAGMENT SHADER***************************** | |
**************************************************** | |
#version 330 core | |
struct Material { | |
sampler2D diffuse; | |
sampler2D specular; | |
float shininess; | |
}; | |
struct Light { | |
vec3 position; | |
vec3 ambient; | |
vec3 diffuse; | |
vec3 specular; | |
float constant; | |
float linear; | |
float quadratic; | |
}; | |
in vec3 FragPos; | |
in vec3 Normal; | |
in vec2 TexCoords; | |
out vec4 color; | |
uniform vec3 viewPos; | |
uniform Material material; | |
uniform Light light; | |
uniform sampler2D texture_diffuse1; | |
void main() | |
{ | |
// Ambient | |
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); | |
// Diffuse | |
vec3 norm = normalize(Normal); | |
vec3 lightDir = normalize(light.position - FragPos); | |
float diff = max(dot(norm, lightDir), 0.0); | |
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); | |
// Specular | |
vec3 viewDir = normalize(viewPos - FragPos); | |
vec3 reflectDir = reflect(-lightDir, norm); | |
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); | |
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords)); | |
// Attenuation | |
float distance = length(light.position - FragPos); | |
float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); | |
ambient *= attenuation; | |
diffuse *= attenuation; | |
specular *= attenuation; | |
//color = vec4(1000.0f + ambient + diffuse + specular, 1.0f); | |
color += vec4(texture(texture_diffuse1, TexCoords)) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment