美文网首页
WebGL基础学习

WebGL基础学习

作者: Coding_Life | 来源:发表于2019-09-25 11:35 被阅读0次
WebGL工作原理

step1:create vertexData
step2:create buffer and load vertexData to buffer

const buffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, buffer) 
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexData), gl.STATIC_DRAW)

step3: create vertex shader and fragment shader, create a program and attach shaders to the program

const vertexShader = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vertexShader, `
attribute vec3 position;
void main() {
  gl_Position = vec4(position, 1);
}
`)
// compiles a GLSL shader into binary data so that it can be used by a  WebGLProgram.
gl.compileShader(vertexShader)

const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(fragmentShader,`
void main() {
  gl_FragColor = vec4(1, 0, 0, 1);
}
`)
gl.compileShader(fragmentShader)

const program = gl.createProgram()
gl.attachShader(program, vertexShader)
gl.attachShader(program, fragmentShader)
gl.linkProgram(program)

step4:enable vertex attributes

//returns the location of an attribute variable in a given WebGLProgram ( get the index )
const positionLocation = gl.getAttribLocation(program, `position`)
//turns on the generic vertex attribute array at the specified index into the list of attribute arrays.
gl.enableVertexAttribArray(positionLocation) // positionLocation is a index present position attribute.
//binds the buffer currently bound to gl.ARRAY_BUFFER to a generic vertex attribute of the current vertex buffer object and specifies its layout.
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0)

step5:draw

gl.useProgram(program)
gl.drawArrays(gl.TRIANGLES, 0, 3)

相关文章

  • WebGL基础学习

    step1:create vertexDatastep2:create buffer and load verte...

  • webgl

    WebGL 理论基础[https://webglfundamentals.org/webgl/lessons/zh...

  • WebGL 基础

    webgl 场景创建 渲染render (scene, camera)setViewport() 场景 相机cam...

  • WebGL学习(2) - 3D场景

      原文地址:WebGL学习(2) - 3D场景  经过前面WebGL学习(1) - 三角形的学习,我们已经掌握了...

  • WebGL学习笔记(一)

    WebGL学习笔记(一) 一个最简单的webgl程序 *引入的js文件是简单的webgl辅助函数 * 程序中有一段...

  • webgl基础进阶

    上一章介绍了webgl的入门级基础,这次打算讲一点稍微深一点层次的基础,当然是基础了,在写本文的时候,本人也...

  • webgl 基础入门

    你既然来到这里了,肯定是在看web前端构建3d相关内容了。本人也是一个菜鸟,所以在学习webgl相关内容时整...

  • WebGL基础介绍

    谈起WebGL可能有一些人比较陌生,实际上WebGL是一种3D绘图标准,这种绘图技术标准允许把JavaScript...

  • webgl基础图形

    WebGL的工作是绘制图形图像,了解WebGL的图形是至关重要的。 一、点 在WebGL中点是一个正方形,用一个三...

  • WebGL学习笔记--WebGL入门

    什么是WebGL?全称是Web Graphics Library,是一个javascript API,用于在支持的...

网友评论

      本文标题:WebGL基础学习

      本文链接:https://www.haomeiwen.com/subject/dhprtctx.html