Skip to content

Commit

Permalink
move grid drawing to debug draw
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffomatic committed Dec 25, 2020
1 parent 17be297 commit b6eacb7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 76 deletions.
34 changes: 25 additions & 9 deletions src/ClientSim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { quat, vec2, vec3 } from 'gl-matrix'
import { quat, vec2, vec3, vec4 } from 'gl-matrix'

import { Camera3d } from '~/camera/Camera3d'
import { Renderable3d } from '~/ClientView'
Expand All @@ -13,7 +13,7 @@ import { EntityManager } from '~/entities/EntityManager'
import { GameState, gameProgression, initMap } from '~/Game'
import { IKeyboard, IMouse } from '~/input/interfaces'
import { Map } from '~/map/interfaces'
import { getModel, loadGrid } from '~/models'
import { getModel } from '~/models'
import { ClientMessage, ClientMessageType } from '~/network/ClientMessage'
import { IServerConnection } from '~/network/ServerConnection'
import { ServerMessage, ServerMessageType } from '~/network/ServerMessage'
Expand Down Expand Up @@ -163,9 +163,6 @@ export class ClientSim {
'standard',
)

const gridModel = loadGrid()
this.modelLoader.loadModel('grid', gridModel, 'standard')

for (const m of ['bullet', 'core', 'tank', 'tree', 'turret', 'wall']) {
this.modelLoader.loadModel(m, getModel(m), 'standard')
}
Expand All @@ -187,6 +184,16 @@ export class ClientSim {
this.tick(SIMULATION_PERIOD_S)
this.tickDurations.sample(time.current() - start)

this.debugDraw.draw3d(() => [
{
object: {
type: 'MODEL',
id: 'wireTiles',
color: vec4.fromValues(0, 1, 1, 1),
},
},
])

this.debugDraw.draw2d(() => {
const text = [
`Player ${this.playerNumber}`,
Expand Down Expand Up @@ -373,14 +380,23 @@ export class ClientSim {
}

getRenderables3d(): Iterable<Renderable3d> {
return [...this.entityManager.renderables].map(([entityId, modelId]) => {
// TODO: reimplement as lazy iterable?

const res: Renderable3d[] = []
if (this.terrainLayer !== undefined) {
res.push({ modelId: 'terrain', posXY: vec2.create(), rotXY: 0 })
}

for (const [entityId, modelId] of this.entityManager.renderables) {
const transform = this.entityManager.transforms.get(entityId)!
return {
res.push({
modelId,
posXY: transform.position,
rotXY: transform.orientation,
}
})
})
}

return res
}

registerParticleEmitter(params: {
Expand Down
7 changes: 0 additions & 7 deletions src/ClientView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,7 @@ export class ClientView {
this.renderer3d.setWvTransform(params.world2ViewTransform)

this.renderer3d.renderStandard((drawModel) => {
drawModel('terrain', vec2.create(), 0)

// GRID DEBUG
drawModel('grid', vec2.create(), 0)

for (const { modelId, posXY, rotXY } of params.renderables3d) {
// const transform = this.entityManager.transforms.get(entityId)!
// drawModel(model, transform.position, transform.orientation)
drawModel(modelId, posXY, rotXY)
}
})
Expand Down
59 changes: 13 additions & 46 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { vec3 } from 'gl-matrix'

import { model as bullet } from './bullet'
import { model as core } from './core'
import { model as tank } from './tank'
import { model as tree } from './tree'
import { model as turret } from './turret'
import { model as wall } from './wall'

import { TILE_SIZE } from '~/constants'
import { model as bullet } from '~/models/bullet'
import { model as core } from '~/models/core'
import { model as tank } from '~/models/tank'
import { model as tree } from '~/models/tree'
import { model as turret } from '~/models/turret'
import { model as wall } from '~/models/wall'

export enum ModelPrimitive {
Lines,
Triangles,
}

const models: {
[key: string]: {
Expand Down Expand Up @@ -36,7 +39,7 @@ const materials: { [key: string]: [number, number, number, number] } = {

type ModelTypes = keyof typeof models
export type Model = {
primitive: 'TRIANGLES' | 'LINES'
primitive: ModelPrimitive
positions: Float32Array
colors?: Float32Array
normals?: Float32Array
Expand Down Expand Up @@ -120,42 +123,6 @@ export const getModel: (modelType: ModelTypes) => Model = (modelType) => {
positions: new Float32Array(vertices),
colors: new Float32Array(colors),
normals: new Float32Array(normals),
primitive: 'TRIANGLES',
}
}

export const loadGrid = (): Model => {
const vertices = []
const colors = []

const y = 0.01
for (let i = -32; i < 32; i++) {
vertices.push(
-32 * TILE_SIZE,
y,
i * TILE_SIZE,
32 * TILE_SIZE,
y,
i * TILE_SIZE,
)
colors.push(...materials.debug, ...materials.debug)
}
for (let i = -32; i < 32; i++) {
vertices.push(
i * TILE_SIZE,
y,
-32 * TILE_SIZE,
i * TILE_SIZE,
y,
32 * TILE_SIZE,
)
colors.push(...materials.debug, ...materials.debug)
}

return {
positions: new Float32Array(vertices),
colors: new Float32Array(colors),
normals: new Float32Array(),
primitive: 'LINES',
primitive: ModelPrimitive.Triangles,
}
}
63 changes: 51 additions & 12 deletions src/renderer/Renderer3d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { vec4 } from 'gl-matrix'
import { mat4, quat, vec2, vec3 } from 'gl-matrix'

import { Model as ModelDef } from '~/models'
import { TILE_SIZE } from '~/constants'
import { Model as ModelDef, ModelPrimitive } from '~/models'
import { shader as standardShader } from '~/renderer/shaders/standard'
import { shader as wireShader } from '~/renderer/shaders/wire'
import { Immutable } from '~/types/immutable'
Expand All @@ -24,7 +25,7 @@ interface Shader {
interface Model {
vao: WebGLVertexArrayObject
numVerts: number
primitive: 'LINES' | 'TRIANGLES'
primitive: ModelPrimitive
shader: string
}

Expand All @@ -42,7 +43,13 @@ interface WireCube {
rot?: quat
}

export type WireObject = WireLines | WireCube
interface WireModel {
type: 'MODEL'
id: string
color: vec4
}

export type WireObject = WireLines | WireCube | WireModel

const wcHalfSize = 0.5
const wcLowNW = [-wcHalfSize, -wcHalfSize, -wcHalfSize]
Expand All @@ -57,7 +64,7 @@ const wcHighSE = [wcHalfSize, wcHalfSize, wcHalfSize]
export const wireCubeModel = {
// "as const" convinces the typechecker that this property will not be
// re-assigned.
primitive: 'LINES' as const,
primitive: ModelPrimitive.Lines,

// prettier-ignore
positions: new Float32Array([
Expand All @@ -78,6 +85,33 @@ export const wireCubeModel = {
]),
}

const wireTilePositions = []
for (let i = -32; i < 32; i++) {
wireTilePositions.push(
-32 * TILE_SIZE,
0,
i * TILE_SIZE,
32 * TILE_SIZE,
0,
i * TILE_SIZE,
)
}
for (let i = -32; i < 32; i++) {
wireTilePositions.push(
i * TILE_SIZE,
0,
-32 * TILE_SIZE,
i * TILE_SIZE,
0,
32 * TILE_SIZE,
)
}

const wireTilesModel = {
positions: new Float32Array(wireTilePositions),
primitive: ModelPrimitive.Lines,
}

export interface IModelLoader {
loadModel: (modelName: string, model: ModelDef, shaderName: string) => void
}
Expand Down Expand Up @@ -111,6 +145,7 @@ export class Renderer3d implements IModelLoader {

this.models = new Map()
this.loadModel('wireCube', wireCubeModel, 'wire')
this.loadModel('wireTiles', wireTilesModel, 'wire')

this.fov = (75 * Math.PI) / 180 // set some sane default
this.viewportDimensions = vec2.fromValues(
Expand Down Expand Up @@ -296,10 +331,6 @@ export class Renderer3d implements IModelLoader {
this.ctx.uniform4fv(this.currentShader!.uniforms.get('color')!, obj.color)

switch (obj.type) {
case 'LINES':
this.drawLines(obj.positions)
break

case 'CUBE':
const rot = obj.rot ?? quat.create()
const scale = obj.scale ?? 1
Expand All @@ -314,6 +345,14 @@ export class Renderer3d implements IModelLoader {
),
)
break

case 'LINES':
this.drawLines(obj.positions)
break

case 'MODEL':
this.drawModel(obj.id, mat4.create())
break
}
})
}
Expand Down Expand Up @@ -398,12 +437,12 @@ export class Renderer3d implements IModelLoader {
this.ctx.bindVertexArray(model.vao)

switch (model.primitive) {
case 'TRIANGLES':
this.ctx.drawArrays(this.ctx.TRIANGLES, 0, model.numVerts)
break
case 'LINES':
case ModelPrimitive.Lines:
this.ctx.drawArrays(this.ctx.LINES, 0, model.numVerts)
break
case ModelPrimitive.Triangles:
this.ctx.drawArrays(this.ctx.TRIANGLES, 0, model.numVerts)
break
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/terrain/Layer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vec2 } from 'gl-matrix'

import { TILE_SIZE } from '~/constants'
import { Model } from '~/models'
import { Model, ModelPrimitive } from '~/models'
import { Type } from '~/terrain/Type'

export class Layer {
Expand Down Expand Up @@ -96,7 +96,7 @@ export class Layer {
positions: this.vertices,
colors: this.colors,
normals: this.normals,
primitive: 'TRIANGLES',
primitive: ModelPrimitive.Triangles,
}
}
}

0 comments on commit b6eacb7

Please sign in to comment.