Skip to content

Commit

Permalink
Loading doom texture into webgl.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokruger committed Sep 26, 2011
1 parent 08a7544 commit ead753b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
27 changes: 13 additions & 14 deletions src/main/javascript/renderlib/renderutil_gl.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ renderutil_gl = function(gl, shaders) {
var cubeVertexPositionBuffer; var cubeVertexPositionBuffer;
var cubeVertexTextureCoordBuffer; var cubeVertexTextureCoordBuffer;
var cubeVertexIndexBuffer; var cubeVertexIndexBuffer;
var neheTexture; var texture;
var shader_map = {}; var shader_map = {};




var module = { var module = {


loadTexture: function(texture_data) {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.pixelStorei(gl.UNPACK_ALIGNMENT, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture_data);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
},

drawScene: function() { drawScene: function() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight); gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
Expand All @@ -29,7 +39,7 @@ renderutil_gl = function(gl, shaders) {
gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0); gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);


gl.activeTexture(gl.TEXTURE0); gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, neheTexture); gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(shaderProgram.samplerUniform, 0); gl.uniform1i(shaderProgram.samplerUniform, 0);


gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
Expand Down Expand Up @@ -148,18 +158,7 @@ renderutil_gl = function(gl, shaders) {
// //
// INIT TEXTURES // INIT TEXTURES
// //
neheTexture = gl.createTexture(); texture = gl.createTexture();
neheTexture.img = new Image();
neheTexture.img.src = "data/nehe.gif";
neheTexture.img.onload = new function() {
gl.bindTexture(gl.TEXTURE_2D, neheTexture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.pixelStorei(gl.UNPACK_ALIGNMENT, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, neheTexture.img);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
}


gl.clearColor(1.0, 0.0, 0.0, 1.0); gl.clearColor(1.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST); gl.enable(gl.DEPTH_TEST);
Expand Down
11 changes: 9 additions & 2 deletions src/main/javascript/renderlib/screens/gl.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ screens.gl = function(game,width,height) {
console.log("SHADERS LOADED"); console.log("SHADERS LOADED");
glutil.util = renderutil_gl(gl, shaders); glutil.util = renderutil_gl(gl, shaders);
}); });
//var glutil = renderutil_gl(gl);


// Temporary canvas for background image // Temporary canvas for background image
var tmpctx = document.createElement("canvas").getContext("2d"); var tmpctx = document.createElement("canvas").getContext("2d");
tmpctx.canvas.width = width + 1; tmpctx.canvas.width = width + 1;
tmpctx.canvas.height = height + 1; tmpctx.canvas.height = height + 1;
delete tmpctx; // free memory ? probably not, I reckon the imagedata reference will keep it alive
var data = tmpctx.createImageData(width + 1, height + 1); var data = tmpctx.createImageData(width + 1, height + 1);


return { return {
Expand All @@ -37,14 +37,21 @@ screens.gl = function(game,width,height) {
}, },


create: function(sectors, x1, y1, x2, y2) { create: function(sectors, x1, y1, x2, y2) {
var drawers = renderutil.scanPolys(_.map(sectors, function(s) { return s.poly }), x1,y1,x2,y2);
return { return {
draw: function() { draw: function(textures) {
if (!shaderloader.ready()) { if (!shaderloader.ready()) {
console.log("shaderloader not ready... not drawing"); console.log("shaderloader not ready... not drawing");
return; return;
} }


//renderutil.fillBuffer(drawers, textures, data);

for (var i = 0; i < width * height * 4; i++) {
data.data[i] = 255;
}
Timer.start("gl"); Timer.start("gl");
glutil.util.loadTexture(textures[0].imageData);
glutil.util.drawScene(); glutil.util.drawScene();
Timer.end(); Timer.end();
} }
Expand Down

0 comments on commit ead753b

Please sign in to comment.