myou-engine/tests/texture.nim

74 lines
1.9 KiB
Nim

import myou_engine
# Create engine, scene, camera
let engine = newMyouEngine(1024, 768)
let scene = engine.newScene()
let cam = scene.newCamera() # this sets the active camera if there's none
scene.background_color = vec4(0.1, 0.1, 0.1, 1)
# Move the camera upwards (the default rotation looks down)
cam.position.z = 10
# Create a simple square mesh with UV
let quad = scene.newMesh("quad",
common_attributes = {vertex, uv},
vertex_array = @[
# x, y, z, U, V,
-1f, -1, 0, 0, 0,
1, -1, 0, 1, 0,
-1, 1, 0, 0, 1,
1, 1, 0, 1, 1,
],
index_array = @[0'u16, 1, 2, 2, 1, 3],
)
# Make the object wider with a 3:2 aspect ratio
quad.scale.x = 1.5
# This example texture is 1 pixel wide and 8 pixels high,
# to make a stripped flag
# NOTE: The colors are SRGB_u8, not RGB, but we're skipping output encoding
# in the shader for simplicity
let width = 1
let height = 8
let format = RGB_u8
# The texture goes from bottom to top
# (unless we invert the Y coordinate of the UV)
let pixels = @[
115, 41, 130,
36, 64, 142,
0, 128, 38,
255, 237, 0,
255, 140, 0,
228, 3, 3,
120, 79, 23,
0, 0, 0,
]
# Create the texture, use Nearest filter
# to have a sharp bands instead of a gradient
doAssert width * height * 3 == pixels.len
let texture = engine.newTexture("test",
width = width, height = height,
format = format, filter = Nearest,
pixels = newArrRef[uint8](pixels).to float32)
# Minimal example of a material with a UV and a texture uniform
quad.materials.add engine.newMaterial("shadeless texture",
fragment = """
uniform sampler2D tex;
in vec2 uv;
out vec4 glOutColor;
void main(){
glOutColor = texture(tex, uv);
}""",
varyings = @[Varying(vtype: Uv, varname: "uv")],
textures = {
"tex": texture,
}.toOrderedTable,
)
scene.enable_render()
engine.run()