empaempa / GLOW

GLOW is a WebGL wrapper, which focuses on easy creation and use of shaders.
http://i-am-glow.com
MIT License
244 stars 28 forks source link

Texture load callback completing after draw command #14

Closed psulat closed 12 years ago

psulat commented 12 years ago

Thank you for making the code available. I'm new to webgl and javascript and it helping a lot.

When I tried doing anything simple with textures I'm getting all back textures. This can be repeated by using the hellocube sample code and changing setInterval( render, 1000 / 60) to just render(). You will see a black cube rendered. When I looked into this I found that the texture calls were being done after the draw call. The GL texture loading commands do not get called until the texture image has been fetched. However, there is nothing that makes the draw call wait for the texture loading to complete. You normally don't see this error because many rendering are done and only first one is not visible. In either case, there is a dependency here that is not being accounted for.

empaempa commented 12 years ago

Thanks for pointing this out and I understand what you mean. However, this is more or less by design. GLOW is merely a WebGL-wrapper, taking care of the low-level WebGL API stuff for you. I suggest using GLOW.Load() instead of creating a texture with a URL (which I guess you're doing right now). Something like...

new GLOW.Load( { myTexture: "theTexture.png", onLoadComplete: function( theLoadedData ) {
  var myShaderConfig = {
    // ...your primitives and indices here
    data: {
      uTexture: new GLOW.Texture( { data: theLoadedData.myTexture } )
      // ...your other data here
    }
  };

  var myShader = new GLOW.Shader( myShaderConfig );
} );

The best part of GLOW.Loader is that it's capable of loading and parsing .glsl files, so you can have your shaders in separate files. It can also load videos and JSON-files (with geometry data) and all in one go - simply add more key-value-pairs to the object passed to the constructor. Also, there's a "onLoadContext" which you can use to set "this" to the object taking care of the onLoadComplete function. For example...

new GLOW.Load( { texture: "texture.png", shaderDiffuse: "shaderDiffuse.glsl", shaderPhong: "shaderPhong.glsl", onLoadComplete: onLoadHandler.onLoadComplete, onLoadContext: onLoadHandler } );

This will call the onLoadComplete function on your onLoadHandler object, setting "this" to onLoadHandler. The data passsed to onLoadComplete will look like:

{
  texture: ...an HTMLImageElement,
  shaderDiffuse: {
    fragmentShader: ...a string with the fragment shader code
    vertexShader: ...a string with the vertex shader code
  },
  shaderPhong: {
    fragmentShader: ...a string with the fragment shader code
    vertexShader: ...a string with the vertex shader code
  }
}

Hope that helps and thanks for trying GLOW :D

psulat commented 12 years ago

Mikael,

Thank you so much for your reply. You are right, I was trying to create a texture with a URL. I didn't realize that you had that type of load functionality. You have built in a lot of conveniences into GLOW! I'm learning a lot going through the code. I come from the C, C++ graphics background and I'm new to javascript. Webgl has sparked my interest in web development now that high performance apps are possible. GLOW is saving me a lot of time. Thanks for making this project open source!

Pete

-----Original Message----- From: Mikael Emtinger [mailto:reply@reply.github.com] Sent: Thursday, June 14, 2012 2:32 AM To: psulat Subject: Re: [GLOW] Texture load callback completing after draw command (#14)

Thanks for pointing this out and I understand what you mean. However, this is more or less by design. GLOW is merely a WebGL-wrapper, taking care of the low-level WebGL API stuff for you. I suggest using GLOW.Load() instead of creating a texture with a URL (which I guess you're doing right now). Something like...

new GLOW.Load( { myTexture: "theTexture.png", onLoadComplete: function( theLoadedData ) {
  var myShaderConfig = {
    // ...your primitives and indices here
    data: {
      uTexture: new GLOW.Texture( { data: theLoadedData.myTexture } )
      // ...your other data here
    }
  };

  var myShader = new GLOW.Shader( myShaderConfig ); } ); ```

The best part of GLOW.Loader is that it's capable of loading and parsing .glsl files, so you can have your shaders in separate files. It can also load videos and JSON-files (with geometry data) and all in one go - simply add more key-value-pairs to the object passed to the constructor. Also, there's a "onLoadContext" which you can use to set "this" to the object taking care of the onLoadComplete function. For example...  

```javascript
new GLOW.Load( { texture: "texture.png", shaderDiffuse: "shaderDiffuse.glsl", shaderPhong: "shaderPhong.glsl", onLoadComplete: onLoadHandler.onLoadComplete, onLoadContext: onLoadHandler } ); ```

This will call the onLoadComplete function on your onLoadHandler object, setting "this" to onLoadHandler. The data passsed to onLoadComplete will look like:

```javascript
{
  texture: ...an HTMLImageElement,
  shaderDiffuse: {
    fragmentShader: ...a string with the fragment shader code
    vertexShader: ...a string with the vertex shader code
  },
  shaderPhong: {
    fragmentShader: ...a string with the fragment shader code
    vertexShader: ...a string with the vertex shader code
  }
}

Hope that helps and thanks for trying GLOW :D


Reply to this email directly or view it on GitHub: https://github.com/empaempa/GLOW/issues/14#issuecomment-6321984

empaempa commented 12 years ago

Great to hear that you as a C/C++ coder is taking the step towards the web! I come from the same background and switched like 6-7 years ago - the web is where IT happens these days.

If you're all new to JavaScript I suggest you buy Douglas Crockford's book "JavaScript - The Good Parts" or simply watch him on http://yuiblog.com/crockford/. JavaScript is quite a quirky language :) I'm not sure GLOW's code is very high standard, but I've tried my best.

Good luck!

psulat commented 12 years ago

Thanks Mikael! Actually, Crockford's book and videos were the first things I read and viewed. Although, I couldn't get through the book on the first try, I was immediately impressed with the potential of the language.

I have to agree that web it where IT is at. With webgl, node.js, mvc frameworks, svg, client-side storage... it seems like a perfect storm, in a good way. I'm quite excited by the potential. I'm actually working nights and weekends to try to transition away from my day job and start a photo related web app.

I'm evaluating coffeescript to handle those javascript quirks.

Pete

-----Original Message----- From: Mikael Emtinger [mailto:reply@reply.github.com] Sent: Friday, June 15, 2012 2:15 AM To: psulat Subject: Re: [GLOW] Texture load callback completing after draw command (#14)

Great to hear that you as a C/C++ coder is taking the step towards the web! I come from the same background and switched like 6-7 years ago - the web is where IT happens these days.

If you're all new to JavaScript I suggest you buy Douglas Crockford's book "JavaScript - The Good Parts" or simply watch him on http://yuiblog.com/crockford/. JavaScript is quite a quirky language :) I'm not sure GLOW's code is very high standard, but I've tried my best.

Good luck!


Reply to this email directly or view it on GitHub: https://github.com/empaempa/GLOW/issues/14#issuecomment-6350758

empaempa commented 12 years ago

Cool! If you'd like to use GLOW in your web app, it's free of charge. Please let me know if you do and get there. Good luck :D

empaempa commented 12 years ago

Hey! This was just added...

https://github.com/empaempa/GLOW/pull/16

It might be something you'd like to use :)

psulat commented 12 years ago

Thanks for thinking of me. I'll certainly make use of that.

-----Original Message----- From: Mikael Emtinger [mailto:reply@reply.github.com] Sent: Friday, July 13, 2012 8:56 AM To: psulat Subject: Re: [GLOW] Texture load callback completing after draw command (#14)

Hey! This was just added...

https://github.com/empaempa/GLOW/pull/16

It might be something you'd like to use :)


Reply to this email directly or view it on GitHub: https://github.com/empaempa/GLOW/issues/14#issuecomment-6963727