Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:37:24+00:00 2026-06-15T08:37:24+00:00

I’m looking at Learning WebGL website and porting their code. I came across a

  • 0

I’m looking at Learning WebGL website and porting their code. I came across a line of code that I’d like to produce with DartVectorMath library:

var pMatrix = createPerspectiveMatrix4(45.0, canvas.width/canvas.height, 0.1, 100.0);

What should createPerspectiveMatrix4 be?

I’ve tried various different things and this is the last one I tried:

  var zNear = 0.1;
  var zFar = 10000;
  double yTop = Math.tan(45.0 * Math.PI / 180.0 / 2.0) * zNear;
  double xRight = canvas.width / canvas.height * yTop;
  double zDepth = zFar - zNear;

  var row1 = new vec4(zNear / xRight, 0.0, 0.0, 0.0);
  var row2 = new vec4(0.0, zNear / yTop, 0.0, 0.0);
  var row3 = new vec4(0.0, 0.0, -(zFar + zNear) / zDepth, -(2 * zNear * zFar) / zDepth);
  var row4 = new vec4(0.0, 0.0, -1.0, 0.0);

  var pMatrix = new mat4(row1, row2, row3, row4);

I simply see nothing rendered on the screen.

The full source is as follows:

import 'dart:html';
import 'dart:math' as Math;
import 'package:vector_math/vector_math_browser.dart';

main() {
  var canvas = query('#back-buffer');
  WebGLRenderingContext gl = canvas.getContext('experimental-webgl');
  gl.viewport(0, 0, canvas.width, canvas.height);

  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  gl.enable(WebGLRenderingContext.DEPTH_TEST);

  // VERTEX SHADER.
  var vertexShader = gl.createShader(WebGLRenderingContext.VERTEX_SHADER);
  gl.shaderSource(vertexShader, """attribute vec3 aVertexPosition;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;

void main(void) {
    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}""");

  gl.compileShader(vertexShader);

  // FRAGMENT SHADER.
  var fragmentShader = gl.createShader(WebGLRenderingContext.FRAGMENT_SHADER);
  gl.shaderSource(fragmentShader, """precision mediump float;

void main(void) {
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}""");

  gl.compileShader(fragmentShader);

  WebGLProgram shaderProgram = gl.createProgram();
  gl.attachShader(shaderProgram, vertexShader);
  gl.attachShader(shaderProgram, fragmentShader);
  gl.linkProgram(shaderProgram);
  gl.useProgram(shaderProgram);

  int vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
  gl.enableVertexAttribArray(vertexPositionAttribute);

  WebGLUniformLocation pMatrixUniform  = gl.getUniformLocation(shaderProgram, "uPMatrix");
  WebGLUniformLocation mvMatrixUniform  = gl.getUniformLocation(shaderProgram, "uMVMatrix");

// init buffers
  WebGLBuffer triangleVertexPositionBuffer = gl.createBuffer();
  gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, triangleVertexPositionBuffer);
  var vertices = [
                  0.0,  1.0,  0.0,
                  -1.0, -1.0,  0.0,
                  1.0, -1.0,  0.0
                  ];
  Float32Array floa = new Float32Array.fromList(vertices);
  gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, floa, WebGLRenderingContext.STATIC_DRAW);

  gl.enable(WebGLRenderingContext.DEPTH_TEST);

  // draw  scene
  gl.viewport(0, 0, canvas.width, canvas.height);
  gl.clearColor(1.0, 0.0, 0.0, 1.0);
  gl.clear(WebGLRenderingContext.COLOR_BUFFER_BIT | WebGLRenderingContext.DEPTH_BUFFER_BIT);


  var zNear = 0.1;
  var zFar = 10000;
  double yTop = Math.tan(45.0 * Math.PI / 180.0 / 2.0) * zNear;
  double xRight = canvas.width / canvas.height * yTop;
  double zDepth = zFar - zNear;

  var row1 = new vec4(zNear / xRight, 0.0, 0.0, 0.0);
  var row2 = new vec4(0.0, zNear / yTop, 0.0, 0.0);
  var row3 = new vec4(0.0, 0.0, -(zFar + zNear) / zDepth, -(2 * zNear * zFar) / zDepth);
  var row4 = new vec4(0.0, 0.0, -1.0, 0.0);

  var pMatrix = new mat4(row1, row2, row3, row4);

  var mvMatrix = new mat4.translation(new vec3(-1.5, 0.0, -7.0));

  gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, triangleVertexPositionBuffer);
  gl.vertexAttribPointer(vertexPositionAttribute, 3, WebGLRenderingContext.FLOAT, false, 0, 0);
  gl.uniformMatrix4fv(pMatrixUniform, false, pMatrix.copyAsArray());
  gl.uniformMatrix4fv(mvMatrixUniform, false, mvMatrix.copyAsArray());
  gl.drawArrays(WebGLRenderingContext.TRIANGLES, 0, 3);

}

So, how do I create perspectives in DartVectorMath?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:37:25+00:00Added an answer on June 15, 2026 at 8:37 am

    See makePerspective found in src/common/opengl.dart.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.