Skip to content

Commit 270e28d

Browse files
committed
flatten opengl examples
1 parent dfa4a30 commit 270e28d

23 files changed

+302
-304
lines changed

OpenGL/README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ OpenGL - Examples
44
Program List
55
------------
66

7-
[*Language*](/OpenGL/Language/)
7+
*Language*
88

99
- **motion**.c
1010
- a constantly moving rectangle
1111
- **triangle**.c
1212
- a simple triangle blending colors across each of the points
1313

14-
[*Shaders*](/OpenGL/Shaders/)
14+
*Shaders*
1515

1616
- **shaders**.c
1717
- how to set up a shader in OpenGL (C)
1818
- includes sample shaders: base, gouraud, phong, texture-g, texture-p, & toon
1919

20-
[*System*](/OpenGL/System/)
20+
*System*
2121

2222
- **gui**.c
2323
- a GUI using OpenGL with GLUT and GLUI
2424

25-
[*Tutorials*](/OpenGL/Tutorials/)
25+
*Tutorials*
2626

2727
- **double**.c
2828
- simple square with mouse interactions
@@ -40,14 +40,12 @@ Program List
4040
Compiling & Running Code
4141
------------------------
4242

43-
all programs use C, OpenGL, GLUT, and GLUI
43+
All programs use C, OpenGL, GLUT, and GLUI.
4444

45-
for Linux, you can use the provided bash script
45+
For Linux, you can use the provided bash script:
4646
> e.g. for program: **triangle.c**
4747
48-
> > cd **Language/**
49-
50-
> > ./../run **triangle**
48+
> > ./run **triangle**
5149
5250
for Windows (using Cygwin), you can use the following commands
5351
> gcc -o FILE.exe FILE.c -lgl -lglu -lglut -lopengl32
@@ -63,4 +61,4 @@ for Mac, you can use the following commands
6361
Credit
6462
------
6563

66-
OpenGL Redbook for inspiring all of the *Tutorials/*
64+
OpenGL Redbook for inspiring all of the *Tutorials*.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// base fragment shader (pass-through)
2-
3-
void main(){
4-
gl_FragColor = gl_Color;
5-
}
1+
// base fragment shader (pass-through)
2+
3+
void main(){
4+
gl_FragColor = gl_Color;
5+
}
+11-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// base vertex shader (pass-through)
2-
3-
void main(){
4-
gl_Position = ftransform();
5-
6-
// can also use:
7-
//gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
8-
//gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
9-
10-
gl_FrontColor = gl_Color;
11-
}
1+
// base vertex shader (pass-through)
2+
3+
void main(){
4+
gl_Position = ftransform();
5+
6+
// can also use:
7+
//gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
8+
//gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
9+
10+
gl_FrontColor = gl_Color;
11+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

OpenGL/System/gui.c OpenGL/gui.c

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// texture fragment shader with Gouraud shading
2-
3-
// from main program, grab the input texture
4-
uniform sampler2D tex;
5-
6-
void main(){
7-
// set the fragment color based on texture
8-
vec4 col = texture2D(tex, gl_TexCoord[0].st);
9-
gl_FragColor = gl_Color * gl_Color * col;
10-
}
1+
// texture fragment shader with Gouraud shading
2+
3+
// from main program, grab the input texture
4+
uniform sampler2D tex;
5+
6+
void main(){
7+
// set the fragment color based on texture
8+
vec4 col = texture2D(tex, gl_TexCoord[0].st);
9+
gl_FragColor = gl_Color * gl_Color * col;
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
// texture vertex shader with Gouraud shading
2-
3-
// from main program, get specular highlight value
4-
uniform float s;
5-
6-
void main(){
7-
// set the texture coordinate
8-
gl_TexCoord[0] = gl_MultiTexCoord0;
9-
10-
// set variables
11-
vec3 ldir;
12-
vec3 lhalf;
13-
vec3 n;
14-
vec4 amb;
15-
vec4 diff;
16-
vec4 spec;
17-
float dotProd;
18-
float dotProd2;
19-
float ldist;
20-
vec4 col;
21-
22-
// calculate ambient and diffuse lighting
23-
amb = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
24-
amb += gl_LightModel.ambient * gl_FrontMaterial.ambient;
25-
diff = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
26-
27-
// normal for vertex, in eye coords
28-
n = normalize(gl_NormalMatrix * gl_Normal);
29-
30-
// light position for vertex
31-
vec4 vPos = gl_ModelViewMatrix * gl_Vertex;
32-
vec3 aux = vec3(gl_LightSource[0].position - vPos);
33-
ldir = normalize(aux);
34-
ldist = length(aux);
35-
36-
// calculate the light half vector
37-
lhalf = normalize(gl_LightSource[0].halfVector.xyz);
38-
39-
// dot product from 0 to 1
40-
dotProd = max(dot(n, ldir), 0.0);
41-
42-
// calculate attenuation
43-
float att = 1.0 / (gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * ldist + gl_LightSource[0].quadraticAttenuation * ldist * ldist);
44-
45-
// calculate specular term
46-
if(dotProd > 0.0){
47-
// compute the specular component
48-
dotProd2 = max(dot(n, lhalf), 0.0);
49-
spec = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(dotProd2, gl_FrontMaterial.shininess * (2.50 - s));
50-
51-
// spec, compute final color
52-
col = att * spec;
53-
gl_FrontColor = amb + att * (dotProd * diff) + att * spec;
54-
55-
}else{
56-
// no spec, compute final color
57-
gl_FrontColor = amb + att * (dotProd * diff);
58-
}
59-
60-
// keep the position the same
61-
gl_Position = ftransform();
62-
}
1+
// texture vertex shader with Gouraud shading
2+
3+
// from main program, get specular highlight value
4+
uniform float s;
5+
6+
void main(){
7+
// set the texture coordinate
8+
gl_TexCoord[0] = gl_MultiTexCoord0;
9+
10+
// set variables
11+
vec3 ldir;
12+
vec3 lhalf;
13+
vec3 n;
14+
vec4 amb;
15+
vec4 diff;
16+
vec4 spec;
17+
float dotProd;
18+
float dotProd2;
19+
float ldist;
20+
vec4 col;
21+
22+
// calculate ambient and diffuse lighting
23+
amb = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
24+
amb += gl_LightModel.ambient * gl_FrontMaterial.ambient;
25+
diff = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
26+
27+
// normal for vertex, in eye coords
28+
n = normalize(gl_NormalMatrix * gl_Normal);
29+
30+
// light position for vertex
31+
vec4 vPos = gl_ModelViewMatrix * gl_Vertex;
32+
vec3 aux = vec3(gl_LightSource[0].position - vPos);
33+
ldir = normalize(aux);
34+
ldist = length(aux);
35+
36+
// calculate the light half vector
37+
lhalf = normalize(gl_LightSource[0].halfVector.xyz);
38+
39+
// dot product from 0 to 1
40+
dotProd = max(dot(n, ldir), 0.0);
41+
42+
// calculate attenuation
43+
float att = 1.0 / (gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * ldist + gl_LightSource[0].quadraticAttenuation * ldist * ldist);
44+
45+
// calculate specular term
46+
if(dotProd > 0.0){
47+
// compute the specular component
48+
dotProd2 = max(dot(n, lhalf), 0.0);
49+
spec = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(dotProd2, gl_FrontMaterial.shininess * (2.50 - s));
50+
51+
// spec, compute final color
52+
col = att * spec;
53+
gl_FrontColor = amb + att * (dotProd * diff) + att * spec;
54+
55+
}else{
56+
// no spec, compute final color
57+
gl_FrontColor = amb + att * (dotProd * diff);
58+
}
59+
60+
// keep the position the same
61+
gl_Position = ftransform();
62+
}
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
// texture fragment shader with Phong shading
2-
3-
// grab lighting vars
4-
varying vec3 ldir;
5-
varying vec3 lhalf;
6-
varying float ldist;
7-
8-
// grab the vertex normal
9-
varying vec3 n;
10-
11-
// grab ambient, diffuse lighting terms
12-
varying vec4 diff;
13-
varying vec4 amb;
14-
15-
// from main program, grab the input texture
16-
uniform sampler2D tex;
17-
18-
// from main program, get specular highlight value
19-
uniform float s;
20-
21-
void main(){
22-
// set the fragment color based on texture
23-
vec4 col = texture2D(tex, gl_TexCoord[0].st);
24-
25-
// final color to output
26-
vec4 c;
27-
28-
// set color to the global ambient
29-
c = amb;
30-
31-
// renormalize for fragment
32-
vec3 norm = normalize(n);
33-
vec3 l = normalize(ldir);
34-
vec3 h = lhalf;
35-
36-
// sets the diffuse darkness (dot product betwee normal and light)
37-
float dotProd = max(dot(norm, l), 0.0);
38-
39-
// add diffuse & specular highlights if bright
40-
if(dotProd > 0.0){
41-
// calculate attenuation
42-
float att = 1.0 / (gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * ldist + gl_LightSource[0].quadraticAttenuation * ldist * ldist);
43-
44-
// calculate diffuse and specular brightness
45-
c += att * (diff * dotProd);
46-
c += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(max(dot(norm, h), 0.0), gl_FrontMaterial.shininess * (2.50 - s));
47-
}
48-
49-
// set the output color
50-
gl_FragColor = c * c * col;
51-
}
1+
// texture fragment shader with Phong shading
2+
3+
// grab lighting vars
4+
varying vec3 ldir;
5+
varying vec3 lhalf;
6+
varying float ldist;
7+
8+
// grab the vertex normal
9+
varying vec3 n;
10+
11+
// grab ambient, diffuse lighting terms
12+
varying vec4 diff;
13+
varying vec4 amb;
14+
15+
// from main program, grab the input texture
16+
uniform sampler2D tex;
17+
18+
// from main program, get specular highlight value
19+
uniform float s;
20+
21+
void main(){
22+
// set the fragment color based on texture
23+
vec4 col = texture2D(tex, gl_TexCoord[0].st);
24+
25+
// final color to output
26+
vec4 c;
27+
28+
// set color to the global ambient
29+
c = amb;
30+
31+
// renormalize for fragment
32+
vec3 norm = normalize(n);
33+
vec3 l = normalize(ldir);
34+
vec3 h = lhalf;
35+
36+
// sets the diffuse darkness (dot product betwee normal and light)
37+
float dotProd = max(dot(norm, l), 0.0);
38+
39+
// add diffuse & specular highlights if bright
40+
if(dotProd > 0.0){
41+
// calculate attenuation
42+
float att = 1.0 / (gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * ldist + gl_LightSource[0].quadraticAttenuation * ldist * ldist);
43+
44+
// calculate diffuse and specular brightness
45+
c += att * (diff * dotProd);
46+
c += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(max(dot(norm, h), 0.0), gl_FrontMaterial.shininess * (2.50 - s));
47+
}
48+
49+
// set the output color
50+
gl_FragColor = c * c * col;
51+
}

0 commit comments

Comments
 (0)