Skip to content

Commit

Permalink
refactor api
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykarnachev committed Jan 2, 2024
1 parent 5d85d98 commit 4c4a566
Show file tree
Hide file tree
Showing 7 changed files with 793 additions and 816 deletions.
105 changes: 105 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
Language: Cpp
AlignAfterOpenBracket: BlockIndent
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: DontAlign
AlignOperands: true
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '<glad/glad.h>'
Priority: 0
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
PenaltyBreakAssignment: 100
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 1000000
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeParens: ControlStatements
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseCRLF: false
UseTab: Never
...
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
examples/raygizmo
compile_flags.txt
include
include/*
!include/raygizmo.h
lib
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
In a simplest case gizmo can be used like this:
```c
#define RAYGIZMO_IMPLEMENTATION
#include "../src/raygizmo.h"
#include "raygizmo.h"

int main(void) {
InitWindow(800, 600, "Gizmo");
InitWindow(800, 450, "raygizmo");

Camera3D camera;
camera.fovy = 45.0f;
Expand All @@ -26,29 +26,31 @@ int main(void) {
camera.projection = CAMERA_PERSPECTIVE;

Model model = LoadModelFromMesh(GenMeshTorus(0.3, 1.5, 16.0, 16.0));

loadGizmo();
RGizmo gizmo = rgizmo_create();

while (!WindowShouldClose()) {
BeginDrawing();
{
Vector3 position = {
model.transform.m12, model.transform.m13, model.transform.m14};
rgizmo_update(&gizmo, camera, position);
model.transform = MatrixMultiply(model.transform, rgizmo_get_tranform(gizmo, position));
ClearBackground(BLACK);
rlEnableDepthTest();

// Draw the model
BeginMode3D(camera);
{
DrawModel(model, (Vector3){0.0, 0.0, 0.0}, 1.0, PURPLE);
}
EndMode3D();

// Immediately update and draw gizmo
Vector3 position = {
model.transform.m12, model.transform.m13, model.transform.m14};
Matrix transform = updateAndDrawGizmo(camera, position);

// Apply gizmo-produced transformation to the model
model.transform = MatrixMultiply(model.transform, transform);
rgizmo_draw(gizmo, camera, position);
}
EndDrawing();
}

unloadGizmo();
rgizmo_unload();
UnloadModel(model);
CloseWindow();

Expand All @@ -57,9 +59,7 @@ int main(void) {
```
Interactive example could be built and run like this (make sure you have libraylib and raylib headers in your lib and include paths):
More complex example could be built and run like this (make sure you have libraylib and raylib headers in your lib and include paths):
```bash
cd examples \
&& gcc -o ./raygizmo ./raygizmo.c -lraylib -lm -lpthread -ldl \
&& ./raygizmo
gcc -o ./examples/raygizmo ./examples/raygizmo.c -lraylib -lm -lpthread -ldl && ./examples/raygizmo
```
3 changes: 3 additions & 0 deletions clang_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
clang-format -style=file -i ./examples/raygizmo.c
clang-format -style=file -i ./include/raygizmo.h
110 changes: 39 additions & 71 deletions examples/raygizmo.c
Original file line number Diff line number Diff line change
@@ -1,99 +1,77 @@
/*******************************************************************************************
*
* raylib [gizmo] example - Gizmo gadget for an interactive object 3D transformations
*
* Example originally created with raylib 5.0, last time updated with raylib 5.0
*
* Example contributed by Alexey Karnachev (@alexeykarnachev) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2023-2024 Alexey Karnachev (@alexeykarnachev)
*
********************************************************************************************/

#include "raylib.h"
#include "rcamera.h"

#define RAYGIZMO_IMPLEMENTATION
#include "../src/raygizmo.h"
#include "raygizmo.h"

#define CAMERA_ROT_SPEED 0.003f
#define CAMERA_MOVE_SPEED 0.01f
#define CAMERA_ZOOM_SPEED 1.0f

// Updates the camera in the orbital-style, i.e camera rotates around the look-at point by the orbit
static void updateCamera(Camera3D* camera)
{
bool isMMBDown = IsMouseButtonDown(2);
bool isShiftDown = IsKeyDown(KEY_LEFT_SHIFT);
Vector2 mouseDelta = GetMouseDelta();
static void update_camera(Camera3D *camera) {
bool is_mmb_down = IsMouseButtonDown(2);
bool is_shift_down = IsKeyDown(KEY_LEFT_SHIFT);
Vector2 mouse_delta = GetMouseDelta();

// Shift + MMB + mouse move -> change the camera position in the right-direction plane
if (isMMBDown && isShiftDown) {
CameraMoveRight(camera, -CAMERA_MOVE_SPEED * mouseDelta.x, true);
if (is_mmb_down && is_shift_down) {
CameraMoveRight(camera, -CAMERA_MOVE_SPEED * mouse_delta.x, true);

Vector3 right = GetCameraRight(camera);
Vector3 up = Vector3CrossProduct(
Vector3Subtract(camera->position, camera->target), right
);
up = Vector3Scale(Vector3Normalize(up), CAMERA_MOVE_SPEED * mouseDelta.y);
up = Vector3Scale(
Vector3Normalize(up), CAMERA_MOVE_SPEED * mouse_delta.y
);
camera->position = Vector3Add(camera->position, up);
camera->target = Vector3Add(camera->target, up);
// Rotate the camera around the look-at point
} else if (isMMBDown) {
CameraYaw(camera, -CAMERA_ROT_SPEED * mouseDelta.x, true);
CameraPitch(camera, CAMERA_ROT_SPEED * mouseDelta.y, true, true, false);
} else if (is_mmb_down) {
CameraYaw(camera, -CAMERA_ROT_SPEED * mouse_delta.x, true);
CameraPitch(
camera, CAMERA_ROT_SPEED * mouse_delta.y, true, true, false
);
}

// Bring camera closer (or move away), to the look-at point
CameraMoveToTarget(camera, -GetMouseWheelMove() * CAMERA_ZOOM_SPEED);
}

int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [gizmo] example - gizmo gadget");
int main(void) {
InitWindow(800, 450, "raygizmo");
SetTargetFPS(60);

// Define 3D perspective camera
Camera3D camera;
camera.fovy = 45.0f;
camera.target = (Vector3){0.0f, 0.0f, 0.0f};
camera.position = (Vector3){5.0f, 5.0f, 5.0f};
camera.up = (Vector3){0.0f, 1.0f, 0.0f};
camera.projection = CAMERA_PERSPECTIVE;

Model model = LoadModelFromMesh(GenMeshTorus(0.3, 1.5, 16.0, 16.0)); // Create simple torus model
loadGizmo(); // Load gizmo

SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
Model model = LoadModelFromMesh(GenMeshTorus(0.3, 1.5, 16.0, 16.0));
RGizmo gizmo = rgizmo_create();

while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
updateCamera(&camera);
while (!WindowShouldClose()) {
update_camera(&camera);

// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
{
Vector3 position = {
model.transform.m12, model.transform.m13, model.transform.m14};

rgizmo_update(&gizmo, camera, position);
model.transform = MatrixMultiply(
model.transform, rgizmo_get_tranform(gizmo, position)
);

ClearBackground(DARKGRAY);
rlEnableDepthTest();

BeginMode3D(camera);
// Draw main model
{
DrawModel(model, (Vector3){0.0, 0.0, 0.0}, 1.0, PURPLE);

// Draw coordinates grid
rlSetLineWidth(1.0);
DrawGrid(100.0, 1.0);

// Draw coordinate x, y and z axis
rlSetLineWidth(2.0);
DrawLine3D(
(Vector3){-50.0f, 0.0f, 0.0f},
Expand All @@ -110,33 +88,23 @@ int main(void)
(Vector3){0.0f, 0.0f, 50.0f},
DARKBLUE
);
EndMode3D();

// Immediately update and draw gizmo
Vector3 position = {
model.transform.m12, model.transform.m13, model.transform.m14};
Matrix transform = updateAndDrawGizmo(camera, position);

// Apply gizmo-produced transformation to the model
model.transform = MatrixMultiply(model.transform, transform);
rgizmo_draw(gizmo, camera, position);
}
EndMode3D();

// Draw camera controll keys
DrawRectangle(0, 0, 280, 90, RAYWHITE);
DrawText("CAMERA:", 5, 5, 20, RED);
DrawText(" zoom: wheel", 5, 25, 20, RED);
DrawText(" rotate: mmb", 5, 45, 20, RED);
DrawText(" translate: shift + mmb", 5, 65, 20, RED);
}
EndDrawing();
}

// De-Initialization
//--------------------------------------------------------------------------------------
unloadGizmo(); // Unload gizmo
UnloadModel(model); // Unload model

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
rgizmo_unload();
UnloadModel(model);
CloseWindow();

return 0;
}

Loading

0 comments on commit 4c4a566

Please sign in to comment.