Game Development Community

Drawing simple shapes in Torqescript

by Jason Gossiaux · in Torque Game Engine · 02/23/2008 (8:24 am) · 3 replies

I'd asked something similar to this in the past, and was directed to look at how the editor drew it's bounding boxes and other polygons. However it all led me into the source code which become somewhat obfuscated and nasty.

Does anyone know how I could draw lines and simply polygons and the like using just Torquescript? All I want to do is draw lines or triangles connecting 2 points to represent paths for pathfinding. I've seen it demonstrated everywhere but no code is ever provided. I could probably write an extension to the engine to give me access to some basic polygon drawing functions, but I don't want to spend too much time on it if possible. Thanks!

#1
02/23/2008 (10:08 pm)
There are a number of functions used primarily in the GUI code for drawing simple shapes, however none of these are directly linked to script.

I recommend opening up some of the simple GUI elements, like the healthbar and the speedometer. They both demonstrate simple line and shape drawing commands, and the code in these files is pretty simple compared to the editor code (where the bounding box stuff is found).

These are all 2D draw commands, however you can also use 3D versions, like in this bit of code I ripped out of an A* path finding resource (www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=8531):

glColor4ub(255, 0, 0, 255);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glDisable(GL_TEXTURE_2D);

		for (int j = 0; j < neighbours.size(); ++j) {
			Point3F start = Point3F(obj->_nodes[i]->getPosition().x, obj->_nodes[i]->getPosition().y, obj->_nodes[i]->getPosition().z + 2.0);
			Point3F end = Point3F(neighbours[j]->getPosition().x, neighbours[j]->getPosition().y, neighbours[j]->getPosition().z + 2.0);
			glBegin(GL_LINES);
			glVertex3fv(start);
			glVertex3fv(end);
			glEnd();
		}

The specifics of what data is going into the "start" and "end" variables isn't important, just that they're 3D coordinates stored in Point3F's.

You could write console draw functions, however it wouldn't be quite as simple as you might think.. these draw functions are meant to go into render functions, which get called every frame; calling a draw function once will blip the line/shape/whatever on the screen for 1 frame, after which it ceases to be drawn.

I recommend piggy-backing on a GUI element, since they're low-overhead objects on the client which get a render call every frame. Even though it's part of the GUI, you should still be able to draw in 3D space (because the draw functions don't really act in 3D, they just take 3D coordinates instead of forcing you to project). I've never actually tried it, but it should (?) work.
#2
03/03/2008 (6:20 pm)
I figured out the problem I was having. I need to pay more attention to signed/unsigned when making my arrays :sigh:

Just to let you know what I did... I took the fxRenderObject and used that to create whatever simple polygons I needed. My only problem is the engine stops rendering it when it is out of sight. This means if I have a single fxRenderObject displaying a map full of pathfinding nodes, it is only visible in specific situations.

To fix this I tried increasing the render box of the object, which worked. I'm just not sure if it is the best solution. I can't imagine people generate millions of fxRenderObjects when rendering their pathfinding node arrays... I guess we'll see.
#3
03/03/2008 (11:17 pm)
Well hopefully this last question finishes this indicator off for me.

I have the fxRenderObject working well. It can display various things in a place of my choosing. However what I would like to do is have the server populate an array of ~100 Point3Fs, then have the client onRender function display a vertex for each item in that array. The only way I know to exchange data between server and client is with persistant fields - and they don't support arrays.

Would anyone have an idea of how I could do this? Thanks.