Jump to content

Texture "disappears"


mightymarcus
 Share

Recommended Posts

I am very new to OpenGL and to get into it I started coding a little GUI on top of BJS. 

Before the scene.render() I draw some textured triangles (a spritesheet with a font) to a framebuffer object that is connected to a BJS-Texture which I use on a regular BJS Plane Mesh.

It all works as desired, when I draw to the texture before the first scene.render() call. But when I want to update the text (drawing again to the texture) on the next scene.render() call it gives me:

There is no texture bound to the unit 0.

The strange thing is, when I create a new Texture that is not connected to anything, just creating it, it works. And it has to be a texture that was not loaded before (is not in the cache).

    

	public static function update()
	{
		for (props in _textsToDraw)
		{
			var texture = new Texture("textures/" + _textures[_texturenum], Gfx.scene);
			var instance:TextRenderer = props.instance;
			instance._drawText(cast props.text, cast props.posX, cast props.posY);
			_texturenum++;		
		}
		
		_textsToDraw.splice(0, _textsToDraw.length);
	}


So I think on the scene.render() function it happens something like that the texture with the font is overriden or is changing somehow. I really can't tell.

Because I am just starting with OpenGL/WebGL I have not really the idea of changing states and what exactly happens "inside" BJS.

Maybe someone can give me a pointer how I could solve this problem with the "disappearing" texture.

Once again what happens:

1. Drawing to the framebuffer that is connected to the BJs Texture.

2. first scene.render(); call

OK. The texture is updated with the text.

3. Drawing to the framebuffer again

4. scene.render()

There is no texture bound to the unit 0 (it has to be the font texture, becaus when I just draw the background which has no texture it works)

But doing

1. Drawing to the framebuffer that is connected to the BJs Texture.

2. first scene.render(); call

OK. The texture is updated with the text.

3. creating a temporary BJS Texture before drawing to the framebuffer

4. scene.render()

5. It is working ...

This is the relevant code that renders the buffers..

	function _render()
	{
		GL.enable(GL.BLEND);
		GL.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);		
		GL.viewport(0, 0, 512, 512);		
		GL.clearColor(0.0, 0.0, 0.0, 1.0);
		GL.colorMask(true, true, true, true);
		GL.disable(GL.DEPTH_TEST);
		GL.disable(GL.CULL_FACE);		
		GL.clear(GL.COLOR_BUFFER_BIT);
		//GL.blendEquation(GL.FUNC_ADD);
		
		GL.bindFramebuffer(GL.FRAMEBUFFER, _textureFramebuffer);		
		
		_drawBackground();
		_drawContent();
		
		GL.bindFramebuffer(GL.FRAMEBUFFER, null);		
		GL.useProgram(null);
		GL.enable(GL.DEPTH_TEST);
		GL.enable(GL.CULL_FACE);
	}	
	
	function _drawContent()
	{
		GL.useProgram(_textContentProgram);
		
		var matrix = Matrix4.createOrtho(0, 512, 512, 0, -1000, 1000);
		GL.uniformMatrix4fv(_textContentMatrixUniform, false, matrix);
		
		GL.uniform4fv(_textContentTextColorUniform, new Float32Array([_textColor.r, _textColor.g, _textColor.b, _textColor.a]));
		
		GL.activeTexture(GL.TEXTURE0);
		
		GL.bindTexture (GL.TEXTURE_2D, _fontTexture);
		
		GL.bindBuffer(GL.ARRAY_BUFFER, _contentVertexBuffer);
								
		GL.enableVertexAttribArray(_textContentVertexAttr);
		GL.enableVertexAttribArray(_textContentTextureAttr);
		GL.vertexAttribPointer (_textContentVertexAttr, 3, GL.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 0);
		GL.vertexAttribPointer (_textContentTextureAttr, 2, GL.FLOAT, false, 5 * Float32Array.BYTES_PER_ELEMENT, 3 * Float32Array.BYTES_PER_ELEMENT);
		
		GL.drawArrays(GL.TRIANGLES, 0, _numVertices);
		
		GL.disableVertexAttribArray(_textContentVertexAttr);
		GL.disableVertexAttribArray(_textContentTextureAttr);
		
		//GL.activeTexture(GL.TEXTURE0 );
		GL.bindTexture(GL.TEXTURE_2D, null);
	}

 

Link to comment
Share on other sites

Look at this. FakeTexture is almost empty, I deleted almost the entire code an there is no image loaded. But with that it works. That's the ugliest "workaround" of all time I think..

	public static function update()
	{
		for (props in _textsToDraw)
		{
			// :(
			var texture = new FakeTexture("ugly", Engine3D.fakeScene);
			texture.dispose();
			
			var instance:TextRenderer = props.instance;
			instance._drawText(cast props.text, cast props.posX, cast props.posY);
		}
		
		_textsToDraw.splice(0, _textsToDraw.length);
	}

 

Link to comment
Share on other sites

Oh man oh man. I'm such a noob. And is was so easy ... 

After drawing the text I had to GL.bindTexture the texture that was bound before I bount the sprite sheet. I thought BJS would do this in every render call itself. That was all ... Now I only have to find out what textureID was bound before the text drawing ...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...