Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 14 additions & 153 deletions examples/2-triangle/triangle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Example was taken from http://learningwebgl.com/lessons/lesson04/index.html.

var gles2 = require('../../gles2');

var WebGLUtils = require('../WebGLUtils.js');
var glMatrix = require('../gl-matrix.js');
var mat4 = glMatrix.mat4;

var options = {width: 1280, height: 720, title: "Triangle", fullscreen: false};
var options = {width: 1280, height: 720, title: "Single Triangle", fullscreen: false};
var gl = gles2.init(options);

function webGLStart() {
Expand All @@ -17,7 +16,7 @@ function webGLStart() {

while(true) {
drawScene();
animate();
//animate();
gles2.nextFrame();
}
}
Expand Down Expand Up @@ -89,7 +88,6 @@ function initShaders() {
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
}


var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();
Expand All @@ -110,20 +108,18 @@ function mvPopMatrix() {

function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
console.log("setMatrixUniforms: error=" + gl.getError());
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
console.log("setMatrixUniforms: error=" + gl.getError());
}


function degToRad(degrees) {
return degrees * Math.PI / 180;
}


var pyramidVertexPositionBuffer;
var pyramidVertexColorBuffer;
var cubeVertexPositionBuffer;
var cubeVertexColorBuffer;
var cubeVertexIndexBuffer;

function initBuffers() {
pyramidVertexPositionBuffer = gl.createBuffer();
Expand All @@ -132,149 +128,37 @@ function initBuffers() {
// Front face
0.0, 1.0, 0.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,

// Right face
0.0, 1.0, 0.0,
1.0, -1.0, 1.0,
1.0, -1.0, -1.0,

// Back face
0.0, 1.0, 0.0,
1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,

// Left face
0.0, 1.0, 0.0,
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0
1.0, -1.0, 1.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
pyramidVertexPositionBuffer.itemSize = 3;
pyramidVertexPositionBuffer.numItems = 12;
pyramidVertexPositionBuffer.numItems = 3;

pyramidVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pyramidVertexColorBuffer);
var colors = [
// Front face
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,

// Right face
1.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 1.0, 0.0, 1.0,

// Back face
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,

// Left face
1.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 1.0, 0.0, 1.0
0.0, 0.0, 1.0, 1.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
pyramidVertexColorBuffer.itemSize = 4;
pyramidVertexColorBuffer.numItems = 12;


cubeVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
vertices = [
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,

// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,

// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,

// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,

// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,

// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
cubeVertexPositionBuffer.itemSize = 3;
cubeVertexPositionBuffer.numItems = 24;

cubeVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexColorBuffer);
colors = [
[1.0, 0.0, 0.0, 1.0], // Front face
[1.0, 1.0, 0.0, 1.0], // Back face
[0.0, 1.0, 0.0, 1.0], // Top face
[1.0, 0.5, 0.5, 1.0], // Bottom face
[1.0, 0.0, 1.0, 1.0], // Right face
[0.0, 0.0, 1.0, 1.0] // Left face
];
var unpackedColors = [];
for (var i in colors) {
var color = colors[i];
for (var j=0; j < 4; j++) {
unpackedColors = unpackedColors.concat(color);
}
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW);
cubeVertexColorBuffer.itemSize = 4;
cubeVertexColorBuffer.numItems = 24;

cubeVertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
var cubeVertexIndices = [
0, 1, 2, 0, 2, 3, // Front face
4, 5, 6, 4, 6, 7, // Back face
8, 9, 10, 8, 10, 11, // Top face
12, 13, 14, 12, 14, 15, // Bottom face
16, 17, 18, 16, 18, 19, // Right face
20, 21, 22, 20, 22, 23 // Left face
];
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
cubeVertexIndexBuffer.itemSize = 1;
cubeVertexIndexBuffer.numItems = 36;
pyramidVertexColorBuffer.numItems = 3;
console.log("initBuffers: error=" + gl.getError());
}


var rPyramid = 0;
var rCube = 0;

function drawScene() {
gl.viewport(0, 0, 1280, 720);
gl.viewport(0, 0, options.width, options.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

mat4.perspective(45, 1280 / 720, 0.1, 100.0, pMatrix);
mat4.perspective(45, options.width / options.height, 0.1, 100.0, pMatrix);

mat4.identity(mvMatrix);

mat4.translate(mvMatrix, [-1.5, 0.0, -8.0]);

mvPushMatrix();
mat4.translate(mvMatrix, [0.0, 0.0, -8.0]);
mat4.rotate(mvMatrix, degToRad(rPyramid), [0, 1, 0]);

gl.bindBuffer(gl.ARRAY_BUFFER, pyramidVertexPositionBuffer);
Expand All @@ -286,26 +170,7 @@ function drawScene() {
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLES, 0, pyramidVertexPositionBuffer.numItems);

mvPopMatrix();


mat4.translate(mvMatrix, [3.0, 0.0, 0.0]);

mvPushMatrix();
mat4.rotate(mvMatrix, degToRad(rCube), [1, 1, 1]);

gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, cubeVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

mvPopMatrix();

gl.bindBuffer(gl.ARRAY_BUFFER, null); // cleanup GL state
}


Expand All @@ -317,13 +182,9 @@ function animate() {
var elapsed = timeNow - lastTime;

rPyramid += (90 * elapsed) / 1000.0;
rCube -= (75 * elapsed) / 1000.0;
}
lastTime = timeNow;
}


webGLStart();



5 changes: 1 addition & 4 deletions examples/3-box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function initTexture() {
var Canvas = require('canvas');
var img = new Canvas.Image();
img.src = res;
var canvas = new Canvas(img.width, img.height);
var canvas = Canvas.createCanvas(img.width, img.height);
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);

Expand Down Expand Up @@ -385,6 +385,3 @@ function animate() {
}

webGLStart();



12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"author": "Metrological, Bas van Meurs <b.van.meurs@metrological.com>",
"name": "wpe-webgl",
"description": "Initializes a full-screen display on the RPi on which OpenGL ES2 graphics can be drawn using a WebGL-compliant interface.",
"version": "0.0.5",
"description": "Initializes a full-screen display under NodeJS on which OpenGL ES2 graphics can be drawn using a WebGL-compliant interface.",
"version": "0.0.6",
"main": "gles2.js",
"keywords": [
"webgl",
Expand All @@ -13,12 +13,12 @@
"pi",
"x11"
],
"license" : "BSD-2-Clause",
"dependencies": {
"nan": "^2.1.0"
"license": "BSD-2-Clause",
"devDependencies": {
"nan": "^2.14.0"
},
"engines": {
"node" : ">=4.0.0"
"node": ">=4.0.0"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "interface/webgl.h"

extern "C" {
void init(Handle<Object> target)
void init(Local<Object> target)
{
atexit(gles2platform::AtExit);
atexit(webgl::WebGLRenderingContext::AtExit);
Expand Down
12 changes: 6 additions & 6 deletions src/gles2platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ using namespace std;
NAN_METHOD(init) {
Nan::HandleScope scope;

int width = info[0]->Int32Value();
int height = info[1]->Int32Value();
bool fullscreen = info[2]->BooleanValue();
int width = Nan::To<uint32_t>(info[0]).FromMaybe(0);
int height = Nan::To<uint32_t>(info[1]).FromMaybe(0);
bool fullscreen = Nan::To<bool>(info[2]).FromMaybe(true);

Nan::Utf8String title(info[3]->ToString());
unsigned int layer = info[4]->Uint32Value();
Nan::Utf8String title(info[3]);
unsigned int layer = Nan::To<uint32_t>(info[4]).FromMaybe(0);

std::string message = gles2impl::init(width, height, fullscreen, *title, layer);
if (message.size()) {
Expand All @@ -32,7 +32,7 @@ NAN_METHOD(init) {
NAN_METHOD(nextFrame) {
Nan::HandleScope scope;

bool swapBuffers = info[0]->BooleanValue();
bool swapBuffers = Nan::To<bool>(info[0]).FromMaybe(true);

gles2impl::nextFrame(swapBuffers);

Expand Down
Loading