Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
How do I get a (sort of) absolute vertex position?

dekon_17
Registered User
Quote
2022-04-30 10:34:58

Some time age, I've posted here about the weird problem with light mapping in CopperCube. As it turned out, the problem was the... rotation. I could use the Blender lightmapping, but nah, it is terrible idea (if I need to place some stuff around the map, I would need to create props in the level geometry file, and I don't want to mess with a lot of stuff in Blender, this is kinda complicated). Another way was to create a few instances of models with different rotations (in Blender, yet again), but hey, should I say why is this idea bad?

And now, there's only two possible solutions that I have, both in CopperCube. The first one is to place a plane mesh with no rotation, and then using "Merge selected mesh into another mesh" add a rotated model geometry to this plane mesh, and then delete plane mesh using polygon editing. Sounds terrible, isn't it? The second way is the plugin! Yeah, I kinda built a plugin for this purpose. Despite the fact that it works... It doesn't works at the same time. Yes, it saves the mesh vertex position, but these are like, a local ones? So, rotation doesn't really affect the positions.

Now's the question (that you've seen in the thread title probably), how can I get a position of vertex, but taking into account the mesh rotation?

Here's the initial plugin code (that does nothing to geometry, but rotates the mesh to 0 degrees):
function RotationCorrection()
{
var Array = [];

var Node = editorGetSelectedSceneNode ();
var Buffers = ccbGetSceneNodeMeshBufferCount (Node);

if (Buffers == 0)
alert ("Ayo, what the ****?");
else
{
for (var b = 0; b < Buffers; ++b)
{
var Verticies = ccbGetMeshBufferVertexCount (Node, b);
for (var v = 0; v < Verticies; ++v)
{
var Pos = ccbGetMeshBufferVertexPosition (Node, b, v);
Array.push (Pos);
}
}
ccbUpdateSceneNodeBoundingBox (Node);
}

ccbSetSceneNodeProperty (Node, "Rotation", 0, 0, 0);

if (Buffers == 0)
alert ("Ayo, what the ****?");
else
{
for (var b = 0; b < Buffers; ++b)
{
var Verticies = ccbGetMeshBufferVertexCount (Node, b);
for (var v = 0; v < Verticies; ++v)
{
ccbSetMeshBufferVertexPosition (Node, b, v, Array [v]);
}
}
ccbUpdateSceneNodeBoundingBox (Node);
}
}

editorRegisterMenuEntry ("RotationCorrection();", "Freeze rotation of object");

And here's the example of this broken light mapping:
🔎︎

The left flag (with propper lighting) was edited with the first engine method I mentioned earlier, and the right one (with wrong lighting) is not edited.


dekon_17
Registered User
Quote
2022-05-01 18:54:32

So, after (obviously) having no replies, I tried to do... At least something.

I found a few articles about rotational matrixes, and those articles had some... Formulas. Despite the fact that I wrote the exact same formulas in my code, it gave me terrible... TERRIBLE results. Simple cube, that I've used for testing purposes, deformed in various shapes... except for cube, if the angle wasn't 0 on every axis. I tried to rewrite some stuff in formulas, but to no success. One of my thoughts is that it kinda happens because in CopperCube Y-axis goes up instead of going to side, so this might be a reason. Now I only left the X rotation thing, and it still works terribly: the mesh, rotated by 90 degrees gave incredibly different rotation (143 degrees), I don't know how it works. Same goes to other degrees, of course. Here's the formulas:
🔎︎

(Things on russian translates as "By X axis", "By Y axis" and "By Z axis").
And here's the script that I wrote:
function RotationCorrection ()
{
var Node = editorGetSelectedSceneNode ();
var Buffers = ccbGetSceneNodeMeshBufferCount (Node);

var Rot = ccbGetSceneNodeProperty (Node, "Rotation");
var Array = [Rot.x, Rot.y, Rot.z];

//Normalizing rotation
for (var Axis = 0; Axis < 3; ++Axis)
{
if (Array [Axis] > 360)
{
do
{
Array [Axis] -= 360;
}
while (Array [Axis] > 360);
}

if (Array [Axis] < 0)
{
do
{
Array [Axis] += 360;
}
while (Array [Axis] < 0);
}
}

Rot.x = Array [0];
Rot.y = Array [1];
Rot.z = Array [2];

ccbSetSceneNodeProperty (Node, "Rotation", 0, 0, 0);

var sin = Math.sin;
var cos = Math.cos;

if (Buffers == 0)
alert ("Ayo, what the ****?");
else
{
for (var b = 0; b < Buffers; ++b)
{
var Verticies = ccbGetMeshBufferVertexCount (Node, b);
for (var v = 0; v < Verticies; ++v)
{
var NewPos = ccbGetMeshBufferVertexPosition (Node, b, v);
var Pos = ccbGetMeshBufferVertexPosition (Node, b, v);

//Rotating by X axis only
NewPos.y = Pos.y * cos(Rot.x) + Pos.z * sin(Rot.x);
NewPos.z = -Pos.y * sin(Rot.x) + Pos.z * cos(Rot.x);

ccbSetMeshBufferVertexPosition (Node, b, v, NewPos);
}
}
ccbUpdateSceneNodeBoundingBox (Node);
}

var Rot = ccbGetSceneNodeProperty (Node, "Rotation");
ccbSetSceneNodeProperty (Node, "Rotation", 0, 0, 0);

var sin = Math.sin;
var cos = Math.cos;
}

editorRegisterMenuEntry ("RotationCorrection();", "Freeze rotation of object");

I am sure that the script might be bad, but at least that's my first plugin for CopperCube.

If you understand something about what's wrong, can you please tell me where did I screwed up.


dekon_17
Registered User
Quote
2022-05-01 21:38:37

Okay, two more things I found:
- The part that was supposed to "normalize" rotation was changing it in different ways, so, I had to delete it;
- Sinus and cosinus calculation acts weird. Despite having the 90 degree angle, the sinus is 0.89 and cosinus is -0.45 (it is supposed to be 1 and 0). To calculate these, I've used basic math functions:
NewPos.x = Pos.x * Math.cos(Rot.y) + Pos.z * Math.sin(Rot.y);
NewPos.z = -Pos.x * Math.sin(Rot.y) + Pos.z * Math.cos(Rot.y);

This is such a ridiculous problem...

[Edit: after doing a little research on internet, I found that Math.sin and Math.cos requires radians, which I didn't knew about all this time, bruh]


Guest
Guest
Quote
2022-05-01 22:01:51

I've talked to you before about lighting in CC many times. I can't help but think this solution of yours is a bit extreme and unnecessary. Without having the exact same scene as you, finding a solution will be difficult. Lighting in games is done specifically by a person whose sole job is to light scenes. It's an art form of its own. Have you tried toggling the light on and off in the scene? I've had to do this before many times to get the light to update once I have moved it. Other stuff we have mentioned before is geometry. Since CC uses vertex lighting, it needs geometry to show up. Are your flags subdivided? Do they both have the same number of subdivisions? I've pretty much given up on making CC look AAA. I've instead been trying to do my lighting after my scene is finished and specifically concentrating on the lights one at time. I believe CC can do lighting like early pre-2010s horror FPS games, which is all I need from it, but anything coming close to new tech is a pipe dream. Another thing I've been experimenting with is avoiding pure white light and have instead been getting great results using various shades of grey.


dekon_17
Registered User
Quote
2022-05-01 22:28:08

The flag model is the same, except one of them doesn't have rotation.

Also, I finally fixed my script... Almost. I mean, it does its job fine now, but it can't handle all the rotations at once (probably my mistake somewhere) and textures are repositioned sometimes.
function RotationCorrection ()
{
var Node = editorGetSelectedSceneNode ();
var Buffers = ccbGetSceneNodeMeshBufferCount (Node);

var Rot = ccbGetSceneNodeProperty (Node, "Rotation");

ccbSetSceneNodeProperty (Node, "Rotation", 0, 0, 0);

if (Buffers == 0)
alert ("Ayo, what the fuck?");
else
{
for (var b = 0; b < Buffers; ++b)
{
var Verticies = ccbGetMeshBufferVertexCount (Node, b);
for (var v = 0; v < Verticies; ++v)
{
var NewPos = new vector3d(0, 0, 0);
var Pos = ccbGetMeshBufferVertexPosition (Node, b, v);

NewPos.x = Pos.x;
NewPos.y = Pos.y * Math.cos(Rot.x * Math.PI / 180) + Pos.z * Math.sin(Rot.x * Math.PI / 180);
NewPos.z = -Pos.y * Math.sin(Rot.x * Math.PI / 180) + Pos.z * Math.cos(Rot.x * Math.PI / 180);

var Pos = new vector3d(NewPos.x, NewPos.y, NewPos.z);

NewPos.x = Pos.x * Math.cos(Rot.y * Math.PI / 180) + Pos.z * Math.sin(Rot.y * Math.PI / 180);
NewPos.y = Pos.y;
NewPos.z = -Pos.x * Math.sin(Rot.y * Math.PI / 180) + Pos.z * Math.cos(Rot.y * Math.PI / 180);

var Pos = new vector3d(NewPos.x, NewPos.y, NewPos.z);

NewPos.x = Pos.x * Math.cos(Rot.z * Math.PI / 180) - Pos.y * Math.sin(Rot.z * Math.PI / 180);
NewPos.y = Pos.x * Math.sin(Rot.z * Math.PI / 180) + Pos.y * Math.cos(Rot.z * Math.PI / 180);
NewPos.z = Pos.z;

ccbSetMeshBufferVertexPosition (Node, b, v, NewPos);
}
}
ccbUpdateSceneNodeBoundingBox (Node);
}
}

editorRegisterMenuEntry ("RotationCorrection();", "Freeze rotation of object");


[Edit: it doesn't really fix the lighting, although, it might be because of rotated normals or something]


Guest
Guest
Quote
2022-05-01 23:09:58

Are you importing the model of the flag and cloning it and placing it by hand or are you importing multiple flags with that chunk of room? If it is the latter then perhaps something is being written over when you import that into your scene. I use separate meshes and import them individually, then make prefabs, and clone them around the scene as needed. I've yet to come across your problem. This to me seems like something is happening when you import from Blender. It could be that the names of the flags are the same which might be causing an issue. In Blender are you saving the rotation of the flag in question? I've forgotten which key combo that uses but when you scale or position something in Blender you have to save it. Ctrl + A to apply scale and rotation?


aiming_bullets
Guest
Quote
2022-05-02 00:07:10

Rotating an object with the polygon tools is not the same as rotating the object in-game, It actually rotates the geometry, which can be a reason Coppercube rotation is always like this.

You can try updating the Normals of the object, you can use the inbuilt function of normalizing normals to check if it can fix things for you.

@Guest don't give up on achieving AAA quality stuff in CC, some users have proved that it is possible with the help of shaders and lightmaps.


just_in_case
Moderator
Quote
2022-05-02 02:03:21

I don't really get why you need to create a plugin for the rotation of the geometry?

you can rotate the object using the polygon editing tools directly there is no need to merge with another mesh. Simply select the object, go to polygon editing tools, chose the option to select the triangles, then select with rectangles, now select the whole model, make sure every triangle is selected, and then use the Rotate option from the polygon editing tools. Don't use the Rotation option from the main bar. Select it from the polygon editing tools and now you can rotate the actual geometry of the model.

or as suggested by others you can go to modify and normalize the normals of the model to see if it enhances the light mapping.
or go to irredit/irrlicht properties. You will find the NormalizeNormals option there, simply checkmark it to see if it does any good to the light mapping.

If you think it is an issue of Normals then you can update the normal manually with the plugins, there is no need to rotate the actual geometry of the model.
I will suggest you download the Normalize Normal plugin and based on that create your own manual Normal updating plugins, in which you can change the Normals of the object rather than the rotation of the object.

P.S:- I haven't gone through any of the code posted above my answer is based on your main thread.
hope that helps



dekon_17
Registered User
Quote
2022-05-02 06:52:17

Okay, I'll try to answer to all this stuff.

About meshes: the one on the left was added as a static mesh, the second one (on the right) is a cloned mesh (Ctrl + C). The problem is that it has no real difference in light mapping, they both have the same problem.

About normalizing normals: I tried to use this already (before I started programming that), it didn't work. However, I'll try to recalculate them after I "froze" rotation, who knows, maybe it will work.

About rotation using vertex editor: I know about that one, but as any other tool in vertex editor, it doesn't have coordinates and some other stuff, and if I will need to rotate the mesh by 90 degrees, it won't be exactly 90 degrees since I can't do it perfectly. Working with coordinates is... More accurate?

That's pretty much it.


dekon_17
Registered User
Quote
2022-05-02 09:03:36

After using my plugin and then recalculating normals... IT WORKS.
Here's the result:
🔎︎

The right one has correct lighting, I applied my plugin on it and then used the "Recalculate normals and tangents" function.
The left one was only affected by plugin, so the lighting is wrong.


DouweDabbe
Guest
Quote
2022-05-03 14:55:32

maybe you mesh node is inside out ?
or has a double face ?
so that your texture is obscured by the outer untextured one.
In blender and MeshLab there are options to clean your mesh.
Or your lighting settings differ for the objects in the scene?
well you checked that one obviously immediately.

maybe you benefit from modelling in sketchup and then exporting as obj and do a cleanup in MeshLab program its free.

I see the texture bug but think the coded solution is adding extra complication and confusement.


dekon_17
Registered User
Quote
2022-05-03 19:46:32

Studying a functional of a new program (MeshLab) might be a bad decision for me, since I am still trying to understand Blender (and how it works with CopperCube). And programming... Well, I need some practice at programming, so, this experience might come in handy soon.

[Edit: yo, I also just finished the normal recalculation part, so, now I don't need to use "Recalculate normals and tangents"!]


Guest
Guest
Quote
2022-05-05 00:03:34

Glad you got it all sorted out, Dekon.


just_in_case
Moderator
Quote
2022-05-07 14:03:49

Glad that recalculating the normals fixed your issue, so rotating the geometry was not necessary.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inte?national" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact