1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gg: revamp the Context pipeline for more effects, implement an additive effect (#16394)

This commit is contained in:
Junko
2022-11-12 15:39:18 +08:00
committed by GitHub
parent 7d31473523
commit 5bb058e35b
8 changed files with 227 additions and 43 deletions

134
examples/gg/additive.v Normal file
View File

@ -0,0 +1,134 @@
module main
import os
import gg
import gx
import math
[heap]
pub struct Window {
pub mut:
ctx &gg.Context = unsafe { 0 }
image gg.Image
}
pub fn (mut window Window) init(_ voidptr) {
logo_path := os.resource_abs_path(os.join_path('..', 'assets', 'logo.png'))
window.image = window.ctx.create_image(logo_path)
}
pub fn (mut window Window) draw(_ voidptr) {
window.ctx.begin()
window.ctx.draw_image_with_config(
img: &window.image
img_id: window.image.id
img_rect: gg.Rect{
x: 400 - window.image.width / 2
y: 300 - window.image.height / 2
width: window.image.width
height: window.image.height
}
// effect: .alpha <-- this can be omitted completely as it is alpha by default.
)
// Red
window.ctx.draw_image_with_config(
img: &window.image
img_id: window.image.id
img_rect: gg.Rect{
x: (400 - window.image.width / 2) + f32(math.sin(f32(window.ctx.frame) / 10.0) * 60)
y: (300 - window.image.height / 2) + f32(math.cos(f32(window.ctx.frame) / 10.0) * 60)
width: window.image.width
height: window.image.height
}
color: gx.Color{255, 0, 0, 255}
effect: .add
)
// Green
window.ctx.draw_image_with_config(
img: &window.image
img_id: window.image.id
img_rect: gg.Rect{
x: (400 - window.image.width / 2) + f32(math.sin(f32(window.ctx.frame) / 10.0) * 80)
y: (300 - window.image.height / 2) + f32(math.cos(f32(window.ctx.frame) / 10.0) * 80)
width: window.image.width
height: window.image.height
}
color: gx.Color{0, 255, 0, 255}
effect: .add
)
// Blue
window.ctx.draw_image_with_config(
img: &window.image
img_id: window.image.id
img_rect: gg.Rect{
x: (400 - window.image.width / 2) + f32(math.sin(f32(window.ctx.frame) / 10.0) * 100)
y: (300 - window.image.height / 2) + f32(math.cos(f32(window.ctx.frame) / 10.0) * 100)
width: window.image.width
height: window.image.height
}
color: gx.Color{0, 0, 255, 255}
effect: .add
)
// More examples
window.ctx.draw_image_with_config(
img: &window.image
img_id: window.image.id
img_rect: gg.Rect{
x: 50
y: 0
width: window.image.width
height: window.image.height
}
color: gx.Color{255, 0, 0, 255}
effect: .add
)
window.ctx.draw_image_with_config(
img: &window.image
img_id: window.image.id
img_rect: gg.Rect{
x: 50
y: 50
width: window.image.width
height: window.image.height
}
color: gx.Color{0, 255, 0, 255}
effect: .add
)
window.ctx.draw_image_with_config(
img: &window.image
img_id: window.image.id
img_rect: gg.Rect{
x: 50
y: 100
width: window.image.width
height: window.image.height
}
color: gx.Color{0, 0, 255, 255}
effect: .add
)
window.ctx.end()
}
fn main() {
mut window := &Window{}
window.ctx = gg.new_context(
width: 800
height: 600
user_data: window
bg_color: gx.gray
// FNs
init_fn: window.init
frame_fn: window.draw
)
window.ctx.run()
}

View File

@ -16,7 +16,7 @@ pub fn (mut window Window) init() {
pub fn (mut window Window) draw() {
angle := f32(window.ctx.frame) / 64 // since window.ctx.frame is increased by 1 on every frame -> the angle will be increasing too
window.ctx.begin()
sgl.load_pipeline(window.ctx.timage_pip)
sgl.load_pipeline(window.ctx.pipeline.alpha)
sgl.translate(400, 400, 0) // center of the screen
sgl.rotate(angle, 0.0, 0.0, 1.0) // rotate around the Z axis pointing towards the camera