Author Topic: Flat Shaded 3D models  (Read 4195 times)

Re: Flat Shaded 3D models
« Reply #20 on: 01 Jun 2018, 03:35 »
Oh... 3D characters in 2D background like Alone in the Dark in a module? (nod)

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: Flat Shaded 3D models
« Reply #21 on: 01 Jun 2018, 10:14 »
TBH, I think it would be better to move rendering code to plugin, if you find a way to transfer drawing surface or bitmap pointer between script and plugin code.

Scavenger

  • Cavefish
  • In Four Glorious Colours!
    • I can help with animation
    •  
    • I can help with backgrounds
    •  
    • I can help with characters
    •  
    • I can help with scripting
    •  
    • Scavenger worked on a game that was nominated for an AGS Award!
      Scavenger worked on a game that won an AGS Award!
Re: Flat Shaded 3D models
« Reply #22 on: 01 Jun 2018, 11:55 »
TBH, I think it would be better to move rendering code to plugin, if you find a way to transfer drawing surface or bitmap pointer between script and plugin code.

Yeah, it's perfectly possible, you can edit a sprite like it was an array in the plugin code, which is how I do all my graphical effects. I'd do it myself if I could understand the code, which is a long way off yet. My C++ isn't good enough to handle it, unless there is a lot of overlap between the AGSScript and C.

Additionally, is it possible to reverse the projection, so that if I point at certain screen coords, I could find out which polygon is being rendered there? It doesn't have to be particularily fast, but it'd be useful in making a painting tool.

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: Flat Shaded 3D models
« Reply #23 on: 01 Jun 2018, 12:46 »
Additionally, is it possible to reverse the projection, so that if I point at certain screen coords, I could find out which polygon is being rendered there? It doesn't have to be particularily fast, but it'd be useful in making a painting tool.

This is done with raycast, that's all I know.

Khris

    • Lifetime Achievement Award Winner
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
    • Khris worked on a game that was nominated for an AGS Award!
Re: Flat Shaded 3D models
« Reply #24 on: 01 Jun 2018, 12:49 »
Yeah, you need to turn the mouse coordinates into 3D camera screen coordinates, unproject them into the world, then intersect the line camera-mouse with all triangles. Then find the closest one of all that get hit.

Re: Flat Shaded 3D models
« Reply #25 on: 24 Jun 2018, 09:20 »


I'm always late to the party...

I started working on a 3d module a few years ago, but gave up. I came back to it last week and rewrote the thing from scratch. I just managed to implement some clipping against the screen boundaries using a version of the Sutherland-Hodgman algorithm (although mine clips at the raster stage, which is not very efficient, I'll consider moving it to before the perspective divide at some point).

It has a light source (sun), the shade is just calculated by comparing the triangle's normal to a vector from the sun, and the tint is customizable.

I'm using a python script to export vertex and triangle information from Blender to AGS script, and once I figure out how to export RGB values from a triangle's material, every triangle will be able to have its own unique tint.

You can move around the scene just like you would in any fps, camera look and WASD, (strafe and forward/backward)

As you can see I have no Z sorting yet. Well, I had one that used the average of all 3 Z values from a triangle, but the sorting algorithm slowed everything to a halt. (Was a bubble sort function, which used an index array into the original array instead of destroying the original array). Since I can't Z-Sort, and I'm using the DrawTriangle function instead of drawing each individual pixel, I can't use the z-buffer method either.

How have you guys managed to solve the Z-sorting issue?

(also, you can see something's not quite right with my backface culling function! I think it's because the vector from the player to the triangle isn't normalized, but haven't tested that yet).

Doing this thing in AGS script is tough, without any support for vec3/vec4 formats, 2d arrays or structs within structs. Oh well!

Edit: Holy crap, khris, I just looked at your code and I had no idea I could write functions that returned arrays in AGS. That would have made this soooo much easier! Time to do a refactor, I guess... Sometimes I forget how powerful AGS Script can be. I guess it's because I haven't made a game since AGS 2.62 lol.
« Last Edit: 24 Jun 2018, 09:47 by Phemar »

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: Flat Shaded 3D models
« Reply #26 on: 24 Jun 2018, 12:33 »
Doing this thing in AGS script is tough, without any support for vec3/vec4 formats, 2d arrays or structs within structs. Oh well!

You now can declare user managed types like this:
Code: Adventure Game Studio
  1. // declaration in script.ash
  2. managed struct Vector4f
  3. {
  4.     float x;
  5.     float y;
  6.     float z;
  7.     float w;
  8.  
  9.     import static Vector4f *Newf(float x, float y, float z, float w);
  10.     import static Vector4f *NewVec(int x, int y, int z);
  11.     import static Vector4f *NewVecf(float x, float y, float z);
  12.     import static Vector4f *NewPt(int x, int y, int z);
  13.     import static Vector4f *NewPtf(float x, float y, float z);
  14. };
  15.  
  16. // In script.asc
  17. static Vector4f *Vector4f::Newf(float x, float y, float z, float w)
  18. {
  19.     Vector4f *v = new Vector4f;
  20.     v.x = x;
  21.     v.y = y;
  22.     v.z = z;
  23.     v.w = w;
  24.     return v;
  25. }
  26.  

They are still limited in some parts, and may work slightly slower than with global variables.
But then you can do something like:

Code: Adventure Game Studio
  1. struct Matrix44
  2. {
  3.     float x[16];
  4.     import Vector4f *Transform(Vector4f *v);
  5. }
  6.  
« Last Edit: 24 Jun 2018, 12:37 by Crimson Wizard »

Re: Flat Shaded 3D models
« Reply #27 on: 24 Jun 2018, 14:04 »
Crimson, that's awesome.

And here I was doing it like this:
Code: Adventure Game Studio
  1. enum Coordinates {
  2.   eX = 0,
  3.   eY = 1,
  4.   eZ = 2,
  5.   eW = 3,
  6. };
  7.  
  8. struct Vertex
  9. {
  10.   float world[3];
  11.   float camera[3];
  12.   float screen[3];
  13.   float normalized[3];
  14.   int raster[2];
  15. };
  16.  
  17. struct Triangle
  18. {
  19.   int vertex[3];
  20.   float vector1[3], vector2[3];
  21.   float vectorToSource[3];
  22.   float normal[3];
  23.   float center[3];
  24. };

Then declaring an array of Triangles and Vertices and accessing them like this:

Code: Adventure Game Studio
  1. Vertex mesh_verts[MAX_VERTICES];
  2. Triangle mesh_tris[MAX_TRIS];
  3.  
  4. //and then to access:
  5. mesh_verts[mesh_tris[24].vertex[0]].raster[eX] = //whatever

Your way is basically what I wanted to do when I started, but I wasn't sure how to do it in AGS.