Skip to content
This repository was archived by the owner on Dec 24, 2019. It is now read-only.

Commit 8284095

Browse files
committed
added not working custom shader
1 parent 178d803 commit 8284095

4 files changed

Lines changed: 177 additions & 12 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#version 150
2+
// GLSL version 1.50
3+
// Fragment shader for diffuse shading in combination with a texture map
4+
5+
// Uniform variables passed in from host program
6+
uniform sampler2D myTexture;
7+
8+
uniform vec4 lightPosition[8];
9+
uniform int nLights;
10+
11+
// diffuse light
12+
uniform vec3 diffuseRadience[8];
13+
uniform vec3 diffuseReflection;
14+
uniform vec4 cameraPosition;
15+
16+
// specular light
17+
uniform vec3 specularRadience[8];
18+
uniform vec3 specularReflection;
19+
uniform int p;
20+
21+
// Variables passed in from the vertex shader
22+
in vec2 frag_texcoord;
23+
in vec3 frag_normal;
24+
in vec4 gl_Position;
25+
26+
// Output variable, will be written to framebuffer automatically
27+
out vec4 frag_shaded;
28+
29+
void main()
30+
{
31+
vec4 L[8];
32+
33+
34+
// calculate Lightvector L for every light source
35+
for (int i=0; i<nLights; i++){
36+
L[i] = lightPosition[i] - gl_Position;
37+
}
38+
39+
// calculate diffuse shading
40+
vec3 sum;
41+
sum.x = 0; sum.y = 0; sum.z=0;
42+
43+
for (int i=0; i<nLights; i++){
44+
float ndotl = max(dot((frag_normal), normalize(vec3 (L[i].x, L[i].y, L[i].z))), 0);
45+
46+
sum.x = sum.x + diffuseRadience[i].x * diffuseReflection.x * ndotl;
47+
sum.y = sum.y + diffuseRadience[i].y * diffuseReflection.y * ndotl;
48+
sum.z = sum.z + diffuseRadience[i].z * diffuseReflection.z * ndotl;
49+
}
50+
51+
frag_shaded.x = sum.x * texture(myTexture, frag_texcoord).x;
52+
frag_shaded.y = sum.y * texture(myTexture, frag_texcoord).y;
53+
frag_shaded.z = sum.z * texture(myTexture, frag_texcoord).z;
54+
55+
56+
//----------------------------------------------------------------------------------------------------------
57+
58+
// calculate R: the vector of the reflection
59+
vec4 R[8];
60+
61+
for (int i=0; i< nLights; i++){
62+
R[i].x = max(2 * dot(L[i], vec4(frag_normal,0)) * frag_normal.x - L[i].x, 0);
63+
R[i].y = max(2 * dot(L[i], vec4(frag_normal,0)) * frag_normal.y - L[i].y, 0);
64+
R[i].z = max(2 * dot(L[i], vec4(frag_normal,0)) * frag_normal.z - L[i].z, 0);
65+
R[i].w = 0;
66+
}
67+
68+
69+
// calculate e: the vector to the camera
70+
vec4 E;
71+
72+
E.x = gl_Position.x - cameraPosition.x;
73+
E.y = gl_Position.y - cameraPosition.y;
74+
E.z = gl_Position.z - cameraPosition.z;
75+
76+
for (int i=0; i<nLights; i++){
77+
sum.x = sum.x + specularRadience[i].x * specularReflection.x * pow(R[i].x * E.x, p);
78+
sum.y = sum.y + specularRadience[i].y * specularReflection.y * pow(R[i].y * E.y, p);
79+
sum.z = sum.z + specularRadience[i].z * specularReflection.z * pow(R[i].z * E.z, p);
80+
}
81+
82+
frag_shaded.x = sum.x * texture(myTexture, frag_texcoord).x;
83+
frag_shaded.y = sum.y * texture(myTexture, frag_texcoord).y;
84+
frag_shaded.z = sum.z * texture(myTexture, frag_texcoord).z;
85+
86+
87+
88+
// The built-in GLSL function "texture" performs the texture lookup
89+
//frag_shaded = ndotl * texture(myTexture, frag_texcoord);
90+
}
91+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#version 150
2+
// GLSL version 1.50
3+
// Vertex shader for diffuse shading in combination with a texture map
4+
5+
// Uniform variables, passed in from host program via suitable
6+
// variants of glUniform*
7+
uniform mat4 projection;
8+
uniform mat4 modelview;
9+
10+
11+
// Input vertex attributes; passed in from host program to shader
12+
// via vertex buffer objects
13+
in vec3 normal;
14+
in vec4 position;
15+
in vec2 texcoord;
16+
17+
// Output variables for fragment shader
18+
out vec2 frag_texcoord;
19+
out vec3 frag_normal;
20+
21+
void main()
22+
{
23+
// Compute dot product of normal and light direction
24+
// and pass color to fragment shader
25+
// Note: here we assume "lightDirection" is specified in camera coordinates,
26+
// so we transform the normal to camera coordinates, and we don't transform
27+
// the light direction, i.e., it stays in camera coordinates
28+
29+
30+
// Pass texture coordiantes to fragment shader, OpenGL automatically
31+
// interpolates them to each pixel (in a perspectively correct manner)
32+
frag_texcoord = texcoord;
33+
frag_normal = normal;
34+
35+
// Transform position, including projection matrix
36+
// Note: gl_Position is a default output variable containing
37+
// the transformed vertex position
38+
gl_Position = projection * modelview * position;
39+
}

Serie 4/Computergrafik-Basecode-master/jrtr/src/main/java/jrtr/GLRenderContext.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* version 3 (or later).
1515
*/
1616
public class GLRenderContext implements RenderContext {
17-
17+
18+
private static final int PHONG_EXPONENT = 64;
1819
private SceneManagerInterface sceneManager;
1920
private GL3 gl;
2021

@@ -314,16 +315,16 @@ private void setMaterial(Material m) {
314315
id = gl.glGetUniformLocation(activeShaderID, lightString);
315316
if(id!=-1)
316317
gl.glUniform4f(id, l.position.x, l.position.y, l.position.z, 0.f); // Set light position
317-
else
318-
System.out.print("Could not get location of uniform variable " + lightString + "\n");
318+
//else
319+
// System.out.print("Could not get location of uniform variable " + lightString + "\n");
319320

320321
// Pass diffuse radience to shader, we assume the shader stores it in an array "lightRadience[]"
321322
lightString = "diffuseRadience[" + nLights + "]";
322323
id = gl.glGetUniformLocation(activeShaderID, lightString);
323324
if(id!=-1)
324325
gl.glUniform4f(id, l.diffuse.x, l.diffuse.y, l.diffuse.z, 0.f); // Set light radience
325-
else
326-
System.out.print("Could not get location of uniform variable " + lightString + "\n");
326+
//else
327+
// System.out.print("Could not get location of uniform variable " + lightString + "\n");
327328

328329

329330
nLights++;
@@ -333,16 +334,33 @@ private void setMaterial(Material m) {
333334
id = gl.glGetUniformLocation(activeShaderID, "diffuseReflection");
334335
if(id!=-1)
335336
gl.glUniform3f(id, m.diffuse.x, m.diffuse.y, m.diffuse.z); // Set diffuse reflection
336-
else
337-
System.out.print("Could not get location of uniform variable diffuseReflection\n");
337+
//else
338+
// System.out.print("Could not get location of uniform variable diffuseReflection\n");
339+
340+
// Pass camera position to shader, we assume this is in a variable "nLights" in the shader
341+
Vector4f cam = new Vector4f();
342+
sceneManager.getCamera().getCameraMatrix().getColumn(3, cam);
343+
344+
id = gl.glGetUniformLocation(activeShaderID, "cameraPosition");
345+
if(id!=-1)
346+
gl.glUniform3f(id, cam.x, cam.y, cam.z); // Set camera position
347+
//else
348+
// System.out.print("Could not get location of uniform variable camera position\n");
338349

339350

340351
// Pass number of lights to shader, we assume this is in a variable "nLights" in the shader
341352
id = gl.glGetUniformLocation(activeShaderID, "nLights");
342353
if(id!=-1)
343354
gl.glUniform1i(id, nLights); // Set number of lights
344-
else
345-
System.out.print("Could not get location of uniform variable nLights\n");
355+
//else
356+
// System.out.print("Could not get location of uniform variable nLights\n");
357+
358+
// Pass number of lights to shader, we assume this is in a variable "nLights" in the shader
359+
id = gl.glGetUniformLocation(activeShaderID, "nLights");
360+
if(id!=-1)
361+
gl.glUniform1i(id, PHONG_EXPONENT); // Set number of lights
362+
//else
363+
// System.out.print("Could not get location of uniform variable nLights\n");
346364
}
347365
}
348366
}

Serie 4/Computergrafik-Basecode-master/simple/src/main/java/simple/simple.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class simple {
1616
static RenderContext renderContext;
1717
static Shader normalShader;
1818
static Shader diffuseShader;
19+
static Shader customShader;
1920
static Material material;
2021
static SimpleSceneManager sceneManager;
2122
static Shape shape;
@@ -87,7 +88,7 @@ public void init(RenderContext r) {
8788
sceneManager.addShape(shape);
8889

8990
// TODO: add lights
90-
91+
9192
Light light = new Light();
9293
light.position = new Vector3f(0, 2, 0);
9394
light.diffuse = new Vector3f(1, 1, 1);
@@ -113,10 +114,26 @@ public void init(RenderContext r) {
113114
System.out.print(e.getMessage());
114115
}
115116

117+
customShader = renderContext.makeShader();
118+
try {
119+
customShader.load("../jrtr/shaders/custom.vert", "../jrtr/shaders/custom.frag");
120+
} catch (Exception e) {
121+
System.out.print("Problem with shader:\n");
122+
System.out.print(e.getMessage());
123+
}
124+
125+
// TODO: set shader: 0 = diffuse shader; 1 = custom shader
126+
int shader = 1;
127+
128+
116129
// Make a material that can be used for shading
117130
material = new Material();
118-
material.shader = diffuseShader;
119-
material.texture = renderContext.makeTexture();
131+
if (shader == 0)
132+
material.shader = diffuseShader;
133+
else if (shader == 1)
134+
material.shader = customShader;
135+
136+
material.texture = renderContext.makeTexture();
120137
try {
121138
material.texture.load("../textures/plant.jpg");
122139
} catch (Exception e) {

0 commit comments

Comments
 (0)