What is the process for calculating the product of a 4x4 Matrix with a vector-array of any size?

When creating a specific scene in webgl, the usual process of transforming an object involves defining the initial vertex position data and sending it to the vertex shader. In the shader, the vertices are manipulated by various matrices that contain information about perspective, translations, rotations, and scales. An example of this in code would be:

gl_Position = projectionMatrix * modelviewMatrix * vec4(vertexPosition, 1.0);

The modelviewMatrix is obtained from multiplying the viewMatrix with the modelMatrix, which are results of different matrix operations like translation, rotation, and scaling.

Now, my question revolves around the final step in this chain of matrix multiplications - the multiplication of a mat4 with a vector array of any length.

someMatrix * vec4(vertexPosition, 1.0);

I am currently working on a webgl editor and I want to save the vertex position data of the created 3D objects after transformation without including matrix transformation details. I simply want to store the raw position data like {x, y, z, x, y, z, x, y, z, etc.}, similar to the original vertices array.

For instance, if we have these vertices for a triangle:

var vertices = [0.0, 1.0, 0.0,  -1.0, -1.0, 0.0,  1.0, -1.0, 0.0];

Then I create a mat4 and perform a translation by [2, 0, 0].

The function I need should take this translated mat4 and the vertices array as inputs, multiply them together, and return a new array of translated vertices such as:

var translatedVertices = [2.0, 1.0, 0.0,  1.0, -1.0, 0.0,  3.0, -1.0, 0.0];

In order to multiply a 4x4 matrix with a vector array, I understand that a fourth value needs to be added. Initially, each triple of values in the original vec3 array must be split and an additional 1.0 inserted for every set to convert it into a vec4 array, [x, y, z] --> [x, y, z, 1]. However, beyond that modification, I lack the knowledge of how exactly this JavaScript function should be structured.

Answer №1

When referring to the operation:

someMatrix * vec4(vertexPosition, 1.0);

The variable someMatrix represents a 4x4 matrix, while vertexPosition always denotes a 3x1 matrix that is extended to a 4x1 matrix by adding 1.0. Multiplying a 4x4 matrix by a 4x1 matrix will result in a valid 4x1 matrix output (referred to as gl_Position). It's important to note that a 4x1 matrix can be represented using a vec4.

This process ensures that vector length remains consistent throughout the calculation. The input data (such as your vertices variable) is organized into triplets, appends 1.0 to each element, and then undergoes the multiplication of a 4x4 by 4x1 matrix.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to incorporate audio in an Angular 4 application

I'm currently developing a project using Electron app with Angular 4. My goal is to incorporate sound effects for specific actions within the application. Are there any existing modules or codes that can assist with this? Ideally, I would like the sol ...

Every time I try to utilize "useCallback," it results in a TypeError error popping up

I am struggling with an issue related to a const experience that involves creating 6 experiences with their respective popovers. I have been instructed to use useCallback, but every time I attempt to do so, I encounter an error message. This relates to my ...

Is there a way to execute the identical promise once more based on the outcome of the previous execution?

My dilemma involves a block of code enclosed within a promise. This particular code is responsible for modifying records in a remote database and reporting the number of records edited. In case the count of edited records exceeds 0 (indicating work done), ...

Is Optional Chaining supported by Next.js on Vercel?

While Next.js does support Optional Chaining, I have encountered an issue when trying to deploy the following code snippet: module.exports = { experimental: { outputStandalone: true, }, images: { domains: process.env.NEXT_PUBLIC_IMAGE_DOMAINS ...

What steps are necessary to add a Contact Us form to my HTML website?

Currently, I am searching for a way to add a "Contact Us" section to my website that is completely HTML-based. I believe the best approach would involve using a PHP script to handle sending emails from a form on the Contact Us page, but I am not familiar ...

I'm attempting to identify the issue with the given JSON data

Having trouble identifying the issue with this JSON data. I've been using http://jsonlint.com/ for validation, but it keeps failing with the following error: Error found on line 9: ...3" } ]} --------------------^ Expecting 'EOF' ...

Developing distinct state values for assigned objects

I developed a star rating component using Material UI components that is based on a mapped object. However, I am facing an issue where all the star ratings show the same value when clicked. How can I ensure that each star section displays its own value bas ...

Utilizing Jquery's Selectable IDs in a repetitive manner

I'm a newbie to jQuery and I'm attempting to create two lists where each item can be selected. Check out my code: <!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI s ...

Issue with exporting Three.js to Maya

I've been attempting to utilize the Three.js exporter for Maya found here. However, when I try to load the threeJsFileTranslator.py plug-in from the plug-ins manager in Maya, I encounter an error in the Script Editor: // Error: line 1: invalid synta ...

JavaScript Stub Pusher (Event)

I rely on pusher for handling my sockets and events. The pusher channels I use are encapsulated within an object that is passed as a key/value pair to the child component. The actual channel is accessed through this.channel.value, while this.channel.key s ...

The Combo Box Select2 display is not showing correctly

In my web application, I'm dynamically adding new rows to a table using JavaScript. Each row contains an element where data is loaded from the ViewBag, and I also want to apply the Select2 class to this element. Here is the current code snippet: < ...

The <h> tag gains height on its own accord

Here is my original code: <h4 class="orange2" ><a href="<?php echo base_url();?>v/<?php echo $this->uri->segment(3);?>/<?php echo $v['uniq'];?>"><?php echo trim(strip_tags($v['video_name']));?> ...

The art of bringing a pseudo class to life through animation

Seeking assistance with achieving a unique effect on click event. I have set up a slanted or razor-blade style div using a parent div element and a pseudo-element :after (as shown below). My goal is to change the skew angle when the user clicks on the pare ...

Repair the navigation bar once it reaches the top of the screen using ReactJS

I have a webpage that contains specific content followed by a bar with tabs. My goal is to have this bar stay fixed at the top of the screen once it reaches that position while scrolling down, and only allow the content below the fixed bar to continue scro ...

Instructions on accessing the subsequent li a starting from a designated location

My goal is to change the link color of the button with the id #idIMMUNOLOGY_9, which has the class .active_default, but I must start from the element with the id #idTERRITORIAL_8. I attempted this approach : $('#idTERRITORIAL_8').parent().find( ...

Detecting collisions in JavaScript

Currently, I am in the process of reviving an old game reminiscent of Tron using HTML5 canvas and JavaScript. The unique twist in this version is that the snakes have the ability to move in curves rather than right angles (known as Achtung Die Kurve). The ...

Having trouble with Vue component not updating Vuex state before it loads?

At times, the token is committed to Vuex store while other times it is not. userLogin() { axios.post('api/login', this.logindata,) .then(response => { let token = JSON.parse(localStorage.getItem('token')); t ...

Form calculation

An issue has arisen with my calculating form for a client where an incorrect amount is displayed when 0.93 is entered into the percentage box. Original calculation: 10 x 10 x 15 x 0.93 = 13.95 Corrected calculation: 10 x 10 x 15 x 0.93 = 1.395 I am seek ...

Tips for incorporating a hashbang into a JavaScript file that is executable without compromising browser compatibility

Is it possible to run code like this in both Node.js and the browser? #! /usr/local/bin/node console.log("Hello world") I have a script that I currently run locally in Node.js, but now I want to also execute it in the browser without having to modify it ...

Tips for Showing Certain Slides When the Page Loads

When using jQuery filter effects to organize div slides on a page, I encountered an issue where all the divs containing different slides are displayed on page load instead of just the default chosen ['active'] div. The filter effect itself is fun ...