Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NOTE(Constantine):
- // Reconstructing 3D point from 2D UV coordinates of a 3D triangle using barycentric coordinates in Game Script C
- // https://discord.gg/D7pKPw4kFf
- // https://discord.com/channels/908452801678561291/908452802165112885/1410190619644788797
- #include "gamescriptc.h"
- #include <stdio.h> // For printf
- #include <math.h> // for sqrt
- typedef struct {
- float x, y, z;
- } Vec3;
- typedef struct {
- float u, v;
- } UV;
- Vec3 reconstruct3DPosition(Vec3 v1, Vec3 v2, Vec3 v3, UV uv) {
- float w = 1.0f - uv.u - uv.v;
- Vec3 point;
- point.x = w * v1.x + uv.u * v2.x + uv.v * v3.x;
- point.y = w * v1.y + uv.u * v2.y + uv.v * v3.y;
- point.z = w * v1.z + uv.u * v2.z + uv.v * v3.z;
- return point;
- }
- cfn frame() {
- framestart();
- UV uv = {0.0f, 0.0f};
- if (getCurrentFrame() == 0) {
- Number inputU = globalArrayPersistentNew8Bit("inputU", 16);
- Number inputV = globalArrayPersistentNew8Bit("inputV", 16);
- pointerSetString(inputU, 0, "0.2");
- pointerSetString(inputV, 0, "0.3");
- }
- imguiBegin();
- if (imguiWindowBegin("UV coordinates", 1, 0) == true) {
- Number inputU = globalArrayPersistentNew8Bit("inputU", 16);
- Number inputV = globalArrayPersistentNew8Bit("inputV", 16);
- imguiInputText("U", inputU, 0, 16);
- imguiInputText("V", inputV, 0, 16);
- String stringU = pointerGetString(inputU);
- String stringV = pointerGetString(inputV);
- uv.u = interpretStringToFloat(stringU);
- uv.v = interpretStringToFloat(stringV);
- }
- imguiWindowEnd();
- imguiEnd();
- Vec3 v1 = {1.0f, 0.0f, 0.0f};
- Vec3 v2 = {0.0f, 0.0f, 1.0f};
- Vec3 v3 = {0.0f, 1.0f, 0.0f};
- if (getCurrentFrame() == 0) {
- gizmoSetVector("v1", v1.x, v1.y, v1.z);
- gizmoSetVector("v2", v2.x, v2.y, v2.z);
- gizmoSetVector("v3", v3.x, v3.y, v3.z);
- }
- NumberArray gv1 = gizmoGetVector("v1");
- NumberArray gv2 = gizmoGetVector("v2");
- NumberArray gv3 = gizmoGetVector("v3");
- v1.x = gv1[0];
- v1.y = gv1[1];
- v1.z = gv1[2];
- v2.x = gv2[0];
- v2.y = gv2[1];
- v2.z = gv2[2];
- v3.x = gv3[0];
- v3.y = gv3[1];
- v3.z = gv3[2];
- Vec3 reconstructedPoint = reconstruct3DPosition(v1, v2, v3, uv);
- printf("Reconstructed 3D Position: (%f, %f, %f)\n", reconstructedPoint.x, reconstructedPoint.y, reconstructedPoint.z);
- drawTriangle("", v1.x,v1.y,v1.z, v2.x,v2.y,v2.z, v3.x,v3.y,v3.z, 255,45,85,255);
- drawPoint("", reconstructedPoint.x, reconstructedPoint.y, reconstructedPoint.z, 0.5f, 255,0,255,255);
- // Gizmo
- Number currentMode = globalArrayPersistentNew8Bit("gizmoCurrentMode", 1 * 4);
- {
- if (pointerGetUnsignedInteger(currentMode, 0) == 0) {
- // https://discord.gg/D7pKPw4kFf, https://discord.com/channels/908452801678561291/908452802165112885/918489067543466054
- if (windowIsFocused() && windowIsHovered()) {
- defaultCameraDefaultControlEnable();
- } else {
- defaultCameraDefaultControlDisable();
- }
- } else {
- defaultCameraDefaultControlDisable();
- }
- if (keyboardGetGlfwKeyEvent(258) == 0 && mouseGetButtonIsPressed(2)) { // Change current modelling mode: right mouse button
- Number v = pointerGetUnsignedInteger(currentMode, 0);
- v = v == 0 ? 1 : 0;
- pointerSetUnsignedInteger(currentMode, 0, v);
- gizmoSetMode("v1", v);
- gizmoSetMode("v2", v);
- gizmoSetMode("v3", v);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment