Skip to content

Commit

Permalink
Added more options in terms of geometries and loaders
Browse files Browse the repository at this point in the history
STLLoader
Cylinder
Sphere
  • Loading branch information
JohnSumskas committed Feb 4, 2020
1 parent 2b70e50 commit 04adcb3
Show file tree
Hide file tree
Showing 11 changed files with 514 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ build
MANIFEST
dist
*.egg-info
venv/
83 changes: 83 additions & 0 deletions examples/blinnphong.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---VERTEX SHADER-------------------------------------------------------
/**
* Based on: https://learnopengl.com/Advanced-Lighting/Advanced-Lighting
*/

#ifdef GL_ES
precision highp float;
#endif

attribute vec3 v_pos;
attribute vec3 v_normal;
attribute vec4 v_color;
attribute vec2 v_tc0;

uniform mat4 modelview_mat;
uniform mat4 projection_mat;

varying vec4 frag_color;
varying vec2 uv_vec;
varying vec4 normal_vec;
varying vec4 vertex_pos;

void main (void) {
vec4 pos = modelview_mat * vec4(v_pos,1.0);
vertex_pos = pos;
gl_Position = projection_mat * pos;
frag_color = v_color;
uv_vec = v_tc0;
normal_vec = modelview_mat * vec4(v_normal, 0.0);
}


---FRAGMENT SHADER-----------------------------------------------------
#ifdef GL_ES
precision highp float;
#endif

varying vec4 frag_color;
varying vec2 uv_vec;
varying vec4 normal_vec;
varying vec4 vertex_pos;

uniform mat4 normal_mat;
uniform sampler2D tex;

uniform vec3 light_pos;
uniform float light_intensity;
uniform vec3 camera_pos;

uniform vec3 Ka; // color (ambient)
uniform vec3 Kd; // diffuse color
uniform vec3 Ks; // specular color
uniform float Tr; // transparency
uniform float Ns; // shininess
uniform float d; // dissolve
uniform float ambient_ratio;

void main (void){
vec4 color = texture2D(tex, uv_vec);
color = vec4(Ka, Tr);
if (Ns > 0.0) {
// ambient
vec3 ambient = ambient_ratio * vec3(color);
// diffuse
vec3 lightDir = normalize(light_pos - vec3(vertex_pos));
vec3 normal = normalize(vec3(normal_vec));
float diff = max(dot(lightDir, normal), 0.0);
vec3 diffuse = diff * vec3(Kd);
// specular
vec3 viewDir = normalize(camera_pos - vec3(vertex_pos));
vec3 reflectDir = reflect(-lightDir, normal);

// Blinn-Phong lighting
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(normal, halfwayDir), 0.0), Ns);

// vec3 specular = vec3(0.3) * spec;// assuming bright white light color
vec3 specular = Ks * spec;// assuming bright white light color
gl_FragColor = vec4(ambient + diffuse + specular, color[3]);
} else {
gl_FragColor = color;
}
}
44 changes: 34 additions & 10 deletions examples/createscene.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,47 @@
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
from kivy3 import Mesh, Material
from kivy3 import Scene, Renderer, PerspectiveCamera
from kivy3.extras.geometries import BoxGeometry
from kivy3 import Scene, Renderer, PerspectiveCamera, OrthographicCamera
from kivy3.extras.geometries import BoxGeometry, CylinderGeometry, SphereGeometry
from kivy3.loaders import OBJLoader, STLLoader

from kivy.graphics import Color, Rectangle


_this_path = os.path.dirname(os.path.realpath(__file__))
shader_file = os.path.join(_this_path, "./blinnphong.glsl")
obj_file = os.path.join(_this_path, "./monkey.obj")
stl_file = os.path.join(_this_path, "./test.stl")

class SceneApp(App):

def build(self):
root = FloatLayout()

self.renderer = Renderer()
self.renderer.set_clear_color((.2, .2, .2, 1.))
self.renderer = Renderer(shader_file=shader_file)
self.renderer.set_clear_color((.16, .30, .44, 1.))


scene = Scene()
geometry = BoxGeometry(1, 1, 1)
material = Material(color=(0., 0., 1.), diffuse=(1., 1., 0.),
specular=(.35, .35, .35))
# geometry = CylinderGeometry(0.5, 2)
geometry = SphereGeometry(1)
# geometry = BoxGeometry(1, 1, 1)
material = Material(color=(0.3, 0., 0.3), diffuse=(0.3, 0.3, 0.3),
specular=(0., 0., 0.))

loader = STLLoader()
obj = loader.load(stl_file,material)
self.item = obj

scene.add(self.item)

self.cube = Mesh(geometry, material)
self.cube.pos.z = -5
camera = PerspectiveCamera(75, 0.3, 1, 1000)
self.item.pos.z = -1.5
#self.cube.pos.z=-5
camera = PerspectiveCamera(75, 0.3, 0.5, 1000)
#camera = OrthographicCamera()

scene.add(self.cube)
#scene.add(self.cube)
self.renderer.render(scene, camera)

root.add_widget(self.renderer)
Expand All @@ -42,6 +63,9 @@ def _rotate_cube(self, dt):
self.cube.rotation.x += 1
self.cube.rotation.y += 1
self.cube.rotation.z += 1
self.item.rotation.x += 1
self.item.rotation.y += 1
self.item.rotation.z += 1


if __name__ == '__main__':
Expand Down
85 changes: 85 additions & 0 deletions examples/extended.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* simple.glsl
simple diffuse lighting based on laberts cosine law; see e.g.:
http://en.wikipedia.org/wiki/Lambertian_reflectance
http://en.wikipedia.org/wiki/Lambert%27s_cosine_law
*/
---VERTEX SHADER-------------------------------------------------------
#ifdef GL_ES
precision highp float;
#endif


attribute vec3 v_pos;
attribute vec3 v_normal;
attribute vec4 v_color;
attribute vec2 v_tc0;

uniform mat4 modelview_mat;
uniform mat4 projection_mat;
uniform mat4 normal_mat;
uniform mat4 model_mat;
uniform mat4 view_mat;

varying vec4 normal_vec;
varying vec4 vertex_pos;
varying vec4 frag_color;
varying vec2 uv_vec;

void main (void) {
//compute vertex position in eye_sapce and normalize normal vector
vec4 pos = modelview_mat * vec4(v_pos,1.0);
vertex_pos = pos;
normal_vec = vec4(v_normal,0.0);

frag_color = v_color;
uv_vec = v_tc0;

gl_Position = projection_mat * pos;
}


---FRAGMENT SHADER-----------------------------------------------------
#ifdef GL_ES
precision highp float;
#endif

varying vec4 normal_vec;
varying vec4 vertex_pos;

uniform mat4 normal_mat;
uniform mat4 model_mat;
uniform mat4 view_mat;

uniform vec3 camera_pos;
uniform vec3 Ka; // color (ambient)
uniform vec3 Kd; // diffuse color
uniform vec3 Ks; // specular color
uniform float Tr; // transparency
uniform float Ns; // shininess
uniform float tex_ratio;

uniform vec3 light_pos;
uniform float light_intensity;


void main (void){
//correct normal, and compute light vector (assume light at the eye)
vec4 v_normal = normalize( normal_mat * normal_vec ) ;
vec4 v_light = normalize( vec4(0,0,0,1) - vertex_pos );

// force lightPos to lower-left (like in Kivy)
vec4 lightPos = model_mat * vec4(light_pos, 0.0) - gl_FragCoord;
float lightPosLen = length(lightPos);

// set ambient, diffuse, specular color
vec3 Ia = Ka * light_intensity / lightPosLen;
vec3 Id = Kd * max(dot(v_light, v_normal), 0.0);
vec3 Is = Ks * pow(max(dot(v_light, v_normal), 0.0), Ns);

//reflectance based on lamberts law of cosine
float theta = clamp(dot(v_normal, v_light), 0.1, 1.0);
vec3 thetaColor = theta * vec3(Ia + Id + Is);

gl_FragColor = vec4(thetaColor, Tr);
}
Binary file added examples/test.stl
Binary file not shown.
Loading

0 comments on commit 04adcb3

Please sign in to comment.