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
38
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
56
Angular the Good Parts
krzychukula
0
230
Yield
krzychukula
0
81
Promises, Promises
krzychukula
5
390
Other Decks in Programming
See All in Programming
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
7
1.4k
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
940
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
1
350
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
140
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
540
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
630
Fixstars高速化コンテスト2024準優勝解法
eijirou
0
190
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
3
580
歴史と現在から考えるスケーラブルなソフトウェア開発のプラクティス
i10416
0
300
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
1.3k
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
Rubyでつくるパケットキャプチャツール
ydah
0
170
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
For a Future-Friendly Web
brad_frost
176
9.5k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
240
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
The Language of Interfaces
destraynor
155
24k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
860
How to Think Like a Performance Engineer
csswizardry
22
1.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
173
51k
Gamification - CAS2011
davidbonilla
80
5.1k
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?