PHP/GD 2D-to-3D Graphics Library

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
jasonjohnson
Forum Newbie
Posts: 6
Joined: Wed Jan 18, 2006 9:21 am
Location: Dallas, TX, USA

PHP/GD 2D-to-3D Graphics Library

Post 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
User avatar
tasteslikepurple
Forum Commoner
Posts: 46
Joined: Thu Jan 26, 2006 3:38 am
Location: Bath, UK

Post 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?
User avatar
jasonjohnson
Forum Newbie
Posts: 6
Joined: Wed Jan 18, 2006 9:21 am
Location: Dallas, TX, USA

Post 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?
User avatar
jasonjohnson
Forum Newbie
Posts: 6
Joined: Wed Jan 18, 2006 9:21 am
Location: Dallas, TX, USA

Post 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();

?>
User avatar
tasteslikepurple
Forum Commoner
Posts: 46
Joined: Thu Jan 26, 2006 3:38 am
Location: Bath, UK

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Interesting guys. Be sure to post in the Code Snipplets forum if you finish :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
tasteslikepurple
Forum Commoner
Posts: 46
Joined: Thu Jan 26, 2006 3:38 am
Location: Bath, UK

Post by tasteslikepurple »

how's the library coming along?
User avatar
jasonjohnson
Forum Newbie
Posts: 6
Joined: Wed Jan 18, 2006 9:21 am
Location: Dallas, TX, USA

Post by jasonjohnson »

Before yelling kicking or screaming commences, let me clarify that to this point, nothing is quite clear to me! :D

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. :?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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)
);
User avatar
tasteslikepurple
Forum Commoner
Posts: 46
Joined: Thu Jan 26, 2006 3:38 am
Location: Bath, UK

Post 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?
Post Reply