// NewImage creates an Image of the given size. // // Both a host-memory *image.RGBA and a GL texture are created. func NewImage(w, h int) *Image { dx := roundToPower2(w) dy := roundToPower2(h) // TODO(crawshaw): Using VertexAttribPointer we can pass texture // data with a stride, which would let us use the exact number of // pixels on the host instead of the rounded up power 2 size. m := image.NewRGBA(image.Rect(0, 0, dx, dy)) glimage.Do(glInit) img := &Image{ RGBA: m.SubImage(image.Rect(0, 0, w, h)).(*image.RGBA), Texture: gl.GenTexture(), texWidth: dx, texHeight: dy, } // TODO(crawshaw): We don't have the context on a finalizer. Find a way. // runtime.SetFinalizer(img, func(img *Image) { gl.DeleteTexture(img.Texture) }) gl.BindTexture(gl.TEXTURE_2D, img.Texture) gl.TexImage2D(gl.TEXTURE_2D, 0, dx, dy, 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 img }
func LoadTexture(name string) (tex gl.Texture, err error) { imgFile, err := asset.Open(name) if err != nil { return } img, _, err := image.Decode(imgFile) if err != nil { return } rgba := image.NewRGBA(img.Bounds()) image_draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, image_draw.Src) tex = gl.CreateTexture() gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, tex) 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.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D( gl.TEXTURE_2D, 0, rgba.Rect.Size().X, rgba.Rect.Size().Y, gl.RGBA, gl.UNSIGNED_BYTE, rgba.Pix) return }
func (video *Video) drawFrame() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(video.prog) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, video.texture) log.Print("Reading") frame := <-video.pixelBuffer buf := make([]float32, len(frame)) log.Print("Read frame") for k, v := range frame { buf[k] = float32(v) } log.Print("Writing") if video.pixelBuffer != nil { gl.TexImage2D(gl.TEXTURE_2D, 0, 256, 256, gl.RGBA, gl.UNSIGNED_SHORT_4_4_4_4, f32.Bytes(binary.LittleEndian, buf...)) } log.Print("Wrote") gl.DrawArrays(gl.TRIANGLES, 0, 6) video.fpsmanager.FramerateDelay() }
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") }
// init creates an underlying GL texture for a key. // Must be called with a valid GL context. // Must hold tm.Mutex before calling. func (tm *texmapCache) init(key texmapKey) { tex := tm.texs[key] if tex.gltex.Value != 0 { panic(fmt.Sprintf("attempting to init key (%v) with valid texture", key)) } tex.gltex = gl.CreateTexture() gl.BindTexture(gl.TEXTURE_2D, tex.gltex) gl.TexImage2D(gl.TEXTURE_2D, 0, tex.width, tex.height, 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) for _, t := range tm.toDelete { gl.DeleteTexture(t) } tm.toDelete = nil }
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 }