Finally I found, but it contain bug (It's not properly work for some configurations of angle):
(the used variables are float 3D vectors: point, new,
obj_coord, obj_coordsys
obj_rad, cam_rad,
world_coordsys,
coordsys_cam, cam_coord,
cam_x, cam_y, cam_z,
cos_x, cos_y, cos_z,
sin_x, sin_y, sin_z
and one integer speed_cam)
- At the beginning, I have a vector point corresponding to a vertex of my object.
- Object_Management:
- I add this vector with obj_coord[].
- Then, I'm passing through my equations for rotations:
cos_x = cos(obj_rad[_x])
cos_y = cos(obj_rad[_y])
cos_z = cos(obj_rad[_z])
sin_x = sin(obj_rad[_x])
sin_y = sin(obj_rad[_y])
sin_z = sin(obj_rad[_z])
// Rotate xyz order matrix
new[_x] = (point[_x] * cos_y * cos_z ) - (point[_y] * cos_y * sin_z ) - (point[_z] * sin_y )
new[_y] = (point[_x] * ((cos_x * sin_z) - (sin_x * sin_y * cos_z))) + (point[_y] * ((sin_x * sin_y * sin_z) + (cos_x * cos_z))) - (point[_z] * (sin_x * cos_y))
new[_z] = (point[_x] * ((cos_x * sin_y * cos_z) + (sin_x * sin_z))) + (point[_y] * ((sin_x * cos_z) - (cos_x * sin_y * sin_z))) + (point[_z] * (cos_x * cos_y))
point[_x] = new[_x]
point[_y] = new[_y]
point[_z] = new[_z]
- I add the output vector with obj_coordsys[].
- I add it with world_coordsys[].
- Camera_Management:
cos_x = cos(-cam_rad[_x])
cos_y = cos(-cam_rad[_y])
cos_z = cos(-cam_rad[_z])
sin_x = sin(-cam_rad[_x])
sin_y = sin(-cam_rad[_y])
sin_z = sin(-cam_rad[_z])
// vector right/left
cam_y[_x] = speed_cam * ( cos_y * cos_z )
cam_y[_y] = speed_cam * (((cos_x * sin_z) - (sin_x * sin_y * cos_z)))
cam_y[_z] = speed_cam * (((cos_x * sin_y * cos_z) + (sin_x * sin_z)))
// vector up/down
cam_x[_x] = speed_cam * ( cos_y * sin_z )
cam_x[_y] = speed_cam * (((sin_x * sin_y * sin_z) + (cos_x * cos_z)))
cam_x[_z] = speed_cam * (((sin_x * cos_z) - (cos_x * sin_y * sin_z)))
// vector forward/backward
cam_z[_x] = speed_cam * ( sin_y )
cam_z[_y] = speed_cam * ((sin_x * cos_y))
cam_z[_z] = speed_cam * ((cos_x * cos_y))
Example:
key 'Z': // Moving Forward
{
coordsys_cam[_x] += cam_z[_x]
coordsys_cam[_y] += cam_z[_y]
coordsys_cam[_z] += cam_z[_z]
}
key 'S': // Moving Backward
{
coordsys_cam[_x] -= cam_z[_x]
coordsys_cam[_y] -= cam_z[_y]
coordsys_cam[_z] -= cam_z[_z]
}
key 'D': // Moving Right
{
coordsys_cam[_x] += cam_x[_x]
coordsys_cam[_y] += cam_x[_y]
coordsys_cam[_z] += cam_x[_z]
}
key 'Q': // Moving Left
{
coordsys_cam[_x] -= cam_x[_x]
coordsys_cam[_y] -= cam_x[_y]
coordsys_cam[_z] -= cam_x[_z]
}
key 'A': // Moving Up
{
coordsys_cam[_x] += cam_y[_x]
coordsys_cam[_y] += cam_y[_y]
coordsys_cam[_z] += cam_y[_z]
}
key 'E': // Moving Down
{
coordsys_cam[_x] -= cam_y[_x]
coordsys_cam[_y] -= cam_y[_y]
coordsys_cam[_z] -= cam_y[_z]
}
- I sub it with cam_coordsys[].
- I'm pass it again through my equations for rotations:
// Rotate xyz order matrix
new[_x] = (point[_x] * cos_y * cos_z ) - (point[_y] * cos_y * sin_z ) - (point[_z] * sin_y )
new[_y] = (point[_x] * ((cos_x * sin_z) - (sin_x * sin_y * cos_z))) + (point[_y] * ((sin_x * sin_y * sin_z) + (cos_x * cos_z))) - (point[_z] * (sin_x * cos_y))
new[_z] = (point[_x] * ((cos_x * sin_y * cos_z) + (sin_x * sin_z))) + (point[_y] * ((sin_x * cos_z) - (cos_x * sin_y * sin_z))) + (point[_z] * (cos_x * cos_y))
point[_x] = new[_x]
point[_y] = new[_y]
point[_z] = new[_z]
- I sub point[] with cam_coord[].
- Screen_Management :
- I place this vector into a table through the below equation, then I displays it as an image:
point[_x] = ((point[_x] * distance) / -point[_z])
point[_y] = ((point[_y] * distance) / -point[_z])
screen[repere - ((x * bpp) + (pitch * y))] = color ; bpp = 4 (bytes_per_pixel), 32 (bits_per_pixel) . pitch = screen_y (y resolution of image) * bpp
; repere = (screen_x * (screen_y - 1)) + ((screen_x / 2) - 1);