Permalink
Browse files

Loading doom texture into webgl.

  • Loading branch information...
1 parent 08a7544 commit ead753bc1e2fb33d0b67466aebbb0e3d313ab8ae @nicokruger committed Sep 26, 2011
Showing with 22 additions and 16 deletions.
  1. +13 −14 src/main/javascript/renderlib/renderutil_gl.js
  2. +9 −2 src/main/javascript/renderlib/screens/gl.js
@@ -7,12 +7,22 @@ renderutil_gl = function(gl, shaders) {
var cubeVertexPositionBuffer;
var cubeVertexTextureCoordBuffer;
var cubeVertexIndexBuffer;
- var neheTexture;
+ var texture;
var shader_map = {};
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() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
@@ -29,7 +39,7 @@ renderutil_gl = function(gl, shaders) {
gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.activeTexture(gl.TEXTURE0);
- gl.bindTexture(gl.TEXTURE_2D, neheTexture);
+ gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(shaderProgram.samplerUniform, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
@@ -148,18 +158,7 @@ renderutil_gl = function(gl, shaders) {
//
// INIT TEXTURES
//
- neheTexture = 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);
- }
+ texture = gl.createTexture();
gl.clearColor(1.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
@@ -23,12 +23,12 @@ screens.gl = function(game,width,height) {
console.log("SHADERS LOADED");
glutil.util = renderutil_gl(gl, shaders);
});
- //var glutil = renderutil_gl(gl);
// Temporary canvas for background image
var tmpctx = document.createElement("canvas").getContext("2d");
tmpctx.canvas.width = width + 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);
return {
@@ -37,14 +37,21 @@ screens.gl = function(game,width,height) {
},
create: function(sectors, x1, y1, x2, y2) {
+ var drawers = renderutil.scanPolys(_.map(sectors, function(s) { return s.poly }), x1,y1,x2,y2);
return {
- draw: function() {
+ draw: function(textures) {
if (!shaderloader.ready()) {
console.log("shaderloader not ready... not drawing");
return;
}
+ //renderutil.fillBuffer(drawers, textures, data);
+
+ for (var i = 0; i < width * height * 4; i++) {
+ data.data[i] = 255;
+ }
Timer.start("gl");
+ glutil.util.loadTexture(textures[0].imageData);
glutil.util.drawScene();
Timer.end();
}

0 comments on commit ead753b

Please sign in to comment.