Skip to main content
GameDev.net gamedev.net
🔒 Locked

3D Cube Example

Started by BlackDragon777 Sep 4, 2006 at 12:14 AM 10 replies 2.8k views
Original Post
BlackDragon777
BlackDragon777
Hi. I would like to write a 3d wireframe cube demo from scratch just by drawing lines. I want to do this as a learning experience. I have been reading the book, "3D Math Premier for Graphics and Game Developers" I am still a little unsure of what it means to project one vector onto another. Therefore I would like to see some example source. Anyone know any good tutorials or source that might help open some doors for me? Thanks in advance! GD Bless you Always!!!!!!
BlackDragon777
BlackDragon777
I took a look at this lua code

http://www.luaplayer.org/gallery/3dcube.zip

However the way Nils projects a vector doesn't look like anything I learned in my book. He did something like this,

local sx = math.sin(axisRotations.x)local cx = math.cos(axisRotations.x)local sy = math.sin(axisRotations.y)local cy = math.cos(axisRotations.y)local sz = math.sin(axisRotations.z)local cz = math.cos(axisRotations.z)		-- perform the rotations around each axis		-- rotation around x		xy = cx*y - sx*z		xz = sx*y + cx*z		-- rotation around y		yz = cy*xz - sy*x		yx = sy*xz + cy*x		-- rotation around z		zx = cz*yx - sz*xy		zy = sz*yx + cz*xy		-- now determine perspective scaling factor		-- yz was the last calculated z value so its the		-- final value for z depth		scaleRatio = focalLength/(focalLength + yz)		-- assign the new x and y		x = zx*scaleRatio		y = zy*scaleRatio


I don't quite follow this code. For example, How does he know that xz should be = to cx*y - sx*z.

Also how does the yz variable relate to the scaleRatio? I have questions like these. Thanks in advance!
paulshady
paulshady
hmmm, you using OpenGl, DirectX or something else....I guess if you don't know yet, check out OpenGL as it makes drawing simple lines pretty easy for ya without dealing with buffers. You could use somehting like this:

glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();

That will draw a quad for ya, of course you need 5 other faces to make a cube...;)

Hmm maybe, get that going on the screen first and then hop into rotations, theyre actually pretty easy...

Good luck
BlackDragon777
BlackDragon777
Hey PaulShady. Thanks but I know how to draw a Cube using OpenGL and DirectX.

However I want to know how to draw it without using any API.

For example, lets say I am given X platform to drawn on which doesn't have OpenGL or any other 3d API. However the X platofrm doesn't give me basic abilities to draw pixels.

Well if I can draw a pixel then I can write a function that draws lines.

From there, I want to draw a 3d rotating Cube. I am trying to understand the theory and practice of drawing realtime 3d using no api. I am getting stuck on th e idea of projecting one vector onto another. I need to understand this well before I can move on to writing shaders.

Thanks in advance!
Demus79
Demus79
The basic matrix math is no different whether you use an API or write a software rasterizer.

You'll still have the world, camera and projection matrices. So, any matrix tutorial should give you the basics of rotating a 3d object and creating the rotational matrices.

Otherwise, writing a software rasterizer is another matter. Perhaps you want to use some existing cross-platform GFX library which gives you the possibility to write your own line/polygon functions etc.

Calculating a projected vector is quite simple.

proj = ((A dot B) / (A dot A)) * A

where B is the vector to be projected and A is the other vector.

Cheers
BlackDragon777
BlackDragon777
So I guess what I am confused about is how to use a projection to translate a 3d vector to a 2d vector. Lets say I have

vector A
(3,2,5)

And I want to get the resultant 2d vector

Vector B
(x,y)


How do I do this using the projection. Where does it come in? That is my biggest problem. Thanks in advance!
Demus79
Demus79

Well, umm, projecting a 3d vector to a 2d surface is something else.

Basically it would be something like

projected.x = position.x / position.z;
projected.y = position.y / position.z;

But with matrices you should be working with 4d vectors and the projection is a bit different.
BlackDragon777
BlackDragon777
Well, umm, if I wanted to use a 4d vector so that I could have translations, how owuld it change? Thanks in advance!
paulshady
paulshady
http://www.devmaster.net/articles/software-rendering/part2.php

check it
paulshady
paulshady
4d vectors can be multiplied by 4X4 matrices, which is important if you want to include rotation, transformation and scaling
BlackDragon777
BlackDragon777
Ok, I managed to render a 3d wireframe cube. How would I go about filling it with a color. Is there any specific algorithm for filling a 3d object with a color via software rendering? In other words, what is the algorithm for making the wireframe cube a solid cube? Thanks in advance!

GOD Bless you Always!!!!!!!!!!!!
pauljan
pauljan
The word you are looking for is "Rasterization" (or "triangle rasterization"). Keep on reading that tutorial Paulshady pointed you at:

http://www.devmaster.net/articles/software-rendering/part3.php
http://www.devmaster.net/articles/software-rendering/part4.php

Then use google to find more.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.