Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project 5 #7

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
283 changes: 13 additions & 270 deletions README.md

Large diffs are not rendered by default.

68 changes: 64 additions & 4 deletions part1/Globe/Globe/Globe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static device_mesh_t current_mesh;
static device_mesh_t device_sphere;

static GLuint current_prog;
static GLuint relief_prog;
static GLuint lambert_prog;
static GLuint blinnphong_prog;
static GLuint fresnel_prog;
Expand Down Expand Up @@ -248,6 +249,7 @@ mat4 get_view() {
}

float time = 0.0;
float toon = 0.95;

void display(void)
{
Expand All @@ -274,6 +276,7 @@ void display(void)
glUniformMatrix4fv(glGetUniformLocation(current_prog,"u_Persp"),1,GL_FALSE,&persp[0][0]);
glUniformMatrix4fv(glGetUniformLocation(current_prog,"u_InvTrans") ,1,GL_FALSE,&inverse_transposed[0][0]);
glUniform1f(glGetUniformLocation(current_prog,"u_time"), time);
glUniform1f(glGetUniformLocation(current_prog,"u_toon"), toon);

glDrawElements(GL_TRIANGLES, current_mesh.num_indices, GL_UNSIGNED_SHORT,0);

Expand All @@ -284,7 +287,8 @@ void display(void)

void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
float m = (w<h?w:h);
glViewport(0.5*(w-m),0.5*(h-m),(GLsizei)m,(GLsizei)m);
}

void initGlobeShader() {
Expand All @@ -294,15 +298,26 @@ void initGlobeShader() {
cloud_tex = (unsigned int)SOIL_load_OGL_texture("earthcloudmap.jpg",0,0,0);
cloudtrans_tex = (unsigned int)SOIL_load_OGL_texture("earthcloudmaptrans.jpg",0,0,0);
earthspec_tex = (unsigned int) SOIL_load_OGL_texture("earthspec1k.jpg",0,0,0);
disp_tex = (unsigned int) SOIL_load_OGL_texture("earthbump1k.jpg",0,0,0);
disp_tex = (unsigned int) SOIL_load_OGL_texture("earthbump1k.jpg",0,0,0);
glBindTexture(GL_TEXTURE_2D, cloudtrans_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, cloud_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, daydiffuse_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, night_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, earthspec_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, disp_tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);
}

void initReliefShader() {
relief_prog = initShader("vs.glsl", "reliefFS.glsl",NULL,NULL,0);
}

void setGlobeShader() {
current_prog = globe_prog;
glUseProgram(current_prog);
Expand All @@ -328,15 +343,59 @@ void setGlobeShader() {

}

void setReliefShader() {
current_prog = relief_prog;
glUseProgram(current_prog);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, daydiffuse_tex);
glUniform1i(glGetUniformLocation(current_prog, "u_DayDiffuse"),0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, night_tex);
glUniform1i(glGetUniformLocation(current_prog, "u_Night"),1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, cloud_tex);
glUniform1i(glGetUniformLocation(current_prog, "u_Cloud"),2);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, cloudtrans_tex);
glUniform1i(glGetUniformLocation(current_prog, "u_CloudTrans"),3);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, earthspec_tex);
glUniform1i(glGetUniformLocation(current_prog, "u_EarthSpec"),4);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, disp_tex);
glUniform1i(glGetUniformLocation(current_prog, "u_Bump"),5);

}

void keyboard(unsigned char key, int x, int y) {
switch (key) {
case '+':
speedUpRotation();
break;
case '-':
slowDownRotation();
break;
case '1':
setGlobeShader();
break;
case '2':
setReliefShader();
break;
case ']':
toon += 0.01f;
break;
case '[':
toon -= 0.01f;
break;
case '}':
toon += 0.05f;
break;
case '{':
toon -= 0.05f;
break;
}
toon = glm::clamp( toon, 0.1f, 0.95f );
}

int mouse_buttons = 0;
Expand Down Expand Up @@ -403,7 +462,8 @@ int main (int argc, char* argv[])
initView();
initSphere();
initGlobeShader();
setGlobeShader();
initReliefShader();
setReliefShader();
setCurrentMesh(device_sphere);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
Expand Down
3 changes: 2 additions & 1 deletion part1/Globe/Globe/Globe.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)$(Configuration)\</OutDir>
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Configuration)\</IntDir>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</LinkIncremental>
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)$(Configuration)\</OutDir>
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Configuration)\</IntDir>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
Expand Down Expand Up @@ -93,6 +93,7 @@
</ItemGroup>
<ItemGroup>
<None Include="fs.glsl" />
<None Include="reliefFS.glsl" />
<None Include="vs.glsl" />
<None Include="earthbump1k.jpg" />
<None Include="earthcloudmap.jpg" />
Expand Down
3 changes: 3 additions & 0 deletions part1/Globe/Globe/Globe.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@
<None Include="vs.glsl">
<Filter>GLSL</Filter>
</None>
<None Include="reliefFS.glsl">
<Filter>GLSL</Filter>
</None>
</ItemGroup>
</Project>
31 changes: 26 additions & 5 deletions part1/Globe/Globe/fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ uniform sampler2D u_EarthSpec;
uniform sampler2D u_Bump;

uniform float u_time;
uniform float u_toon;
uniform mat4 u_InvTrans;

varying vec3 v_Normal; // surface normal in camera coordinates
varying vec2 v_Texcoord;
varying vec3 v_Position; // position in camera coordinates
varying vec3 v_viewMC;
varying vec3 v_positionMC; // position in model coordinates

mat3 eastNorthUpToEyeCoordinates(vec3 positionMC, vec3 normalEC);
Expand All @@ -31,19 +33,38 @@ void main(void)
{
vec3 normal = normalize(v_Normal); // surface normal - normalized after rasterization
vec3 eyeToPosition = normalize(v_Position); // normalized eye-to-position vector in camera coordinates

float rim = dot( normal, v_Position )+1;
vec4 rimlight = vec4(rim/4, rim/3, rim/2, 0.0);

float diffuse = max(dot(u_CameraSpaceDirLight, normal), 0.0);
if( rim < 0 )
rimlight = vec4( 0.0 );

float top = texture2D( u_Bump, v_Texcoord + vec2(0.0, 1.0/500.0) );
float center = texture2D( u_Bump, v_Texcoord );
float right = texture2D( u_Bump, v_Texcoord + vec2(1.0/1000.0, 0.0) );

vec3 bnorm = normalize(eastNorthUpToEyeCoordinates(v_positionMC, normal) * normalize(vec3(center - right, center - top, 0.2)));

float diffuse = max(dot(u_CameraSpaceDirLight, bnorm), 0.0);

vec3 toReflectedLight = reflect(-u_CameraSpaceDirLight, normal);
float specular = max(dot(toReflectedLight, -eyeToPosition), 0.0);
specular = pow(specular, 20.0);
specular = pow(specular, 20.0) * texture2D(u_EarthSpec, v_Texcoord);
vec2 cloudTexcoord = v_Texcoord + vec2( u_time, 0.0 );
float cloudAlpha = texture2D(u_CloudTrans,cloudTexcoord);
float gammaCorrect = 1/1.2; //gamma correct by 1/1.8

vec4 dayColor = ((0.6 * diffuse) + (0.6 * specular)) * texture2D(u_DayDiffuse, v_Texcoord);

float gammaCorrect = 1/1.8; //gamma correct by 1/1.8
diffuse = max(dot(u_CameraSpaceDirLight, normal), 0.0);

dayColor = mix( (0.6 * diffuse)*texture2D(u_Cloud,cloudTexcoord), dayColor, cloudAlpha );

vec4 dayColor = texture2D(u_DayDiffuse, v_Texcoord);
vec4 nightColor = pow(texture2D(u_Night, v_Texcoord),gammaCorrect); //apply gamma correction to nighttime texture
nightColor = mix( vec4( 0.0 ), nightColor, cloudAlpha );

gl_FragColor = ((0.6 * diffuse) + (0.4 * specular)) * dayColor;
gl_FragColor = rimlight + mix( dayColor, nightColor, pow(1.0 - diffuse, 8.0) );
}

mat3 eastNorthUpToEyeCoordinates(vec3 positionMC, vec3 normalEC)
Expand Down
120 changes: 120 additions & 0 deletions part1/Globe/Globe/reliefFS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//View-Space directional light
//A unit vector
uniform vec3 u_CameraSpaceDirLight;

//Diffuse texture map for the day
uniform sampler2D u_DayDiffuse;
//Ambient texture map for the night side
uniform sampler2D u_Night;
//Color map for the clouds
uniform sampler2D u_Cloud;
//Transparency map for the clouds. Note that light areas are where clouds are NOT
//Dark areas are were clouds are present
uniform sampler2D u_CloudTrans;
//Mask of which areas of the earth have specularity
//Oceans are specular, landmasses are not
uniform sampler2D u_EarthSpec;
//Bump map
uniform sampler2D u_Bump;

uniform float u_time;
uniform float u_toon;
uniform mat4 u_InvTrans;

varying vec3 v_Normal; // surface normal in camera coordinates
varying vec2 v_Texcoord;
varying vec3 v_Position; // position in camera coordinates
varying vec3 v_viewMC;
varying vec3 v_positionMC; // position in model coordinates

mat3 eastNorthUpToEyeCoordinates(vec3 positionMC, vec3 normalEC);

void main(void)
{
vec3 normal = normalize(v_Normal); // surface normal - normalized after rasterization
vec3 eyeToPosition = normalize(v_Position); // normalized eye-to-position vector in camera coordinates

float rim = max(0, dot( normal, v_Position )+1);
vec4 rimlight = vec4(rim/4, rim/3, rim/2, 0.0);

vec2 cloudTexcoord = v_Texcoord + vec2( u_time, 0.0 );
float cloudAlpha = texture2D(u_CloudTrans,cloudTexcoord);

float sealevel = u_toon;
float height = sealevel + (1-sealevel)*texture2D( u_Bump, v_Texcoord );

bool escaped = false;
vec3 rpos = v_positionMC;
vec3 viewing = normalize(v_viewMC);
float r = length( rpos );
vec2 last = v_Texcoord;
float lastHeight = height;
float lastR = r;

while( height < r )
{
lastR = r;
last = v_Texcoord;
lastHeight = height;
rpos += 0.01*viewing;
r = length( rpos );
float theta = acos( rpos.z / r ) / 3.14159;
float phi = ( atan2( rpos.x, rpos.y ) / 6.28318 );
v_Texcoord = vec2(-phi, theta);
height = sealevel + (1-sealevel)*texture2D( u_Bump, v_Texcoord );
if( r > 1 )
{
escaped = true;
break;
}
}
float t = (lastHeight - lastR) / ((r-lastR)-(height-lastHeight));
v_Texcoord = last*(1-t) + t*v_Texcoord;

if( !escaped )
{

float top = texture2D( u_Bump, v_Texcoord + vec2(0.0, 1.0/500.0) );
float center = texture2D( u_Bump, v_Texcoord );
float right = texture2D( u_Bump, v_Texcoord + vec2(1.0/1000.0, 0.0) );

vec3 bnorm = normalize(eastNorthUpToEyeCoordinates(v_positionMC, normal) * normalize(vec3(center - right, center - top, 0.2)));

float diffuse = max(dot(u_CameraSpaceDirLight, bnorm), 0.0);

vec3 toReflectedLight = reflect(-u_CameraSpaceDirLight, normal);
float specular = max(dot(toReflectedLight, -eyeToPosition), 0.0);
specular = pow(specular, 20.0) * texture2D(u_EarthSpec, v_Texcoord);
float gammaCorrect = 1/1.2; //gamma correct by 1/1.8

vec4 dayColor = ((0.6 * diffuse) + (0.6 * specular)) * texture2D(u_DayDiffuse, v_Texcoord);

diffuse = max(dot(u_CameraSpaceDirLight, normal), 0.0);

dayColor = mix( (0.6 * diffuse)*texture2D(u_Cloud,cloudTexcoord), dayColor, cloudAlpha );

vec4 nightColor = pow(texture2D(u_Night, v_Texcoord),gammaCorrect); //apply gamma correction to nighttime texture
nightColor = mix( vec4( 0.0 ), nightColor, cloudAlpha );

gl_FragColor = rimlight + mix( dayColor, nightColor, pow(1.0 - diffuse, 8.0) );
}
else
{
float diffuse = max(dot(u_CameraSpaceDirLight, normal), 0.0);
vec4 dayColor = mix( (0.6 * diffuse)*texture2D(u_Cloud,cloudTexcoord), vec4(0), cloudAlpha );

gl_FragColor = rimlight + dayColor;
}
}

mat3 eastNorthUpToEyeCoordinates(vec3 positionMC, vec3 normalEC)
{
vec3 tangentMC = normalize(vec3(-positionMC.y, positionMC.x, 0.0)); // normalized surface tangent in model coordinates
vec3 tangentEC = normalize(mat3(u_InvTrans) * tangentMC); // normalized surface tangent in eye coordiantes
vec3 bitangentEC = normalize(cross(normalEC, tangentEC)); // normalized surface bitangent in eye coordinates

return mat3(
tangentEC.x, tangentEC.y, tangentEC.z,
bitangentEC.x, bitangentEC.y, bitangentEC.z,
normalEC.x, normalEC.y, normalEC.z);
}
2 changes: 2 additions & 0 deletions part1/Globe/Globe/vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ varying vec3 v_Normal;
varying vec2 v_Texcoord;
varying vec3 v_Position;
varying vec3 v_positionMC;
varying vec3 v_viewMC;

void main(void)
{
Expand All @@ -20,5 +21,6 @@ void main(void)
vec4 camera = u_View * world;
v_Position = camera.xyz;
v_positionMC = Position;
v_viewMC = (v_positionMC - (inverse( u_Model ) * inverse( u_View ) * vec4( 0, 0, 0, 1.0 ))).xyz;
gl_Position = u_Persp * camera;
}
4 changes: 3 additions & 1 deletion part2/565GLFrame/565GLFrame/565GLFrame.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)$(Configuration)\</OutDir>
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(Configuration)\</IntDir>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</LinkIncremental>
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)$(Configuration)\</OutDir>
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(Configuration)\</IntDir>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
Expand Down Expand Up @@ -99,6 +99,7 @@
<ClCompile Include="main.cpp" />
<ClCompile Include="obj.cpp" />
<ClCompile Include="objloader.cpp" />
<None Include="blip.vert" />
<ClCompile Include="Utility.cpp" />
</ItemGroup>
<ItemGroup>
Expand All @@ -108,6 +109,7 @@
<ClInclude Include="Utility.h" />
</ItemGroup>
<ItemGroup>
<None Include="melt.vert" />
<None Include="pass.frag" />
<None Include="pass.vert" />
<None Include="random.png" />
Expand Down
6 changes: 6 additions & 0 deletions part2/565GLFrame/565GLFrame/565GLFrame.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<None Include="random.png">
<Filter>Resources</Filter>
</None>
<None Include="melt.vert">
<Filter>Source Files</Filter>
</None>
<None Include="blip.vert">
<Filter>Source Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Filter Include="Header Files">
Expand Down
Loading