forked from mikeseven/node-webgl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mikeseven#51 from bpedrosa/master
First Attempt to fix windows version
- Loading branch information
Showing
9 changed files
with
220 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ Debug | |
node_modules | ||
prebuilds/* | ||
angle/build/* | ||
example/*.ppm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
// Create context | ||
var width = 256 | ||
var height = 256 | ||
var gl = require('../index.js')(width, height) | ||
var createContext = require('../index'); | ||
var utils = require('./utils.js'); | ||
|
||
// Clear screen to red | ||
gl.clearColor(1.0, 1.0, 1.0, 1.0) | ||
gl.colorMask(true, true, true, true) | ||
gl.clear(gl.COLOR_BUFFER_BIT) | ||
function main() { | ||
// Create context | ||
var width = 64 | ||
var height = 64 | ||
var gl = createContext(width, height) | ||
|
||
// Write output | ||
var pixels = new Uint8Array(width * height * 4) | ||
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels) | ||
process.stdout.write(['P3\n# gl.ppm\n', width, ' ', height, '\n255\n'].join('')) | ||
for (var i = 0; i < pixels.length; i += 3) { | ||
process.stdout.write(pixels[i] + ' ' + pixels[i + 1] + ' ' + pixels[i + 2] + ' ') | ||
// Clear screen to red | ||
gl.clearColor(1.0, 0.0, 0.0, 1.0) | ||
gl.colorMask(true, true, true, true) | ||
gl.clear(gl.COLOR_BUFFER_BIT) | ||
|
||
utils.dumpBuffer(gl, width, height) | ||
|
||
gl.destroy() | ||
} | ||
|
||
gl.destroy() | ||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
var createContext = require('../index') | ||
var utils = require('./utils'); | ||
|
||
function main() { | ||
// Create context | ||
var width = 64 | ||
var height = 64 | ||
var gl = createContext(width, height) | ||
|
||
var vertex_src = [ | ||
'attribute vec2 a_position;', | ||
'void main() {', | ||
'gl_Position = vec4(a_position, 0, 1);', | ||
'}' | ||
].join('\n') | ||
|
||
var fragment_src = [ | ||
'void main() {', | ||
'gl_FragColor = vec4(0, 1, 0, 1); // green', | ||
'}' | ||
].join('\n') | ||
|
||
// setup a GLSL program | ||
var program = utils.createProgramFromSources(gl, [vertex_src, fragment_src]); | ||
|
||
if(!program) { | ||
return; | ||
} | ||
gl.useProgram(program); | ||
|
||
// look up where the vertex data needs to go. | ||
var positionLocation = gl.getAttribLocation(program, "a_position"); | ||
|
||
// Create a buffer and put a single clipspace rectangle in | ||
// it (2 triangles) | ||
var buffer = gl.createBuffer(); | ||
gl.bindBuffer(gl.ARRAY_BUFFER, buffer); | ||
gl.bufferData( | ||
gl.ARRAY_BUFFER, | ||
new Float32Array([ | ||
-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.STATIC_DRAW); | ||
gl.enableVertexAttribArray(positionLocation); | ||
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0); | ||
|
||
// draw | ||
gl.drawArrays(gl.TRIANGLES, 0, 6); | ||
|
||
utils.dumpBuffer(gl, width, height) | ||
|
||
gl.destroy() | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
function dumpBuffer(gl, width, height) { | ||
// Write output | ||
var pixels = new Uint8Array(width * height * 4) | ||
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels) | ||
process.stdout.write(['P3\n# gl.ppm\n', width, ' ', height, '\n255\n'].join('')) | ||
for (var i = 0; i < pixels.length; i += 4) { | ||
process.stdout.write(pixels[i] + ' ' + pixels[i + 1] + ' ' + pixels[i + 2] + ' ') | ||
} | ||
} | ||
|
||
function drawTriangle(gl) { | ||
var buffer = gl.createBuffer() | ||
gl.bindBuffer(gl.ARRAY_BUFFER, buffer) | ||
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-2, -2, -2, 4, 4, -2]), gl.STREAM_DRAW) | ||
gl.enableVertexAttribArray(0) | ||
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0) | ||
gl.drawArrays(gl.TRIANGLES, 0, 3) | ||
gl.bindBuffer(gl.ARRAY_BUFFER, null) | ||
gl.disableVertexAttribArray(0) | ||
gl.deleteBuffer(buffer) | ||
} | ||
|
||
function loadShader(gl, shaderSource, shaderType) { | ||
var shader = gl.createShader(shaderType); | ||
gl.shaderSource(shader, shaderSource); | ||
gl.compileShader(shader); | ||
|
||
// Check the compile status | ||
var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS); | ||
if (!compiled) { | ||
// Something went wrong during compilation; get the error | ||
var lastError = gl.getShaderInfoLog(shader); | ||
console.log("*** Error compiling shader '" + shader + "':" + lastError); | ||
gl.deleteShader(shader); | ||
return null; | ||
} | ||
|
||
return shader; | ||
} | ||
|
||
function createProgram(gl, shaders, opt_attribs, opt_locations) { | ||
var program = gl.createProgram(); | ||
shaders.forEach(function(shader) { | ||
gl.attachShader(program, shader); | ||
}); | ||
if (opt_attribs) { | ||
obj_attrib.forEach(function(attrib, ndx) { | ||
gl.bindAttribLocation( | ||
program, | ||
opt_locations ? opt_locations[ndx] : ndx, | ||
attrib); | ||
}); | ||
} | ||
gl.linkProgram(program); | ||
|
||
// Check the link status | ||
var linked = gl.getProgramParameter(program, gl.LINK_STATUS); | ||
if (!linked) { | ||
// something went wrong with the link | ||
var lastError = gl.getProgramInfoLog(program); | ||
console.log("Error in program linking:" + lastError); | ||
|
||
gl.deleteProgram(program); | ||
return null; | ||
} | ||
return program; | ||
} | ||
|
||
function createProgramFromSources(gl, shaderSources, opt_attribs, opt_locations) { | ||
var defaultShaderType = [ | ||
"VERTEX_SHADER", | ||
"FRAGMENT_SHADER", | ||
]; | ||
|
||
var shaders = []; | ||
for (var ii = 0; ii < shaderSources.length; ++ii) { | ||
shaders.push(loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]])); | ||
} | ||
return createProgram(gl, shaders, opt_attribs, opt_locations); | ||
} | ||
|
||
|
||
module.exports.dumpBuffer = dumpBuffer; | ||
module.exports.drawTriangle = drawTriangle; | ||
module.exports.loadShader = loadShader; | ||
module.exports.createProgram = createProgram; | ||
module.exports.createProgramFromSources = createProgramFromSources; |