AntonioND / nitro-engine

3D engine for the Nintendo DS
150 stars 10 forks source link

Found NE_CameraSet() bug and a sprite sheet question #25

Closed rVicio closed 5 months ago

rVicio commented 5 months ago

Hi, I've been using your library to learn to program in c for my old dsi, I'm using version 0.9.1 and there seems to be a problem with the NE_CameraSet() function, the X unit vector up generates rendering problems with values greater and lower than zero, however, this input doesn't seems to do anything if the other two inputs, Y up and Z up, are not zero (either positive and negative values), I'm testing a the function to get an idea of a way to implement a fully free camera rotation using quaternions (though I don't know if there's another way to do this). Also, I have a question, maybe there's already a function or a way to do this but I haven't found nor understand it yet, it is how can I specify a non power of 2 sector/area from a power of 2 material to be the texture for a quad, because I have the textures and sprite sheets extracted from Age of Empires 3 Mobile (j2me) and all of them are non power of 2 textures (i.e. the background tiles are 48x23, many sprites are 12x48 and so on), I can merge most of them in a power of 2 sprite sheet to use nitro-engine material function. I've seen Fewinty's minecraft ds project for reference but I want to use your library because it makes it alot of it way easier.

AntonioND commented 5 months ago

Sorry, I don't understand the conditions that cause the bug, can you write concrete examples here, with actual numbers?

Regarding the texture sizes, NE used to support non-power-of-two texture widths, but it was just resized them internally. This is quite misleading because a 48x23 texture (1104 bytes) will be 64x23 internally (1472 bytes). The actual memory usage would be 33% higher than what you expect.

rVicio commented 5 months ago

Thanks for the reply about the textures, that explains why these renders just as white boxes since v0.8.0, my question is about a way to specify a section in a sprite sheet as the texture for a quad so I don't have to resize all the sprites and tiles wasting memory or deforming them, I can combine 36 tiles in one png of 256x184 pixel (about 5x8 tiles), only a section of 16x184 would be empty instead of using 48x23 pixels from a 64x23 png for each tile.

About the camera function, I tested v0.10.0 and the problem still happens, is quite simple to replicate, it happens when the last three inputs are just 1, 0, 0. i.e. the free_camera example, in main.c, set the up direction to the x axis and the model wont render correctly, you can rotate the camera down to force to render the model but when you do the model rotates like crazy, the camera seems to roll indefinitely: NE_CameraSet(Camera, -8, 3, 0, // Position 0, 3, 0, // Look at 1, 0, 0); // Up direction

Also, I tried to compile v0.11.0 with msys2 (included with devkitpro), but it says there's an error: $ make NE2D.c NEAlloc.c NEAnimation.c NECamera.c NEDisplayList.c NEFAT.c NEFormats.c NEGUI.c NEGeneral.c NEModel.c NEPalette.c NEPhysics.c NEPolygon.c NERichText.c C:/devkitPro/nitro-engine/source/NERichText.c: In function 'NE_RichTextBitmapLoadGRF': C:/devkitPro/nitro-engine/source/NERichText.c:178:11: error: 'tex' undeclared (first use in this function) 178 | (void)tex; | ^~~ C:/devkitPro/nitro-engine/source/NERichText.c:178:11: note: each undeclared identifier is reported only once for each function it appears in C:/devkitPro/nitro-engine/source/NERichText.c:179:11: error: 'pal' undeclared (first use in this function) 179 | (void)pal; | ^~~ C:/devkitPro/nitro-engine/source/NERichText.c:180:11: error: 'flags' undeclared (first use in this function); did you mean 'fls'? 180 | (void)flags; | ^~~~~ | fls make[1]: [/opt/devkitpro/devkitARM/base_rules:85: NERichText.o] Error 1 make: [Makefile:111: build_release] Error 2

AntonioND commented 5 months ago

Okay, I see the problem now, even examples/loading/simple_model exposes this issue if you use 1, 0, 0 instead of 0, 1, 0. I'll take a look at it.

Regarding the build issue, have you updated to the current master branch? I fixed that a few commits ago: https://github.com/AntonioND/nitro-engine/commit/227bffddc5fac2a2fe353c5c4b5b0e6cb67d1d8b

AntonioND commented 5 months ago

Oh, almost forgot.

Thanks for the reply about the textures, that explains why these renders just as white boxes since v0.8.0, my question is about a way to specify a section in a sprite sheet as the texture for a quad so I don't have to resize all the sprites and tiles wasting memory or deforming them, I can combine 36 tiles in one png of 256x184 pixel (about 5x8 tiles), only a section of 16x184 would be empty instead of using 48x23 pixels from a 64x23 png for each tile.

Yes, the change to the texture sizes that aren't powers of two happened at that version.

How are you going to use the textures? Are they going to be applied just to 2D quads, or to full 3D models exported by something like obj2dl? I'm trying to think of how that would work in practice.

I guess you can also setup the texture matrix to scale the texture size... but I'm not sure if that would be accurate enough, I would need to test it.

rVicio commented 5 months ago

Just to 2D quads, you see, I want to recreate a version of Age of Empires 3 Mobile (the j2me version, it is in 2D), but all the textures have their own pixel resolution thats not a power of 2 nor a multiple of 8, I used kemulator 1.0.3 Xray option to see all the textures seems to be applied to quads (tiles, sprites, animations) and I though the DS's 2D procesors wont help with that, especially if I resize all the textures they'll be deformed and might loss the aspect ratio. Also, all versions of this game uses the exact same textures except the Main menu and the ingame ui. (I can upload screenshots if you want). What i want to know specificly is if there's a way to do something like many game engines allows with sprite sheets, that is, select one sprite from a power of 2 texture or png t hat contains many sprites in rows and columns. At least, I think that could have the possibility to specify any 2D quad resolution.

AntonioND commented 5 months ago

Ok, I understand.

Yes, I agree this would be useful. Are you using NE_Sprite to draw your 2D quads? I could add a function like NE_SpriteSetTextureCanvas (name TBD) that would let you define the chunk of texture to draw by default.

rVicio commented 5 months ago

Yes, I'm using NE_Sprite functions to draw the quads, both tiles and sprites, and a function like that it would be a big addition to your engine. I think that is a good name, I only knew of this textures as sprite sheets, didn't think of them as a canvas.

About the compilation problem, it happened with the version avaliable in releases.

Regarding the build issue, have you updated to the current master branch? I fixed that a few commits ago: 227bffd

So instead I downloaded the source code from the nitro-engine main page (the "<> code" button), and it compiles just fine.

AntonioND commented 5 months ago

Okay, I've implemented the function! https://github.com/AntonioND/nitro-engine/commit/8cb10264bb9592becfa24c5224f3d9292a649484 I've also improved the sprites example to show how to use it, check it here: https://github.com/AntonioND/nitro-engine/tree/5ea29c4f81d3c4f756faae37f2fae2c0b5453f15/examples/2d_system/sprites

I'll take a look at the camera issue later.

AntonioND commented 5 months ago

Ah, wait, of course the camera isn't working.

NE_CameraSet(Camera,
             -8, 3, 0, // Position
              0, 3, 0, // Look at
              1, 0, 0); // Up direction

This means that the camera is looking from -8 to 0 in the X axis, and that "up" goes backwards in the X axis (from 0 to 1). Those two vectors are parallel, so the projection can't work. The "up" vector and "look at - position" can't be parallel.

This is also the reason why you can't look directly up if the "up" vector is (0, 1, 0).

rVicio commented 5 months ago

You're right, it makes sense, I think I could have guessed it myself, mi bad then for not paying atention enough to the parameters. Also, I think you should check the engine available in releases, only compiles correctly if I compile the latest version downloaded from the main page, at least, for me.

AntonioND commented 5 months ago

You're right, it makes sense, I think I could have guessed it myself, mi bad then for not paying atention enough to the parameters.

No worries, it confused me quite a bit as well.

Also, I think you should check the engine available in releases, only compiles correctly if I compile the latest version downloaded from the main page, at least, for me.

Yes, version v0.11.0 is broken in devkitPro, it only builds for BlocksDS. I'm going to release a new version of the library this weekend, use the master branch until then.