Skip to content

Instantly share code, notes, and snippets.

View FarazzShaikh's full-sized avatar
👻

Faraz Shaikh FarazzShaikh

👻
View GitHub Profile
Contributor Agreement
Individual Contributor Exclusive License Agreement
(including the Traditional Patent License OPTION)
Thank you for your interest in contributing to Faraz Shaikh's three-shader-baker ("We" or "Us").
The purpose of this contributor agreement ("Agreement") is to clarify and document the rights granted by contributors to Us. To make this document effective, please follow the instructions at ____________________.
How to use this Contributor Agreement
If You are an employee and have created the Contribution as part of your employment, You need to have Your employer approve this Agreement or sign the Entity version of this document. If You do not own the Copyright in the entire work of authorship, any other author of the Contribution should also sign this – in any event, please contact Us at farazzshaikh@gmail.com
@FarazzShaikh
FarazzShaikh / checksum.cpp
Last active January 13, 2021 16:36
IP Header Checksum
#include <iostream>
#include <bitset>
using namespace std;
string getChecksum(string text) {
int sum = 0;
for (int i = 0; i < text.length(); i++) {
// Cast char to int, making it it's ASCII value.
sum += (int)text[i];
// Function to apply Projection Matrix to a Triangle
function applyMatProj(triangle) {
const result = new Triangle([])
// Loop over all vertices in the triangle
for (const v in triangle.vertices) {
const vert = triangle.vertices[v]; // Get the vert at index v
const temp4d = new TSM.vec4([...vert.values, 1]); // Make a temporary 4D vertex
// We can skip the above step and multiply the Projection Matrix with
// vert directly if we define vert to be a 4D vector from the start
// Our Constants
const n = 0;
const f = 1000;
const fov = 90;
const a = canvas.height / canvas.width;
const S = 1 / Math.tan(((fov * 0.5) / 180) * Math.PI);
// Our Projection Matrix
const projectionMatrix = (function() {
const mat = new TSM.mat4(new Array(16).fill(0)); // Make a 4x4 matrix filled with 0s
// Function to draw a triangle on screen given a triangle
// and a color
function drawTriangle(triangle, color) {
// Begin drawing a new path (like)
ctx.beginPath();
// Moves the "cursor" to the first point
ctx.moveTo(triangle.vertices[0].x, triangle.vertices[0].y);
ctx.lineTo(triangle.vertices[1].x, triangle.vertices[1].y); // Draw a line from first to second vertex
// A Single Vertex
class Vertex {
constructor(pos) {
this.x = pos[0]; // Position along the X-axis
this.y = pos[1]; // Position along the Y-axis
this.z = pos[2]; // Position along the Z-axis
}
}
// We will be using the vec3 class from the TSM library
const theta = (Math.PI / 4)
const scaleFac = 1.2
const scale = {
x: Math.cos(theta) * scaleFac,
y: Math.cos(theta) * scaleFac
}
const skew = {
const skewFactor = {
x: 1.2,
y: 1.2
}
const mat_transform = new DOMMatrix([
scale, skewFactor.x, // Sx Qx
skewFactor.y, scale, // Qy Sy
translateVec.x, translateVec.y, // Tx Ty
])
const translationFactor = {
x: 50,
y: 50
}
const translateVec = {
x: translationFactor.x + offset.x,
y: translationFactor.y + offset.y,
}
const origin = {
x: (canvas.width / 2),
y: (canvas.height / 2)
}
const offset = {
x: (1 - scale) * origin.x,
y: (1 - scale) * origin.y
}