[RESOLVED] DirectX - Rotated Text?
Hi, does anybody have any idea how to draw rotated text with directx? I know how to just draw text using the "DrawText" function. In its syntax it doesn't have any place for angle and i don't see any other functions. Maybe if there isn't a simple function for drawing rotated text there could be another way? Like, what if on a picture box you would print your text and make a texture out of it, then draw the texture on rotated? ..... hopefully somebody knows.
Re: DirectX - Rotated Text?
Quote:
what if on a picture box you would print your text and make a texture out of it, then draw the texture on rotated?
Yes, this is the first variant. The second variant - D3DXDrawText to RenderTarget texture, then render rotated quad with this texture.
Re: DirectX - Rotated Text?
So, rotating the polygon and drawing the texture is a breeze. How would I go about making a a texture out of drawn text? Also, would I need to draw the text with DX or would Print or TextOut be enough?
1 Attachment(s)
Re: DirectX - Rotated Text?
1 Attachment(s)
Re: DirectX - Rotated Text?
The example is awesome, just what I need! I'm a newbie to DX and I'm trying to mix this in with other images. I modified the example so that it draws an image as well as the text. Now when I draw the text on last the whole screen is black and it's only the text. I must have messed up something in it but can't fix it. Maybe the problem is in the Initialization of DX. I'm also wondering how to make the texture transparent. In the example there was blue in the background and a black box around the text. More relevantly to my modified version is that I can't see the image behind the text. Is there a way to fix this? Thanks again for the help.
Modified Example:
1 Attachment(s)
Re: DirectX - Rotated Text?
Re: DirectX - Rotated Text?
Thanks so much for all your help. Now the image and the text are working together - the image casts a shadow on the text one way and the text draws on transparent over the image the other way. Examples are really the best to tinker with and discover what's happening.
Now i just have a question of preference. In the example the unit was kindof weird. 0,0 was the center of the screen and the top left corner is about -2.5,-2.5. How could we change this so that it is working in pixels from the top left corner of the screen? One thing which would appeal to me(because i know it) would be if the texture could be made to be drawn like i drew my image in my example. I made that off of the examples made by JacobRoman which work in pixels. You were using matrices to rotate and some other stuff to position the text. How does that work? Thanks again.
Re: DirectX - Rotated Text?
I removed the RHW and used an isometric projection matrix in order to be able to rotate images using matrices.
Change this:
Code:
D3DXMatrixOrthoLH Mtrx, 5, ScaleHeight * 5 / ScaleWidth, 1, 10
to this:
Code:
D3DXMatrixOrthoLH Mtrx, ScaleWidth, ScaleHeight, 1, 10
Now all sizes in pixels.
1 Attachment(s)
Re: DirectX - Rotated Text?
So... let me ask you a strait question. What is the difference between the way i'm drawing the image in the attached example and the way you're drawing the text? Why can't we have it so that the text texture can be drawn the same way? In the attached example i'm drawing text made in Photoshop, how can I make it so that the created text texture is drawn the same way?
Re: DirectX - Rotated Text?
Well anyway, the way it is right now I'm good to go. Thanks again for the help!
Re: [RESOLVED] DirectX - Rotated Text?
In this example, a lot of mistakes. For example:
wrong:
Code:
Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, True
right:
Code:
Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, 1
Because in VB6 True = -1
On some video drivers can be buggy.
Also, there are many superfluous:
Specular, Color are not used,
Texture Size - 20 * 400, but it creates a 256 * 256 with an unknown purpose.
It was necessary to unite in Enum:
Code:
Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
Create_Polygon not needed inside the Render loop.
If "Although the Unload statement located above exits the program, you will end up with an Automation error after doing so" then the program has a bug, use End - an attempt to hide it.
On such programs should not be learn.
I understand very little in English, tell me what is in the program, then you are missing.
1 Attachment(s)
Re: [RESOLVED] DirectX - Rotated Text?
Those are some good things which you've pointed out (oops i forgot to fix the attached program :)). I've taken out unesesary code and fixed all the problems. As to this i am not sure what you meant.
Quote:
It was necessary to unite in Enum:
Code:
Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
The reason why there's and END statement at the end of the program isn't to hide an error, it's so that the game loop will not run again. If you Rem it out and run the program, after closing the window the error will appear the next time the game loop tries to draw. We don't want to go there again so an END statement is inserted and the problem is solved!
In fact I've actually come up with a solution for my problem. I was thinking to make the texture just "inside" the program but loading a file works fine. What I do is I draw text on a picturebox, save the image as a bmp, load it into a DX texture and delete the file. It isn't fast enough to make a texture every time the loop goes(by that i mean for more than one texture) but it can make 500 different textures in one second. This works well enough for me so i'm good to go.
Re: [RESOLVED] DirectX - Rotated Text?
Yea he got those from my tutorial:
Code:
Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
It's my personal style. And yea End did solve the problem cause if you comment out the End you would get an automation error. I know that there are many other people who tell me to avoid using End and that it's horrible and doesn't fully end the program when infact it does when combined with unload <form name here> no matter what Windows OS you use. If it ain't broke, don't fix it :bigyello:
Re: [RESOLVED] DirectX - Rotated Text?
Quote:
The reason why there's and END statement at the end of the program isn't to hide an error, it's so that the game loop will not run again.
This is because the main loop is organized with an error.
Here I have corrected this error, but not correcting others:
Code:
Option Explicit
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
'The 2D (Transformed and Lit) vertex format type.
Private Type TLVERTEX
x As Single
y As Single
Z As Single
RHW As Single
Color As Long
Specular As Long
TU As Single
TV As Single
End Type
'Some color depth constants to help make the DX constants more readable.
Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
'The 2D (Transformed and Lit) vertex format.
Private Const FVF_TLVERTEX As Long = D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR
Private DirectX8 As DirectX8 'The master DirectX object.
Private Direct3D As Direct3D8 'Controls all things 3D.
Private Direct3D_Device As Direct3DDevice8 'Represents the hardware rendering.
Private Direct3DX As D3DX8
Private Running As Boolean 'Helps determine whether the main game loop is running.
'This function will make it much easier to setup the vertices with the info it needs.
Private Function Create_TLVertex(x As Single, y As Single, Z As Single, RHW As Single, Color As Long, Specular As Long, TU As Single, TV As Single) As TLVERTEX
Create_TLVertex.x = x
Create_TLVertex.y = y
Create_TLVertex.Z = Z
Create_TLVertex.RHW = RHW
Create_TLVertex.Color = Color
Create_TLVertex.Specular = Specular
Create_TLVertex.TU = TU
Create_TLVertex.TV = TV
End Function
Private Function DirectX_Initialize() As Boolean
On Error GoTo Error_Handler
Dim Display_Mode As D3DDISPLAYMODE 'Display mode desciption.
Dim Direct3D_Window As D3DPRESENT_PARAMETERS 'Backbuffer and viewport description.
Set DirectX8 = New DirectX8 'Creates the DirectX object.
Set Direct3D = DirectX8.Direct3DCreate() 'Creates the Direct3D object using the DirectX object.
Set Direct3DX = New D3DX8
Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode
Direct3D_Window.Windowed = True 'The app will be in windowed mode.
Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC 'Refresh when the monitor does.
Direct3D_Window.BackBufferFormat = Display_Mode.Format 'Sets the format that was retrieved into the backbuffer.
Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)
Direct3D_Device.SetVertexShader FVF_TLVERTEX 'Set the type of vertex shading. (Required)
'Sets up properties for transparency.
Direct3D_Device.SetRenderState D3DRS_ALPHAREF, 255
Direct3D_Device.SetRenderState D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL
'These lines are not needed, but it's nice to be able to filter the
'textures to make them look nicer.
Direct3D_Device.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_POINT
Direct3D_Device.SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_POINT
Exit Function
Error_Handler:
MsgBox "An error occured while initializing DirectX", vbCritical
Close_Program
DirectX_Initialize = False
End Function
Private Function Create_TextTexture(Text As String) As Direct3DTexture8
picTxt.Width = picTxt.TextWidth(Text): picTxt.Height = picTxt.TextHeight(Text)
picTxt.Cls
picTxt.Print Text
SavePicture picTxt.Image, App.Path & "\Junk.bmp"
Dim File_Path As String, Width As Long, Height As Long, Transparency_Color As Long
File_Path = App.Path & "\Junk.bmp"
Transparency_Color = D3DColorRGBA(100, 100, 100, 255)
Set Create_TextTexture = Direct3DX.CreateTextureFromFileEx(Direct3D_Device, File_Path, _
Width, Height, 0, 0, _
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, _
D3DX_FILTER_POINT, D3DX_FILTER_POINT, _
Transparency_Color, ByVal 0, ByVal 0)
DeleteFile App.Path & "\Junk.bmp"
End Function
Private Sub Game_Loop()
Dim Vertex_List(3) As TLVERTEX, Num As Single, Cap As String
Dim LastTime
LastTime = Timer
Do While Running = True
DoEvents
If Timer - LastTime > 0.05 Then
LastTime = Timer
Num = Num + 1
If Num > 500 Then Num = 0
Cap = "Numer = " & Num
End If
'Clears the backbuffer.
Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 255, 0, 0), 1#, 0
Direct3D_Device.BeginScene
Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE, True
Direct3D_Device.SetTexture 0, Create_TextTexture(Cap) 'Texture
With picTxt
Vertex_List(0) = Create_TLVertex(0, 1, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 0)
Vertex_List(1) = Create_TLVertex(.TextWidth(Cap) + 1, 1, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 0)
Vertex_List(2) = Create_TLVertex(0, .TextHeight(Cap), 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 1)
Vertex_List(3) = Create_TLVertex(.TextWidth(Cap) + 1, .TextHeight(Cap), 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 1)
End With
Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
Direct3D_Device.EndScene
'Flips the backbuffer into the form window.
Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0
Loop
Close_Program
End Sub
Private Sub Close_Program()
'Running = False 'This helps the program bail out of the game loop.
'Unload all of the DirectX objects.
Set Direct3DX = Nothing
Set Direct3D_Device = Nothing
Set Direct3D = Nothing
Set DirectX8 = Nothing
Unload Me 'Unload the form.
'End
End Sub
Private Sub Form_Load()
picTxt.BackColor = RGB(100, 100, 100)
DirectX_Initialize 'Initializes DirectX and Direct3D.
Running = True 'Initializations all set. It's now ok to activate the game loop.
Me.Show
Game_Loop
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Running Then Running = False: Cancel = 1
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then Close_Program
End Sub
Tell me the following questions:
For what is given Transparency_Color, if it is nowhere in the picture is not found?
Code:
Transparency_Color = D3DColorRGBA(100, 100, 100, 255)
For what is specified Color and Specular, if they are not used?
What is inside the main loop every time the array is filled Vertex_List(), if it can be done once at initialization?
Why use a very slow creation of textures, using a file, if I gave an example fast creation with GetDIBits?
Also, write to the file causes an error when you run the program on a CD or flash drive with a blocked writing.
Re: [RESOLVED] DirectX - Rotated Text?
Yea speculars not needed. I mainly use that in my 3D apps. But forgot I had them in my DX tutorials 5 years ago o.O (whoops). However the Transparency_Color is definitely needed if you plan on using an image with a background color you want to be transparent. Like for example if the backgrounds black it'll eliminate it and only draw whats not black. And I found something even better than this
vb Code:
'Sets up properties for transparency.
Direct3D_Device.SetRenderState D3DRS_ALPHAREF, 255
Direct3D_Device.SetRenderState D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL
Instead I typically now use this:
vb Code:
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE
Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE
Direct3D_Device.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
Direct3D_Device.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
Direct3D_Device.SetRenderState D3DRS_BLENDOP, D3DBLENDOP_ADD
with Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE set the 0 or not use it at all cause it's no longer needed and use this instead:
vb Code:
Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, True
Now it's not only is the background color eliminated, but you have total control over the alpha values now of your vertex colors of the polygons to control how see through your image is with 255 being completely opaque with a transparent background, and 0 being completely invisible! Any values in between will have that ghost effect. You can even make images fade away. It's pretty neat. If I didn't want an image to have a transparent background I wouldn't have that color selected to begin with. So if I were him I recommend this way.
Re: [RESOLVED] DirectX - Rotated Text?
Yes that is a better way of exiting the main loop. I thought of an even simpler method - put the doevents at the end of the loop and it will run fine.
Quote:
What is inside the main loop every time the array is filled Vertex_List(), if it can be done once at initialization?
Well this is a very basic example. In a game the text would be moving and/or (as i intended it) rotating.
Quote:
Why use a very slow creation of textures, using a file, if I gave an example fast creation with GetDIBits? Also, write to the file causes an error when you run the program on a CD or flash drive with a blocked writing.
I would absolutely love to use your method!!! The problem is that using your example i have no clue of how to control where it's drawn. Look at these two examples:
Code:
D3DXMatrixLookAtLH Mtrx, vec3(0, 0, -5), vec3(0, 0, 0), vec3(0, 1, 0)
Dev.SetTransform D3DTS_VIEW, Mtrx
D3DXMatrixPerspectiveFovLH Mtrx, pi / 3, ScaleHeight / ScaleWidth, 1, 10
Dev.SetTransform D3DTS_PROJECTION, Mtrx
D3DXMatrixRotationZ Mtrx, Timer * 0.25
Dev.SetTransform D3DTS_WORLD, Mtrx
\\\\\\\\
Vertex_List(0) = Create_TLVertex(0, 0, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 0)
Vertex_List(1) = Create_TLVertex(100, 0, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 0)
Vertex_List(2) = Create_TLVertex(0, 0, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 1)
Vertex_List(3) = Create_TLVertex(100, 0, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 1)
Yours is using matrices and other methods of drawing while the other is simple. With the second one(the one i like a lot more) you can set the exact point(X, Y) in pixels, where each corner of the texture will be drawn. As far as i see it this gives the most simplicity and best control.
I would be really happy if you remake your example so that instead of using matrices each corner of the text is specified in X and Y.
Re: [RESOLVED] DirectX - Rotated Text?
What if there was a way of doing by instead of making it straight from a file it was made using "CreateTextureFromFileInMemory" or "CreateTexture"? Although i think that Mikle was doing all the work in his example, it was just in another format or something.
Re: [RESOLVED] DirectX - Rotated Text?
Yes his method of using the above code is only used in 3D, while my tutorials and your program is in 2D so the above code is not needed:
Code:
D3DXMatrixLookAtLH Mtrx, vec3(0, 0, -5), vec3(0, 0, 0), vec3(0, 1, 0)
Dev.SetTransform D3DTS_VIEW, Mtrx
D3DXMatrixPerspectiveFovLH Mtrx, pi / 3, ScaleHeight / ScaleWidth, 1, 10
Dev.SetTransform D3DTS_PROJECTION, Mtrx
D3DXMatrixRotationZ Mtrx, Timer * 0.25
Dev.SetTransform D3DTS_WORLD, Mtrx
Also I find CreateTextureFromFileEx to be the easiest method to simply load a texture prior to entering the game loop. Of all my years of programming in DirectX I never once had a problem loading from flash, burned cd/dvd, etc. As long as the textures are within the same directory as the exe/ide or within a \Graphics folder itll be fine. It's not slow loading the textures at all. I made games that loaded 100s of files and it loaded nearly instantly. That's cause I was doing it before I entered the main game loop and not during.
1 Attachment(s)
Re: [RESOLVED] DirectX - Rotated Text?
All coordinates - the standard pixel, you can instead:
Code:
D3DXMatrixTranslation Mtrx, 0, 0, 0 'Position
Dev.SetTransform D3DTS_WORLD, Mtrx
D3DXMatrixScaling Mtrx, ScaleWidth, ScaleHeight, 0 'Size
Dev.MultiplyTransform D3DTS_WORLD, Mtrx
leave only this:
Code:
D3DXMatrixIdentity Mtrx
Dev.SetTransform D3DTS_WORLD, Mtrx
Then have all the coordinates of the vertex set manually, but the matrices do it better.
Re: [RESOLVED] DirectX - Rotated Text?
Jacob Roman
I mean, that will slowly create a texture for the text in each cycle while maintaining image to a file and loading the file into a texture. And this is what can cause an error when running from a CD.
Upload a file immutable regular texture - this is normal.
Re: [RESOLVED] DirectX - Rotated Text?
Quote:
Originally Posted by
Mikle
Jacob Roman
I mean, that will slowly create a texture for the text in each cycle while maintaining image to a file and loading the file into a texture. And this is what can cause an error when running from a CD.
Upload a file immutable regular texture - this is normal.
Ahhhhh gotcha :p
Wow vbForums looks freaking awesome on my new 40 inch TV! And my movies and games O.O .....I know I know off subject there. But still! :eek: