annotate tests/test_materials.nim @ 334:2533f524bdb6

fix: remaining tests and an issue with updating uniforms
author Sam <sam@basx.dev>
date Mon, 04 Sep 2023 00:48:00 +0700
parents e4528d97a687
children b83b3a1ccb05
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
1 import std/times
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 253
diff changeset
2 import std/tables
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
3
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
4 import semicongine
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
5
332
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
6 let
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
7 sampler = Sampler(magnification: VK_FILTER_NEAREST, minification: VK_FILTER_NEAREST)
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
8 (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
9 thai = Image(width: 7, height: 5, imagedata: @[
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
10 RT, RT, RT, RT, RT, RT, RT,
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
11 WT, WT, WT, WT, WT, WT, WT,
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
12 PT, PT, PT, PT, PT, PT, PT,
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
13 WT, WT, WT, WT, WT, WT, WT,
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
14 RT, RT, RT, RT, RT, RT, RT,
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
15 ])
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
16 swiss = loadImage("flag.png")
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
17 material = Material(name: "material", textures: {
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
18 "tex1": Texture(image: thai, sampler: sampler),
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
19 "tex2": Texture(image: swiss, sampler: sampler),
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
20 }.toTable)
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
21
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
22 proc main() =
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 302
diff changeset
23 var flag = rect()
332
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
24 flag.material = material
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
25 var scene = Scene(name: "main", meshes: @[flag])
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
diff changeset
26 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32])
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
27
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
28 var engine = initEngine("Test materials")
253
ad078e26a1c7 fix: API changes
sam <sam@basx.dev>
parents: 244
diff changeset
29
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
30 const
332
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
31 shaderConfiguration1 = createShaderConfiguration(
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
32 inputs=[
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
33 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
34 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
35 ],
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
36 intermediates=[
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
37 attr[Vec2f]("uvout"),
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
38 ],
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
39 uniforms=[attr[float32]("test2", arrayCount=2)],
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
40 samplers = @[
334
2533f524bdb6 fix: remaining tests and an issue with updating uniforms
Sam <sam@basx.dev>
parents: 332
diff changeset
41 attr[Texture]("tex1", arrayCount=2),
2533f524bdb6 fix: remaining tests and an issue with updating uniforms
Sam <sam@basx.dev>
parents: 332
diff changeset
42 attr[Texture]("tex2", arrayCount=2),
332
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
43 ],
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
44 outputs=[attr[Vec4f]("color")],
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 315
diff changeset
45 vertexCode="""
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 315
diff changeset
46 gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0);
332
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
47 uvout = uv;""",
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 315
diff changeset
48 fragmentCode="""
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 315
diff changeset
49 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5;
332
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
50 color = texture(tex1[0], uvout) * (1 - d) + texture(tex2[0], uvout) * d;
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 315
diff changeset
51 """,
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
52 )
332
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
53 engine.initRenderer({
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
54 "material": shaderConfiguration1,
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
55 }.toTable)
e4528d97a687 fix: material tests
Sam <sam@basx.dev>
parents: 316
diff changeset
56 engine.addScene(scene)
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
57 var t = cpuTime()
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
58 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
diff changeset
59 var d = float32(cpuTime() - t)
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
diff changeset
60 setShaderGlobalArray(scene, "test2", @[d, d * 2])
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
61 engine.renderScene(scene)
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
62 engine.destroy()
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
63
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
64
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
65 when isMainModule:
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
66 main()