3D in Parallax

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
1337
DCEmu Newbie
DCEmu Newbie
Posts: 7
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Tue Nov 29, 2022 7:09 am
Location: Where sailors hang out
Has thanked: 9 times
Been thanked: 3 times

3D in Parallax

Post by 1337 »

I'm trying to write a free Beat Saber clone for the Dreamcast.
swords.png
I find importing 3D assets from Blender and moving (translating) them straightforward enough but when I try to rotate stuff it all stops making sense. How do you rotate individual 3D objects without the rotations accumulating and also rotating the rest of the world? All of the demos I compiled rotate the entire matrix, are there any good code examples of working with multiple 3D objects?

I rotate the left (red) sword 90 degrees, the camera and the other object - right sword (blue) stay stationary, then I rotate the blue sword and the red one also rotates with it. I tried inserting and swapping around the push pop commands, putting them in-between rotations and they didn't seem to do anything.

Code: Select all

plx_mat3d_push();

plx_mat_identity();
plx_mat3d_apply(PLX_MAT_MODELVIEW);

plx_mat_identity();
plx_mat3d_apply_all();

//TRANSFORM RIGHT CONTROLLER
vector_t transformedVertsRightController[76]; 
plx_mat_transform(sword, transformedVertsRightController, 76, 4 * sizeof(float));

//mat_rotate_x(2.2f);

//rotates left (red) controller
mat_rotate_x(-2.2f);
mat_rotate_y(-2.0f);

//TRANSFORM LEFT CONTROLLER
vector_t transformedVertsLeftController[76]; 
plx_mat_transform(sword, transformedVertsLeftController, 76, 4 * sizeof(float));

plx_mat3d_pop();
When I try to submit more that one triangle list the program crashes on the DC therefore I need to combine all 3D objects consisting of triangle strips into one list.
Can't I first rotate the vertex array of the object, then submit it to the matrix?

Code: Select all

// Vertices for the sword
vector_t sword[76] = {
    //right edge
	{  -0.055163, -3.947190, -0.000000, 1.0f },
	{  -0.041008, -3.800521, -0.007942, 1.0f },
	{  0.044857, -3.809751, -0.000000, 1.0f },
	{  0.014696, -3.274535, -0.012691, 1.0f },
	{  0.151422, -2.841005, -0.000000, 1.0f },
	{  0.069818, -2.490783, -0.018990, 1.0f },
	{  0.188623, -2.133870, -0.000000, 1.0f },
	{  0.083189, -1.770629, -0.018990, 1.0f },
	{  0.194529, -1.477070, -0.000000, 1.0f },
	{  0.060419, -1.161509, -0.018990, 1.0f },
	{  0.165813, -0.773039, -0.000000, 1.0f },
	{  0.018869, -0.507682, -0.018990, 1.0f },
	{  0.125755, -0.330209, -0.000000, 1.0f },
	{  -0.003405, -0.341672, -0.018990, 1.0f },
 
    //handle
	{  0.173421, -0.289250, -0.000016, 1.0f },
	{  -0.056396, 1.036529, -0.000009, 1.0f },
	{  -0.063859, -0.311720, -0.000016, 1.0f },
	{  -0.269681, 1.001541, -0.000009, 1.0f },
	{  -0.000011, -0.305318, -0.054207, 1.0f },
	{  -0.213438, 1.022534, -0.045449, 1.0f },
	{  0.115654, -0.295042, -0.054207, 1.0f },
	{  -0.111428, 1.034082, -0.045449, 1.0f },
	{  -0.000011, -0.305318, 0.053408, 1.0f },
	{  -0.213438, 1.022534, 0.044650, 1.0f },
	{  0.115654, -0.295042, 0.053408, 1.0f },
	{  -0.111428, 1.034082, 0.044650, 1.0f },

    //guard front
    //        X       Z         Y       blender coordinates
	{  -0.156711, -0.356212, 0.000175, 1.0f },
	{  -0.003405, -0.341672, -0.018990, 1.0f },
	{  -0.156711, -0.356212, 0.000175, 1.0f },

	{  -0.156711, -0.356212, 0.000175, 1.0f },
	{  -0.045441, -0.345056, 0.093277, 1.0f },
	{  -0.003405, -0.341672, -0.018990, 1.0f },
	{  0.156134, -0.327147, 0.093277, 1.0f },    
	{  0.125755, -0.330209, -0.000000, 1.0f },
   	{  0.256806, -0.317053, 0.000175, 1.0f },
	{  0.156134, -0.327147, -0.094267, 1.0f },

	{  0.125755, -0.330209, -0.000000, 1.0f },
 	{  0.156134, -0.327147, -0.094267, 1.0f },
	{  -0.003405, -0.341672, 0.018990, 1.0f },
	{  -0.045441, -0.345056, -0.094267, 1.0f },
	{  -0.156711, -0.356212, 0.000175, 1.0f },

    //guard edge 24
    //        X       Z         Y       blender coordinates
	{  0.256806, -0.317053, 0.000175, 1.0f },
	{  0.152616, -0.292066, 0.093277, 1.0f },
        {  0.156134, -0.327147, 0.093277, 1.0f },
	{  -0.045441, -0.345056, -0.094267, 1.0f },
	{  0.152616, -0.292066, -0.094267, 1.0f },
	{  -0.048958, -0.309976, 0.093277, 1.0f },

 	{  -0.048958, -0.309976, 0.093277, 1.0f },
 	{  -0.045441, -0.345056, -0.094267, 1.0f },
 	{  -0.156711, -0.356212, 0.000175, 1.0f },
 	{  -0.048958, -0.309976, 0.093277, 1.0f },
   	{  -0.160228, -0.321132, 0.000175, 1.0f },

 	{  -0.156711, -0.356212, 0.000175, 1.0f },
 	{  -0.160228, -0.321132, 0.000175, 1.0f },
	{  -0.048958, -0.309976, -0.094267, 1.0f },
 	{  -0.156711, -0.356212, 0.000175, 1.0f },
 	{  -0.045441, -0.345056, 0.093277, 1.0f },

	{  -0.048958, -0.309976, -0.094267, 1.0f },
 	{  -0.045441, -0.345056, 0.093277, 1.0f },
	{  0.152616, -0.292066, 0.093277, 1.0f },
	{  0.156134, -0.327147, -0.094267, 1.0f },

 	{  0.156134, -0.327147, -0.094267, 1.0f },
   	{  0.152616, -0.292066, 0.093277, 1.0f }, 
	{  0.256806, -0.317053, 0.000175, 1.0f },
	{  0.253289, -0.281973, 0.000175, 1.0f },
};

//- end of katana geometry
//--------------------------------------------------------------
How do I apply rotation to the above table of polygon strip coordinates?

I know I can use OpenGL but I want to keep the refresh rate as high as possible.

A workaround would be to apply manual math to two of the columns, say if I want to rotate on the X axis I apply cos and sin functions on the angle value and multiply it with the Y and Z columns but it's going to look ugly.

I know this is a VR game, I will drop stereoscopic vision to get higher frame rate. It would be interesting however to know how one would render from 2 cameras at the same time, probably it would cut the frame rate in half literally. I'm guessing it would involve moving the camera position left and right and then combining buffers.

Update:
How do you get a more realistic 3D camera? The parallax one appears to be the equivalent of an 'orthographic' camera in Blender, meaning that regardless whether an object is near or far it's the same size in the frame. The equivalent of this in real life is looking at everything with long telescopic lens.

Code: Select all

plx_mat3d_perspective(60.0f, 640.0f / 480.0f, 0.1f, 100.0f);//60 deg. FOV, aspect ratio, near plane, far plane
The FOV value just scales the view, it doesn't add fish-eye distortion.
Instead of this:
perspective.png
I'm getting this:
ortho.png
Last edited by 1337 on Sun Jan 29, 2023 1:30 pm, edited 1 time in total.
TapamN
DC Developer
DC Developer
Posts: 109
Joined: Sun Oct 04, 2009 11:13 am
Has thanked: 2 times
Been thanked: 96 times

Re: 3D rotations in Parallax

Post by TapamN »

I've thought about doing red-blue anaglyphs or connecting the Master System 3D glasses to the serial port to do stereography on the Dreamcast, but the DC isn't, uh, exactly equipped to do VR.

About getting the objects to move seperately, you have to remake the matrix for each object. I don't know about Parallax, but if you use the regular KOS matrix functions, the code would roughly go like this:

Code: Select all

//Set up camera
mat_identity();
mat_perspective(...);
mat_scale(1,-1,1); //Make +Y go up

//You could replace these two lines with mat_lookat, if you wanted
mat_rotate(...); //Camera rotation
mat_translate(...); //Camera position

mat_store(&camera_matrix);

for each object {
    mat_load(&camera_matrix);
    mat_rotate(...); //Object rotation
    mat_translate(...); //Object position
    
    //Draw object
}
These users thanked the author TapamN for the post:
1337
Post Reply