Skip to content

Commit 5370aa0

Browse files
committed
fix bug with attributes causing light-4 not to display
1 parent 0814363 commit 5370aa0

File tree

5 files changed

+69
-37
lines changed

5 files changed

+69
-37
lines changed

exercises/light-4/files/fragment.glsl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
precision highp float;
1+
precision mediump float;
2+
3+
uniform mat4 model;
4+
uniform mat4 view;
5+
uniform mat4 projection;
6+
7+
uniform mat4 inverseModel;
8+
uniform mat4 inverseView;
9+
uniform mat4 inverseProjection;
10+
11+
uniform vec3 ambient;
12+
uniform vec3 diffuse;
13+
uniform vec3 specular;
14+
15+
uniform vec3 lightPosition;
216

3-
uniform mat4 model, view, projection;
4-
uniform mat4 inverseModel, inverseView, inverseProjection;
5-
uniform vec3 ambient, diffuse, specular, lightPosition, eyeDirection;
617
uniform float shininess;
718

819
void main() {

exercises/light-4/files/vertex.glsl

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

3-
attribute vec4 position, normal;
3+
attribute vec3 position;
4+
attribute vec3 normal;
45

5-
uniform mat4 model, view, projection;
6-
uniform mat4 inverseModel, inverseView, inverseProjection;
7-
uniform vec3 ambient, diffuse, specular, lightPosition;
8-
uniform float shininess;
6+
uniform mat4 model;
7+
uniform mat4 view;
8+
uniform mat4 projection;
9+
10+
uniform mat4 inverseModel;
11+
uniform mat4 inverseView;
12+
uniform mat4 inverseProjection;
13+
14+
uniform vec3 lightPosition;
915

1016
void main() {
11-
gl_Position = position;
17+
gl_Position = vec4(position, 1);
1218
}

exercises/light-4/index.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ require('../common')({
3535
window.addEventListener('resize', fit(canvas), false)
3636

3737
var vertexNormals = getNormals(dragon.cells, dragon.positions, 0.1)
38+
var vertexCount = dragon.cells.length * 3
3839
var vertexData = []
3940
for(var i=0; i<dragon.cells.length; ++i) {
4041
var loop = dragon.cells[i]
@@ -43,30 +44,17 @@ for(var i=0; i<dragon.cells.length; ++i) {
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
5951
, vert: process.env.file_vertex_glsl
6052
})(gl)
61-
actualShader.attributes.position.location = 0
62-
actualShader.attributes.normal.location = 1
6353

6454
var expectedShader = createShader({
6555
frag: './shaders/fragment.glsl'
6656
, vert: './shaders/vertex.glsl'
6757
})(gl)
68-
expectedShader.attributes.position.location = 0
69-
expectedShader.attributes.normal.location = 1
7058

7159
var pointShader = createShader({
7260
frag: './shaders/point-fragment.glsl'
@@ -136,12 +124,17 @@ function actual(fbo) {
136124
actualShader.bind()
137125
actualShader.uniforms = camera
138126

139-
vertexArray.bind()
140-
vertexArray.draw(gl.TRIANGLES, vertexData.length / 3)
127+
vertexBuffer.bind()
128+
actualShader.attributes.position.pointer()
129+
130+
normalBuffer.bind()
131+
actualShader.attributes.normal.pointer()
132+
133+
gl.drawArrays(gl.TRIANGLES, 0, vertexCount)
141134

142135
pointShader.bind()
143136
pointShader.uniforms = camera
144-
vertexArray.draw(gl.POINTS, 1)
137+
gl.drawArrays(gl.POINTS, 0, 1)
145138
}
146139

147140
function expected(fbo) {
@@ -156,10 +149,15 @@ function expected(fbo) {
156149
expectedShader.bind()
157150
expectedShader.uniforms = camera
158151

159-
vertexArray.bind()
160-
vertexArray.draw(gl.TRIANGLES, vertexData.length / 3)
152+
vertexBuffer.bind()
153+
expectedShader.attributes.position.pointer()
154+
155+
normalBuffer.bind()
156+
expectedShader.attributes.normal.pointer()
157+
158+
gl.drawArrays(gl.TRIANGLES, 0, vertexCount)
161159

162160
pointShader.bind()
163161
pointShader.uniforms = camera
164-
vertexArray.draw(gl.POINTS, 1)
162+
gl.drawArrays(gl.POINTS, 0, 1)
165163
}

exercises/light-4/shaders/fragment.glsl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
precision mediump float;
22

3-
uniform vec3 ambient, diffuse, specular, lightPosition;
3+
uniform vec3 ambient;
4+
uniform vec3 diffuse;
5+
uniform vec3 specular;
6+
uniform vec3 lightPosition;
7+
48
uniform float shininess;
5-
varying vec3 fragNormal, fragPosition, lightDirection;
9+
10+
varying vec3 fragNormal;
11+
varying vec3 fragPosition;
12+
varying vec3 lightDirection;
613

714
void main() {
815
vec3 eyeDirection = normalize(fragPosition);

exercises/light-4/shaders/vertex.glsl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
precision mediump float;
22

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

5-
uniform mat4 model, view, projection;
6-
uniform mat4 inverseModel, inverseView, inverseProjection;
714
uniform vec3 lightPosition;
8-
varying vec3 fragNormal, fragPosition, lightDirection;
15+
16+
varying vec3 fragNormal;
17+
varying vec3 fragPosition;
18+
varying vec3 lightDirection;
919

1020
void main() {
1121
vec4 viewPosition = view * model * vec4(position, 1.0);

0 commit comments

Comments
 (0)