Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
A Novice's Guide to WebGL
Search
krzychukula
April 26, 2017
Programming
0
37
A Novice's Guide to WebGL
Introduction to 3D, shaders and WebGL.
krzychukula
April 26, 2017
Tweet
Share
More Decks by krzychukula
See All by krzychukula
Introduction to WebGL
krzychukula
1
100
Simplicity
krzychukula
0
54
Angular the Good Parts
krzychukula
0
230
Yield
krzychukula
0
78
Promises, Promises
krzychukula
5
380
Other Decks in Programming
See All in Programming
事業成長を爆速で進めてきたプロダクトエンジニアたちの成功談・失敗談
nealle
3
1.4k
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
250
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
890
Jakarta EE meets AI
ivargrimstad
0
230
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
선언형 UI에서의 상태관리
l2hyunwoo
0
130
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
720
Security_for_introducing_eBPF
kentatada
0
110
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
130
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
710
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
The Cost Of JavaScript in 2023
addyosmani
45
7k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
GitHub's CSS Performance
jonrohan
1030
460k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Visualization
eitanlees
146
15k
Mobile First: as difficult as doing things right
swwweet
222
9k
For a Future-Friendly Web
brad_frost
175
9.4k
Making Projects Easy
brettharned
116
5.9k
Transcript
A Novice’s Guide to WebGL @Krzychukula
Plan 1. Intro 2. How it all works 3. Triangle
4. Multiple colours 5. Matrix 6. Learn
ASK QUESTIONS!
None
Data Visualisation
What my learning looked like?
But this is just a hobby
OpenGL books Courses Three.js Shaders Workshop WebGL workshop WebGL Books
Disclaimer: mental models over correctness
Who this talk is for?
[-1 …. 0 …. 1]
NO SHINY DEMOS
None
None
Dreyfus model of skill acquisition
Masters of DOOM YOUme
I realised that we are all Novices!
Novice WebGL Programmer!
Let’s start
2. How it all works?
Back in the old days…
None
Graphic Card
None
Map Reduce?
None
None
What is WebGL?
History
Shaders
Vertex Fragment
Vertex Shader Fragment Shader
It has to be FAST
Vertex Shader void main() { gl_Position = vec4(0.0, 0.0, 0.0,
1.0); }
Vertex Shader attribute vec4 a_Position; void main() { gl_Position =
a_Position; }
Fragment Shader void main() { gl_FragColor = vec4(0.0, 1.0, 0.0,
1.0); }
None
Fragment Shader uniform vec4 a_Color; void main() { gl_FragColor =
a_Color; }
But… What about JS?
Context var gl = canvas.getContext("webgl");
Compile Vertex Shader var vertexShader = gl.createShader(gl.VERTEX_SHADER) gl.shaderSource(vertexShader, ` void
main() { … } `) gl.compileShader(vertexShader)
Compile Fragment Shader var fragShader = gl.createShader(gl.FRAGMENT_SHADER) gl.shaderSource(fragShader, ` void
main() { … } `) gl.compileShader(fragShader)
Program var program = gl.createProgram() gl.attachShader(program, vertexShader) gl.attachShader(program, fragmentShader) gl.linkProgram(program)
u_Color var u_Color = gl.getUniformLocation(program, ‘u_Color’); gl.uniform4fv(u_Color, [0.0, 1.0, 0.0,
1.0]);
Fragment Shader uniform vec4 u_Color; void main() { gl_FragColor =
u_Color; }
None
None
1 -1 1
None
0, 0.5
-0.5, -0.5 0, 0.5
-0.5, -0.5 0, 0.5 0.5, -0.5
gl_Position = vec4(0.0, 0.5, 0.0, 1.0); -0.5, -0.5 0, 0.5
-0.5, 0.5
gl_Position = vec4(0.0, 0.5, 0.0, 1.0); gl_Position = vec4(-0.5, -0.5,
0.0, 1.0); gl_Position = vec4(0.5, -0.5, 0.0, 1.0); -0.5, -0.5 0, 0.5 -0.5, 0.5
var positions = new Float32Array([ 0.0, 0.5, 0.0, 1.0, -0.5,
-0.5, 0.0, 1.0, 0.5, -0.5, 0.0, 1.0 ]);
Buffers
var buffer = gl.createBuffer() gl.bindBuffer(gl.ARRAY_BUFFER, buffer) gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW)
var positions = new Float32Array([ 0.0, 0.5, 0.0, 1.0, -0.5,
-0.5, 0.0, 1.0, 0.5, -0.5, 0.0, 1.0 ]);
var a_Position = gl.getAttribLocation(program, ‘a_Position') gl.enableVertexAttribArray(a_Position) gl.vertexAttribPointer(a_Position, 4, gl.FLOAT, false,
0, 0)
gl.drawArrays(gl.TRIANGLES, 0, 3)
None
Vertex Shader Fragment Shaders
None
Fragment Shader attribute vec4 a_Color; void main() { gl_FragColor =
a_Color; }
varying Vertex Shaders Fragment Shaders attributes
Vertex Shader attribute vec4 a_Position; attribute vec4 a_Color; varying vec4
v_Color; void main() { gl_Position = a_Position; v_Color = a_Color; }
Vertex Shader void main(vec4 a_Position, vec4 a_Color) { return {
gl_Position: a_Position, v_Color: a_Color }; }
v_Color = vec4(1.0, 0.0, 0.0, 1.0); v_Color = vec4(0.0, 1.0,
0.0, 1.0); v_Color = vec4(0.0, 0.0, 1.0, 1.0);
What about the middle?
None
None
Fragment Shader varying vec4 v_Color; void main() { gl_FragColor =
v_Color; }
None
var colors = new Float32Array([ 1.0, 0.0, 0.0, 1.0, 0.0,
1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0 ]);
var buffer = gl.createBuffer() gl.bindBuffer(gl.ARRAY_BUFFER, buffer) gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW)
var a_Color = gl.getAttribLocation(program, ‘a_Color') gl.enableVertexAttribArray(a_Color) gl.vertexAttribPointer(a_Position, 4, gl.FLOAT, false,
0, 0)
gl.drawArrays(gl.TRIANGLES, 0, 3)
None
Almost the end
Matrix
diffX diffX rotateAngle scaleX scaleY
Matrix
Don’t worry about Math Linear Algebra
var modelMatrix = new Matrix4() modelMatrix.translate(x, 0, 0, 1) modelMatrix.rotate(angle,
0, 0, 1) In JavaScript
Vertex Shader attribute vec4 a_Position; uniform mat4 u_modelMatrix; void main()
{ gl_Position = u_modelMatrix * a_Position; }
What to learn from?
WebGL Beginner’s Guide Diego Cantor & Brandon Jones
Interactive 3D Graphics Eric Haines & Gundega Dekena
WebGL Programming Guide Kouichi Matsuda & Rodger Lea
The End @Krzychukula
https:/ /hacks.mozilla.org/2017 /01/webgl-2-lands-in-firefox/ https:/ /www.chromeexperiments.com/globe https:/ /codepen.io/krzychukula/pen/qmZaMz?editors=0010 Triangle https:/ /codepen.io/krzychukula/pen/OmXgKe?editors=0010
Triangle 3 colors https:/ /codepen.io/krzychukula/pen/OmXgKe?editors=0010 Triangle 3 colors in one buffer https:/ /www.packtpub.com/game-development/webgl-beginners-guide https:/ /www.udacity.com/course/interactive-3d-graphics--cs291 https:/ /sites.google.com/site/webglbook/
QA (ideas) Libraries? How shadertoy works? How to use one
buffer for vertex and colors data? How textures work? How many lines of code those examples have?