Permalink
Browse files

texturing is nearly complete, just not rendering

  • Loading branch information...
1 parent 2b995ec commit 3a28720bffe740c5b61a7dcef6222db274d9d8c1 @tmpvar committed Aug 21, 2011
View
Binary file not shown.
View
@@ -10,6 +10,7 @@
#include "WebGL.h"
#include "module.h"
#include <stdlib.h>
+#include <string>
/* The class of the global object. */
static JSClass global_class = {
@@ -61,6 +62,75 @@ JSBool js_get_file_contents(JSContext *cx, uintN argc, jsval *argv) {
return JS_TRUE;
}
+JSBool js_readimage(JSContext *cx, uintN argc, jsval *argv) {
+ JSString *filename_string;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "S", &filename_string)) {
+ return JS_FALSE;
+ }
+
+ const char *filename = JS_EncodeString(cx, filename_string);
+
+ GLFWimage img;
+ if (!glfwReadImage(filename, &img, NULL)) {
+ JS_ReportError(cx, "Image not found: %s", filename);
+ return JS_FALSE;
+ }
+
+ JSObject *ret = JS_NewObject(cx, NULL, NULL, NULL);
+ unsigned int length = img.Height*img.Width*img.BytesPerPixel;
+ char c_length[10];
+ sprintf(c_length, "%d", length);
+
+ // TODO: there has to be a better way!
+ jsval array;
+ string cpp_string = "new Uint32Array(";
+ cpp_string += c_length;
+ cpp_string += ");";
+ cout << cpp_string << endl;
+ const char *src = cpp_string.c_str();
+
+ if (!JS_EvaluateScript(cx, JS_GetGlobalObject(cx), src, strlen(src), __FILE__, __LINE__, &array)) {
+ return JS_FALSE;
+ }
+
+ JS_ASSERT(array);
+
+ JSObject *typedArrayObject = JSVAL_TO_OBJECT(array);
+
+ if (!js_IsTypedArray(typedArrayObject)) {
+ JS_ReportError(cx, "array is not a typed array!");
+ return JS_FALSE;
+ }
+
+ js::TypedArray *tarray = js::TypedArray::fromJSObject(typedArrayObject);
+ memcpy(tarray->data, img.Data, length);
+
+ if (!JS_SetProperty(cx, ret, "data", &array)) {
+ return JS_FALSE;
+ }
+
+ jsval width = INT_TO_JSVAL(img.Width);
+ if (!JS_SetProperty(cx, ret, "width", &width)) {
+ return JS_FALSE;
+ }
+
+ jsval height = INT_TO_JSVAL(img.Height);
+ if (!JS_SetProperty(cx, ret, "height", &height)) {
+ return JS_FALSE;
+ }
+
+ jsval bytesPerPixel = INT_TO_JSVAL(img.BytesPerPixel);
+ if (!JS_SetProperty(cx, ret, "bytesPerPixel", &bytesPerPixel)) {
+ return JS_FALSE;
+ }
+
+
+
+ JS_SET_RVAL(cx, argv, OBJECT_TO_JSVAL(ret));
+ return JS_TRUE;
+}
+
JSBool js_abort(JSContext *cx, uintN argc, jsval *argv) {
c_exit(-1);
}
@@ -83,6 +153,7 @@ static JSFunctionSpec module_global_functions[] = {
JS_FS("stdout_print", stdout_print, 1, 0),
JS_FS("fail", js_abort, 1, 0),
JS_FS("readfile", js_get_file_contents, 1, 0),
+ JS_FS("readimage", js_readimage, 1, 0),
JS_FS("webgl_rendering_context_getContextAttributes", webgl_rendering_context_getContextAttributes, 0 /* */, 0),
JS_FS("webgl_rendering_context_isContextLost", webgl_rendering_context_isContextLost, 0 /* */, 0),
JS_FS("webgl_rendering_context_getSupportedExtensions", webgl_rendering_context_getSupportedExtensions, 0 /* */, 0),
@@ -109,7 +180,8 @@ static JSFunctionSpec module_global_functions[] = {
JS_FS("webgl_rendering_context_clearStencil", webgl_rendering_context_clearStencil, 1 /* GLint s */, 0),
JS_FS("webgl_rendering_context_colorMask", webgl_rendering_context_colorMask, 4 /* GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha */, 0),
JS_FS("webgl_rendering_context_compileShader", webgl_rendering_context_compileShader, 1 /* WebGLShader shader */, 0),
- JS_FS("webgl_rendering_context_copyTexImage2D", webgl_rendering_context_copyTexImage2D, 8 /* GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border */, 0),
+ JS_FS("webgl_rendering_context_copyTexImage2D", webgl_rendering_context_texImage2D, 8 /* GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border */, 0),
+ JS_FS("webgl_rendering_context_texImage2D_Image", webgl_rendering_context_texImage2D_Image, 6, 0),
JS_FS("webgl_rendering_context_copyTexSubImage2D", webgl_rendering_context_copyTexSubImage2D, 8 /* GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height */, 0),
JS_FS("webgl_rendering_context_createBuffer", webgl_rendering_context_createBuffer, 0 /* */, 0),
JS_FS("webgl_rendering_context_createFramebuffer", webgl_rendering_context_createFramebuffer, 0 /* */, 0),
View
@@ -1,13 +1,23 @@
var webgl = require('../lib/webgl.js');
+var Image = webgl.DOMImage;
var vertices = [
- 0.75, 0.75, 0.0, 1,
- 0.75, -0.75, 0.0, 1,
- -0.75, -0.75, 0.0, 1,
+ 0.8, 0.8, 0.0,
+ -0.8, 0.8, 0.0,
+ 0.8, -0.8, 0.0,
+ -0.8, -0.8, 0.0
];
+var textureCoords = [
+ 0.0, 0.0,
+ 1.0, 0.0,
+ 1.0, 1.0,
+ 0.0, 1.0,
+];
+
+
function e(name, result) {
- var e = ctx.getError()
+ var e = gl.getError()
if (e) {
console.log(name, 'failed!', e);
fail();
@@ -17,50 +27,87 @@ function e(name, result) {
return result;
}
-var ctx = new webgl.WebGLRenderingContext();
-var program = e('create program', ctx.createProgram());
+var gl = new webgl.WebGLRenderingContext();
+var program = e('create program', gl.createProgram());
var shaders = {
- vertex : "attribute vec3 pos;\nvoid main() {\n gl_Position = vec4(pos, 1.0);\n}",
- frag : "void main() {\n gl_FragColor = vec4(1.0, 0, 1, 1.0);\n}"
+ frag : "#ifdef GL_ES\nprecision highp float;\n#endif\n\nvarying vec2 vTextureCoord;\n\nuniform sampler2D uSampler;\n\nvoid main(void) {\n gl_FragColor = texture2D(uSampler, vec2(0, 0));\n}",
+ vertex : "attribute vec3 aVertexPosition;\nattribute vec2 aTextureCoord;varying vec2 vTextureCoord;\n\n\nvoid main(void) {\n gl_Position = vec4(aVertexPosition, 1.0);\n vTextureCoord = aTextureCoord;\n}"
+};
+
+var vertexShader = e('create shader', gl.createShader(gl.VERTEX_SHADER));
+var fragShader = e('create shader', gl.createShader(gl.FRAGMENT_SHADER));
+
+e('shader source', gl.shaderSource(vertexShader, shaders.vertex));
+e('shader source', gl.shaderSource(fragShader, shaders.frag));
+e('compile shader', gl.compileShader(vertexShader));
+e('compile shader', gl.compileShader(fragShader));
+e('attach shader', gl.attachShader(program, vertexShader));
+e('attach shader', gl.attachShader(program, fragShader));
+e('link program', gl.linkProgram(program));
+
+// Setup texture
+var img = new Image();
+var texture;
+img.onload = function() {
+ texture = e('create texture', gl.createTexture());
+
+ e('bind texture', gl.bindTexture(gl.TEXTURE_2D, texture));
+ e('pixel store', gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true));
+
+ e('tex image 2d', gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, img));
+ e('tex param mag filter', gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST));
+ e('tex param min filter', gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST));
+ e('bind texture', gl.bindTexture(gl.TEXTURE_2D, null));
};
-var vertexShader = e('create shader', ctx.createShader(ctx.VERTEX_SHADER));
-var fragShader = e('create shader', ctx.createShader(ctx.FRAGMENT_SHADER));
+// TODO: this will not always be sync!
+img.src = "../example/white32.tga";
+
+var squareTextureBuffer = e('create texture buffer', gl.createBuffer());
+e('bind texture buffer', gl.bindBuffer(gl.ARRAY_BUFFER, squareTextureBuffer));
+e('buffer texture coords', gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW));
-e('shader source', ctx.shaderSource(vertexShader, shaders.vertex));
-e('shader source', ctx.shaderSource(fragShader, shaders.frag));
-e('compile shader', ctx.compileShader(vertexShader));
-e('compile shader', ctx.compileShader(fragShader));
-e('attach shader', ctx.attachShader(program, vertexShader));
-e('attach shader', ctx.attachShader(program, fragShader));
-e('link program', ctx.linkProgram(program));
-if (!ctx.getProgramParameter(program, ctx.LINK_STATUS)) {
- console.log("Could not link program.\n Error:", ctx.getError());
- console.log(ctx.getProgramInfoLog(program));
+if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
+ console.log("Could not link program.\n Error:", gl.getError());
+ console.log(gl.getProgramInfoLog(program));
fail();
}
-var attr = e('attribute location', ctx.getAttribLocation(program, "pos"));
+var attr = e('attribute location', gl.getAttribLocation(program, "aVertexPosition"));
+var textureAttr = e('texture location', gl.getAttribLocation(program, "aTextureCoord"));
-var vertexBuffer = e('create buffer', ctx.createBuffer());
-e('bind buffer', ctx.bindBuffer(ctx.ARRAY_BUFFER, vertexBuffer));
-e('buffer data', ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(vertices), ctx.STATIC_DRAW));
+var samplerUniform = e('get uSampler uniform', gl.getUniformLocation(program, "uSampler"));
+var vertexBuffer = e('create buffer', gl.createBuffer());
+e('bind buffer', gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer));
+e('buffer data', gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW));
-e('viewport', ctx.viewport(0, 0, 300, 300));
-e('use program', ctx.useProgram(program));
-e('enable vertex attrib array', ctx.enableVertexAttribArray(attr));
+e('viewport', gl.viewport(0, 0, 300, 300));
-var a = 10000;
+e('use program', gl.useProgram(program));
+e('enable vertex attrib array', gl.enableVertexAttribArray(attr));
+
+var a = 2000;
while(a--) {
- e('clear color', ctx.clearColor(0.5, 0.5, 0.5, 1));
- e('clear', ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT));
- e('bind buffer', ctx.bindBuffer(ctx.ARRAY_BUFFER, vertexBuffer));
- e('vertex pointer', ctx.vertexAttribPointer(attr, 3, ctx.FLOAT, false, 0, 0));
- e('draw arrays', ctx.drawArrays(ctx.TRIANGLES, 0, 3));
- e('flush', ctx.flush());
+
+
+ e('bind buffer', gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer));
+ e('vertex pointer', gl.vertexAttribPointer(attr, 3, gl.FLOAT, false, 0, 0));
+
+ e('bind texture buffer', gl.bindBuffer(gl.ARRAY_BUFFER, squareTextureBuffer));
+ e('vertex point texture', gl.vertexAttribPointer(textureAttr, 2, gl.FLOAT, false, 0, 0));
+
+ e('set active texture', gl.activeTexture(gl.TEXTURE0));
+ e('bind texture', gl.bindTexture(gl.TEXTURE_2D, texture));
+ e('uniformi', gl.uniform1i(samplerUniform, 0));
+
+ e('enable depth test', gl.enable(gl.DEPTH_TEST));
+ e('clear color', gl.clearColor(0.5, 0.5, 0.5, 1));
+ e('clear', gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT));
+ e('draw arrays', gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4));
+ e('flush', gl.flush());
}
console.log("DONE");
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
Oops, something went wrong.

0 comments on commit 3a28720

Please sign in to comment.