Skip to content

Commit ce0c40c

Browse files
committed
fix point sprite bug and light-5
1 parent 5370aa0 commit ce0c40c

File tree

8 files changed

+72
-34
lines changed

8 files changed

+72
-34
lines changed

exercises/light-4/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,14 @@ function actual(fbo) {
124124
actualShader.bind()
125125
actualShader.uniforms = camera
126126

127+
if(actualShader.attributes.normal.location >= 0) {
128+
normalBuffer.bind()
129+
actualShader.attributes.normal.pointer()
130+
}
131+
127132
vertexBuffer.bind()
128133
actualShader.attributes.position.pointer()
129134

130-
normalBuffer.bind()
131-
actualShader.attributes.normal.pointer()
132-
133135
gl.drawArrays(gl.TRIANGLES, 0, vertexCount)
134136

135137
pointShader.bind()
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
attribute vec3 position;
12
uniform mat4 model, view, projection;
23
uniform vec3 lightPosition;
34
void main() {
4-
gl_Position = projection * view * model * vec4(lightPosition, 1);
5+
gl_Position = projection * view * model * vec4(lightPosition + 0.0 * position, 1);
56
gl_PointSize = 10.0;
67
}

exercises/light-5/files/fragment.glsl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
precision highp float;
1+
precision mediump float;
22

33
#pragma glslify: PointLight = require(./light.glsl)
44

5-
uniform mat4 model, view, projection;
6-
uniform mat4 inverseModel, inverseView, inverseProjection;
5+
uniform mat4 model;
6+
uniform mat4 view;
7+
uniform mat4 projection;
8+
9+
uniform mat4 inverseModel;
10+
uniform mat4 inverseView;
11+
uniform mat4 inverseProjection;
12+
713
uniform vec3 ambient;
14+
815
uniform PointLight lights[4];
916

1017
void main() {

exercises/light-5/files/vertex.glsl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
precision highp float;
1+
precision mediump float;
22

33
#pragma glslify: PointLight = require(./light.glsl)
44

5-
attribute vec4 position, normal;
5+
attribute vec3 position;
6+
attribute vec3 normal;
7+
8+
uniform mat4 model;
9+
uniform mat4 view;
10+
uniform mat4 projection;
11+
12+
uniform mat4 inverseModel;
13+
uniform mat4 inverseView;
14+
uniform mat4 inverseProjection;
615

7-
uniform mat4 model, view, projection;
8-
uniform mat4 inverseModel, inverseView, inverseProjection;
916
uniform vec3 ambient;
17+
1018
uniform PointLight lights[4];
1119

1220
void main() {
13-
gl_Position = position;
21+
gl_Position = vec4(position, 1);
1422
}

exercises/light-5/index.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,15 @@ window.addEventListener('resize', fit(canvas), false)
3636

3737
var vertexNormals = getNormals(dragon.cells, dragon.positions, 0.1)
3838
var vertexData = []
39+
var vertexCount = dragon.cells.length * 3
3940
for(var i=0; i<dragon.cells.length; ++i) {
4041
var loop = dragon.cells[i]
4142
for(var j=0; j<loop.length; ++j) {
4243
vertexData.push.apply(vertexData, dragon.positions[loop[j]])
4344
}
4445
}
4546
var vertexBuffer = createBuffer(gl, vertexData)
46-
var vertexArray = createVAO(gl, [
47-
{
48-
"buffer": vertexBuffer,
49-
"size": 3
50-
},
51-
{
52-
"buffer": createBuffer(gl, vertexNormals),
53-
"size": 3
54-
}
55-
])
47+
var normalBuffer = createBuffer(gl, vertexNormals)
5648

5749
var actualShader = createShader({
5850
frag: process.env.file_fragment_glsl
@@ -72,6 +64,7 @@ var pointShader = createShader({
7264
frag: './shaders/point-fragment.glsl'
7365
, vert: './shaders/point-vertex.glsl'
7466
})(gl)
67+
pointShader.attributes.position.location = 0
7568

7669
function getCamera() {
7770
var projection = mat4.perspective(
@@ -154,8 +147,15 @@ function actual(fbo) {
154147
actualShader.bind()
155148
actualShader.uniforms = camera
156149

157-
vertexArray.bind()
158-
vertexArray.draw(gl.TRIANGLES, vertexData.length / 3)
150+
if(actualShader.attributes.normal.location > 0) {
151+
normalBuffer.bind()
152+
actualShader.attributes.normal.pointer()
153+
}
154+
155+
vertexBuffer.bind()
156+
actualShader.attributes.position.pointer()
157+
158+
gl.drawArrays(gl.TRIANGLES, 0, vertexCount)
159159

160160
pointShader.bind()
161161
pointShader.uniforms.model = camera.model
@@ -164,7 +164,7 @@ function actual(fbo) {
164164
for(var i=0; i<4; ++i) {
165165
pointShader.uniforms.lightPosition = camera.lights[i].position
166166
pointShader.uniforms.diffuse = camera.lights[i].diffuse
167-
vertexArray.draw(gl.POINTS, 1)
167+
gl.drawArrays(gl.POINTS, 0, 1)
168168
}
169169
}
170170

@@ -180,16 +180,24 @@ function expected(fbo) {
180180
expectedShader.bind()
181181
expectedShader.uniforms = camera
182182

183-
vertexArray.bind()
184-
vertexArray.draw(gl.TRIANGLES, vertexData.length / 3)
183+
normalBuffer.bind()
184+
expectedShader.attributes.normal.pointer()
185+
186+
vertexBuffer.bind()
187+
expectedShader.attributes.position.pointer()
188+
189+
gl.drawArrays(gl.TRIANGLES, 0, vertexCount)
185190

186191
pointShader.bind()
187192
pointShader.uniforms.model = camera.model
188193
pointShader.uniforms.view = camera.view
189194
pointShader.uniforms.projection = camera.projection
195+
196+
vertexBuffer.bind()
197+
pointShader.attributes.position.pointer()
190198
for(var i=0; i<4; ++i) {
191199
pointShader.uniforms.lightPosition = camera.lights[i].position
192200
pointShader.uniforms.diffuse = camera.lights[i].diffuse
193-
vertexArray.draw(gl.POINTS, 1)
201+
gl.drawArrays(gl.POINTS, 0, 1)
194202
}
195203
}

exercises/light-5/shaders/fragment.glsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ precision mediump float;
55
uniform vec3 ambient;
66
uniform PointLight lights[4];
77

8-
varying vec3 fragNormal, fragPosition, lightDirection[4];
8+
varying vec3 fragNormal;
9+
varying vec3 fragPosition;
10+
varying vec3 lightDirection[4];
911

1012
void main() {
1113
vec3 eyeDirection = normalize(fragPosition);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
attribute vec3 position;
12
uniform mat4 model, view, projection;
23
uniform vec3 lightPosition;
34
void main() {
4-
gl_Position = projection * view * model * vec4(lightPosition, 1);
5+
gl_Position = projection * view * model * vec4(lightPosition + 0.0 * position, 1);
56
gl_PointSize = 10.0;
67
}

exercises/light-5/shaders/vertex.glsl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@ precision mediump float;
22

33
#pragma glslify: PointLight = require(./light.glsl)
44

5-
attribute vec3 position, normal;
5+
attribute vec3 position;
6+
attribute vec3 normal;
7+
8+
uniform mat4 model;
9+
uniform mat4 view;
10+
uniform mat4 projection;
11+
12+
uniform mat4 inverseModel;
13+
uniform mat4 inverseView;
14+
uniform mat4 inverseProjection;
615

7-
uniform mat4 model, view, projection;
8-
uniform mat4 inverseModel, inverseView, inverseProjection;
916
uniform PointLight lights[4];
1017

11-
varying vec3 fragNormal, fragPosition, lightDirection[4];
18+
varying vec3 fragNormal;
19+
varying vec3 fragPosition;
20+
varying vec3 lightDirection[4];
1221

1322
void main() {
1423
vec4 viewPosition = view * model * vec4(position, 1.0);

0 commit comments

Comments
 (0)