Skip to content

Commit

Permalink
✨♻️ allow models of type Model to be imported + turn models into type…
Browse files Browse the repository at this point in the history
… Model
  • Loading branch information
slooi committed Oct 1, 2024
1 parent b91e07c commit 30377ef
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export type Model = {
},
vs: string,
fs: string,
texture: string
texture?: string
}
export type Vertex = {
format: {
size: number,
type: Float32Array | Uint8Array,
type: Float32ArrayConstructor | Uint8ArrayConstructor,
normalize: boolean
},
data: number[]
Expand Down
4 changes: 3 additions & 1 deletion src/model/modelL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const model = {
import { Model } from "./model";

export const model: Model = {
vertexData: {

a_position: {
Expand Down
3 changes: 2 additions & 1 deletion src/model/modelPlane.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import f from "../assets/tex.png"
import { Model } from "./model"

export const model = {
export const model: Model = {
vertexData: {

a_position: {
Expand Down
13 changes: 8 additions & 5 deletions src/webgl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { m4 } from "./m4"
import { model } from "./model/modelPlane"
import { Model } from "./model/model"
import { model } from "./model/modelL"


export function createWebglRenderer(canvas: HTMLCanvasElement) {
Expand Down Expand Up @@ -50,7 +51,7 @@ export function createWebglRenderer(canvas: HTMLCanvasElement) {
// ########################################################################
setupModelTexture(gl, modelContainer)

function setupModelTexture(gl: WebGL2RenderingContext, modelContainer: { programAttributeLocations: ReturnType<typeof getAttributeLocationsFromProgram>, programUniformLocations: ReturnType<typeof getUniformLocationsFromProgram> } & typeof model) {
function setupModelTexture(gl: WebGL2RenderingContext, modelContainer: { programAttributeLocations: ReturnType<typeof getAttributeLocationsFromProgram>, programUniformLocations: ReturnType<typeof getUniformLocationsFromProgram> } & Model) {
if (!modelContainer.texture) return

const texture = gl.createTexture()
Expand Down Expand Up @@ -133,17 +134,19 @@ export function createWebglRenderer(canvas: HTMLCanvasElement) {
/*
WEBGL FUNCTIONS
*/
function setupProgramVertexAttributeArrayAndBuffers(gl: WebGL2RenderingContext, modelContainer: { programAttributeLocations: ReturnType<typeof getAttributeLocationsFromProgram>, programUniformLocations: ReturnType<typeof getUniformLocationsFromProgram> } & typeof model) {
function setupProgramVertexAttributeArrayAndBuffers(gl: WebGL2RenderingContext, modelContainer: { programAttributeLocations: ReturnType<typeof getAttributeLocationsFromProgram>, programUniformLocations: ReturnType<typeof getUniformLocationsFromProgram> } & Model) {
const vao = gl.createVertexArray()
if (!vao) throw new Error("ERROR: vao is null!")
gl.bindVertexArray(vao)

// Create list of attribute names using the model's `vertexData`
const attributeNameArray = (Object.keys(modelContainer.vertexData) as Array<keyof typeof modelContainer.vertexData>)
attributeNameArray.map(name => {
const vertexAttribute = modelContainer.vertexData[name]
if (!vertexAttribute) throw new Error("ERROR: this vertdata does NOT exist!")
// Get vertexAttributeArray params
const location = modelContainer.programAttributeLocations[name]
const format = modelContainer.vertexData[name].format
const format = vertexAttribute.format
const glType = format.type === Float32Array ? gl.FLOAT : format.type === Uint8Array ? gl.UNSIGNED_BYTE : (() => { throw new Error("ERROR: NOT SUPPOSED TYPE WAS USED") })()
const normalize = format.normalize
const size = format.size
Expand All @@ -153,7 +156,7 @@ function setupProgramVertexAttributeArrayAndBuffers(gl: WebGL2RenderingContext,
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
gl.vertexAttribPointer(location, size, glType, normalize, 0, 0)
gl.enableVertexAttribArray(location)
gl.bufferData(gl.ARRAY_BUFFER, new format.type(model.vertexData[name].data), gl.STATIC_DRAW)
gl.bufferData(gl.ARRAY_BUFFER, new format.type(vertexAttribute.data), gl.STATIC_DRAW)
})
return vao
}
Expand Down

0 comments on commit 30377ef

Please sign in to comment.