3DProjection of a cube

Brenden

Member
My goal is to draw a 3D cube made out of primitives, for each face, and have the perspective of the cube to be off centered to look toward the top left corner a bit. The one problem is that I want the front face to be perfectly 1 to 1; That means that it should look as if you were to draw the front face facing it directly, or to better state each edge is the same length. Also the cube should be centered on the screen. Here is a rough picture of what it might look like:

I have achieved this through creating a surface and setting the main view to that surface.
Then copying the bottom right corner of that surface to the application surface.
The view and surface are are dubbed the size of the application surface so the aspect ratio should be the same.
Also this is done in the post draw event.
The perspective or projection is moved to be looking from a bit up and to the left but parallel with the cube, so that the cube is within the bottom right corner of the view.
Yet this attempt still has flaws. The face is distorted or skewed just a bit to change the pixels that make up a block on the face or to be precise the whole face.

If you need them here are the varibales for the projection
Code:
``````global.gameWidth = 2732
global.gameHeight = 1536
global.fov = 40;
xFrom = 1083;
yFrom=global.screenDepth
zFrom=784
xTo=1083
yTo=400
zTo=784
xUp=0
yUp=0
zUp=1
angle=global.fov
var r = view_wview[0] / view_hview[0];
aspect=r``````
The cube is drawn with a center at 200, 200, 200 and a width, height and depth of 400
I am also viewing the cube from the negative y axis looking at the positive y.

Last edited:

flyingsaucerinvasion

Member
what's the problem exactly?

Brenden

Member
what's the problem exactly?
The problem for my attempt at my goal is that the front face dose not have the correct pixel to pixel ratio with the display.
I want the front face to not be skew at all so each pixel of the face matches up with each pixel on the display.

For example, the front face primitive is 400 by 400 pixels so when viewing the game on a display or your screen the front face should still be 400 by 400 pixels.

flyingsaucerinvasion

Member
EDIT:

cam_up_x = 0;
cam_up_y = 0;
cam_up_z = 1;
cam_to_x = cam_x_offset+0.5;
cam_to_y = -cube_depth/2;
cam_to_z = cam_y_offset+0.5;
cam_from_x = cam_to_x;
cam_from_y = cam_to_y - surf_height/(2*dtan(cam_fov/2));
cam_from_z = cam_to_z;

The first thing that needs to be said, is that the texture of the cube needs to have the same resolution as the size of the cube face. So if your cube is 400 across, then the surface texture also needs to have a size of 400.

By default in gamemaker, when using a perspective projection, we get a left-handed coordinate system, and based on the position and orientation of the camera (positive z up I'm guessing), the offset of the camera should be positive along the x and y axes in order to put the cube in the bottom right corner of the port. If negative z is up, then reverse the sign of all that. Adding 0.5 to those offsets will correct blurry pixels if you have texture interpolation turned on. (BUT not if you are also scaling up the view port).

Last edited: