Page 1 of 1
PHP/GD 2D-to-3D Graphics Library
Posted: Wed Jan 25, 2006 5:38 pm
by jasonjohnson
An infinite number of things can be accomplished in PHP, of this I would say we are all sure -- it is simply a matter of how much time and effort one puts into his projects and visions... erm. Right?:)
I have a handful of one-off projects that I mess around with, one of which is a
graphics generation abstraction "library" rolled up around GD. While it is nice having an object-oriented interface to drawing rudimentary graphics on a canvas, it just doesn't get me nearly excited enough to bring it to completion.
My questions for all of you:
What's the best approach for taking my little library 3D? Are there tutorials buried in the Internet explaining the rendering techniques for 3D graphics? Is there a PHP/GD-based library available which already accomplishes this? Is the math so excruciatingly difficult as to be impassable?
All discussion, links to existing stuff, and whatever else is very much appreciated!:D
Posted: Thu Jan 26, 2006 3:39 am
by tasteslikepurple
what kind of 3D stuff do you want to do? just a few simple line drawings or full scale ray-tracing photo-realistic 3D images?
Posted: Thu Jan 26, 2006 1:09 pm
by jasonjohnson
I believe the magnitude of what I proposed would require me to start very, very small. First with making a line or cube appear three-dimensional, or simply representing the X, Y, Z axis visually.
For the real-world application of a library like this, I haven't an answer for you. Graphing and reporting would most likely be first, but equation and map modeling shouldn't be discounted as an eventual reality.
The more I search, the less I find in regard to this specific type of PHP/GD usage. If it doesn't already exist, a reason for me not to attempt a three-dimensional canvas isn't apparent.
While 3D bar chart and pie chart graphs are readily available, none of these libraries have the control I'm speaking of. What about a "view from" positioning of the camera? Just as is available in OpenGL? The rotation of objects? The scaling of objects?
Posted: Thu Jan 26, 2006 2:01 pm
by jasonjohnson
Getting something like this to function properly should probably be my first step. Though I see the math behind rotation and scale being troublesome. What would you say to initially disabling perspective? I hadn't thought of that before, but it seems as if I could neglect that aspect in starting out, it would prove to be much simpler to develop.
Code: Select all
<?php
// X, Y, Z = 200
$canvas = new Canvas(200, 200, 200);
// Cube construction. Width, Height, Depth = 50
$cube = new Cube(50, 50, 50);
// Cube position. X, Y, Z = 0
$cube -> position(0, 0, 0);
// Future methods mentioned.
// Rotation by point.
// $cube -> rotate_x(34);
// $cube -> rotate_y(45);
// $cube -> rotate_z(78);
// Scale by percent.
// $cube -> scale(-10);
// Attach cube to canvas.
$canvas -> attach($cube);
// Draw and destroy.
$canvas -> draw();
$canvas -> destroy();
?>
Posted: Thu Jan 26, 2006 8:10 pm
by tasteslikepurple
ok, the first thing i recommend looking at it something called linear transformations and look at this with the use of matrices. the easiest way to think of a cube in computer terms is with an array of 3 points. then to do 'transformations' (rotate (move the viewpoint), scale (zoom in), etc) you can multiply by a 3x3 matrix and then that gives you the new points.
to rotate in the X direction, use
1 0 0
0 cosA sinA
0 -sinA cosA
rotate in Y
cosA 0 -sinA
0 1 0
sinA 0 cosA
rotate in Z
cosA sinA 0
-sinA cosA 0
0 0 1
that should be enough to get you started
i'd bee happy to help with writing this library if you need it
hope that makes sense... if not let me knoww and i'll explain further
Posted: Thu Jan 26, 2006 8:33 pm
by John Cartwright
Interesting guys. Be sure to post in the Code Snipplets forum if you finish

Posted: Fri Jan 27, 2006 3:21 am
by onion2k
I see your graphics library only contains the basic geometric drawing functions from GD .. check out some of the code on
http://www.phpgd.com .. there's code for drawing geometric shapes, stars, etc. I've got some code for curves too but I've not added it yet.
Posted: Sat Jan 28, 2006 4:11 am
by tasteslikepurple
how's the library coming along?
Posted: Sun Jan 29, 2006 12:37 am
by jasonjohnson
Before yelling kicking or screaming commences, let me clarify that to this point, nothing is quite clear to me!
This really is okay, it is certainly part of the learning process. Let me assure you, I have been trying to wrap my head around this issue, but to help me push through the muck of the beginning, lets start with a little real code. Hopefully we can use this to put it into perspective.
Code: Select all
<?php
class Cube
{
var $x;
var $y;
var $z;
var $w;
var $h;
var $d;
var $matrix = array();
function Cube($x, $y, $z, $w, $h, $d)
{
$this -> matrix = array(
0, 0, 0,
0, 0, 0,
0, 0, 0
);
}
function rotate_x($d)
{
$this -> matrix = array(
/* ? */, 0, 0,
0, cos(/* ? */), sin(/* ? */),
0, -sin(/* ? */), cos(/* ? */)
);
}
}
$cube = new Cube();
$cube -> rotate_x(-10);
?>
Okay, first note it is a complete mess. Secondly, I am not using any of the variables passed to any of the functions. It wasn't clear in your initial example in the use of cos() and sin() where I would apply the change in rotation. Thirdly, the initialization could prove to be a beast.
Hrumph. I guess it's a start. Not as much of one I would expect from myself. I apologize in advance of the critical stares.

Posted: Sun Jan 29, 2006 11:49 am
by Ambush Commander
Since it's a matrix, it would probably make more sense like...
Code: Select all
$this->matrix = array(
array(0, 0, 0),
array(0, 0, 0),
array(0, 0, 0)
);
Posted: Fri Feb 03, 2006 4:47 am
by tasteslikepurple
the A in cosA and sinA is the angle to rotate by, so $d in your function.
it all looks (and is) very technical, but the methods which i described are the ways in which real graphics programmers work. it might look harder in the beginning but in the end it's much easier to use and much more efficient.
before I either insult your intelligence or talk way over your head again, how much do you know about matrices, or even know what they are and why they are useful?