# HG changeset patch # User sam # Date 1721225049 -25200 # Node ID 59e683c4728fd42414915f14d36a717a108eb4f9 # Parent c61658d2d22793412443c301e1f1d27450128d7b del: outdated examples diff -r c61658d2d227 -r 59e683c4728f examples/E01_hello_triangle.nim --- a/examples/E01_hello_triangle.nim Wed Jul 17 21:03:30 2024 +0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -import std/tables - -import ../semicongine - -# shader setup -const - shaderConfiguration = CreateShaderConfiguration( - name = "default shader", - inputs = [ - Attr[Vec3f]("position"), - Attr[Vec4f]("color"), - ], - intermediates = [Attr[Vec4f]("outcolor")], - outputs = [Attr[Vec4f]("color")], - vertexCode = "gl_Position = vec4(position, 1.0); outcolor = color;", - fragmentCode = "color = outcolor;", - ) - -# scene setup -var - scene = Scene(name: "scene", - meshes: @[NewMesh( - positions = [NewVec3f(-0.5, 0.5), NewVec3f(0, -0.5), NewVec3f(0.5, 0.5)], - colors = [NewVec4f(1, 0, 0, 1), NewVec4f(0, 1, 0, 1), NewVec4f(0, 0, 1, 1)], - material = VERTEX_COLORED_MATERIAL.InitMaterialData() - )] - ) - myengine = InitEngine("Hello triangle", showFps = true) - -myengine.InitRenderer({VERTEX_COLORED_MATERIAL: shaderConfiguration}, inFlightFrames = 2) -myengine.LoadScene(scene) - -while myengine.UpdateInputs() and not KeyWasPressed(Escape): - Transform[Vec3f](scene.meshes[0][], "position", Scale(1.001, 1.001)) - myengine.RenderScene(scene) - -myengine.Destroy() diff -r c61658d2d227 -r 59e683c4728f examples/E02_squares.nim --- a/examples/E02_squares.nim Wed Jul 17 21:03:30 2024 +0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +0,0 @@ -import std/sequtils -import std/tables -import std/random - -import ../semicongine - - -when isMainModule: - randomize() - const - COLUMNS = 10 - ROWS = 10 - WIDTH = 2'f32 / COLUMNS - HEIGHT = 2'f32 / ROWS - var - vertices: array[COLUMNS * ROWS * 4, Vec3f] - colors: array[COLUMNS * ROWS * 4, Vec4f] - iValues: array[COLUMNS * ROWS * 4, uint32] - indices: array[COLUMNS * ROWS * 2, array[3, uint16]] - - for row in 0 ..< ROWS: - for col in 0 ..< COLUMNS: - let - y: float32 = (row * 2 / COLUMNS) - 1 - x: float32 = (col * 2 / ROWS) - 1 - color = NewVec4f((x + 1) / 2, (y + 1) / 2, 0, 1) - squareIndex = row * COLUMNS + col - vertIndex = squareIndex * 4 - vertices[vertIndex + 0] = NewVec3f(x, y) - vertices[vertIndex + 1] = NewVec3f(x + WIDTH, y) - vertices[vertIndex + 2] = NewVec3f(x + WIDTH, y + HEIGHT) - vertices[vertIndex + 3] = NewVec3f(x, y + HEIGHT) - colors[vertIndex + 0] = color - colors[vertIndex + 1] = color - colors[vertIndex + 2] = color - colors[vertIndex + 3] = color - iValues[vertIndex + 0] = uint32(squareIndex) - iValues[vertIndex + 1] = uint32(squareIndex) - iValues[vertIndex + 2] = uint32(squareIndex) - iValues[vertIndex + 3] = uint32(squareIndex) - indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)] - indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)] - - - const - shaderConfiguration = CreateShaderConfiguration( - name = "default shader", - inputs = [ - Attr[Vec3f]("position"), - Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), - Attr[uint32]("index"), - ], - intermediates = [Attr[Vec4f]("outcolor")], - uniforms = [Attr[float32]("time")], - outputs = [Attr[Vec4f]("color")], - vertexCode = """ -float pos_weight = index / 100.0; // add some gamma correction? -float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5; -float v = min(1, max(0, pow(pos_weight - t, 2))); -v = pow(1 - v, 3000); -outcolor = vec4(color.r, color.g, v * 0.5, 1); -gl_Position = vec4(position, 1.0); -""", - fragmentCode = "color = outcolor;", - ) - let matDef = MaterialType(name: "default", vertexAttributes: { - "position": Vec3F32, - "color": Vec4F32, - "index": UInt32, - }.toTable) - var squaremesh = NewMesh( - positions = vertices, - indices = indices, - colors = colors, - ) - squaremesh[].InitVertexAttribute("index", iValues.toSeq) - squaremesh.material = matDef.InitMaterialData(name = "default") - - var myengine = InitEngine("Squares") - myengine.InitRenderer({matDef: shaderConfiguration}) - - var scene = Scene(name: "scene", meshes: @[squaremesh]) - scene.AddShaderGlobal("time", 0.0'f32) - myengine.LoadScene(scene) - while myengine.UpdateInputs() and not KeyWasPressed(Escape): - scene.SetShaderGlobal("time", GetShaderGlobal[float32](scene, "time") + 0.0005'f) - myengine.RenderScene(scene) - - myengine.Destroy() diff -r c61658d2d227 -r 59e683c4728f examples/E03_hello_cube.nim --- a/examples/E03_hello_cube.nim Wed Jul 17 21:03:30 2024 +0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -import std/tables -import std/times - -import ../semicongine - -const - TopLeftFront = NewVec3f(-0.5'f32, -0.5'f32, -0.5'f32) - TopRightFront = NewVec3f(0.5'f32, -0.5'f32, -0.5'f32) - BottomRightFront = NewVec3f(0.5'f32, 0.5'f32, -0.5'f32) - BottomLeftFront = NewVec3f(-0.5'f32, 0.5'f32, -0.5'f32) - TopLeftBack = NewVec3f(0.5'f32, -0.5'f32, 0.5'f32) - TopRightBack = NewVec3f(-0.5'f32, -0.5'f32, 0.5'f32) - BottomRightBack = NewVec3f(-0.5'f32, 0.5'f32, 0.5'f32) - BottomLeftBack = NewVec3f(0.5'f32, 0.5'f32, 0.5'f32) -const - cube_pos = @[ - TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front - TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back - TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left - TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right - TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top - BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom - ] - R = NewVec4f(1, 0, 0, 1) - G = NewVec4f(0, 1, 0, 1) - B = NewVec4f(0, 0, 1, 1) - cube_color = @[ - R, R, R, R, - R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, - G, G, G, G, - G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, - B, B, B, B, - B * 0.5'f32, B * 0.5'f32, B * 0.5'f32, B * 0.5'f32, - ] -var - tris: seq[array[3, uint16]] -for i in 0'u16 ..< 6'u16: - let off = i * 4 - tris.add [off + 0'u16, off + 1'u16, off + 2'u16] - tris.add [off + 2'u16, off + 3'u16, off + 0'u16] - -when isMainModule: - var myengine = InitEngine("Hello cube") - - const - shaderConfiguration = CreateShaderConfiguration( - name = "default shader", - inputs = [ - Attr[Vec3f]("position"), - Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), - ], - intermediates = [Attr[Vec4f]("outcolor")], - uniforms = [ - Attr[Mat4]("projection"), - Attr[Mat4]("view"), - Attr[Mat4]("model"), - ], - outputs = [Attr[Vec4f]("color")], - vertexCode = """ - outcolor = color; - // gl_Position = vec4(position, 1) * (Uniforms.model * Uniforms.projection * Uniforms.view); - gl_Position = vec4(position, 1) * (Uniforms.model * Uniforms.projection * Uniforms.view); - """, - fragmentCode = "color = outcolor;", - ) - var matDef = MaterialType(name: "default material", vertexAttributes: {"position": Vec3F32, "color": Vec4F32}.toTable) - var cube = Scene(name: "scene", meshes: @[NewMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.InitMaterialData(name = "default"))]) - cube.AddShaderGlobal("projection", Unit4f32) - cube.AddShaderGlobal("view", Unit4f32) - cube.AddShaderGlobal("model", Unit4f32) - myengine.InitRenderer({matDef: shaderConfiguration}) - myengine.LoadScene(cube) - - var t: float32 = cpuTime() - while myengine.UpdateInputs() and not KeyWasPressed(Escape): - SetShaderGlobal(cube, "model", Translate(0'f32, 0'f32, 10'f32) * Rotate(t, Yf32)) - SetShaderGlobal(cube, "projection", - Perspective( - float32(PI / 4), - float32(myengine.GetWindow().Size[0]) / float32(myengine.GetWindow().Size[1]), - 0.1'f32, - 100'f32 - ) - ) - t = cpuTime() - myengine.RenderScene(cube) - - myengine.Destroy() diff -r c61658d2d227 -r 59e683c4728f examples/E04_input.nim --- a/examples/E04_input.nim Wed Jul 17 21:03:30 2024 +0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,208 +0,0 @@ -import std/enumerate -import std/tables -import std/typetraits -import std/math - -import ../semicongine - -const - arrow = @[ - NewVec3f(-1, -1), - NewVec3f(1, -1), - NewVec3f(-0.3, -0.3), - NewVec3f(-0.3, -0.3), - NewVec3f(-1, 1), - NewVec3f(-1, -1), - ] - # keyboard layout, specifying rows with key widths, negative numbers are empty spaces - keyrows = ( - [1.0, -0.6, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, 1.0, 1.0, -0.1, 1.0, 1.0, 1.0], - [1.2, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.8, -0.1, 1.0, 1.0, 1.0], - [1.8, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.5, 1.0, 1.0, 1.0], - [2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], - [2.6, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.8, -1.3, 1.0], - [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0], - ) - keyDimension = 50'f32 - keyGap = 10'f32 - backgroundColor = NewVec4f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471, 1) - baseColor = NewVec4f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118, 1) - activeColor = NewVec4f(0.6509803921568628, 0.22745098039215686, 0.3137254901960784, 1) - arrow_colors = @[ - baseColor * 0.9'f32, - baseColor * 0.9'f32, - baseColor * 0.9'f32, - baseColor * 0.8'f32, - baseColor * 0.8'f32, - baseColor * 0.8'f32, - ] - keyIndices = [ - Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen, - ScrollLock, Pause, - - NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`, - - NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp, - Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1, - LetterRow1Extra2, Delete, End, PageDown, - - CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2, - LetterRow2Extra3, Enter, - - ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2, - LetterRow3Extra3, ShiftR, Up, - - CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right - ] - -# build keyboard and cursor meshes -var - scene: Scene - keyvertexpos: seq[Vec3f] - keyvertexcolor: seq[Vec4f] - keymeshindices: seq[array[3, uint16]] - rowpos = NewVec2f(0, 0) - i = 0'u16 - firstRow = true - rowWidth = 0'f32 -for row in keyrows.fields: - for key in row: - let keySpace = float32(keyDimension * key) - if key > 0: - if keyIndices[i div 4] == Enter: - keyvertexpos.add NewVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap) - keyvertexpos.add NewVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap) - else: - keyvertexpos.add NewVec3f(rowpos[0], rowpos[1]) - keyvertexpos.add NewVec3f(rowpos[0] + keySpace, rowpos[1]) - keyvertexpos.add NewVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension) - keyvertexpos.add NewVec3f(rowpos[0], rowpos[1] + keyDimension) - keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] - keymeshindices.add [i, i + 1, i + 2] - keymeshindices.add [i + 2, i + 3, i] - rowpos[0] += keySpace + keyGap - i += 4 - else: - rowpos[0] += -keySpace + keyGap - if firstRow: - rowWidth = rowpos[0] - rowpos[0] = 0 - rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) - firstRow = false - - -when isMainModule: - var myengine = InitEngine("Input") - - # transform the cursor a bit to make it look nice - let cursorscale = ( - Scale2d(20'f32, 20'f32) * - Translate2d(1'f32, 1'f32) * - Rotate2d(-float32(PI) / 4'f32) * - Scale2d(0.5'f32, 1'f32) * - Rotate2d(float32(PI) / 4'f32) - ) - var positions = arrow - for i in 0 ..< positions.len: - positions[i] = cursorscale * NewVec3f(positions[i].x, positions[i].y) - - # define mesh objects - var - matDef = MaterialType(name: "default", vertexAttributes: { - "position": Vec3F32, - "color": Vec4F32, - }.toTable) - cursormesh = NewMesh( - positions = positions, - colors = arrow_colors, - material = matDef.InitMaterialData(), - ) - keyboardmesh = NewMesh( - positions = keyvertexpos, - colors = keyvertexcolor, - indices = keymeshindices, - material = matDef.InitMaterialData(), - ) - backgroundmesh = NewMesh( - positions = @[ - NewVec3f(0'f32, 0'f32), - NewVec3f(1'f32, 0'f32), - NewVec3f(1'f32, 1'f32), - NewVec3f(0'f32, 1'f32), - ], - colors = @[ - backgroundColor, - backgroundColor, - backgroundColor, - backgroundColor, - ], - indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], - material = matDef.InitMaterialData(), - ) - - # define mesh objects - var keyboard_center = Translate( - -float32(rowWidth) / 2'f32, - -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, - 0'f32 - ) - scene = Scene(name: "scene", meshes: @[backgroundmesh, keyboardmesh, cursormesh]) - - # shaders - const - shaderConfiguration = CreateShaderConfiguration( - name = "default shader", - inputs = [ - Attr[Vec3f]("position"), - Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), - Attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), - ], - intermediates = [Attr[Vec4f]("outcolor")], - uniforms = [Attr[Mat4]("projection")], - outputs = [Attr[Vec4f]("color")], - vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", - fragmentCode = "color = outcolor;", - ) - - # set up rendering - myengine.InitRenderer({matDef: shaderConfiguration}) - scene.AddShaderGlobal("projection", Unit4f32) - myengine.LoadScene(scene) - myengine.HideSystemCursor() - - # mainloop - while myengine.UpdateInputs(): - if WindowWasResized(): - scene.SetShaderGlobal("projection", - Ortho( - 0, float32(myengine.GetWindow().Size[0]), - 0, float32(myengine.GetWindow().Size[1]), - 0, 1, - ) - ) - let - winsize = myengine.GetWindow().Size - center = Translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) - keyboardmesh.transform = keyboard_center * center - backgroundmesh.transform = Scale(float32(winsize[0]), float32(winsize[1]), 1'f32) - - let mousePos = Translate(MousePosition().x + 20, MousePosition().y + 20, 0'f32) - cursormesh.transform = mousePos - - for (index, key) in enumerate(keyIndices): - if KeyWasPressed(key): - let baseIndex = index * 4 - keyboardmesh["color", baseIndex + 0] = activeColor - keyboardmesh["color", baseIndex + 1] = activeColor - keyboardmesh["color", baseIndex + 2] = activeColor - keyboardmesh["color", baseIndex + 3] = activeColor - if KeyWasReleased(key): - let baseIndex = index * 4 - keyboardmesh["color", baseIndex + 0] = baseColor - keyboardmesh["color", baseIndex + 1] = baseColor - keyboardmesh["color", baseIndex + 2] = baseColor - keyboardmesh["color", baseIndex + 3] = baseColor - - myengine.RenderScene(scene) - - myengine.Destroy() diff -r c61658d2d227 -r 59e683c4728f examples/E10_pong.nim --- a/examples/E10_pong.nim Wed Jul 17 21:03:30 2024 +0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -import std/times -import std/tables - -import ../semicongine - -let - barcolor = ToRGBA("5A3F00").ToSRGB().ColorToHex() - barSize = 0.1'f - barWidth = 0.01'f - ballcolor = ToRGBA("B17F08").ToSRGB().ColorToHex() - ballSize = 0.01'f - ballSpeed = 60'f - matDef = MaterialType(name: "default", vertexAttributes: { - "position": Vec3F32, - "color": Vec4F32, - }.toTable) - -var - level: Scene - ballVelocity = NewVec2f(1, 1).Normalized * ballSpeed - -when isMainModule: - var myengine = InitEngine("Pong") - - var player = Rect(color = barcolor, width = barWidth, height = barSize) - player.material = matDef.InitMaterialData(name = "player material") - var ball = Circle(color = ballcolor) - ball.material = matDef.InitMaterialData(name = "player material") - level = Scene(name: "scene", meshes: @[ball, player]) - - const - shaderConfiguration = CreateShaderConfiguration( - name = "default shader", - inputs = [ - Attr[Vec3f]("position"), - Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), - Attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), - ], - intermediates = [Attr[Vec4f]("outcolor")], - uniforms = [Attr[Mat4]("projection")], - outputs = [Attr[Vec4f]("color")], - vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", - fragmentCode = "color = outcolor;", - ) - - # set up rendering - myengine.InitRenderer({matDef: shaderConfiguration}) - level.AddShaderGlobal("projection", Unit4f32) - myengine.LoadScene(level) - - var - winsize = myengine.GetWindow().Size - height = float32(winsize[1]) / float32(winsize[0]) - width = 1'f - currentTime = cpuTime() - showSystemCursor = true - fullscreen = false - while myengine.UpdateInputs() and not KeyIsDown(Escape): - if KeyWasPressed(C): - if showSystemCursor: - myengine.HideSystemCursor() - else: - myengine.ShowSystemCursor() - showSystemCursor = not showSystemCursor - if KeyWasPressed(F): - fullscreen = not fullscreen - myengine.Fullscreen = fullscreen - - let dt: float32 = cpuTime() - currentTime - currentTime = cpuTime() - if WindowWasResized(): - winsize = myengine.GetWindow().Size - height = float32(winsize[1]) / float32(winsize[0]) - width = 1'f - SetShaderGlobal(level, "projection", Ortho(0, width, 0, height, 0, 1)) - if KeyIsDown(Down) and (player.transform.Col(3).y + barSize/2) < height: - player.transform = player.transform * Translate(0'f, 1'f * dt, 0'f) - if KeyIsDown(Up) and (player.transform.Col(3).y - barSize/2) > 0: - player.transform = player.transform * Translate(0'f, -1'f * dt, 0'f) - - # bounce level - if ball.transform.Col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] - if ball.transform.Col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] - if ball.transform.Col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] - - ball.transform = ball.transform * Translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) - - # loose - if ball.transform.Col(3).x - ballSize/2 <= 0: - ball.transform = Scale(ballSize, ballSize, 1'f) * Translate(30'f, 30'f, 0'f) - ballVelocity = NewVec2f(1, 1).Normalized * ballSpeed - - # bar - if ball.transform.Col(3).x - ballSize/2 <= barWidth: - let - barTop = player.transform.Col(3).y - barSize/2 - barBottom = player.transform.Col(3).y + barSize/2 - ballTop = ball.transform.Col(3).y - ballSize/2 - ballBottom = ball.transform.Col(3).y + ballSize/2 - if ballTop >= barTop and ballBottom <= barBottom: - ballVelocity[0] = abs(ballVelocity[0]) - - myengine.RenderScene(level) - myengine.Destroy()