func start() { var err error glimage.program, err = CreateProgram(vertexShader, fragmentShader) if err != nil { panic(err) } glimage.quadXY = gl.CreateBuffer() glimage.quadUV = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, glimage.quadXY) gl.BufferData(gl.ARRAY_BUFFER, quadXYCoords, gl.STATIC_DRAW) gl.BindBuffer(gl.ARRAY_BUFFER, glimage.quadUV) gl.BufferData(gl.ARRAY_BUFFER, quadUVCoords, gl.STATIC_DRAW) glimage.pos = gl.GetAttribLocation(glimage.program, "pos") glimage.mvp = gl.GetUniformLocation(glimage.program, "mvp") glimage.uvp = gl.GetUniformLocation(glimage.program, "uvp") glimage.inUV = gl.GetAttribLocation(glimage.program, "inUV") glimage.textureSample = gl.GetUniformLocation(glimage.program, "textureSample") texmap.Lock() defer texmap.Unlock() for key, tex := range texmap.texs { texmap.init(key) tex.needsUpload = true } }
func (shape *StaticShape) Draw(shader Shader, camera Camera) { gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO) stride := shape.Stride() gl.EnableVertexAttribArray(shader.Attrib("vertCoord")) gl.VertexAttribPointer(shader.Attrib("vertCoord"), vertexDim, gl.FLOAT, false, stride, 0) if len(shape.normals) > 0 { gl.EnableVertexAttribArray(shader.Attrib("vertNormal")) gl.VertexAttribPointer(shader.Attrib("vertNormal"), normalDim, gl.FLOAT, false, stride, vertexDim*vecSize) } // TODO: texture if len(shape.indices) > 0 { gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, shape.IBO) gl.DrawElements(gl.TRIANGLES, len(shape.indices), gl.UNSIGNED_BYTE, 0) } else { gl.DrawArrays(gl.TRIANGLES, 0, shape.Len()) } gl.DisableVertexAttribArray(shader.Attrib("vertCoord")) if len(shape.normals) > 0 { gl.DisableVertexAttribArray(shader.Attrib("vertNormal")) } // TODO: texture }
func (e *Engine) Start() { var err error e.shader.program, err = LoadProgram("shader.v.glsl", "shader.f.glsl") if err != nil { panic(fmt.Sprintln("LoadProgram failed:", err)) } e.shape.buf = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, e.shape.buf) gl.BufferData(gl.ARRAY_BUFFER, cubeData, gl.STATIC_DRAW) fmt.Println(len(cubeData)) e.shader.vertCoord = gl.GetAttribLocation(e.shader.program, "vertCoord") e.shader.projection = gl.GetUniformLocation(e.shader.program, "projection") e.shader.view = gl.GetUniformLocation(e.shader.program, "view") e.shader.modelx = gl.GetUniformLocation(e.shader.program, "modelx") e.shader.modely = gl.GetUniformLocation(e.shader.program, "modely") e.shader.color = gl.GetAttribLocation(e.shader.program, "color") e.shape.colorbuf = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, e.shape.colorbuf) gl.BufferData(gl.ARRAY_BUFFER, colorData, gl.STATIC_DRAW) gl.VertexAttribPointer(e.shader.color, colorsPerVertex, gl.FLOAT, false, 4, 0) //更新color值 gl.DrawArrays(gl.TRIANGLES, 0, vertexCount) e.started = time.Now() }
func (w *windowImpl) Draw(src2dst f64.Aff3, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) { t := src.(*textureImpl) a := w.vertexAff3(sr) gl.UseProgram(w.s.texture.program) writeAff3(w.s.texture.mvp, mul(a, src2dst)) // OpenGL's fragment shaders' UV coordinates run from (0,0)-(1,1), // unlike vertex shaders' XY coordinates running from (-1,+1)-(+1,-1). // // We are drawing a rectangle PQRS, defined by two of its // corners, onto the entire texture. The two quads may actually // be equal, but in the general case, PQRS can be smaller. // // (0,0) +---------------+ (1,0) // | P +-----+ Q | // | | | | // | S +-----+ R | // (0,1) +---------------+ (1,1) // // The PQRS quad is always axis-aligned. First of all, convert // from pixel space to texture space. tw := float64(t.size.X) th := float64(t.size.Y) px := float64(sr.Min.X-0) / tw py := float64(sr.Min.Y-0) / th qx := float64(sr.Max.X-0) / tw sy := float64(sr.Max.Y-0) / th // Due to axis alignment, qy = py and sx = px. // // The simultaneous equations are: // 0 + 0 + a02 = px // 0 + 0 + a12 = py // a00 + 0 + a02 = qx // a10 + 0 + a12 = qy = py // 0 + a01 + a02 = sx = px // 0 + a11 + a12 = sy writeAff3(w.s.texture.uvp, f64.Aff3{ qx - px, 0, px, 0, sy - py, py, }) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, t.id) gl.Uniform1i(w.s.texture.sample, 0) gl.BindBuffer(gl.ARRAY_BUFFER, w.s.texture.quadXY) gl.EnableVertexAttribArray(w.s.texture.pos) gl.VertexAttribPointer(w.s.texture.pos, 2, gl.FLOAT, false, 0, 0) gl.BindBuffer(gl.ARRAY_BUFFER, w.s.texture.quadUV) gl.EnableVertexAttribArray(w.s.texture.inUV) gl.VertexAttribPointer(w.s.texture.inUV, 2, gl.FLOAT, false, 0, 0) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.DisableVertexAttribArray(w.s.texture.pos) gl.DisableVertexAttribArray(w.s.texture.inUV) }
func (e *Engine) Draw(c size.Event) { gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(0.2, 0.2, 0.2, 1) gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.DEPTH_BUFFER_BIT) gl.UseProgram(e.shader.program) m := mgl32.Perspective(0.785, float32(c.WidthPt/c.HeightPt), 0.1, 10.0) gl.UniformMatrix4fv(e.shader.projection, m[:]) eye := mgl32.Vec3{0, 0, 8} center := mgl32.Vec3{0, 0, 0} up := mgl32.Vec3{0, 1, 0} m = mgl32.LookAtV(eye, center, up) gl.UniformMatrix4fv(e.shader.view, m[:]) m = mgl32.HomogRotate3D(float32(e.touchLoc.X/c.WidthPt-0.5)*3.14*2, mgl32.Vec3{0, 1, 0}) gl.UniformMatrix4fv(e.shader.modelx, m[:]) m = mgl32.HomogRotate3D(float32(e.touchLoc.Y/c.HeightPt-0.5)*3.14, mgl32.Vec3{1, 0, 0}) gl.UniformMatrix4fv(e.shader.modely, m[:]) coordsPerVertex := 3 for _, obj := range e.shape.Objs { gl.BindBuffer(gl.ARRAY_BUFFER, obj.coord) gl.EnableVertexAttribArray(e.shader.vertCoord) gl.VertexAttribPointer(e.shader.vertCoord, coordsPerVertex, gl.FLOAT, false, 12, 0) if obj.useuv == true { gl.Uniform1i(e.shader.useuv, 1) texCoordsPerVertex := 2 gl.BindBuffer(gl.ARRAY_BUFFER, obj.uvcoord) gl.EnableVertexAttribArray(e.shader.vertTexCoord) gl.VertexAttribPointer(e.shader.vertTexCoord, texCoordsPerVertex, gl.FLOAT, false, 8, 0) gl.BindTexture(gl.TEXTURE_2D, obj.tex) } else { gl.Uniform1i(e.shader.useuv, 0) gl.Uniform4f(e.shader.color, obj.color[0], obj.color[1], obj.color[2], obj.color[3]) } gl.DrawArrays(gl.TRIANGLES, 0, obj.vcount) if obj.useuv { gl.DisableVertexAttribArray(e.shader.vertTexCoord) } gl.DisableVertexAttribArray(e.shader.vertCoord) } debug.DrawFPS(c) }
func (e *Engine) Draw(c size.Event) { gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(0.5, 0.8, 0.8, 1) gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.DEPTH_BUFFER_BIT) gl.UseProgram(e.shader.program) gl.Uniform3fv(e.shader.lightdir, []float32{0.5, 0.6, 0.7}) m := mgl32.Perspective(1.3, float32(c.WidthPt/c.HeightPt), 0.1, 10.0) gl.UniformMatrix4fv(e.shader.projectionmatrix, m[:]) eye := mgl32.Vec3{0, 0, 0.2} center := mgl32.Vec3{0, 0, 0} up := mgl32.Vec3{0, 1, 0} m = mgl32.LookAtV(eye, center, up) gl.UniformMatrix4fv(e.shader.viewmatrix, m[:]) m = mgl32.HomogRotate3D((e.touchx/float32(c.WidthPt)-0.5)*6.28, mgl32.Vec3{0, 1, 0}) gl.UniformMatrix4fv(e.shader.modelmatrix, m[:]) m = mgl32.HomogRotate3D((e.touchx/float32(c.WidthPt)-0.5)*6.28, mgl32.Vec3{0, -1, 0}) gl.UniformMatrix4fv(e.shader.lightmatrix, m[:]) coordsPerVertex := 3 for _, obj := range e.shape.Objs { gl.BindBuffer(gl.ARRAY_BUFFER, obj.coord) gl.EnableVertexAttribArray(e.shader.vertCoord) gl.VertexAttribPointer(e.shader.vertCoord, coordsPerVertex, gl.FLOAT, false, 12, 0) texCoordsPerVertex := 2 gl.BindBuffer(gl.ARRAY_BUFFER, obj.uvcoord) gl.EnableVertexAttribArray(e.shader.texcoord) gl.VertexAttribPointer(e.shader.texcoord, texCoordsPerVertex, gl.FLOAT, false, 8, 0) gl.BindBuffer(gl.ARRAY_BUFFER, obj.normal) gl.EnableVertexAttribArray(e.shader.normal) gl.VertexAttribPointer(e.shader.normal, 3, gl.FLOAT, false, 12, 0) gl.BindTexture(gl.TEXTURE_2D, obj.tex) gl.DrawArrays(gl.TRIANGLES, 0, obj.vcount) gl.DisableVertexAttribArray(e.shader.texcoord) gl.DisableVertexAttribArray(e.shader.normal) gl.DisableVertexAttribArray(e.shader.vertCoord) } debug.DrawFPS(c) }
func onPaint(sz size.Event) { gl.ClearColor(rgb(156), rgb(39), rgb(176), 1) gl.Clear(gl.COLOR_BUFFER_BIT) var rotationMatrix = []float32{ f32.Cos(-alpha), -f32.Sin(-alpha), 0.0, f32.Sin(-alpha), f32.Cos(-alpha), 0.0, 0.0, 0.0, 1.0, } gl.UseProgram(program) // setting color gl.Uniform4f(color, rgb(255), rgb(255), rgb(255), 1) gl.UniformMatrix3fv(matrixId, rotationMatrix) gl.Uniform1f(resolutionId, resIndex) gl.BindBuffer(gl.ARRAY_BUFFER, swasBuffer) gl.EnableVertexAttribArray(position) gl.VertexAttribPointer(position, 3, gl.FLOAT, false, 0, 0) gl.DrawArrays(gl.LINES, 0, 16) gl.DisableVertexAttribArray(position) gl.UseProgram(texProgram) // setting color gl.Uniform4f(color2, rgb(130), rgb(50), rgb(80), 1) gl.Uniform1f(resolutionId2, resIndex) gl.UniformMatrix3fv(matrixId2, rotationMatrix) gl.BindBuffer(gl.ARRAY_BUFFER, quadBuffer) gl.EnableVertexAttribArray(position2) gl.VertexAttribPointer(position2, 3, gl.FLOAT, false, 0, 0) gl.BindBuffer(gl.ARRAY_BUFFER, quadTexBuffer) gl.EnableVertexAttribArray(textureCoords) gl.VertexAttribPointer(textureCoords, 2, gl.FLOAT, false, 0, 0) gl.Uniform1i(gl.GetUniformLocation(texProgram, "myTexture"), 0) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, textureId) gl.DrawArrays(gl.TRIANGLES, 0, 6) gl.DisableVertexAttribArray(position2) gl.DisableVertexAttribArray(textureCoords) if spin == true { alpha += 0.1 } if alpha >= 360 { alpha = 0.0 } }
func (video *Video) initGL() { log.Print("Initing") video.fpsmanager = gfx.NewFramerate() video.fpsmanager.SetFramerate(60) gl.ClearColor(0.0, 0.0, 0.0, 1.0) gl.Enable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) log.Print("Creating program") video.prog = createProgram(vertShaderSrcDef, fragShaderSrcDef) log.Print("Attrib loc 1") posAttrib := attribLocation(video.prog, "vPosition") log.Print("Attrib loc 2") texCoordAttr := attribLocation(video.prog, "vTexCoord") log.Print("Uniform loc 1") paletteLoc := uniformLocation(video.prog, "palette") log.Print("Uniform loc 2") video.textureUni = uniformLocation(video.prog, "texture") log.Print("Gen Texture") video.texture = genTexture() gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, video.texture) log.Print("TexParam") gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.UseProgram(video.prog) gl.EnableVertexAttribArray(posAttrib) gl.EnableVertexAttribArray(texCoordAttr) gl.Uniform3iv(paletteLoc, nes.SPaletteRgb) log.Print("VertBO") vertVBO := genBuffer() checkGLError() gl.BindBuffer(gl.ARRAY_BUFFER, vertVBO) verts := f32.Bytes(binary.LittleEndian, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0) gl.BufferData(gl.ARRAY_BUFFER, verts, gl.STATIC_DRAW) textCoorBuf := genBuffer() checkGLError() gl.BindBuffer(gl.ARRAY_BUFFER, textCoorBuf) texVerts := f32.Bytes(binary.LittleEndian, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0) gl.BufferData(gl.ARRAY_BUFFER, texVerts, gl.STATIC_DRAW) gl.VertexAttribPointer(posAttrib, 2, gl.FLOAT, false, 0, 0) gl.VertexAttribPointer(texCoordAttr, 2, gl.FLOAT, false, 0, 0) log.Print("Started") }
func (shape *StaticShape) Buffer() { data := shape.Bytes() if len(data) > 0 { gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO) gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW) } if len(shape.indices) > 0 { data = EncodeObjects(0, len(shape.indices), NewDimSlice(1, shape.indices)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, shape.IBO) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, data, gl.STATIC_DRAW) } }
func (e *Engine) Start() { var err error e.shader.program, err = LoadProgram("shader.v.glsl", "shader.f.glsl") if err != nil { panic(fmt.Sprintln("LoadProgram failed:", err)) } e.shader.models, err = wavefront.Read("spiritframe.obj") check(err) e.shader.projectionmatrix = gl.GetUniformLocation(e.shader.program, "u_projectionMatrix") e.shader.viewmatrix = gl.GetUniformLocation(e.shader.program, "u_viewMatrix") e.shader.modelmatrix = gl.GetUniformLocation(e.shader.program, "u_modelMatrix") e.shader.normalmatrix = gl.GetUniformLocation(e.shader.program, "u_normalMatrix") e.shader.lightdir = gl.GetUniformLocation(e.shader.program, "u_lightDirection") e.shader.lightmatrix = gl.GetUniformLocation(e.shader.program, "u_lightmatrix") e.shader.vertCoord = gl.GetAttribLocation(e.shader.program, "a_vertex") e.shader.normal = gl.GetAttribLocation(e.shader.program, "a_normal") e.shader.texcoord = gl.GetAttribLocation(e.shader.program, "a_texCoord") for _, model := range e.shader.models { for _, group := range model.Groups { //颜色 color := group.Material.Ambient //顶点 data := f32.Bytes(binary.LittleEndian, group.Vertexes...) vertexCount := len(group.Vertexes) / 3 databuf := gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, databuf) gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW) //UV坐标 textcoords := f32.Bytes(binary.LittleEndian, group.Textcoords...) uvbuf := gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, uvbuf) gl.BufferData(gl.ARRAY_BUFFER, textcoords, gl.STATIC_DRAW) //发现坐标 normals := f32.Bytes(binary.LittleEndian, group.Normals...) normalbuf := gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, normalbuf) gl.BufferData(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW) tex, _ := LoadTexture(group.Material.Texturefile) e.shape.Objs = append(e.shape.Objs, Obj{vcount: vertexCount, coord: databuf, color: color, uvcoord: uvbuf, tex: tex, normal: normalbuf}) } } }
func (e *Engine) Start() { var err error e.shader.program, err = LoadProgram("shader.v.glsl", "shader.f.glsl") if err != nil { panic(fmt.Sprintln("LoadProgram failed:", err)) } e.shader.models, err = wavefront.Read("girl.obj") check(err) e.shader.vertCoord = gl.GetAttribLocation(e.shader.program, "vertCoord") e.shader.vertTexCoord = gl.GetAttribLocation(e.shader.program, "vertTexCoord") e.shader.projection = gl.GetUniformLocation(e.shader.program, "projection") e.shader.view = gl.GetUniformLocation(e.shader.program, "view") e.shader.modelx = gl.GetUniformLocation(e.shader.program, "modelx") e.shader.modely = gl.GetUniformLocation(e.shader.program, "modely") e.shader.color = gl.GetUniformLocation(e.shader.program, "color") e.shader.useuv = gl.GetUniformLocation(e.shader.program, "useuv") for _, model := range e.shader.models { for _, group := range model.Groups { //颜色 color := group.Material.Ambient //顶点 data := f32.Bytes(binary.LittleEndian, group.Vertexes...) vertexCount := len(group.Vertexes) / 3 databuf := gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, databuf) gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW) //UV坐标 textcoords := f32.Bytes(binary.LittleEndian, group.Textcoords...) uvbuf := gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, uvbuf) gl.BufferData(gl.ARRAY_BUFFER, textcoords, gl.STATIC_DRAW) //贴图文件 var useuv bool tex, err := LoadTexture(group.Material.Texturefile) if err != nil { useuv = false } else { useuv = true } e.shape.Objs = append(e.shape.Objs, Obj{vcount: vertexCount, coord: databuf, color: color, useuv: useuv, uvcoord: uvbuf, tex: tex}) } } }
func draw() { if program.Value == 0 { initGL() initCL() } if numPlatforms == 0 { gl.ClearColor(1, 0, 0, 1) } else { gl.ClearColor(0, 1, 0, 1) } gl.Clear(gl.COLOR_BUFFER_BIT) gl.UseProgram(program) blue += 0.01 if blue > 1 { blue = 0 } gl.Uniform4f(color, 0, 0, blue, 1) gl.Uniform2f(offset, float32(touchLoc.X/geom.Width), float32(touchLoc.Y/geom.Height)) gl.BindBuffer(gl.ARRAY_BUFFER, buf) gl.EnableVertexAttribArray(position) gl.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0) gl.DrawArrays(gl.TRIANGLES, 0, vertexCount) gl.DisableVertexAttribArray(position) debug.DrawFPS() }
func onPaint(c size.Event) { gl.ClearColor(0, 0.3, 0.3, 1) gl.Clear(gl.COLOR_BUFFER_BIT) gl.UseProgram(program) green += direction if green > 1 { green = 1 direction = -direction } if green < 0.4 { green = 0.4 direction = -direction } gl.Uniform4f(color, 0, green, 0, 1) gl.Uniform2f(offset, touchX/float32(c.WidthPx), touchY/float32(c.HeightPx)) gl.BindBuffer(gl.ARRAY_BUFFER, buf) gl.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW) // ? gl.EnableVertexAttribArray(position) gl.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0) gl.DrawArrays(gl.TRIANGLES, 0, vertexCount) gl.DisableVertexAttribArray(position) debug.DrawFPS(c) }
func onPaint(c size.Event) { //清场 gl.ClearColor(1, 1, 1, 1) //设置背景颜色 gl.Clear(gl.COLOR_BUFFER_BIT) //使用program gl.UseProgram(program) gl.Uniform4f(color, 0, 0.5, 0.8, 1) //设置color对象值,设置4个浮点数. //offset有两个值X,Y,窗口左上角为(0,0),右下角为(1,1) //gl.Uniform4f(offset,5.0,1.0,1.0,1.0 ) //gl.Uniform2f(offset,offsetx,offsety )//为2参数的uniform变量赋值 //log.Println("offset:",offsetx,offsety, 0, 0) gl.UniformMatrix4fv(scan, []float32{ float32(touchLoc.X/c.WidthPt*4 - 2), 0, 0, 0, 0, float32(touchLoc.Y/c.HeightPt*4 - 2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, }) gl.BindBuffer(gl.ARRAY_BUFFER, buf) gl.EnableVertexAttribArray(position) /*glVertexAttribPointer 指定了渲染时索引值为 index 的顶点属性数组的数据格式和位置。调用gl.vertexAttribPointer()方法,把顶点着色器中某个属性相对应的通用属性索引连接到绑定的webGLBUffer对象上。 index 指定要修改的顶点属性的索引值 size 指定每个顶点属性的组件数量。必须为1、2、3或者4。初始值为4。(如position是由3个(x,y,z)组成,而颜色是4个(r,g,b,a)) type 指定数组中每个组件的数据类型。可用的符号常量有GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT,GL_UNSIGNED_SHORT, GL_FIXED, 和 GL_FLOAT,初始值为GL_FLOAT。 normalized 指定当被访问时,固定点数据值是否应该被归一化(GL_TRUE)或者直接转换为固定点值(GL_FALSE)。 stride 指定连续顶点属性之间的偏移量。如果为0,那么顶点属性会被理解为:它们是紧密排列在一起的。初始值为0。 pointer 指定第一个组件在数组的第一个顶点属性中的偏移量。该数组与GL_ARRAY_BUFFER绑定,储存于缓冲区中。初始值为0; */ gl.VertexAttribPointer(position, coordsPerVertex, gl.FLOAT, false, 0, 0) //更新position值 gl.DrawArrays(gl.TRIANGLES, 0, vertexCount) gl.DisableVertexAttribArray(position) debug.DrawFPS(c) }
func (e *Engine) Start() { var err error e.shader.program, err = LoadProgram("shader.v.glsl", "shader.f.glsl") if err != nil { panic(fmt.Sprintln("LoadProgram failed:", err)) } e.shader.models, err = wavefront.Read("gopher.obj") check(err) e.shader.vertCoord = gl.GetAttribLocation(e.shader.program, "vertCoord") e.shader.projection = gl.GetUniformLocation(e.shader.program, "projection") e.shader.view = gl.GetUniformLocation(e.shader.program, "view") e.shader.modelx = gl.GetUniformLocation(e.shader.program, "modelx") e.shader.modely = gl.GetUniformLocation(e.shader.program, "modely") e.shader.color = gl.GetUniformLocation(e.shader.program, "color") for _, model := range e.shader.models { for _, group := range model.Groups { data := f32.Bytes(binary.LittleEndian, group.Vertexes...) color := group.Material.Ambient vertexCount := len(data) databuf := gl.CreateBuffer() e.shape.bufs = append(e.shape.bufs, Buf{vertexCount, databuf, color}) gl.BindBuffer(gl.ARRAY_BUFFER, databuf) gl.BufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW) } } }
func onStart() { var err error program, err = glutil.CreateProgram(vertexShader, fragmentShader) if err != nil { log.Printf("error creating GL program: %v", err) return } //创建一个WebGLBuffer对象,把它绑定到顶点缓冲上,并把顶点数据载入到顶点冲。 buf = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, buf) gl.BufferData(gl.ARRAY_BUFFER, lineData, gl.STATIC_DRAW) /*opengl中三种变量 uniform变量是外部application程序传递给(vertex和fragment)shader的变量。因此它是application通过函数glUniform**()函数赋值的。 在(vertex和fragment)shader程序内部,uniform变量就像是C语言里面的常量(const ),它不能被shader程序修改。(shader只能用,不能改) attribute变量是只能在vertex shader中使用的变量。(它不能在fragment shader中声明attribute变量,也不能被fragment shader中使用) 一般用attribute变量来表示一些顶点的数据,如:顶点坐标,法线,纹理坐标,顶点颜色等。 在application中,一般用函数glBindAttribLocation()来绑定每个attribute变量的位置,然后用函数glVertexAttribPointer()为每个attribute变量赋值。 varying变量是vertex和fragment shader之间做数据传递用的。一般vertex shader修改varying变量的值,然后fragment shader使用该varying变量的值。 因此varying变量在vertex和fragment shader二者之间的声明必须是一致的。application不能使用此变量。 */ position = gl.GetAttribLocation(program, "position") //获取位置对象(索引) color = gl.GetUniformLocation(program, "color") // 获取颜色对象(索引) scan = gl.GetUniformLocation(program, "scan") // 获取偏移对象(索引) // fmt.Println(position.String(),color.String(),offset.String())//Attrib(0) Uniform(1) Uniform(0) // TODO(crawshaw): the debug package needs to put GL state init here // Can this be an event.Register call now?? }
func (e *Engine) Start() { var err error e.shader.program, err = LoadProgram("shader.v.glsl", "shader.f.glsl") if err != nil { panic(fmt.Sprintln("LoadProgram failed:", err)) } e.shape.buf = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, e.shape.buf) gl.BufferData(gl.ARRAY_BUFFER, EncodeObject(cubeData), gl.STATIC_DRAW) e.shader.vertCoord = gl.GetAttribLocation(e.shader.program, "vertCoord") e.shader.vertTexCoord = gl.GetAttribLocation(e.shader.program, "vertTexCoord") e.shader.projection = gl.GetUniformLocation(e.shader.program, "projection") e.shader.view = gl.GetUniformLocation(e.shader.program, "view") e.shader.model = gl.GetUniformLocation(e.shader.program, "model") e.shape.texture, err = LoadTexture("gopher.png") if err != nil { panic(fmt.Sprintln("LoadTexture failed:", err)) } e.started = time.Now() }
func (emitter *particleEmitter) Buffer() { data := emitter.Bytes() if len(data) > 0 { gl.BindBuffer(gl.ARRAY_BUFFER, emitter.VBO) gl.BufferSubData(gl.ARRAY_BUFFER, 0, data) } }
func (shape *DynamicShape) Buffer(offset int) { gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO) data := shape.BytesOffset(offset) if len(data) == 0 { return } gl.BufferSubData(gl.ARRAY_BUFFER, offset*shape.Stride(), data) }
func (shape *Line) Buffer(offset int) { data := shape.BytesOffset(offset) if len(data) == 0 { return } gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO) gl.BufferSubData(gl.ARRAY_BUFFER, lineEmittedVertices*offset*shape.Stride(), data) }
func NewDynamicShape(bufSize int) *DynamicShape { shape := &DynamicShape{} shape.VBO = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO) gl.BufferInit(gl.ARRAY_BUFFER, bufSize, gl.DYNAMIC_DRAW) return shape }
func onStart() { gl.Enable(gl.BLEND) gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) texture := loadImages("495.png") textureId = gl.CreateTexture() gl.BindTexture(gl.TEXTURE_2D, textureId) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexImage2D(gl.TEXTURE_2D, 0, texture.Rect.Size().X, texture.Rect.Size().Y, gl.RGBA, gl.UNSIGNED_BYTE, texture.Pix) // loading Shaders & linking programs program = createProgram("vShader.vs", "fShader.vs") texProgram = createProgram("vTexShader.vs", "fTexShader.vs") quadBuffer = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, quadBuffer) gl.BufferData(gl.ARRAY_BUFFER, quadData, gl.STATIC_DRAW) quadTexBuffer = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, quadTexBuffer) gl.BufferData(gl.ARRAY_BUFFER, quadTexData, gl.STATIC_DRAW) swasBuffer = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, swasBuffer) gl.BufferData(gl.ARRAY_BUFFER, swastikaData, gl.STATIC_DRAW) position = gl.GetAttribLocation(program, "position") color = gl.GetUniformLocation(program, "color") matrixId = gl.GetUniformLocation(program, "rotationMatrix") resolutionId = gl.GetUniformLocation(program, "resIndex") position2 = gl.GetAttribLocation(texProgram, "position") textureCoords = gl.GetAttribLocation(texProgram, "texCoords") matrixId2 = gl.GetUniformLocation(texProgram, "rotationMatrix") resolutionId2 = gl.GetUniformLocation(texProgram, "resIndex") color2 = gl.GetUniformLocation(texProgram, "color") }
func (emitter *particleEmitter) Draw(shader Shader, camera Camera) { gl.BindBuffer(gl.ARRAY_BUFFER, emitter.VBO) gl.EnableVertexAttribArray(shader.Attrib("vertCoord")) gl.VertexAttribPointer(shader.Attrib("vertCoord"), vertexDim, gl.FLOAT, false, emitter.Stride(), 0) gl.DrawArrays(gl.TRIANGLES, 0, emitter.Len()*particleLen/vertexDim) gl.DisableVertexAttribArray(shader.Attrib("vertCoord")) }
func (e *Engine) Draw(c size.Event) { gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(0, 0, 0, 1) gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.DEPTH_BUFFER_BIT) gl.UseProgram(e.shader.program) m := mgl32.Perspective(0.785, float32(c.WidthPt/c.HeightPt), 0.1, 10.0) gl.UniformMatrix4fv(e.shader.projection, m[:]) eye := mgl32.Vec3{0, 3, 3} center := mgl32.Vec3{0, 0, 0} up := mgl32.Vec3{0, 1, 0} m = mgl32.LookAtV(eye, center, up) gl.UniformMatrix4fv(e.shader.view, m[:]) m = mgl32.HomogRotate3D(float32(e.touchLocX*5/c.WidthPt), mgl32.Vec3{0, 1, 0}) gl.UniformMatrix4fv(e.shader.modelx, m[:]) m = mgl32.HomogRotate3D(float32(e.touchLocY*5/c.HeightPt), mgl32.Vec3{1, 0, 0}) gl.UniformMatrix4fv(e.shader.modely, m[:]) gl.BindBuffer(gl.ARRAY_BUFFER, e.shape.buf) gl.EnableVertexAttribArray(e.shader.vertCoord) gl.VertexAttribPointer(e.shader.vertCoord, coordsPerVertex, gl.FLOAT, false, 0, 0) gl.BindBuffer(gl.ARRAY_BUFFER, e.shape.colorbuf) gl.EnableVertexAttribArray(e.shader.color) gl.VertexAttribPointer(e.shader.color, colorsPerVertex, gl.FLOAT, false, 0, 0) //更新color值 gl.DrawArrays(gl.TRIANGLES, 0, vertexCount) gl.DisableVertexAttribArray(e.shader.vertCoord) gl.DisableVertexAttribArray(e.shader.color) debug.DrawFPS(c) }
func glInit() { var err error glimage.program, err = CreateProgram(vertexShader, fragmentShader) if err != nil { panic(err) } glimage.quadXY = gl.GenBuffer() glimage.quadUV = gl.GenBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, glimage.quadXY) gl.BufferData(gl.ARRAY_BUFFER, gl.STATIC_DRAW, quadXYCoords) gl.BindBuffer(gl.ARRAY_BUFFER, glimage.quadUV) gl.BufferData(gl.ARRAY_BUFFER, gl.STATIC_DRAW, quadUVCoords) glimage.pos = gl.GetAttribLocation(glimage.program, "pos") glimage.mvp = gl.GetUniformLocation(glimage.program, "mvp") glimage.uvp = gl.GetUniformLocation(glimage.program, "uvp") glimage.inUV = gl.GetAttribLocation(glimage.program, "inUV") glimage.textureSample = gl.GetUniformLocation(glimage.program, "textureSample") }
func (line *Line) Reset() { gl.BindBuffer(gl.ARRAY_BUFFER, line.VBO) gl.BufferInit(gl.ARRAY_BUFFER, line.bufSize, gl.DYNAMIC_DRAW) line.height = 1.0 line.step = 3.0 // Per second line.angle = 0 line.angleBuffer = 0 line.offset = 0 line.direction = mgl.Vec3{1, 0, 1} // angle=0 line.position = mgl.Vec3{0, 0, 0} line.segments = []mgl.Vec3{line.position} }
func (s *screenImpl) NewTexture(size image.Point) (screen.Texture, error) { s.mu.Lock() defer s.mu.Unlock() if !gl.IsProgram(s.texture.program) { p, err := compileProgram(textureVertexSrc, textureFragmentSrc) if err != nil { return nil, err } s.texture.program = p s.texture.pos = gl.GetAttribLocation(p, "pos") s.texture.mvp = gl.GetUniformLocation(p, "mvp") s.texture.uvp = gl.GetUniformLocation(p, "uvp") s.texture.inUV = gl.GetAttribLocation(p, "inUV") s.texture.sample = gl.GetUniformLocation(p, "sample") s.texture.quadXY = gl.CreateBuffer() s.texture.quadUV = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, s.texture.quadXY) gl.BufferData(gl.ARRAY_BUFFER, quadXYCoords, gl.STATIC_DRAW) gl.BindBuffer(gl.ARRAY_BUFFER, s.texture.quadUV) gl.BufferData(gl.ARRAY_BUFFER, quadUVCoords, gl.STATIC_DRAW) } t := &textureImpl{ id: gl.CreateTexture(), size: size, } gl.BindTexture(gl.TEXTURE_2D, t.id) gl.TexImage2D(gl.TEXTURE_2D, 0, size.X, size.Y, gl.RGBA, gl.UNSIGNED_BYTE, nil) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) return t, nil }
func (shape *Skybox) Draw(camera Camera) { shader := shape.shader gl.DepthFunc(gl.LEQUAL) gl.DepthMask(false) projection, view := camera.Projection(), camera.View().Mat3().Mat4() gl.UniformMatrix4fv(shader.Uniform("projection"), projection[:]) gl.UniformMatrix4fv(shader.Uniform("view"), view[:]) gl.BindTexture(gl.TEXTURE_CUBE_MAP, shape.Texture) gl.BindBuffer(gl.ARRAY_BUFFER, shape.VBO) gl.EnableVertexAttribArray(shader.Attrib("vertCoord")) gl.VertexAttribPointer(shader.Attrib("vertCoord"), vertexDim, gl.FLOAT, false, shape.Stride(), 0) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, shape.IBO) gl.DrawElements(gl.TRIANGLES, len(shape.indices), gl.UNSIGNED_BYTE, 0) gl.DisableVertexAttribArray(shader.Attrib("vertCoord")) gl.DepthMask(true) gl.DepthFunc(gl.LESS) }
func ParticleEmitter(origin mgl.Vec3, num int, rate float32) Emitter { bufSize := num * particleLen * vecSize vbo := gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.BufferInit(gl.ARRAY_BUFFER, bufSize, gl.DYNAMIC_DRAW) return &particleEmitter{ VBO: vbo, origin: origin, rate: rate, particles: make([]*particle, 0, num), num: num, } }
func (w *windowImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) { if !gl.IsProgram(w.s.fill.program) { p, err := compileProgram(fillVertexSrc, fillFragmentSrc) if err != nil { // TODO: initialize this somewhere else we can better handle the error. panic(err.Error()) } w.s.fill.program = p w.s.fill.pos = gl.GetAttribLocation(p, "pos") w.s.fill.mvp = gl.GetUniformLocation(p, "mvp") w.s.fill.color = gl.GetUniformLocation(p, "color") w.s.fill.quadXY = gl.CreateBuffer() gl.BindBuffer(gl.ARRAY_BUFFER, w.s.fill.quadXY) gl.BufferData(gl.ARRAY_BUFFER, quadXYCoords, gl.STATIC_DRAW) } gl.UseProgram(w.s.fill.program) writeAff3(w.s.fill.mvp, w.vertexAff3(dr)) r, g, b, a := src.RGBA() gl.Uniform4f( w.s.fill.color, float32(r)/65535, float32(g)/65535, float32(b)/65535, float32(a)/65535, ) gl.BindBuffer(gl.ARRAY_BUFFER, w.s.fill.quadXY) gl.EnableVertexAttribArray(w.s.fill.pos) gl.VertexAttribPointer(w.s.fill.pos, 2, gl.FLOAT, false, 0, 0) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.DisableVertexAttribArray(w.s.fill.pos) }