Dynamicly drawing to a texture

BlitzMax Forums/BlitzMax OpenGL Programming/Dynamicly drawing to a texture

There are a number of extra buffers, but the they don't make any difference in my case. I found out that, eventhough not intended to, my draw-to-texture code was running during the main render loop in some cases. this caused the flickering of the screen. It's solved now.

Now all I need is to find a proper way to render Text to a texture and still have it look OK. Atm either the texture turns out to be to small for the object im putting it on, or it looks really horrible.


New drawing code
Method Invalidate( r:Rectangle = Null )
	Super.Invalidate( r );

	If( (Text = oldText) Or (Tokens.Length = 0) ) Then
		Return;
	End If

	'// are we in Render or Select mode?
	'// (Select mode is used by Entity.__getPicked() routine)
	Local rm:Int;
	glGetIntegerv( GL_RENDER_MODE, Varptr rm );
	If( rm <> GL_RENDER ) Then Return;

	If( (Texture[0] = Null) Or ((Texture[0].Width <> (Size.x * 2)) Or (Texture[0].Height <> (Size.y * 2))) ) Then
		Local img:TImage = CreateImage( (Size.x * 2), (Size.y * 2) );
		Texture[0] = GLTexture.FromImage( img );
	End If

	Local tw:Int = Texture[0].Width, th:Int = Texture[0].Height;
	GLTexture.AdjustTexSize( tw, th );

	glViewPort( 0, 0, tw, th );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	SetAlpha( 1 );
	SetBlend( ALPHABLEND );
	TileImage( Texture[1].Image );

	If( MultiLine ) Then
		Local x:Float = 2.0, y:Float = th;
		Local ncw:Int = tw / TextWidth( "M" );
		Local nch:Int = th / TextHeight( "M" );
		Local nx:Int = 0, ny:Int = 0, count:Int = 0;
		Local startline:Int = 0;

		If( ActiveControl = Self ) Then
			If( MouseZ() <> 0 ) Then
				If( OldMouseZ < MouseZ() ) Then
					ScrollOffset :- 1;
				Else If( OldMouseZ > MouseZ() ) Then
					ScrollOffset :+ 1;
				End If
			End If
		End If
	
		If( Linecount > nch ) Then
			startline = (Linecount - nch);
			startline :+ ScrollOffset;
	
			If( startline < 0 ) Then
				startline = 0;
				ScrollOffset :+ 1;
			End If
	
			If( startline > Linecount - nch ) Then
				startline = Linecount - nch;
				ScrollOffset :- 1;
			End If
		End If

		For Local t:TBToken = EachIn Tokens
			If( count >= startline + nch ) Then
				Exit;
			End If

			If( count >= startline ) Then
				Select( Chr(t.Value) )
				 Case "~n", "~r", "~r~n";
				 Default;
					t.Draw( x, y );
					x :+ t.Rect.w; nx :+ 1;
					If( nx >= ncw ) Then
						x = 0; nx = 0;
						y :- t.Rect.h; ny :+ 1;
					End If
				End Select
			End If

			Select( Chr(t.Value) )
			 Case "~n", "~r", "~r~n";
				If( count >= startline And count <= (startline + nch) ) Then
					x = 0; nx = 0;
					y :- TextHeight("M"); ny :+ 1;
				End If
				count :+ 1;
			End Select
		Next

	Else
		Local x:Float = 2.0, y:Float = 2.0;

		For Local t:TBToken = EachIn Tokens
			Select( Chr(t.Value) )
			 Case "~n", "~r", "~r~n";
			 Default;
				t.Draw( x, y );
				x :+ t.Rect.w;
			End Select
		Next
	End If

	glBindTexture( GL_TEXTURE_2D, Texture[0].Texture.ID );
	glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, tw, th, 0);
	glViewport( 0, 0, Stage.Instance.Width, Stage.Instance.Height );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glBindTexture( GL_TEXTURE_2D, 0 );

	oldText = Text;

End Method


The resulting texture is not of the proper size to fit over the entire control (sprite) though. I have to adjust the texure size oin the above code to fit a Power of 2 Size, otherwise the glCopyTexImage2D() function fails. this causes some problems when Im trying to render the characters to it in the proper location.
To make the texture and characters allign properly, I am resetting the viewport to the texture's size before the drawing starts. This method is taken from NeHe's Blur tutorial (#36).

It doesnt work quite as intended though. The characters are very blurry. and the texture does not scale properly with the quad. the lines should stretch from the top of the screen, to the blue bar in the center. and from the left to the very right of the screen.



This is what the texture looks like just after it has been drawn to the screen and before it is copied to the actual OpenGL texture. It is still somewhat blurry, but the direction of the characters, the horizontal size and the scaling of the lines is correct.