简体   繁体   中英

Webgl - Create texture in program 1 and send it to program 2. No texture bound to unit 0

Im trying to create an alpha map in my first program to use in my second program. I feel like ive got it all set up correctly however im getting an error:

[.Offscreen-For-WebGL-0x7fc3281a4200]RENDER WARNING: there is no texture bound to the unit 0

Im using two separate classes since for these since the second program will sometimes run without the first masking it.

Both my programs are running on the same gl context one after the other. I run the setup code in order of first program then second, and then of course run the draw functions in the same order thereafter.

Setup of program 1 (Creating the alpha map):

// Get gl, add blending ect, then...

// The webgl variable below just holds webgl constants from https://google.github.io/closure-library/api/goog.webgl.html

this.targetTexture_ = this.gl_.createTexture();
this.gl_.activeTexture(webgl.TEXTURE0);
this.gl_.bindTexture(webgl.TEXTURE_2D, this.targetTexture_);
this.gl_.texImage2D(webgl.TEXTURE_2D, 0, webgl.RGBA, this.gl_.canvas.width,
    this.gl_.canvas.height, 0, webgl.RGBA, webgl.UNSIGNED_BYTE, null);

this.gl_.texParameteri(webgl.TEXTURE_2D, webgl.TEXTURE_MIN_FILTER, webgl.LINEAR);
this.gl_.texParameteri(webgl.TEXTURE_2D, webgl.TEXTURE_WRAP_S, webgl.CLAMP_TO_EDGE);
this.gl_.texParameteri(webgl.TEXTURE_2D, webgl.TEXTURE_WRAP_T, webgl.CLAMP_TO_EDGE);

this.textureFrameBuffer_ = this.gl_.createFramebuffer();
this.gl_.bindFramebuffer(webgl.FRAMEBUFFER, this.textureFrameBuffer_);
this.gl_.framebufferTexture2D(webgl.FRAMEBUFFER, webgl.COLOR_ATTACHMENT0, webgl.TEXTURE_2D, this.targetTexture_, 0);

// Now i set the maskTexture on the second programs class.
this.secondProgramClass.maskTexture = this.targetTexture_;

// Now i compile & attach the shaders and link the program

Then setup of program 2:

// Compile, attach, link up the program...

// This is the texture location to use.
this.maskTextureLocation_ =
    this.gl_.getUniformLocation(this.program_, LocationName.MASK);

Draw function of program 1:

this.gl_.bindFramebuffer(webgl.FRAMEBUFFER, this.textureFrameBuffer_);
this.gl_.bindTexture(webgl.TEXTURE_2D, this.targetTexture_);
this.gl_.viewport(0, 0, this.gl_.canvas.width, this.gl_.canvas.height);

// Clear to transparent
this.gl_.clearColor(0, 0, 0, 0);
this.gl_.clear(webgl.COLOR_BUFFER_BIT | webgl.DEPTH_BUFFER_BIT);

this.gl_.useProgram(this.program_);

// A helper function that runs: enableVertexAttribArray, bindBuffer, vertexAttribPointer on the objects location and buffer.
// In this case its just a square the size of the viewport.
renderBufferAttribute(this.gl_, this.position_);

// Draw triangles
this.gl_.drawArrays(webgl.TRIANGLES, 0, 6);

Draw function of program 2:

Now that this class has maskTexture set to it from the first classes setup and after the first class has run its draw function to the texture, i should be able to draw this program with the texture passed through, right?

this.gl_.bindFramebuffer(webgl.FRAMEBUFFER, null);

this.gl_.clearColor(0, 0, 0, 0);
this.gl_.clear(webgl.COLOR_BUFFER_BIT);

this.gl_.useProgram(this.program_);

// A helper function that runs: enableVertexAttribArray, bindBuffer, vertexAttribPointer on the objects location and buffer.
renderBufferAttribute(this.gl_, this.position_);

if (this.maskTexture) {
  this.gl_.activeTexture(this.gl_.TEXTURE0);
  this.gl_.bindTexture(webgl.TEXTURE_2D, this.maskTexture);
  this.gl_.uniform1i(this.maskTextureLocation_, 0);
}

// Draw triangles
this.gl_.drawArrays(webgl.TRIANGLES, 0,
    this.particleCount_.total * PARTICLE_ARRAY_COUNT / 2);

The shaders at the moment are probably irrelevant to this question, we can just assume they both render a block with the second one expecting:

// Stored in LocationName.MASK above.
uniform sampler2D u_mask;

I dont think im missing anything, but any direction would be appreciated. Im getting the error mentioned above on every draw cycle. Ill update with more details when needed.

Thanks!

Turns out i didnt even need to send the texture anywhere since its automatically bound to texture unit 0 on that webgl context. So once the first program has drawn to texture 0, the second program has the same data already on texture 0, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM