Added texture related functions to gl_functions.js

This commit is contained in:
2025-09-01 20:46:25 -07:00
parent 81f5457b61
commit 7c61189d3a
5 changed files with 259 additions and 38 deletions

View File

@@ -4,6 +4,7 @@ import { wasmPntrToJsString, wasmPntrAndLengthToJsString } from './wasm_function
export var glObjects = {
buffers: [ null ],
textures: [ null ],
vaos: [ null ],
shaders: [ null ],
programs: [ null ],
@@ -22,6 +23,15 @@ function verifyGlBufferId(bufferId, allowZero)
if (glObjects.buffers[bufferId] == null) { return "BufferId is for a destroyed vertBuffer!"; }
return null;
}
function verifyGlTextureId(textureId, allowZero)
{
if (typeof(textureId) != "number") { return "TextureId is not a number!"; }
if (textureId == 0) { return allowZero ? null : "TextureId is 0!"; }
if (glObjects == null || glObjects.textures == null) { return "Textures array has not been initialized yet!"; }
if (textureId >= glObjects.textures.length) { return "TextureId is too high!"; }
if (glObjects.textures[textureId] == null) { return "TextureId is for a destroyed texture!"; }
return null;
}
function verifyGlVaoId(vaoId, allowZero)
{
if (typeof(vaoId) != "number") { return "VaoId is not a number!"; }
@@ -97,6 +107,9 @@ export function jsGlFrontFace(cullMode)
appGlobals.glContext.frontFace(cullMode);
}
// +==============================+
// | Buffer Functions |
// +==============================+
export function jsGlDeleteBuffer(bufferId)
{
if (!verifyParameter(verifyGlBufferId(bufferId, false), "gl.deleteBuffer", "bufferId", bufferId)) { return; }
@@ -120,10 +133,65 @@ export function jsGlBindBuffer(bufferType, bufferId)
export function jsGlBufferData(bufferType, dataLength, dataPntr, usageHint)
{
let dataArray = appGlobals.memDataView.buffer.slice(dataPntr, dataPntr + dataLength)
let dataArray = appGlobals.memDataView.buffer.slice(dataPntr, dataPntr + dataLength);
appGlobals.glContext.bufferData(bufferType, dataArray, usageHint);
}
// +==============================+
// | Texture Functions |
// +==============================+
export function jsGlDeleteTexture(textureId)
{
if (!verifyParameter(verifyGlTextureId(textureId, false), "gl.deleteTexture", "textureId", textureId)) { return; }
appGlobals.glContext.deleteTexture(glObjects.textures[textureId]);
glObjects.textures[textureId] = null;
}
export function jsGlCreateTexture()
{
let newTexture = appGlobals.glContext.createTexture();
let newTextureId = glObjects.textures.length;
glObjects.textures.push(newTexture);
return newTextureId;
}
export function jsGlActiveTexture(textureIndex)
{
appGlobals.glContext.activeTexture(textureIndex);
}
export function jsGlBindTexture(target, textureId)
{
if (!verifyParameter(verifyGlTextureId(textureId, true), "gl.bindTexture", "textureId", textureId)) { return; }
let texture = glObjects.textures[textureId];
appGlobals.glContext.bindTexture(target, texture);
}
export function jsGlTexImage2D(target, level, internalFormat, width, height, border, format, type, dataLength, dataPntr)
{
// let dataBuffer = appGlobals.memDataView.buffer.slice(dataPntr, dataPntr + dataLength);
let dataBuffer = new Uint8Array(appGlobals.memDataView.buffer, dataPntr, dataLength);
appGlobals.glContext.texImage2D(target, level, internalFormat, width, height, border, format, type, dataBuffer);
}
export function jsGlTexParameteri(target, parameter, value)
{
appGlobals.glContext.texParameteri(target, parameter, value)
}
export function jsGlGenerateMipmap(target)
{
appGlobals.glContext.generateMipmap(target);
}
// +==============================+
// | Vertex Array Functions |
// +==============================+
export function jsGlDeleteVertexArray(vaoId)
{
if (!verifyParameter(verifyGlVaoId(vaoId, true), "gl.deleteVertexArray", "vaoId", vaoId)) { return; }
appGlobals.glContext.deleteVertexArray(glObjects.vaos[vaoId]);
glObjects.vaos[vaoId] = null;
}
export function jsGlCreateVertexArray()
{
let newVao = appGlobals.glContext.createVertexArray();
@@ -132,7 +200,6 @@ export function jsGlCreateVertexArray()
return newVaoId;
}
//TODO: jsGlDeleteVertexArray?
export function jsGlBindVertexArray(vaoId)
{
if (!verifyParameter(verifyGlVaoId(vaoId, true), "gl.bindVertexArray", "vaoId", vaoId)) { return; }
@@ -150,6 +217,9 @@ export function jsGlVertexAttribPointer(attribLocation, componentCount, componen
appGlobals.glContext.vertexAttribPointer(attribLocation, componentCount, componentType, normalized, stride, offset);
}
// +==============================+
// | Shader Functions |
// +==============================+
export function jsGlDeleteShader(shaderId)
{
if (!verifyParameter(verifyGlShaderId(shaderId, false), "gl.deleteShader", "shaderId", shaderId)) { return; }
@@ -199,7 +269,7 @@ export function jsGlGetShaderParameterInt(shaderId, parameter)
export function jsGlDeleteProgram(programId)
{
if (!verifyParameter(verifyGlProgramId(programId, false), "gl.deleteProgram", "programId", programId)) { return; }
appGlobals.glContext.deleteShader(glObjects.programs[programId]);
appGlobals.glContext.deleteProgram(glObjects.programs[programId]);
glObjects.programs[programId] = null;
}
export function jsGlCreateProgram()
@@ -250,6 +320,9 @@ export function jsGlGetProgramParameterInt(programId, parameter)
return paramValue;
}
// +==============================+
// | Clearing Functions |
// +==============================+
export function jsGlClearColor(rValue, gValue, bValue, aValue)
{
appGlobals.glContext.clearColor(rValue, gValue, bValue, aValue);
@@ -268,11 +341,17 @@ export function jsGlClear(bufferBits)
appGlobals.glContext.clear(bufferBits);
}
// +==============================+
// | Drawing Functions |
// +==============================+
export function jsGlDrawArrays(geometryType, startIndex, count)
{
appGlobals.glContext.drawArrays(geometryType, startIndex, count);
}
// +==============================+
// | Uniform Functions |
// +==============================+
export function jsGlGetUniformLocation(programId, nameLength, namePntr)
{
if (!verifyParameter(verifyGlProgramId(programId, false), "gl.getUniformLocation", "programId", programId)) { return false; }
@@ -414,6 +493,9 @@ export function jsGlUniformMatrix4fv(locationId, valuesPntr)
appGlobals.glContext.uniformMatrix4fv(location, false, valuesArray);
}
// +==============================+
// | Functions List |
// +==============================+
export let jsGlFunctions = {
jsGlEnable: jsGlEnable,
jsGlDisable: jsGlDisable,
@@ -425,6 +507,14 @@ export let jsGlFunctions = {
jsGlCreateBuffer: jsGlCreateBuffer,
jsGlBindBuffer: jsGlBindBuffer,
jsGlBufferData: jsGlBufferData,
jsGlDeleteTexture: jsGlDeleteTexture,
jsGlCreateTexture: jsGlCreateTexture,
jsGlActiveTexture: jsGlActiveTexture,
jsGlBindTexture: jsGlBindTexture,
jsGlTexImage2D: jsGlTexImage2D,
jsGlTexParameteri: jsGlTexParameteri,
jsGlGenerateMipmap: jsGlGenerateMipmap,
jsGlDeleteVertexArray: jsGlDeleteVertexArray,
jsGlCreateVertexArray: jsGlCreateVertexArray,
jsGlBindVertexArray: jsGlBindVertexArray,
jsGlEnableVertexAttribArray: jsGlEnableVertexAttribArray,
@@ -469,9 +559,5 @@ export let jsGlFunctions = {
jsGlUniformMatrix4fv: jsGlUniformMatrix4fv,
};
//TODO: deleteTexture(webglObjects.textures[textureId])
//TODO: texImage2D(target, level, internalformat, width, height, border, format, type, dataBuffer)
//TODO: generateMipmap(target)
//TODO: string getShaderInfoLog(shaderId)
//TODO: string getProgramInfoLog(programId)