cs381/as2/as2.cpp

127 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "Color.hpp"
#include "Keyboard.hpp"
#include "Matrix.hpp"
#include "Model.hpp"
#include "raylib.h"
#include <raylib-cpp.hpp>
#include <iostream>
static bool scaleModel = false;
void DrawBoundedModel(raylib::Model &model, auto transformer) {
// store the original transform to apply a different transform to the
// model without affecting the next time we draw
raylib::Matrix oldTransform = model.GetTransform();
// apply the transform that we get from whatever the transformer callback
// gives us
raylib::Matrix transform = transformer(model.GetTransform());
// default scale doesn't render correctly, so scale the model by 10 if the
// spacebar is pressed
if (scaleModel) {
transform = transform.Scale(10);
}
// apply the transform that we got from the transformer to the model
model.SetTransform(transform);
// draw the model, passing the origin and default scale as arguments since
// the transform is already applied to the model
model.Draw({ 0, 0, 0 }, 1.0f, raylib::Color::White());
// get the bounding box of the model after applying the transform
auto box = model.GetTransformedBoundingBox();
// draw the bounding box of the model using raylib's built in function
DrawBoundingBox(box, raylib::Color::White());
// restore the model's transform to its original state so that the next time we
// draw the model, it doesn't have the previous transform applied to it
model.SetTransform(oldTransform);
}
int main() {
raylib::Window window(800, 600, "CS381 - Assignment 2");
window.SetState(FLAG_WINDOW_RESIZABLE);
raylib::Model penguin("models/penguin.glb");
raylib::Model eagle("models/eagle.glb");
raylib::Camera3D camera(
{ 0, 120, 500 },
{ 0, 0, -1 },
{ 0, 1, 0 },
45.0f);
window.SetTargetFPS(60); // save cpu cycles
while (!window.ShouldClose()) {
window.BeginDrawing();
window.ClearBackground(raylib::Color::Gray());
camera.BeginMode();
if (raylib::Keyboard::IsKeyPressed(KEY_SPACE)) {
scaleModel = !scaleModel;
}
// in addition to the required transforms, the models will rotate 10
// degrees per second (extra credit), and the penguin is scaled by 30
// eagle scaled by 10
// Draw one eagle located at (0, 0, 0), default scale, and with default
// orientation (10 points).
DrawBoundedModel(eagle, [](raylib::Matrix transform) {
return transform
.RotateY(GetTime() * raylib::Degree(10.0f))
.Scale(10.0);
});
// Draw one eagle located at (100, 100, 0), scaled by (1, 1, 1), and
// yawed by 180 degrees (10 points).
DrawBoundedModel(eagle, [](raylib::Matrix transform) {
return transform
.Translate(-100.0f, 100.0f, 0.0f)
.Scale(1.0f, -1.0f, 1.0f)
.RotateY(raylib::Degree(180.0f))
.Scale(10.0)
.RotateY(GetTime() * raylib::Degree(10.0f));
});
// Draw one penguin located at (200, 0, 0) with default orientation (10
// points).
DrawBoundedModel(penguin, [](raylib::Matrix transform) {
return transform
.Translate(-200.0f, 0.0f, 0.0f)
.Scale(30.0f);
});
// Draw one penguin located at (200, 0, 0) and be yawed 90 degrees (10
// points).
DrawBoundedModel(penguin, [](raylib::Matrix transform) {
return transform
.Translate(200.0f, 0.0f, 0.0f)
.RotateY(raylib::Degree(90.0f))
.Scale(30.0)
.RotateY(GetTime() * raylib::Degree(10.0f));
});
// Penguin 3: at (100,100,0), scaled by (1,2,1) and yawed 270 degrees
DrawBoundedModel(penguin, [](raylib::Matrix transform) {
return transform
.Translate(100.0f, 100.0f, 0.0f)
.Scale(1.0f, 2.0f, 1.0f)
.RotateY(raylib::Degree(270.0f))
.Scale(30.0)
.RotateY(GetTime() * raylib::Degree(10.0f));
});
camera.EndMode();
window.EndDrawing();
}
return 0;
}