-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_obj.html
141 lines (132 loc) · 3.19 KB
/
sample_obj.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<title>WWG (objload)</title>
<script src="js/WWG.js"></script>
<script src="js/CanvasMatrix.js"></script>
<script src="js/WWModel.js"></script>
<script src="js/Pointer.js"></script>
<script>
onload = function() {
var render = {
env:{clear_color:[0.4,0.4,0.4,1.0],cull:true},
vshader:{
text: document.getElementById('vshader').innerText
},
fshader:{
text: document.getElementById('fshader').innerText
},
vs_uni:{
},
fs_uni:{
lightVec:[1,1,1],
color:[1.0,0.2,0.2,1.0],
mode:0
},
model:[]
}
function update(render,p) {
var modelMatrix = new CanvasMatrix4().
translate(0,p.ofsY,0).
rotate(p.rotY,0,1,0).
rotate(p.rotX,1,0,0)
var uni = {vs_uni:{
mvpMatrix:new CanvasMatrix4(modelMatrix).
lookat(0,0,10,0,0,0,0,1,0).
perspective(40,1.0, 0.1, 100).getAsWebGLFloatArray()},
fs_uni:{invMatrix:modelMatrix.invert().getAsWebGLFloatArray()},
}
render.draw(uni,false) ;
}
var can = document.getElementById('screen1') ;
can.width= can.offsetWidth ;
can.height = can.offsetHeight ;
var wwg = new WWG() ;
if(!wwg.init(can)) {
alert("not supported") ;
return ;
}
//load obj file
var m= new WWModel() ;
var of = "res/teapot.obj" ;
var p = {rotX:0,rotY:0,ofsY:0}
m.loadObj(of,0.03).then(function(data) {
var obj = data.objModel() ;
render.model.push( {geo:obj} );
render.model.push( {geo:data.normLines(),fs_uni:{ color:[0.5,0.5,1.0,0.5],mode:1}} );
//create render unit
var r = wwg.createRender() ;
r.setRender(render).then(function() {
console.log(r) ;
var rotX = 0 ,rotY = 0 ;
//draw first time
update(r,p);
//mouse intraction
var mag = 300/can.width;
var m = new Pointer(can,{
down:function(d) {
return false ;
},
move:function(d) {
p.rotX = rotX+d.dy*mag ;
p.rotY = rotY+d.dx*mag ;
update(r,p) ;
return false ;
},
up:function(d) {
rotX += d.dy*mag ;
rotY += d.dx*mag;
return false ;
}
})
}).catch(function(err){
console.log(err) ;
})
}).catch(function(err){
console.log(err) ;
})
} //onload
</script>
<! --------------- Vertex Shader ---------------- ->
<script id="vshader" type="x-shader/x-vertex">
attribute vec3 position;
attribute vec3 norm;
attribute vec2 uv ;
uniform mat4 mvpMatrix;
varying vec3 tnorm ;
varying vec2 tuv ;
void main() {
tnorm = norm ;
tuv = uv ;
gl_Position = mvpMatrix * vec4(position, 1.0) ;
}
</script>
<! --------------- Fragment Shader ---------------- ->
<script id="fshader" type="x-shader/x-fragment">
precision highp float;
uniform mat4 invMatrix;
uniform vec3 lightVec ;
uniform vec4 color ;
uniform int mode ;
varying vec3 tnorm ;
varying vec2 tuv ;
void main() {
float light =clamp(dot(tnorm, normalize(invMatrix * vec4(lightVec,0.0)).xyz),0.0,1.0)*0.8+0.2;
gl_FragColor = (mode==0) ? vec4(light*color.x, light*color.y, light*color.z, 1.0):vec4(color.x,color.y,color.z,1.0);
}
</script>
<style>
body {
margin:0 ;
background-color:#888 ;
}
canvas {
width:100vw ;
height:100vw ;
}
</style>
</head>
<body>
<canvas id="screen1" ></canvas>