How would I draw a simple line? (re: how to convert coordinates)
by John Klimek · in Torque Game Builder · 03/20/2007 (11:47 am) · 9 replies
I'm trying to learn how TGB works and so I'm trying to create a simple object that draws a line on the screen.
I understand that TGB uses "virtual" coordinates so that the game will look on the same on every machine regardless of the resolution used.
So, let's say I have a simple t2dSceneObject called a t2dLine. It inherits everything from t2dSceneObject (ex. setPosition()).
Inside of t2dLine.renderObject (or whatever it's called), how would I draw the line?
I'm new to OpenGL but I understand the whole idea of pushing vertices, etc.
What I'm confused about is how do I get this position and translate it to an OpenGL glVertex2f() call. How do the coordinates translate?
Also, any other information about positioning would be excellent. For example, I see references to mWorldClipBoundary and other variables inside of t2dStaticSprite that I'm not sure what they mean...
I understand that TGB uses "virtual" coordinates so that the game will look on the same on every machine regardless of the resolution used.
So, let's say I have a simple t2dSceneObject called a t2dLine. It inherits everything from t2dSceneObject (ex. setPosition()).
Inside of t2dLine.renderObject (or whatever it's called), how would I draw the line?
I'm new to OpenGL but I understand the whole idea of pushing vertices, etc.
What I'm confused about is how do I get this position and translate it to an OpenGL glVertex2f() call. How do the coordinates translate?
Also, any other information about positioning would be excellent. For example, I see references to mWorldClipBoundary and other variables inside of t2dStaticSprite that I'm not sure what they mean...
About the author
Recent Threads
#2
How do you use the position from the t2dSceneObject?
For example, let's say you do this:
What would I specify in my glVertex2f calls? The positions of (100, 200) and (300, 400) translate into different coordinates based on the actual screen resolution and design/camera resolution, right?
03/20/2007 (1:11 pm)
Thanks for the reply and the explanation!How do you use the position from the t2dSceneObject?
For example, let's say you do this:
t2dLine.setPosition("100 200"); // this would be the starting point of the line
t2dLine.setPosition2("300 400"); // fake call, but lets pretend this sets the end point of the lineWhat would I specify in my glVertex2f calls? The positions of (100, 200) and (300, 400) translate into different coordinates based on the actual screen resolution and design/camera resolution, right?
#3
The design coordinates are relative to the viewport/projection/etc set up in ogl and that is set to match.
E.g. if your point vars were called the following:
Notice the last option - there are a lot of utility functions in the dgl library to do basic drawing, check it out.
The only time to start worrying about coord conversion is if you get your coords from e.g the window or relative to a a scene object, etc.
03/20/2007 (1:30 pm)
You use the exact parameters passed in as they are already in t2d "world space".The design coordinates are relative to the viewport/projection/etc set up in ogl and that is set to match.
E.g. if your point vars were called the following:
t2dVector mStartPoint; // set by setPosition( "100 200" );
t2dVector mEndPoint; // set by setPosition2( "300 400" );
// at render time using this...
glVertex2fv( (GLFloat*)&mStartPoint );
glVertex2fv( (GLFloat*)&mEndPoint );
// or this...
glVertex2f( mStartPoint.mX, mStartPoint.mY );
glVertex2f( mEndPoint.mX, mEndPoint.mY );
// or this...
dglDrawLine( mStartPoint.mX, mStartPoint.mY, mEndPoint.mX, mEndPoint.mY, mLineColor ); Notice the last option - there are a lot of utility functions in the dgl library to do basic drawing, check it out.
The only time to start worrying about coord conversion is if you get your coords from e.g the window or relative to a a scene object, etc.
#4
I'll have to fool around with it a bit more and I'm sure I'll have more questions, but for now I'm good.
Thanks for your help!!
03/20/2007 (6:32 pm)
Oh... interesting. I thought I had to convert the coordinates but since I don't thats even better!I'll have to fool around with it a bit more and I'm sure I'll have more questions, but for now I'm good.
Thanks for your help!!
#5
It supports drawing primatives. It isn't in the TGB documentation but here is a link to it on TDN.
tdn.garagegames.com/wiki/TGB/Other_Resources/Primitives
I think the article has a link to source for older versions of T2D/TGB.
I hope this is beneficial for you.
03/21/2007 (8:01 am)
If you are running TGB 1.13 you may want to take a look at the t2dShapeVector class. It supports drawing primatives. It isn't in the TGB documentation but here is a link to it on TDN.
tdn.garagegames.com/wiki/TGB/Other_Resources/Primitives
I think the article has a link to source for older versions of T2D/TGB.
I hope this is beneficial for you.
#6
I did take a look at t2dShapeVector but I was still a bit confused on how the coordinates were translated...
I think a t2dTexturedShapeVector would be quite useful too. (eg. textured circles, polygons, etc, etc...)
03/21/2007 (8:32 am)
Thanks for the information!I did take a look at t2dShapeVector but I was still a bit confused on how the coordinates were translated...
I think a t2dTexturedShapeVector would be quite useful too. (eg. textured circles, polygons, etc, etc...)
#7
I'm sure John was leaning more toward learning to do things himself than using stuff that was already there ;p
@John:
I have created a quite a few extensions (the textured poly being one of them) as part of the iso builder code, but I am considering releasing those as resources or even better as additions to the TGB core. I was discussing this on a another thread about old school dungeon crawlers and agree that there are a number of uses for stuff like this.
03/21/2007 (12:33 pm)
@Guy:I'm sure John was leaning more toward learning to do things himself than using stuff that was already there ;p
@John:
I have created a quite a few extensions (the textured poly being one of them) as part of the iso builder code, but I am considering releasing those as resources or even better as additions to the TGB core. I was discussing this on a another thread about old school dungeon crawlers and agree that there are a number of uses for stuff like this.
#8
I'll definitely keep an eye on the threads to see what happens.
Also, good look on your iso builder. I've been forwarding to that for quite a while :)
03/21/2007 (1:03 pm)
Ahh, that would be really great if a textured polygon would part of the TGB core (or as a free resource).I'll definitely keep an eye on the threads to see what happens.
Also, good look on your iso builder. I've been forwarding to that for quite a while :)
#9
I try to not assume I know what someone is trying to accomplish without having an indepth conversation with them. I am simply offering alternatives that John may have been unaware of and could use, or even study to gain a better understanding of how TGB handles things. I find it better to give more information and let people decide for themselves, rather not sharing information and possibly denying someone what may be just what they need. I figure if I throw it out there and it isn't used no harm. :)
@John:
Good Luck!
03/21/2007 (4:42 pm)
@Neo:I try to not assume I know what someone is trying to accomplish without having an indepth conversation with them. I am simply offering alternatives that John may have been unaware of and could use, or even study to gain a better understanding of how TGB handles things. I find it better to give more information and let people decide for themselves, rather not sharing information and possibly denying someone what may be just what they need. I figure if I throw it out there and it isn't used no harm. :)
@John:
Good Luck!
Torque Owner Neo Binedell
For doing OGL stuff you usually follow these steps:
1) Set any render states that you need e.g blending on/off, color, etc
2) Set any textures etc or disable texturing
3) draw any primitives
4) Reset the render state
Quite simple really...
So for you to draw a line you could do:
// Save state we are changing glPushAttrib( GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT ); // No textures thanks glDisable( GL_TEXTURE_2D ); // Use this color glColor4f( 0.4f, 0.0f, 0.4f, 1.0f ); // Lines please glBegin(GL_LINES); glVertex2f( x0, y0 ); // From this vertex... glVertex2f( x1, y1 ); // ...to this vertex... // End line batch glEnd(); // Restore colour/blend glPopAttrib();Of course you would usually want to draw more than just one line at a time ;p