diff options
author | Matt Strapp <matt@mattstrapp.net> | 2021-09-24 10:05:55 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2021-09-24 10:05:55 -0500 |
commit | 458496b2db6d67736fd1ffc18c6e51a7a4e1180c (patch) | |
tree | 3d6ee4e37a48d71a6ac781cbd93a6d6dcb234f98 | |
parent | Pushed feedback file submission-w1.1 (diff) | |
parent | publish a2 (diff) | |
download | csci4611-458496b2db6d67736fd1ffc18c6e51a7a4e1180c.tar csci4611-458496b2db6d67736fd1ffc18c6e51a7a4e1180c.tar.gz csci4611-458496b2db6d67736fd1ffc18c6e51a7a4e1180c.tar.bz2 csci4611-458496b2db6d67736fd1ffc18c6e51a7a4e1180c.tar.lz csci4611-458496b2db6d67736fd1ffc18c6e51a7a4e1180c.tar.xz csci4611-458496b2db6d67736fd1ffc18c6e51a7a4e1180c.tar.zst csci4611-458496b2db6d67736fd1ffc18c6e51a7a4e1180c.zip |
Merge branch 'support-code' of github.umn.edu:umn-csci-4611-f21/shared-upstream
38 files changed, 2967 insertions, 1316 deletions
diff --git a/dev/MinGfx/src/gfxmath.cc b/dev/MinGfx/src/gfxmath.cc index 20c9cc4..28cfedf 100644 --- a/dev/MinGfx/src/gfxmath.cc +++ b/dev/MinGfx/src/gfxmath.cc @@ -1,82 +1,137 @@ -/*
- Copyright (c) 2017,2018 Regents of the University of Minnesota.
- All Rights Reserved.
- See corresponding header file for details.
- */
-
-#include "gfxmath.h"
-
-#define _USE_MATH_DEFINES
-#include <math.h>
-#include <algorithm>
-
-#include "ray.h"
-
-
-namespace mingfx {
-
-const float GfxMath::PI = 3.14159265359f;
-const float GfxMath::TWO_PI = 6.28318530718f;
-const float GfxMath::HALF_PI = 1.57079632679f;
-
-
-float GfxMath::Clamp(float x, float a, float b) {
- return std::min(std::max(x, a), b);
-}
-
-float GfxMath::ToRadians(float degrees) {
- return degrees * GfxMath::PI / 180.0f;
-}
-
-float GfxMath::ToDegrees(float radians) {
- return radians * 180.0f / GfxMath::PI;
-}
-
-Vector3 GfxMath::ToRadians(Vector3 degrees) {
- return Vector3(ToRadians(degrees[0]), ToRadians(degrees[1]), ToRadians(degrees[2]));
-}
-
-Vector3 GfxMath::ToDegrees(Vector3 radians) {
- return Vector3(ToDegrees(radians[0]), ToDegrees(radians[1]), ToDegrees(radians[2]));
-}
-
-float GfxMath::Lerp(float a, float b, float alpha) {
- return (1.0f-alpha)*a + alpha*b;
-}
-
-int GfxMath::iLerp(int a, int b, float alpha) {
- return (int)std::round((1.0f-alpha)*(float)a + alpha*(float)b);
-}
-
-Point3 GfxMath::ScreenToNearPlane(const Matrix4 &V, const Matrix4 &P, const Point2 &ndcPoint) {
- Matrix4 filmPtToWorld = (P*V).Inverse();
- return filmPtToWorld * Point3(ndcPoint[0], ndcPoint[1], -1.0);
-}
-
-
-Point3 GfxMath::ScreenToWorld(const Matrix4 &V, const Matrix4 &P, const Point2 &ndcPoint, float zValue) {
- Matrix4 filmPtToWorld = (P*V).Inverse();
- float zneg1topos1 = zValue*2.0f - 1.0f;
- return filmPtToWorld * Point3(ndcPoint[0], ndcPoint[1], zneg1topos1);
-}
-
-
-Point3 GfxMath::ScreenToDepthPlane(const Matrix4 &V, const Matrix4 &P, const Point2 &ndcPoint, float planeDepth) {
- Point3 pNear = ScreenToNearPlane(V, P, ndcPoint);
-
- Matrix4 camMat = V.Inverse();
- Point3 eye = camMat.ColumnToPoint3(3);
- Vector3 look = -camMat.ColumnToVector3(2);
-
- Ray r(eye, pNear - eye);
-
- Point3 p3D;
- float t;
- if (!r.IntersectPlane(eye + planeDepth*look, -look, &t, &p3D)) {
- std::cerr << "filmplane2D_to_plane3D() error -- no intersection found!" << std::endl;
- }
- return p3D;
-}
-
-
-} // end namespace
+/* + Copyright (c) 2017,2018 Regents of the University of Minnesota. + All Rights Reserved. + See corresponding header file for details. + */ + +#include "gfxmath.h" + +#define _USE_MATH_DEFINES +#include <math.h> +#include <algorithm> + +#include "ray.h" + + +namespace mingfx { + +const float GfxMath::PI = 3.14159265359f; +const float GfxMath::TWO_PI = 6.28318530718f; +const float GfxMath::HALF_PI = 1.57079632679f; + +float GfxMath::sin(float a) { +#ifdef WIN32 + return std::sinf(a); +#else + return std::sin(a); +#endif +} + +float GfxMath::cos(float a) { +#ifdef WIN32 + return std::cosf(a); +#else + return std::cos(a); +#endif +} + +float GfxMath::tan(float a) { +#ifdef WIN32 + return std::tanf(a); +#else + return std::tan(a); +#endif +} + +float GfxMath::asin(float a) { +#ifdef WIN32 + return std::asinf(a); +#else + return std::asin(a); +#endif +} + +float GfxMath::acos(float a) { +#ifdef WIN32 + return std::acosf(a); +#else + return std::acos(a); +#endif +} + +float GfxMath::atan(float a) { +#ifdef WIN32 + return std::atanf(a); +#else + return std::atan(a); +#endif +} + +float GfxMath::atan2(float a, float b) { +#ifdef WIN32 + return std::atan2f(a, b); +#else + return std::atan2(a, b); +#endif +} + +float GfxMath::Clamp(float x, float a, float b) { + return std::min(std::max(x, a), b); +} + +float GfxMath::ToRadians(float degrees) { + return degrees * GfxMath::PI / 180.0f; +} + +float GfxMath::ToDegrees(float radians) { + return radians * 180.0f / GfxMath::PI; +} + +Vector3 GfxMath::ToRadians(Vector3 degrees) { + return Vector3(ToRadians(degrees[0]), ToRadians(degrees[1]), ToRadians(degrees[2])); +} + +Vector3 GfxMath::ToDegrees(Vector3 radians) { + return Vector3(ToDegrees(radians[0]), ToDegrees(radians[1]), ToDegrees(radians[2])); +} + +float GfxMath::Lerp(float a, float b, float alpha) { + return (1.0f-alpha)*a + alpha*b; +} + +int GfxMath::iLerp(int a, int b, float alpha) { + return (int)std::round((1.0f-alpha)*(float)a + alpha*(float)b); +} + +Point3 GfxMath::ScreenToNearPlane(const Matrix4 &V, const Matrix4 &P, const Point2 &ndcPoint) { + Matrix4 filmPtToWorld = (P*V).Inverse(); + return filmPtToWorld * Point3(ndcPoint[0], ndcPoint[1], -1.0); +} + + +Point3 GfxMath::ScreenToWorld(const Matrix4 &V, const Matrix4 &P, const Point2 &ndcPoint, float zValue) { + Matrix4 filmPtToWorld = (P*V).Inverse(); + float zneg1topos1 = zValue*2.0f - 1.0f; + return filmPtToWorld * Point3(ndcPoint[0], ndcPoint[1], zneg1topos1); +} + + +Point3 GfxMath::ScreenToDepthPlane(const Matrix4 &V, const Matrix4 &P, const Point2 &ndcPoint, float planeDepth) { + Point3 pNear = ScreenToNearPlane(V, P, ndcPoint); + + Matrix4 camMat = V.Inverse(); + Point3 eye = camMat.ColumnToPoint3(3); + Vector3 look = -camMat.ColumnToVector3(2); + + Ray r(eye, pNear - eye); + + Point3 p3D; + float t; + if (!r.IntersectPlane(eye + planeDepth*look, -look, &t, &p3D)) { + std::cerr << "filmplane2D_to_plane3D() error -- no intersection found!" << std::endl; + } + return p3D; +} + + +} // end namespace diff --git a/dev/MinGfx/src/gfxmath.h b/dev/MinGfx/src/gfxmath.h index 8bb6c25..86c1061 100644 --- a/dev/MinGfx/src/gfxmath.h +++ b/dev/MinGfx/src/gfxmath.h @@ -1,81 +1,97 @@ -/*
- This file is part of the MinGfx Project.
-
- Copyright (c) 2017,2018 Regents of the University of Minnesota.
- All Rights Reserved.
-
- Original Author(s) of this File:
- Dan Keefe, 2018, University of Minnesota
-
- Author(s) of Significant Updates/Modifications to the File:
- ...
- */
-
-#ifndef SRC_GFXMATH_H_
-#define SRC_GFXMATH_H_
-
-#include "point2.h"
-#include "point3.h"
-#include "vector3.h"
-#include "matrix4.h"
-
-namespace mingfx {
-
-
-/** This class holds a variety of static math functions that are useful to have
- defined with creating graphics programs.
- */
-class GfxMath {
-public:
-
- /// Returns a if x is less than a and b if x is greater than b.
- static float Clamp(float x, float a, float b);
-
- static float ToRadians(float degrees);
-
- static float ToDegrees(float radians);
-
- static Vector3 ToRadians(Vector3 degrees);
-
- static Vector3 ToDegrees(Vector3 radians);
-
- static float Lerp(float a, float b, float alpha);
-
- static int iLerp(int a, int b, float alpha);
-
- /// Converts a 2D point on the filmplane represented in Normalized Device
- /// Coorindates, which means (-1,1) for the top left corner of the screen and
- /// (1,-1) for the bottom right corner, to a 3D point that lies on the camera's
- /// near plane. Useful for converting mouse coordinates into a 3D point.
- /// Remember that this uses NORMALIZED device coordinates for the screenPt,
- /// not pixels. GraphicsApp and most other graphics engines report mouse move
- /// events in pixels, so you need to convert these to normalized device coordinates
- /// first. If you are using GraphicsApp, you can do this with:
- /// Point2 normPos = graphicsApp->pixels_to_normalized_coordinates(mousePos);
- static Point3 ScreenToNearPlane(const Matrix4 &viewMatrix, const Matrix4 &projMatrix, const Point2 &normalizedScreenPt);
-
- /// Similar to filmplane2D_to_nearplane3D() but here rather than using the
- /// nearplane, you specify the depth of the plane to use as a distance away
- /// from the camera's focal point.
- static Point3 ScreenToDepthPlane(const Matrix4 &viewMatrix, const Matrix4 &projMatrix, const Point2 &normalizedScreenPt, float planeDepth);
-
- /// Converts a 2D point on the filmplane represented in Normalized Device
- /// Coorindates, which means (-1,1) for the top left corner of the screen and
- /// (1,-1) for the bottom right corner, to a 3D point in the world. The depth
- /// buffer value under the pixel must be supplied. If you are using GraphicsApp,
- /// you can use the mouse pos in pixels to get the required arguments like this:
- /// Point2 normPos = graphicsApp->pixels_to_normalized_coordinates(mousePos);
- /// float normZ = graphicsApp->z_value_at_pixel(mousePos);
- static Point3 ScreenToWorld(const Matrix4 &viewMatrix, const Matrix4 &projMatrix, const Point2 &normalizedScreenPt, float normalizedZ);
-
-
- static const float PI;
- static const float TWO_PI;
- static const float HALF_PI;
-};
-
-
-
-} // end namespace
-
+/* + This file is part of the MinGfx Project. + + Copyright (c) 2017,2018 Regents of the University of Minnesota. + All Rights Reserved. + + Original Author(s) of this File: + Dan Keefe, 2018, University of Minnesota + + Author(s) of Significant Updates/Modifications to the File: + ... + */ + +#ifndef SRC_GFXMATH_H_ +#define SRC_GFXMATH_H_ + +#include "point2.h" +#include "point3.h" +#include "vector3.h" +#include "matrix4.h" + +namespace mingfx { + + +/** This class holds a variety of static math functions that are useful to have + defined with creating graphics programs. + */ +class GfxMath { +public: + + /// MinGfx specific implementations of trigonometric functions included to + /// solve compilation issues between different platforms. + static float sin(float a); + + static float cos(float a); + + static float tan(float a); + + static float asin(float a); + + static float acos(float a); + + static float atan(float a); + + static float atan2(float a, float b); + + /// Returns a if x is less than a and b if x is greater than b. + static float Clamp(float x, float a, float b); + + static float ToRadians(float degrees); + + static float ToDegrees(float radians); + + static Vector3 ToRadians(Vector3 degrees); + + static Vector3 ToDegrees(Vector3 radians); + + static float Lerp(float a, float b, float alpha); + + static int iLerp(int a, int b, float alpha); + + /// Converts a 2D point on the filmplane represented in Normalized Device + /// Coorindates, which means (-1,1) for the top left corner of the screen and + /// (1,-1) for the bottom right corner, to a 3D point that lies on the camera's + /// near plane. Useful for converting mouse coordinates into a 3D point. + /// Remember that this uses NORMALIZED device coordinates for the screenPt, + /// not pixels. GraphicsApp and most other graphics engines report mouse move + /// events in pixels, so you need to convert these to normalized device coordinates + /// first. If you are using GraphicsApp, you can do this with: + /// Point2 normPos = graphicsApp->pixels_to_normalized_coordinates(mousePos); + static Point3 ScreenToNearPlane(const Matrix4 &viewMatrix, const Matrix4 &projMatrix, const Point2 &normalizedScreenPt); + + /// Similar to filmplane2D_to_nearplane3D() but here rather than using the + /// nearplane, you specify the depth of the plane to use as a distance away + /// from the camera's focal point. + static Point3 ScreenToDepthPlane(const Matrix4 &viewMatrix, const Matrix4 &projMatrix, const Point2 &normalizedScreenPt, float planeDepth); + + /// Converts a 2D point on the filmplane represented in Normalized Device + /// Coorindates, which means (-1,1) for the top left corner of the screen and + /// (1,-1) for the bottom right corner, to a 3D point in the world. The depth + /// buffer value under the pixel must be supplied. If you are using GraphicsApp, + /// you can use the mouse pos in pixels to get the required arguments like this: + /// Point2 normPos = graphicsApp->pixels_to_normalized_coordinates(mousePos); + /// float normZ = graphicsApp->z_value_at_pixel(mousePos); + static Point3 ScreenToWorld(const Matrix4 &viewMatrix, const Matrix4 &projMatrix, const Point2 &normalizedScreenPt, float normalizedZ); + + + static const float PI; + static const float TWO_PI; + static const float HALF_PI; +}; + + + +} // end namespace + #endif
\ No newline at end of file diff --git a/dev/MinGfx/src/quaternion.cc b/dev/MinGfx/src/quaternion.cc index 7551c62..24830c8 100644 --- a/dev/MinGfx/src/quaternion.cc +++ b/dev/MinGfx/src/quaternion.cc @@ -1,260 +1,261 @@ -/*
-Copyright (c) 2017,2018 Regents of the University of Minnesota.
-All Rights Reserved.
-See corresponding header file for details.
-*/
-
-#define _USE_MATH_DEFINES
-#include "quaternion.h"
-
-#include "gfxmath.h"
-
-namespace mingfx {
-
-
-Quaternion::Quaternion() {
- q[0] = 0.0;
- q[1] = 0.0;
- q[2] = 0.0;
- q[3] = 1.0;
-}
-
-Quaternion::Quaternion(float qx, float qy, float qz, float qw) {
- q[0] = qx;
- q[1] = qy;
- q[2] = qz;
- q[3] = qw;
-}
-
-Quaternion::Quaternion(float *ptr) {
- q[0] = ptr[0];
- q[1] = ptr[1];
- q[2] = ptr[2];
- q[3] = ptr[3];
-}
-
-Quaternion::Quaternion(const Quaternion& other) {
- q[0] = other[0];
- q[1] = other[1];
- q[2] = other[2];
- q[3] = other[3];
-}
-
-Quaternion::~Quaternion() {
-}
-
-bool Quaternion::operator==(const Quaternion& other) const {
- return (fabs(other[0] - q[0]) < MINGFX_MATH_EPSILON &&
- fabs(other[1] - q[1]) < MINGFX_MATH_EPSILON &&
- fabs(other[2] - q[2]) < MINGFX_MATH_EPSILON &&
- fabs(other[3] - q[3]) < MINGFX_MATH_EPSILON);
-}
-
-bool Quaternion::operator!=(const Quaternion& other) const {
- return (fabs(other[0] - q[0]) >= MINGFX_MATH_EPSILON ||
- fabs(other[1] - q[1]) >= MINGFX_MATH_EPSILON ||
- fabs(other[2] - q[2]) >= MINGFX_MATH_EPSILON ||
- fabs(other[3] - q[3]) >= MINGFX_MATH_EPSILON);
-}
-
-Quaternion& Quaternion::operator=(const Quaternion& other) {
- q[0] = other[0];
- q[1] = other[1];
- q[2] = other[2];
- q[3] = other[3];
- return *this;
-}
-
-float Quaternion::operator[](const int i) const {
- if ((i>=0) && (i<=3)) {
- return q[i];
- }
- else {
- // this is an error!
- return 0.0;
- }
-}
-
-float& Quaternion::operator[](const int i) {
- return q[i];
-}
-
-
-const float * Quaternion::value_ptr() const {
- return q;
-}
-
-Quaternion Quaternion::Slerp(const Quaternion &other, float alpha) const {
- // https://en.wikipedia.org/wiki/Slerp
-
- Quaternion v0 = *this;
- Quaternion v1 = other;
-
- // Only unit quaternions are valid rotations.
- // Normalize to avoid undefined behavior.
- v0.Normalize();
- v1.Normalize();
-
- // Compute the cosine of the angle between the two vectors.
- float dot = v0.Dot(v1);
-
- // If the dot product is negative, the quaternions
- // have opposite handed-ness and slerp won't take
- // the shorter path. Fix by reversing one quaternion.
- if (dot < 0.0f) {
- v1 = -v1;
- dot = -dot;
- }
-
- const double DOT_THRESHOLD = 0.9995;
- if (dot > DOT_THRESHOLD) {
- // If the inputs are too close for comfort, linearly interpolate
- // and normalize the result.
-
- Quaternion result = v0 + alpha*(v1 - v0);
- result.Normalize();
- return result;
- }
-
- GfxMath::Clamp(dot, -1, 1); // Robustness: Stay within domain of acos()
- float theta_0 = acos(dot); // theta_0 = angle between input vectors
- float theta = theta_0 * alpha; // theta = angle between v0 and result
-
- float s0 = cos(theta) - dot * sin(theta) / sin(theta_0); // == sin(theta_0 - theta) / sin(theta_0)
- float s1 = sin(theta) / sin(theta_0);
-
- return (s0 * v0) + (s1 * v1);
-}
-
-Quaternion Quaternion::Slerp(const Quaternion &a, const Quaternion &b, float alpha) {
- return a.Slerp(b, alpha);
-}
-
-
-std::ostream & operator<< ( std::ostream &os, const Quaternion &q) {
- return os << "<" << q[0] << ", " << q[1] << ", " << q[2] << ", " << q[3] << ")";
-}
-
-std::istream & operator>> ( std::istream &is, Quaternion &q) {
- // format: <qx, qy, qz, qw>
- char dummy;
- return is >> dummy >> q[0] >> dummy >> q[1] >> dummy >> q[2] >> dummy >> q[3] >> dummy;
-}
-
-
-float Quaternion::Dot(const Quaternion& other) const {
- return q[0]*other[0] + q[1]*other[1] + q[2]*other[2] + q[3]*other[3];
-
-}
-
-float Quaternion::Length() const {
- return sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
-}
-
-void Quaternion::Normalize() {
- float sizeSq = + q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
- if (sizeSq < MINGFX_MATH_EPSILON) {
- return; // do nothing to zero quats
- }
- float scaleFactor = (float)1.0/(float)sqrt(sizeSq);
- q[0] *= scaleFactor;
- q[1] *= scaleFactor;
- q[2] *= scaleFactor;
- q[3] *= scaleFactor;
-}
-
-Quaternion Quaternion::ToUnit() const {
- Quaternion qtmp(*this);
- qtmp.Normalize();
- return qtmp;
-}
-
-/// Returns the conjugate of the quaternion.
-Quaternion Quaternion::Conjugate() const {
- return Quaternion(-q[0], -q[1], -q[2], q[3]);
-}
-
-
-Quaternion Quaternion::FromAxisAngle(const Vector3 &axis, float angle) {
- // [qx, qy, qz, qw] = [sin(a/2) * vx, sin(a/2)* vy, sin(a/2) * vz, cos(a/2)]
- float x = sin(angle/2.0f) * axis[0];
- float y = sin(angle/2.0f) * axis[1];
- float z = sin(angle/2.0f) * axis[2];
- float w = cos(angle/2.0f);
- return Quaternion(x,y,z,w);
-}
-
-
-Quaternion Quaternion::FromEulerAnglesZYX(const Vector3 &angles) {
- Quaternion rot_x = Quaternion::FromAxisAngle(Vector3::UnitX(), angles[0]);
- Quaternion rot_y = Quaternion::FromAxisAngle(Vector3::UnitY(), angles[1]);
- Quaternion rot_z = Quaternion::FromAxisAngle(Vector3::UnitZ(), angles[2]);
- return rot_z * rot_y * rot_x;
-}
-
-Vector3 Quaternion::ToEulerAnglesZYX() const {
- // https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
-
- Vector3 angles;
-
- // roll (x-axis rotation)
- float sinr = +2.0f * (w() * x() + y() * z());
- float cosr = +1.0f - 2.0f * (x() * x() + y() * y());
- angles[0] = std::atan2(sinr, cosr);
-
- // pitch (y-axis rotation)
- float sinp = +2.0f * (w() * y() - z() * x());
- if (std::fabs(sinp) >= 1.f)
- angles[1] = std::copysign(GfxMath::HALF_PI, sinp); // use 90 degrees if out of range
- else
- angles[1] = std::asin(sinp);
-
- // yaw (z-axis rotation)
- float siny = +2.0f * (w() * z() + x() * y());
- float cosy = +1.0f - 2.0f * (y() * y() + z() * z());
- angles[2] = std::atan2(siny, cosy);
-
- return angles;
-}
-
-
-Quaternion operator*(const Quaternion& q1, const Quaternion& q2) {
- float real1 = q1[3];
- Vector3 imag1 = Vector3(q1[0], q1[1], q1[2]);
-
- float real2 = q2[3];
- Vector3 imag2 = Vector3(q2[0], q2[1], q2[2]);
-
- float real = real1*real2 - imag1.Dot(imag2);
- Vector3 imag = real1*imag2 + real2*imag1 + imag1.Cross(imag2);
-
- return Quaternion(imag[0], imag[1], imag[2], real);
-}
-
-
-Quaternion operator/(const Quaternion& q, const float s) {
- const float invS = 1.0f / s;
- return Quaternion(q[0]*invS, q[1]*invS, q[2]*invS, q[3]*invS);
-}
-
-Quaternion operator*(const float s, const Quaternion& q) {
- return Quaternion(q[0]*s, q[1]*s, q[2]*s, q[3]*s);
-}
-
-Quaternion operator*(const Quaternion& q, const float s) {
- return Quaternion(q[0]*s, q[1]*s, q[2]*s, q[3]*s);
-}
-
-Quaternion operator-(const Quaternion& q) {
- return Quaternion(-q[0], -q[1], -q[2], -q[3]);
-}
-
-Quaternion operator+(const Quaternion& q1, const Quaternion& q2) {
- return Quaternion(q1[0] + q2[0], q1[1] + q2[1], q1[2] + q2[2], q1[3] + q2[3]);
-}
-
-Quaternion operator-(const Quaternion& q1, const Quaternion& q2) {
- return Quaternion(q1[0] - q2[0], q1[1] - q2[1], q1[2] - q2[2], q1[3] - q2[3]);
-}
-
-} // end namespace
+/* +Copyright (c) 2017,2018 Regents of the University of Minnesota. +All Rights Reserved. +See corresponding header file for details. +*/ + +#define _USE_MATH_DEFINES +#include "quaternion.h" + +#include "gfxmath.h" + +namespace mingfx { + + +Quaternion::Quaternion() { + q[0] = 0.0; + q[1] = 0.0; + q[2] = 0.0; + q[3] = 1.0; +} + +Quaternion::Quaternion(float qx, float qy, float qz, float qw) { + q[0] = qx; + q[1] = qy; + q[2] = qz; + q[3] = qw; +} + +Quaternion::Quaternion(float *ptr) { + q[0] = ptr[0]; + q[1] = ptr[1]; + q[2] = ptr[2]; + q[3] = ptr[3]; +} + +Quaternion::Quaternion(const Quaternion& other) { + q[0] = other[0]; + q[1] = other[1]; + q[2] = other[2]; + q[3] = other[3]; +} + +Quaternion::~Quaternion() { +} + +bool Quaternion::operator==(const Quaternion& other) const { + return (fabs(other[0] - q[0]) < MINGFX_MATH_EPSILON && + fabs(other[1] - q[1]) < MINGFX_MATH_EPSILON && + fabs(other[2] - q[2]) < MINGFX_MATH_EPSILON && + fabs(other[3] - q[3]) < MINGFX_MATH_EPSILON); +} + +bool Quaternion::operator!=(const Quaternion& other) const { + return (fabs(other[0] - q[0]) >= MINGFX_MATH_EPSILON || + fabs(other[1] - q[1]) >= MINGFX_MATH_EPSILON || + fabs(other[2] - q[2]) >= MINGFX_MATH_EPSILON || + fabs(other[3] - q[3]) >= MINGFX_MATH_EPSILON); +} + +Quaternion& Quaternion::operator=(const Quaternion& other) { + q[0] = other[0]; + q[1] = other[1]; + q[2] = other[2]; + q[3] = other[3]; + return *this; +} + +float Quaternion::operator[](const int i) const { + if ((i>=0) && (i<=3)) { + return q[i]; + } + else { + // this is an error! + return 0.0; + } +} + +float& Quaternion::operator[](const int i) { + return q[i]; +} + + +const float * Quaternion::value_ptr() const { + return q; +} + +Quaternion Quaternion::Slerp(const Quaternion &other, float alpha) const { + // https://en.wikipedia.org/wiki/Slerp + + Quaternion v0 = *this; + Quaternion v1 = other; + + // Only unit quaternions are valid rotations. + // Normalize to avoid undefined behavior. + v0.Normalize(); + v1.Normalize(); + + // Compute the cosine of the angle between the two vectors. + float dot = v0.Dot(v1); + + // If the dot product is negative, the quaternions + // have opposite handed-ness and slerp won't take + // the shorter path. Fix by reversing one quaternion. + if (dot < 0.0f) { + v1 = -v1; + dot = -dot; + } + + const double DOT_THRESHOLD = 0.9995; + if (dot > DOT_THRESHOLD) { + // If the inputs are too close for comfort, linearly interpolate + // and normalize the result. + + Quaternion result = v0 + alpha*(v1 - v0); + result.Normalize(); + return result; + } + + GfxMath::Clamp(dot, -1, 1); // Robustness: Stay within domain of acos() + float theta_0 = GfxMath::acos(dot); // theta_0 = angle between input vectors + float theta = theta_0 * alpha; // theta = angle between v0 and result + + float s0 = GfxMath::cos(theta) - dot + * GfxMath::sin(theta) / GfxMath::sin(theta_0); // == sin(theta_0 - theta) / sin(theta_0) + float s1 = GfxMath::sin(theta) / GfxMath::sin(theta_0); + + return (s0 * v0) + (s1 * v1); +} + +Quaternion Quaternion::Slerp(const Quaternion &a, const Quaternion &b, float alpha) { + return a.Slerp(b, alpha); +} + + +std::ostream & operator<< ( std::ostream &os, const Quaternion &q) { + return os << "<" << q[0] << ", " << q[1] << ", " << q[2] << ", " << q[3] << ")"; +} + +std::istream & operator>> ( std::istream &is, Quaternion &q) { + // format: <qx, qy, qz, qw> + char dummy; + return is >> dummy >> q[0] >> dummy >> q[1] >> dummy >> q[2] >> dummy >> q[3] >> dummy; +} + + +float Quaternion::Dot(const Quaternion& other) const { + return q[0]*other[0] + q[1]*other[1] + q[2]*other[2] + q[3]*other[3]; + +} + +float Quaternion::Length() const { + return sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]); +} + +void Quaternion::Normalize() { + float sizeSq = + q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]; + if (sizeSq < MINGFX_MATH_EPSILON) { + return; // do nothing to zero quats + } + float scaleFactor = (float)1.0/(float)sqrt(sizeSq); + q[0] *= scaleFactor; + q[1] *= scaleFactor; + q[2] *= scaleFactor; + q[3] *= scaleFactor; +} + +Quaternion Quaternion::ToUnit() const { + Quaternion qtmp(*this); + qtmp.Normalize(); + return qtmp; +} + +/// Returns the conjugate of the quaternion. +Quaternion Quaternion::Conjugate() const { + return Quaternion(-q[0], -q[1], -q[2], q[3]); +} + + +Quaternion Quaternion::FromAxisAngle(const Vector3 &axis, float angle) { + // [qx, qy, qz, qw] = [sin(a/2) * vx, sin(a/2)* vy, sin(a/2) * vz, cos(a/2)] + float x = GfxMath::sin(angle/2.0) * axis[0]; + float y = GfxMath::sin(angle/2.0) * axis[1]; + float z = GfxMath::sin(angle/2.0) * axis[2]; + float w = GfxMath::cos(angle/2.0); + return Quaternion(x,y,z,w); +} + + +Quaternion Quaternion::FromEulerAnglesZYX(const Vector3 &angles) { + Quaternion rot_x = Quaternion::FromAxisAngle(Vector3::UnitX(), angles[0]); + Quaternion rot_y = Quaternion::FromAxisAngle(Vector3::UnitY(), angles[1]); + Quaternion rot_z = Quaternion::FromAxisAngle(Vector3::UnitZ(), angles[2]); + return rot_z * rot_y * rot_x; +} + +Vector3 Quaternion::ToEulerAnglesZYX() const { + // https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles + + Vector3 angles; + + // roll (x-axis rotation) + float sinr = +2.0f * (w() * x() + y() * z()); + float cosr = +1.0f - 2.0f * (x() * x() + y() * y()); + angles[0] = std::atan2(sinr, cosr); + + // pitch (y-axis rotation) + float sinp = +2.0f * (w() * y() - z() * x()); + if (std::fabs(sinp) >= 1.f) + angles[1] = std::copysign(GfxMath::HALF_PI, sinp); // use 90 degrees if out of range + else + angles[1] = std::asin(sinp); + + // yaw (z-axis rotation) + float siny = +2.0f * (w() * z() + x() * y()); + float cosy = +1.0f - 2.0f * (y() * y() + z() * z()); + angles[2] = std::atan2(siny, cosy); + + return angles; +} + + +Quaternion operator*(const Quaternion& q1, const Quaternion& q2) { + float real1 = q1[3]; + Vector3 imag1 = Vector3(q1[0], q1[1], q1[2]); + + float real2 = q2[3]; + Vector3 imag2 = Vector3(q2[0], q2[1], q2[2]); + + float real = real1*real2 - imag1.Dot(imag2); + Vector3 imag = real1*imag2 + real2*imag1 + imag1.Cross(imag2); + + return Quaternion(imag[0], imag[1], imag[2], real); +} + + +Quaternion operator/(const Quaternion& q, const float s) { + const float invS = 1.0f / s; + return Quaternion(q[0]*invS, q[1]*invS, q[2]*invS, q[3]*invS); +} + +Quaternion operator*(const float s, const Quaternion& q) { + return Quaternion(q[0]*s, q[1]*s, q[2]*s, q[3]*s); +} + +Quaternion operator*(const Quaternion& q, const float s) { + return Quaternion(q[0]*s, q[1]*s, q[2]*s, q[3]*s); +} + +Quaternion operator-(const Quaternion& q) { + return Quaternion(-q[0], -q[1], -q[2], -q[3]); +} + +Quaternion operator+(const Quaternion& q1, const Quaternion& q2) { + return Quaternion(q1[0] + q2[0], q1[1] + q2[1], q1[2] + q2[2], q1[3] + q2[3]); +} + +Quaternion operator-(const Quaternion& q1, const Quaternion& q2) { + return Quaternion(q1[0] - q2[0], q1[1] - q2[1], q1[2] - q2[2], q1[3] - q2[3]); +} + +} // end namespace diff --git a/dev/MinGfx/src/quick_shapes.cc b/dev/MinGfx/src/quick_shapes.cc index dbd6c9e..01f187a 100644 --- a/dev/MinGfx/src/quick_shapes.cc +++ b/dev/MinGfx/src/quick_shapes.cc @@ -1,727 +1,733 @@ -/*
- Copyright (c) 2017,2018 Regents of the University of Minnesota.
- All Rights Reserved.
- See corresponding header file for details.
- */
-
-#include "quick_shapes.h"
-#include "platform.h"
-
-#include <cmath>
-#include <iostream>
-#include <string>
-
-
-namespace mingfx {
-
-
-
-#define PI 3.14159265359f
-#define TWOPI 6.28318530718f
-
-
-
-// Helper datastructure for building shapes algorithmically
-class Vertex {
-public:
- Vertex(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat nnx, GLfloat nny, GLfloat nnz) :
- x(xx), y(yy), z(zz), nx(nnx), ny(nny), nz(nnz) {}
-
- GLfloat x;
- GLfloat y;
- GLfloat z;
- GLfloat nx;
- GLfloat ny;
- GLfloat nz;
-};
-
-
-
-
-QuickShapes::QuickShapes() {
-}
-
-QuickShapes::~QuickShapes() {
-}
-
-
-
-
-// ------------ CUBE ------------
-
-
-void QuickShapes::initCube() {
- GLfloat vertices[] = {
- 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f,-1.0f, 1.0f, // v0-v1-v2 (front)
- -1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // v2-v3-v0
-
- 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f,-1.0f,-1.0f, // v0-v3-v4 (right)
- 1.0f,-1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, // v4-v5-v0
-
- 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, -1.0f, 1.0f,-1.0f, // v0-v5-v6 (top)
- -1.0f, 1.0f,-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // v6-v1-v0
-
- -1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, -1.0f,-1.0f,-1.0f, // v1-v6-v7 (left)
- -1.0f,-1.0f,-1.0f, -1.0f,-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, // v7-v2-v1.0
-
- -1.0f,-1.0f,-1.0f, 1.0f,-1.0f,-1.0f, 1.0f,-1.0f, 1.0f, // v7-v4-v3 (bottom)
- 1.0f,-1.0f, 1.0f, -1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, // v3-v2-v7
-
- 1.0f,-1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f, 1.0f,-1.0f, // v4-v7-v6 (back)
- -1.0f, 1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f,-1.0f,-1.0f // v6-v5-v4
- };
-
- GLfloat normals[] = {
- 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2 (front)
- 0, 0, 1, 0, 0, 1, 0, 0, 1, // v2-v3-v0
-
- 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4 (right)
- 1, 0, 0, 1, 0, 0, 1, 0, 0, // v4-v5-v0
-
- 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6 (top)
- 0, 1, 0, 0, 1, 0, 0, 1, 0, // v6-v1-v0
-
- -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7 (left)
- -1, 0, 0, -1, 0, 0, -1, 0, 0, // v7-v2-v1
-
- 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3 (bottom)
- 0,-1, 0, 0,-1, 0, 0,-1, 0, // v3-v2-v7
-
- 0, 0,-1, 0, 0,-1, 0, 0,-1, // v4-v7-v6 (back)
- 0, 0,-1, 0, 0,-1, 0, 0,-1 // v6-v5-v4
- };
-
- cubeMesh_.SetVertices(vertices, 36);
- cubeMesh_.SetNormals(normals, 36);
- cubeMesh_.UpdateGPUMemory();
-}
-
-
-void QuickShapes::DrawCube(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix, const Color &color)
-{
- if (cubeMesh_.num_vertices() == 0) {
- initCube();
- }
- defaultMaterial_.ambient_reflectance = color;
- defaultMaterial_.diffuse_reflectance = color;
- defaultMaterial_.surface_texture = emptyTex_;
- defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &cubeMesh_, defaultMaterial_);
-}
-
-
-
-
-// ------------ SQUARE ------------
-
-
-void QuickShapes::initSquare() {
- GLfloat vertices[] = {
- 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,-1.0f, -1.0f, 0.0f,-1.0f, // v0-v5-v6 (top)
- -1.0f, 0.0f,-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f // v6-v1-v0
- };
-
- GLfloat normals[] = {
- 0, 1, 0, 0, 1, 0, 0, 1, 0,
- 0, 1, 0, 0, 1, 0, 0, 1, 0
- };
-
- GLfloat texcoords[] = {
- 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f
- };
-
- squareMesh_.SetVertices(vertices, 6);
- squareMesh_.SetNormals(normals, 6);
- squareMesh_.SetTexCoords(0, texcoords, 6);
- squareMesh_.UpdateGPUMemory();
-}
-
-
-void QuickShapes::DrawSquare(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix, const Color &color)
-{
- if (squareMesh_.num_vertices() == 0) {
- initSquare();
- }
- defaultMaterial_.ambient_reflectance = color;
- defaultMaterial_.diffuse_reflectance = color;
- defaultMaterial_.surface_texture = emptyTex_;
- defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &squareMesh_, defaultMaterial_);
-}
-
-
-void QuickShapes::DrawSquare(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix, const Color &color,
- const Texture2D &tex)
-{
- if (squareMesh_.num_vertices() == 0) {
- initSquare();
- }
- defaultMaterial_.ambient_reflectance = color;
- defaultMaterial_.diffuse_reflectance = color;
- defaultMaterial_.surface_texture = tex;
- defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &squareMesh_, defaultMaterial_);
-}
-
-
-
-
-
-// ------------ CYLINDER ------------
-
-
-void QuickShapes::initCyl() {
-
- std::vector<Vertex> verts;
-
- Vertex top(0,1,0, 0,1,0);
- Vertex bot(0,-1,0, 0,-1,0);
-
- const int nslices = 20;
- for (int s=1; s<nslices+1; s++) {
- int slast = s - 1;
- GLfloat xlast = std::cosf(-TWOPI * (float)slast / (float)nslices);
- GLfloat zlast = std::sinf(-TWOPI * (float)slast/(float)nslices);
- GLfloat xnew = std::cosf(-TWOPI * (float)(s)/(float)nslices);
- GLfloat znew = std::sinf(-TWOPI * (float)(s)/(float)nslices);
-
- // one triangle on the top
- verts.push_back(top);
- verts.push_back(Vertex(xlast,1,zlast, 0,1,0));
- verts.push_back(Vertex(xnew,1,znew, 0,1,0));
-
- // two triangles to create a rect on the side
- verts.push_back(Vertex(xlast,1,zlast, xlast,0,zlast));
- verts.push_back(Vertex(xlast,-1,zlast, xlast,0,zlast));
- verts.push_back(Vertex(xnew,1,znew, xnew,0,znew));
-
- verts.push_back(Vertex(xnew,-1,znew, xnew,0,znew));
- verts.push_back(Vertex(xnew,1,znew, xnew,0,znew));
- verts.push_back(Vertex(xlast,-1,zlast, xlast,0,zlast));
-
- // one triangle on the bottom
- verts.push_back(bot);
- verts.push_back(Vertex(xnew,-1,znew, 0,-1,0));
- verts.push_back(Vertex(xlast,-1,zlast, 0,-1,0));
- }
-
- std::vector<Point3> vertices;
- std::vector<Vector3> normals;
- for (int i=0; i<verts.size(); i++) {
- vertices.push_back(Point3(verts[i].x, verts[i].y, verts[i].z));
- normals.push_back(Vector3(verts[i].nx, verts[i].ny, verts[i].nz));
- }
- cylMesh_.SetVertices(vertices);
- cylMesh_.SetNormals(normals);
- cylMesh_.UpdateGPUMemory();
-}
-
-
-void QuickShapes::DrawCylinder(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix, const Color &color)
-{
- if (cylMesh_.num_vertices() == 0) {
- initCyl();
- }
- defaultMaterial_.ambient_reflectance = color;
- defaultMaterial_.diffuse_reflectance = color;
- defaultMaterial_.surface_texture = emptyTex_;
- defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &cylMesh_, defaultMaterial_);
-}
-
-
-
-
-// ------------ CONE ------------
-
-
-void QuickShapes::initCone() {
-
- std::vector<Vertex> verts;
-
- Vertex top(0,1,0, 0,1,0);
- Vertex bot(0,-1,0, 0,-1,0);
-
- const int nslices = 20;
- for (int s=1; s<nslices+1; s++) {
- int slast = s - 1;
- GLfloat xlast = std::cosf(-TWOPI * (float)slast/(float)nslices);
- GLfloat zlast = std::sinf(-TWOPI * (float)slast/(float)nslices);
- GLfloat xnew = std::cosf(-TWOPI * (float)(s)/(float)nslices);
- GLfloat znew = std::sinf(-TWOPI * (float)(s)/(float)nslices);
-
- // one triangle on the side
- // normals are a bit more complex than for other shapes...
- Vector3 nlast = Vector3(xlast, 2, zlast).ToUnit();
- Vector3 nnew = Vector3(xnew, 2, znew).ToUnit();
- Vector3 ntop = 0.5*(nlast + nnew);
-
- verts.push_back(Vertex(top.x, top.y, top.z, ntop[0], ntop[1], ntop[2]));
- verts.push_back(Vertex(xlast,-1,zlast, nlast[0], nlast[1], nlast[2]));
- verts.push_back(Vertex(xnew,-1,znew, nnew[0], nnew[1], nnew[2]));
-
- // one triangle on the bottom
- verts.push_back(bot);
- verts.push_back(Vertex(xnew,-1,znew, 0,-1,0));
- verts.push_back(Vertex(xlast,-1,zlast, 0,-1,0));
- }
-
- std::vector<Point3> vertices;
- std::vector<Vector3> normals;
- for (int i = 0; i < verts.size(); i++) {
- vertices.push_back(Point3(verts[i].x, verts[i].y, verts[i].z));
- normals.push_back(Vector3(verts[i].nx, verts[i].ny, verts[i].nz));
- }
-
- coneMesh_.SetVertices(vertices);
- coneMesh_.SetNormals(normals);
- coneMesh_.UpdateGPUMemory();
-}
-
-
-void QuickShapes::DrawCone(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix, const Color &color)
-{
- if (coneMesh_.num_vertices() == 0) {
- initCone();
- }
- defaultMaterial_.ambient_reflectance = color;
- defaultMaterial_.diffuse_reflectance = color;
- defaultMaterial_.surface_texture = emptyTex_;
- defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &coneMesh_, defaultMaterial_);
-}
-
-
-
-
-
-// ------------ SPHERE ------------
-
-
-void QuickShapes::initSph() {
-
- std::vector<Vertex> verts;
-
- Vertex top(0,1,0, 0,1,0);
- Vertex bot(0,-1,0, 0,-1,0);
-
- const int nslices = 40;
- const int nstacks = 40;
- for (int s=1; s<nslices+1; s++) {
- int slast = s - 1;
- GLfloat xlast = std::cosf(-TWOPI * (float)slast/(float)nslices);
- GLfloat zlast = std::sinf(-TWOPI * (float)slast/(float)nslices);
- GLfloat xnew = std::cosf(-TWOPI * (float)(s)/(float)nslices);
- GLfloat znew = std::sinf(-TWOPI * (float)(s)/(float)nslices);
-
- float stackstep = PI/(float)nstacks;
-
- // one triangle on the top
- verts.push_back(top);
- verts.push_back(Vertex(std::sinf(stackstep)*xlast,std::cosf(stackstep),std::sinf(stackstep)*zlast,
- std::sinf(stackstep)*xlast,std::cosf(stackstep),std::sinf(stackstep)*zlast));
- verts.push_back(Vertex(std::sinf(stackstep)*xnew,std::cosf(stackstep),std::sinf(stackstep)*znew,
- std::sinf(stackstep)*xnew,std::cosf(stackstep),std::sinf(stackstep)*znew));
-
- for (int t=2; t<nstacks; t++) {
- int tlast = t - 1;
- GLfloat ylast = std::cosf(PI*(float)(tlast)/(float)nstacks);
- GLfloat ynew = std::cosf(PI*(float)(t)/(float)nstacks);
-
- GLfloat rlast = std::sinf(PI * (float)(tlast)/(float)nstacks);
- GLfloat rnew = std::sinf(PI * (float)(t)/(float)nstacks);
-
- // two triangles to create a rect on the side
- verts.push_back(Vertex(rlast*xlast,ylast,rlast*zlast, rlast*xlast,ylast,rlast*zlast));
- verts.push_back(Vertex(rnew*xlast,ynew,rnew*zlast, rnew*xlast,ynew,rnew*zlast));
- verts.push_back(Vertex(rnew*xnew,ynew,rnew*znew, rnew*xnew,ynew,rnew*znew));
-
- verts.push_back(Vertex(rnew*xnew,ynew,rnew*znew, rnew*xnew,ynew,rnew*znew));
- verts.push_back(Vertex(rlast*xnew,ylast,rlast*znew, rlast*xnew,ylast,rlast*znew));
- verts.push_back(Vertex(rlast*xlast,ylast,rlast*zlast, rlast*xlast,ylast,rlast*zlast));
- }
-
- // one triangle on the bottom
- verts.push_back(bot);
- verts.push_back(Vertex(std::sinf(stackstep)*xnew,std::cosf(PI-stackstep),std::sinf(stackstep)*znew,
- std::sinf(stackstep)*xnew,std::cosf(PI-stackstep),std::sinf(stackstep)*znew));
- verts.push_back(Vertex(std::sinf(stackstep)*xlast,std::cosf(PI-stackstep),std::sinf(stackstep)*zlast,
- std::sinf(stackstep)*xlast,std::cosf(PI-stackstep),std::sinf(stackstep)*zlast));
- }
-
- std::vector<Point3> vertices;
- std::vector<Vector3> normals;
- for (int i = 0; i < verts.size(); i++) {
- vertices.push_back(Point3(verts[i].x, verts[i].y, verts[i].z));
- normals.push_back(Vector3(verts[i].nx, verts[i].ny, verts[i].nz));
- }
- sphereMesh_.SetVertices(vertices);
- sphereMesh_.SetNormals(normals);
- sphereMesh_.UpdateGPUMemory();
-}
-
-
-void QuickShapes::DrawSphere(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix, const Color &color)
-{
- if (sphereMesh_.num_vertices() == 0) {
- initSph();
- }
- defaultMaterial_.ambient_reflectance = color;
- defaultMaterial_.diffuse_reflectance = color;
- defaultMaterial_.surface_texture = emptyTex_;
- defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &sphereMesh_, defaultMaterial_);
-}
-
-
-
-
-// ------------ BRUSH ------------
-
-
-void QuickShapes::initBrush() {
-
- // Raw vertices -- points that make up the brush geometry
- const GLfloat v[19][3] = {
- { 0.5f, 0.0f, 0.0f}, // 0
- {-0.5f, 0.0f, 0.0f}, // 1
-
- { 0.5f, 0.1f, 0.25f}, // 2
- {-0.5f, 0.1f, 0.25f}, // 3
- { 0.5f, 0.1f, 0.75f}, // 4
- {-0.5f, 0.1f, 0.75f}, // 5
- { 0.1f, 0.06f, 1.0f}, // 6
- {-0.1f, 0.06f, 1.0f}, // 7
- { 0.15f, 0.1f, 1.75f}, // 8
- {-0.15f, 0.1f, 1.75f}, // 9
-
- { 0.0f, 0.0f, 1.85f}, // 10
-
- { 0.5f, -0.1f, 0.25f}, // 11
- {-0.5f, -0.1f, 0.25f}, // 12
- { 0.5f, -0.1f, 0.75f}, // 13
- {-0.5f, -0.1f, 0.75f}, // 14
- { 0.1f, -0.06f, 1.0f}, // 15
- {-0.1f, -0.06f, 1.0f}, // 16
- { 0.15f, -0.1f, 1.75f}, // 17
- {-0.15f, -0.1f, 1.75f} // 18
- };
-
-
- // Vertices arranged into triangles
- const GLfloat verts[34][3][3] = {
- // top
- {{v[0][0], v[0][1], v[0][2]}, {v[1][0], v[1][1], v[1][2]}, {v[2][0], v[2][1], v[2][2]}},
- {{v[1][0], v[1][1], v[1][2]}, {v[3][0], v[3][1], v[3][2]}, {v[2][0], v[2][1], v[2][2]}},
-
- {{v[2][0], v[2][1], v[2][2]}, {v[3][0], v[3][1], v[3][2]}, {v[4][0], v[4][1], v[4][2]}},
- {{v[3][0], v[3][1], v[3][2]}, {v[5][0], v[5][1], v[5][2]}, {v[4][0], v[4][1], v[4][2]}},
-
- {{v[4][0], v[4][1], v[4][2]}, {v[5][0], v[5][1], v[5][2]}, {v[6][0], v[6][1], v[6][2]}},
- {{v[5][0], v[5][1], v[5][2]}, {v[7][0], v[7][1], v[7][2]}, {v[6][0], v[6][1], v[6][2]}},
-
- {{v[6][0], v[6][1], v[6][2]}, {v[7][0], v[7][1], v[7][2]}, {v[8][0], v[8][1], v[8][2]}},
- {{v[7][0], v[7][1], v[7][2]}, {v[9][0], v[9][1], v[9][2]}, {v[8][0], v[8][1], v[8][2]}},
-
- {{v[8][0], v[8][1], v[8][2]}, {v[9][0], v[9][1], v[9][2]}, {v[10][0], v[10][1], v[10][2]}},
-
- // bottom
- {{v[0][0], v[0][1], v[0][2]}, {v[12][0], v[12][1], v[12][2]}, {v[1][0], v[1][1], v[1][2]}},
- {{v[11][0], v[11][1], v[11][2]}, {v[12][0], v[12][1], v[12][2]}, {v[0][0], v[0][1], v[0][2]}},
-
- {{v[11][0], v[11][1], v[11][2]}, {v[14][0], v[14][1], v[14][2]}, {v[12][0], v[12][1], v[12][2]}},
- {{v[13][0], v[13][1], v[13][2]}, {v[14][0], v[14][1], v[14][2]}, {v[11][0], v[11][1], v[11][2]}},
-
- {{v[13][0], v[13][1], v[13][2]}, {v[16][0], v[16][1], v[16][2]}, {v[14][0], v[14][1], v[14][2]}},
- {{v[15][0], v[15][1], v[15][2]}, {v[16][0], v[16][1], v[16][2]}, {v[13][0], v[13][1], v[13][2]}},
-
- {{v[15][0], v[15][1], v[15][2]}, {v[18][0], v[18][1], v[18][2]}, {v[16][0], v[16][1], v[16][2]}},
- {{v[17][0], v[17][1], v[17][2]}, {v[18][0], v[18][1], v[18][2]}, {v[15][0], v[15][1], v[15][2]}},
-
- {{v[18][0], v[18][1], v[18][2]}, {v[17][0], v[17][1], v[17][2]}, {v[10][0], v[10][1], v[10][2]}},
-
- // one side
- {{v[11][0], v[11][1], v[11][2]}, {v[0][0], v[0][1], v[0][2]}, {v[2][0], v[2][1], v[2][2]}},
-
- {{v[11][0], v[11][1], v[11][2]}, {v[2][0], v[2][1], v[2][2]}, {v[4][0], v[4][1], v[4][2]}},
- {{v[4][0], v[4][1], v[4][2]}, {v[13][0], v[13][1], v[13][2]}, {v[11][0], v[11][1], v[11][2]}},
-
- {{v[13][0], v[13][1], v[13][2]}, {v[4][0], v[4][1], v[4][2]}, {v[6][0], v[6][1], v[6][2]}},
- {{v[6][0], v[6][1], v[6][2]}, {v[15][0], v[15][1], v[15][2]}, {v[13][0], v[13][1], v[13][2]}},
-
- {{v[15][0], v[15][1], v[15][2]}, {v[6][0], v[6][1], v[6][2]}, {v[8][0], v[8][1], v[8][2]}},
- {{v[8][0], v[8][1], v[8][2]}, {v[17][0], v[17][1], v[17][2]}, {v[15][0], v[15][1], v[15][2]}},
-
- {{v[17][0], v[17][1], v[17][2]}, {v[8][0], v[8][1], v[8][2]}, {v[10][0], v[10][1], v[10][2]}},
-
- // other side
- {{v[3][0], v[3][1], v[3][2]}, {v[1][0], v[1][1], v[1][2]}, {v[12][0], v[12][1], v[12][2]}},
-
- {{v[3][0], v[3][1], v[3][2]}, {v[12][0], v[12][1], v[12][2]}, {v[14][0], v[14][1], v[14][2]}},
- {{v[14][0], v[14][1], v[14][2]}, {v[5][0], v[5][1], v[5][2]}, {v[3][0], v[3][1], v[3][2]}},
-
- {{v[5][0], v[5][1], v[5][2]}, {v[14][0], v[14][1], v[14][2]}, {v[16][0], v[16][1], v[16][2]}},
- {{v[16][0], v[16][1], v[16][2]}, {v[7][0], v[7][1], v[7][2]}, {v[5][0], v[5][1], v[5][2]}},
-
- {{v[7][0], v[7][1], v[7][2]}, {v[16][0], v[16][1], v[16][2]}, {v[18][0], v[18][1], v[18][2]}},
- {{v[18][0], v[18][1], v[18][2]}, {v[9][0], v[9][1], v[9][2]}, {v[7][0], v[7][1], v[7][2]}},
-
- {{v[9][0], v[9][1], v[9][2]}, {v[18][0], v[18][1], v[18][2]}, {v[10][0], v[10][1], v[10][2]}}
-
- };
-
-
- // Normals defined so as to make each face of the brush a flat surface
- const GLfloat norms[34][3][3] = {
- // top
- {{0.0f, 0.93f, -0.37f}, {0.0f, 0.93f, -0.37f}, {0.0f, 0.93f, -0.37f}},
- {{0.0f, 0.93f, -0.37f}, {0.0f, 0.93f, -0.37f}, {0.0f, 0.93f, -0.37f}},
-
- {{0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}},
- {{0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}},
-
- {{0.0f, 0.988f, 0.158f}, {0.0f, 0.988f, 0.158f}, {0.0f, 0.988f, 0.158f}},
- {{0.0f, 0.988f, 0.158f}, {0.0f, 0.988f, 0.158f}, {0.0f, 0.988f, 0.158f}},
-
- {{0.0f, 0.999f, -0.0533f}, {0.0f, 0.999f, -0.0533f}, {0.0f, 0.999f, -0.0533f}},
- {{0.0f, 0.999f, -0.0533f}, {0.0f, 0.999f, -0.0533f}, {0.0f, 0.999f, -0.0533f}},
-
- {{0.0f, 0.709f, 0.709f}, {0.0f, 0.709f, 0.709f}, {0.0f, 0.709f, 0.709f}},
-
- // bottom
- {{0.0f, -0.93f, -0.37f}, {0.0f, -0.93f, -0.37f}, {0.0f, -0.93f, -0.37f}},
- {{0.0f, -0.93f, -0.37f}, {0.0f, -0.93f, -0.37f}, {0.0f, -0.93f, -0.37f}},
-
- {{0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}},
- {{0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}},
-
- {{0.0f, -0.988f, 0.158f}, {0.0f, -0.988f, 0.158f}, {0.0f, -0.988f, 0.158f}},
- {{0.0f, -0.988f, 0.158f}, {0.0f, -0.988f, 0.158f}, {0.0f, -0.988f, 0.158f}},
-
- {{0.0f, -0.999f, -0.0533f}, {0.0f, -0.999f, -0.0533f}, {0.0f, -0.999f, -0.0533f}},
- {{0.0f, -0.999f, -0.0533f}, {0.0f, -0.999f, -0.0533f}, {0.0f, -0.999f, -0.0533f}},
-
- {{0.0f, -0.709f, 0.709f}, {0.0f, -0.709f, 0.709f}, {0.0f, -0.709f, 0.709f}},
-
- // one side
- {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
-
- {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
- {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
-
- {{0.848f, 0.0f, 0.530f}, {0.848f, 0.0f, 0.530f}, {0.848f, 0.0f, 0.530f}},
- {{0.848f, 0.0f, 0.530f}, {0.848f, 0.0f, 0.530f}, {0.848f, 0.0f, 0.530f}},
-
- {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
- {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
-
- {{0.709f, 0.0f, 0.709f}, {0.709f, 0.0f, 0.709f}, {0.709f, 0.0f, 0.709f}},
-
- // other side
- {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}},
-
- {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}},
- {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}},
-
- {{-0.848f, 0.0f, 0.530f}, {-0.848f, 0.0f, 0.530f}, {-0.848f, 0.0f, 0.530f}},
- {{-0.848f, 0.0f, 0.530f}, {-0.848f, 0.0f, 0.530f}, {-0.848f, 0.0f, 0.530f}},
-
- {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}},
- {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}},
-
- {{-0.709f, 0.0f, 0.709f}, {-0.709f, 0.0f, 0.709f}, {-0.709f, 0.0f, 0.709f}}
- };
-
- brushMesh_.SetVertices((float*)verts, 102);
- brushMesh_.SetNormals((float*)norms, 102);
- brushMesh_.UpdateGPUMemory();
-}
-
-
-void QuickShapes::DrawBrush(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix, const Color &color)
-{
- if (brushMesh_.num_vertices() == 0) {
- initBrush();
- }
- defaultMaterial_.ambient_reflectance = color;
- defaultMaterial_.diffuse_reflectance = color;
- defaultMaterial_.surface_texture = emptyTex_;
- defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &brushMesh_, defaultMaterial_);
-}
-
-
-
-// ----------------
-
-
-void QuickShapes::DrawLineSegment(const Matrix4 &modelMatrix,
- const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix,
- const Color &color,
- const Point3 &p1,
- const Point3 &p2,
- float radius)
-{
- Matrix4 S = Matrix4::Scale(Vector3(radius, 0.5f*(p2-p1).Length(), radius));
- Vector3 y = (p2-p1).ToUnit();
- Vector3 z = Vector3(1,0,0).Cross(y).ToUnit();
- if (z == Vector3(0,0,0)) {
- z = Vector3(0,0,1).Cross(y).ToUnit();
- }
- Vector3 x = y.Cross(z);
- Matrix4 R = Matrix4::FromRowMajorElements(
- x[0], y[0], z[0], 0,
- x[1], y[1], z[1], 0,
- x[2], y[2], z[2], 0,
- 0, 0, 0, 1
- );
- Matrix4 T = Matrix4::Translation(0.5 * Vector3(p1[0]+p2[0], p1[1]+p2[1], p1[2]+p2[2]));
-
- Matrix4 M = T * R * S;
-
- DrawCylinder(modelMatrix * M, viewMatrix, projectionMatrix, color);
-}
-
-
-
-void QuickShapes::DrawLines(const Matrix4 &modelMatrix,
- const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix,
- const Color &color,
- const std::vector<Point3> &points,
- LinesType ltype,
- float radius)
-{
- if (ltype == LinesType::LINES) {
- for (size_t i=0; i<points.size(); i+=2) {
- DrawLineSegment(modelMatrix, viewMatrix, projectionMatrix, color, points[i], points[i+1], radius);
- }
- }
- else {
- for (size_t i=0; i<points.size()-1; i++) {
- DrawLineSegment(modelMatrix, viewMatrix, projectionMatrix, color, points[i], points[i+1], radius);
- }
- if (ltype == LinesType::LINE_LOOP) {
- DrawLineSegment(modelMatrix, viewMatrix, projectionMatrix, color, points[points.size()-1], points[0], radius);
- }
- }
-}
-
-
-
-void QuickShapes::DrawArrow(const Matrix4 &modelMatrix,
- const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix,
- const Color &color,
- Point3 p, Vector3 dir, float radius)
-{
- float d = dir.Length() - 8.0f*radius;
- DrawLineSegment(modelMatrix, viewMatrix, projectionMatrix, color, p, p + d*dir.ToUnit(), radius);
-
- Matrix4 S = Matrix4::Scale(Vector3(radius*3.0f, radius*4.0f, radius*3.0f));
- Vector3 y = dir.ToUnit();
- Vector3 z = Vector3(1,0,0).Cross(y).ToUnit();
- if (z == Vector3(0,0,0)) {
- z = Vector3(0,0,1).Cross(y).ToUnit();
- }
- Vector3 x = y.Cross(z);
- Matrix4 R = Matrix4::FromRowMajorElements(
- x[0], y[0], z[0], 0,
- x[1], y[1], z[1], 0,
- x[2], y[2], z[2], 0,
- 0, 0, 0, 1
- );
- Matrix4 T = Matrix4::Translation((p + d*dir.ToUnit()) - Point3::Origin());
-
- Matrix4 M = T * R * S * Matrix4::Translation(Vector3(0,1,0));
-
- DrawCone(modelMatrix * M, viewMatrix, projectionMatrix, color);
-}
-
-
-void QuickShapes::DrawAxes(const Matrix4 &modelMatrix,
- const Matrix4 &viewMatrix,
- const Matrix4 &projectionMatrix)
-{
- DrawArrow(modelMatrix, viewMatrix, projectionMatrix, Color(1.0f, 0.6f, 0.6f), Point3::Origin(), Vector3::UnitX(), 0.02f);
- DrawArrow(modelMatrix, viewMatrix, projectionMatrix, Color(0.6f, 1.0f, 0.6f), Point3::Origin(), Vector3::UnitY(), 0.02f);
- DrawArrow(modelMatrix, viewMatrix, projectionMatrix, Color(0.6f, 0.6f, 1.0f), Point3::Origin(), Vector3::UnitZ(), 0.02f);
-
-}
-
-
-void QuickShapes::initFull() {
- GLfloat vertices[] = {
- -1, -1, 0, 1, -1, 0, 1, 1, 0,
- -1, -1, 0, 1, 1, 0, -1, 1, 0
- };
-
- GLfloat normals[] = {
- 0, 0, 1, 0, 0, 1, 0, 0, 1,
- 0, 0, 1, 0, 0, 1, 0, 0, 1
- };
-
- GLfloat texcoords[] = {
- 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
- 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f
- };
-
- fullMesh_.SetVertices(vertices, 6);
- fullMesh_.SetNormals(normals, 6);
- fullMesh_.SetTexCoords(0, texcoords, 6);
- fullMesh_.UpdateGPUMemory();
-}
-
-
-void QuickShapes::DrawFullscreenTexture(const Color &color, const Texture2D &tex) {
- if (fullMesh_.num_vertices() == 0) {
- initFull();
- }
- DrawWithFullscreen(color, &fullMesh_, tex);
-}
-
-
-
-
-
-void QuickShapes::DrawWithFullscreen(const Color &color, Mesh *mesh, const Texture2D &tex) {
- if (!fullscreenShader_.initialized()) {
- fullscreenShader_.AddVertexShaderFromFile(Platform::FindMinGfxShaderFile("fullscreen.vert"));
- fullscreenShader_.AddFragmentShaderFromFile(Platform::FindMinGfxShaderFile("fullscreen.frag"));
- fullscreenShader_.LinkProgram();
- }
-
- glDisable(GL_DEPTH_TEST);
- glDepthMask(GL_FALSE);
-
- // Activate the shader program
- fullscreenShader_.UseProgram();
-
- // Pass uniforms and textures from C++ to the GPU Shader Program
- fullscreenShader_.SetUniform("TintColor", color);
- fullscreenShader_.BindTexture("SurfaceTexture", tex);
-
- // Draw the mesh using the shader program
- mesh->Draw();
-
- // Deactivate the shader program
- fullscreenShader_.StopProgram();
-
- glEnable(GL_DEPTH_TEST);
- glDepthMask(GL_TRUE);
-}
-
-
-DefaultShader* QuickShapes::default_shader() {
- return &defaultShader_;
-}
-
-
-DefaultShader::MaterialProperties* QuickShapes::material() {
- return &defaultMaterial_;
-}
-
-
-} // end namespace
+/* + Copyright (c) 2017,2018 Regents of the University of Minnesota. + All Rights Reserved. + See corresponding header file for details. + */ + +#include "quick_shapes.h" +#include "platform.h" + +#include <cmath> +#include <iostream> +#include <string> + +#include "gfxmath.h" + +namespace mingfx { + + + +#define PI 3.14159265359f +#define TWOPI 6.28318530718f + + + +// Helper datastructure for building shapes algorithmically +class Vertex { +public: + Vertex(GLfloat xx, GLfloat yy, GLfloat zz, GLfloat nnx, GLfloat nny, GLfloat nnz) : + x(xx), y(yy), z(zz), nx(nnx), ny(nny), nz(nnz) {} + + GLfloat x; + GLfloat y; + GLfloat z; + GLfloat nx; + GLfloat ny; + GLfloat nz; +}; + + + + +QuickShapes::QuickShapes() { +} + +QuickShapes::~QuickShapes() { +} + + + + +// ------------ CUBE ------------ + + +void QuickShapes::initCube() { + GLfloat vertices[] = { + 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f,-1.0f, 1.0f, // v0-v1-v2 (front) + -1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // v2-v3-v0 + + 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f,-1.0f,-1.0f, // v0-v3-v4 (right) + 1.0f,-1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, // v4-v5-v0 + + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, -1.0f, 1.0f,-1.0f, // v0-v5-v6 (top) + -1.0f, 1.0f,-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // v6-v1-v0 + + -1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, -1.0f,-1.0f,-1.0f, // v1-v6-v7 (left) + -1.0f,-1.0f,-1.0f, -1.0f,-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, // v7-v2-v1.0 + + -1.0f,-1.0f,-1.0f, 1.0f,-1.0f,-1.0f, 1.0f,-1.0f, 1.0f, // v7-v4-v3 (bottom) + 1.0f,-1.0f, 1.0f, -1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, // v3-v2-v7 + + 1.0f,-1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f, 1.0f,-1.0f, // v4-v7-v6 (back) + -1.0f, 1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f,-1.0f,-1.0f // v6-v5-v4 + }; + + GLfloat normals[] = { + 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2 (front) + 0, 0, 1, 0, 0, 1, 0, 0, 1, // v2-v3-v0 + + 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4 (right) + 1, 0, 0, 1, 0, 0, 1, 0, 0, // v4-v5-v0 + + 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6 (top) + 0, 1, 0, 0, 1, 0, 0, 1, 0, // v6-v1-v0 + + -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7 (left) + -1, 0, 0, -1, 0, 0, -1, 0, 0, // v7-v2-v1 + + 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3 (bottom) + 0,-1, 0, 0,-1, 0, 0,-1, 0, // v3-v2-v7 + + 0, 0,-1, 0, 0,-1, 0, 0,-1, // v4-v7-v6 (back) + 0, 0,-1, 0, 0,-1, 0, 0,-1 // v6-v5-v4 + }; + + cubeMesh_.SetVertices(vertices, 36); + cubeMesh_.SetNormals(normals, 36); + cubeMesh_.UpdateGPUMemory(); +} + + +void QuickShapes::DrawCube(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, const Color &color) +{ + if (cubeMesh_.num_vertices() == 0) { + initCube(); + } + defaultMaterial_.ambient_reflectance = color; + defaultMaterial_.diffuse_reflectance = color; + defaultMaterial_.surface_texture = emptyTex_; + defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &cubeMesh_, defaultMaterial_); +} + + + + +// ------------ SQUARE ------------ + + +void QuickShapes::initSquare() { + GLfloat vertices[] = { + 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,-1.0f, -1.0f, 0.0f,-1.0f, // v0-v5-v6 (top) + -1.0f, 0.0f,-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f // v6-v1-v0 + }; + + GLfloat normals[] = { + 0, 1, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, 0 + }; + + GLfloat texcoords[] = { + 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f + }; + + squareMesh_.SetVertices(vertices, 6); + squareMesh_.SetNormals(normals, 6); + squareMesh_.SetTexCoords(0, texcoords, 6); + squareMesh_.UpdateGPUMemory(); +} + + +void QuickShapes::DrawSquare(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, const Color &color) +{ + if (squareMesh_.num_vertices() == 0) { + initSquare(); + } + defaultMaterial_.ambient_reflectance = color; + defaultMaterial_.diffuse_reflectance = color; + defaultMaterial_.surface_texture = emptyTex_; + defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &squareMesh_, defaultMaterial_); +} + + +void QuickShapes::DrawSquare(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, const Color &color, + const Texture2D &tex) +{ + if (squareMesh_.num_vertices() == 0) { + initSquare(); + } + defaultMaterial_.ambient_reflectance = color; + defaultMaterial_.diffuse_reflectance = color; + defaultMaterial_.surface_texture = tex; + defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &squareMesh_, defaultMaterial_); +} + + + + + +// ------------ CYLINDER ------------ + + +void QuickShapes::initCyl() { + + std::vector<Vertex> verts; + + Vertex top(0,1,0, 0,1,0); + Vertex bot(0,-1,0, 0,-1,0); + + const int nslices = 20; + for (int s=1; s<nslices+1; s++) { + GLfloat xlast = GfxMath::cos(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat zlast = GfxMath::sin(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat xnew = GfxMath::cos(-TWOPI * (float)(s)/(float)nslices); + GLfloat znew = GfxMath::sin(-TWOPI * (float)(s)/(float)nslices); + + // one triangle on the top + verts.push_back(top); + verts.push_back(Vertex(xlast,1,zlast, 0,1,0)); + verts.push_back(Vertex(xnew,1,znew, 0,1,0)); + + // two triangles to create a rect on the side + verts.push_back(Vertex(xlast,1,zlast, xlast,0,zlast)); + verts.push_back(Vertex(xlast,-1,zlast, xlast,0,zlast)); + verts.push_back(Vertex(xnew,1,znew, xnew,0,znew)); + + verts.push_back(Vertex(xnew,-1,znew, xnew,0,znew)); + verts.push_back(Vertex(xnew,1,znew, xnew,0,znew)); + verts.push_back(Vertex(xlast,-1,zlast, xlast,0,zlast)); + + // one triangle on the bottom + verts.push_back(bot); + verts.push_back(Vertex(xnew,-1,znew, 0,-1,0)); + verts.push_back(Vertex(xlast,-1,zlast, 0,-1,0)); + } + + std::vector<Point3> vertices; + std::vector<Vector3> normals; + for (int i=0; i<verts.size(); i++) { + vertices.push_back(Point3(verts[i].x, verts[i].y, verts[i].z)); + normals.push_back(Vector3(verts[i].nx, verts[i].ny, verts[i].nz)); + } + cylMesh_.SetVertices(vertices); + cylMesh_.SetNormals(normals); + cylMesh_.UpdateGPUMemory(); +} + + +void QuickShapes::DrawCylinder(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, const Color &color) +{ + if (cylMesh_.num_vertices() == 0) { + initCyl(); + } + defaultMaterial_.ambient_reflectance = color; + defaultMaterial_.diffuse_reflectance = color; + defaultMaterial_.surface_texture = emptyTex_; + defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &cylMesh_, defaultMaterial_); +} + + + + +// ------------ CONE ------------ + + +void QuickShapes::initCone() { + + std::vector<Vertex> verts; + + Vertex top(0,1,0, 0,1,0); + Vertex bot(0,-1,0, 0,-1,0); + + const int nslices = 20; + for (int s=1; s<nslices+1; s++) { + GLfloat xlast = GfxMath::cos(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat zlast = GfxMath::sin(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat xnew = GfxMath::cos(-TWOPI * (float)(s)/(float)nslices); + GLfloat znew = GfxMath::sin(-TWOPI * (float)(s)/(float)nslices); + + // one triangle on the side + // normals are a bit more complex than for other shapes... + Vector3 nlast = Vector3(xlast, 2, zlast).ToUnit(); + Vector3 nnew = Vector3(xnew, 2, znew).ToUnit(); + Vector3 ntop = 0.5*(nlast + nnew); + + verts.push_back(Vertex(top.x, top.y, top.z, ntop[0], ntop[1], ntop[2])); + verts.push_back(Vertex(xlast,-1,zlast, nlast[0], nlast[1], nlast[2])); + verts.push_back(Vertex(xnew,-1,znew, nnew[0], nnew[1], nnew[2])); + + // one triangle on the bottom + verts.push_back(bot); + verts.push_back(Vertex(xnew,-1,znew, 0,-1,0)); + verts.push_back(Vertex(xlast,-1,zlast, 0,-1,0)); + } + + std::vector<Point3> vertices; + std::vector<Vector3> normals; + for (int i = 0; i < verts.size(); i++) { + vertices.push_back(Point3(verts[i].x, verts[i].y, verts[i].z)); + normals.push_back(Vector3(verts[i].nx, verts[i].ny, verts[i].nz)); + } + + coneMesh_.SetVertices(vertices); + coneMesh_.SetNormals(normals); + coneMesh_.UpdateGPUMemory(); +} + + +void QuickShapes::DrawCone(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, const Color &color) +{ + if (coneMesh_.num_vertices() == 0) { + initCone(); + } + defaultMaterial_.ambient_reflectance = color; + defaultMaterial_.diffuse_reflectance = color; + defaultMaterial_.surface_texture = emptyTex_; + defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &coneMesh_, defaultMaterial_); +} + + + + + +// ------------ SPHERE ------------ + + +void QuickShapes::initSph() { + + std::vector<Vertex> verts; + + Vertex top(0,1,0, 0,1,0); + Vertex bot(0,-1,0, 0,-1,0); + + const int nslices = 40; + const int nstacks = 40; + for (int s=1; s<nslices+1; s++) { + GLfloat xlast = GfxMath::cos(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat zlast = GfxMath::sin(-TWOPI * (float)(s-1)/(float)nslices); + GLfloat xnew = GfxMath::cos(-TWOPI * (float)(s)/(float)nslices); + GLfloat znew = GfxMath::sin(-TWOPI * (float)(s)/(float)nslices); + + float stackstep = PI/(float)nstacks; + + // one triangle on the top + verts.push_back(top); + verts.push_back(Vertex(GfxMath::sin(stackstep)*xlast,GfxMath::cos(stackstep), + GfxMath::sin(stackstep)*zlast, + GfxMath::sin(stackstep)*xlast,GfxMath::cos(stackstep), + GfxMath::sin(stackstep)*zlast)); + verts.push_back(Vertex(GfxMath::sin(stackstep)*xnew,GfxMath::cos(stackstep), + GfxMath::sin(stackstep)*znew, + GfxMath::sin(stackstep)*xnew,GfxMath::cos(stackstep), + GfxMath::sin(stackstep)*znew)); + + for (int t=2; t<nstacks; t++) { + GLfloat ylast = GfxMath::cos(PI*(float)(t-1)/(float)nstacks); + GLfloat ynew = GfxMath::cos(PI*(float)(t)/(float)nstacks); + + GLfloat rlast = GfxMath::sin(PI * (float)(t-1)/(float)nstacks); + GLfloat rnew = GfxMath::sin(PI * (float)(t)/(float)nstacks); + + // two triangles to create a rect on the side + verts.push_back(Vertex(rlast*xlast,ylast,rlast*zlast, rlast*xlast,ylast,rlast*zlast)); + verts.push_back(Vertex(rnew*xlast,ynew,rnew*zlast, rnew*xlast,ynew,rnew*zlast)); + verts.push_back(Vertex(rnew*xnew,ynew,rnew*znew, rnew*xnew,ynew,rnew*znew)); + + verts.push_back(Vertex(rnew*xnew,ynew,rnew*znew, rnew*xnew,ynew,rnew*znew)); + verts.push_back(Vertex(rlast*xnew,ylast,rlast*znew, rlast*xnew,ylast,rlast*znew)); + verts.push_back(Vertex(rlast*xlast,ylast,rlast*zlast, rlast*xlast,ylast,rlast*zlast)); + } + + // one triangle on the bottom + verts.push_back(bot); + verts.push_back(Vertex(GfxMath::sin(stackstep)*xnew,GfxMath::cos(PI-stackstep), + GfxMath::sin(stackstep)*znew, + GfxMath::sin(stackstep)*xnew,GfxMath::cos(PI-stackstep), + GfxMath::sin(stackstep)*znew)); + verts.push_back(Vertex(GfxMath::sin(stackstep)*xlast,GfxMath::cos(PI-stackstep), + GfxMath::sin(stackstep)*zlast, + GfxMath::sin(stackstep)*xlast,GfxMath::cos(PI-stackstep), + GfxMath::sin(stackstep)*zlast)); + } + + + std::vector<Point3> vertices; + std::vector<Vector3> normals; + for (int i = 0; i < verts.size(); i++) { + vertices.push_back(Point3(verts[i].x, verts[i].y, verts[i].z)); + normals.push_back(Vector3(verts[i].nx, verts[i].ny, verts[i].nz)); + } + sphereMesh_.SetVertices(vertices); + sphereMesh_.SetNormals(normals); + sphereMesh_.UpdateGPUMemory(); +} + + +void QuickShapes::DrawSphere(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, const Color &color) +{ + if (sphereMesh_.num_vertices() == 0) { + initSph(); + } + defaultMaterial_.ambient_reflectance = color; + defaultMaterial_.diffuse_reflectance = color; + defaultMaterial_.surface_texture = emptyTex_; + defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &sphereMesh_, defaultMaterial_); +} + + + + +// ------------ BRUSH ------------ + + +void QuickShapes::initBrush() { + + // Raw vertices -- points that make up the brush geometry + const GLfloat v[19][3] = { + { 0.5f, 0.0f, 0.0f}, // 0 + {-0.5f, 0.0f, 0.0f}, // 1 + + { 0.5f, 0.1f, 0.25f}, // 2 + {-0.5f, 0.1f, 0.25f}, // 3 + { 0.5f, 0.1f, 0.75f}, // 4 + {-0.5f, 0.1f, 0.75f}, // 5 + { 0.1f, 0.06f, 1.0f}, // 6 + {-0.1f, 0.06f, 1.0f}, // 7 + { 0.15f, 0.1f, 1.75f}, // 8 + {-0.15f, 0.1f, 1.75f}, // 9 + + { 0.0f, 0.0f, 1.85f}, // 10 + + { 0.5f, -0.1f, 0.25f}, // 11 + {-0.5f, -0.1f, 0.25f}, // 12 + { 0.5f, -0.1f, 0.75f}, // 13 + {-0.5f, -0.1f, 0.75f}, // 14 + { 0.1f, -0.06f, 1.0f}, // 15 + {-0.1f, -0.06f, 1.0f}, // 16 + { 0.15f, -0.1f, 1.75f}, // 17 + {-0.15f, -0.1f, 1.75f} // 18 + }; + + + // Vertices arranged into triangles + const GLfloat verts[34][3][3] = { + // top + {{v[0][0], v[0][1], v[0][2]}, {v[1][0], v[1][1], v[1][2]}, {v[2][0], v[2][1], v[2][2]}}, + {{v[1][0], v[1][1], v[1][2]}, {v[3][0], v[3][1], v[3][2]}, {v[2][0], v[2][1], v[2][2]}}, + + {{v[2][0], v[2][1], v[2][2]}, {v[3][0], v[3][1], v[3][2]}, {v[4][0], v[4][1], v[4][2]}}, + {{v[3][0], v[3][1], v[3][2]}, {v[5][0], v[5][1], v[5][2]}, {v[4][0], v[4][1], v[4][2]}}, + + {{v[4][0], v[4][1], v[4][2]}, {v[5][0], v[5][1], v[5][2]}, {v[6][0], v[6][1], v[6][2]}}, + {{v[5][0], v[5][1], v[5][2]}, {v[7][0], v[7][1], v[7][2]}, {v[6][0], v[6][1], v[6][2]}}, + + {{v[6][0], v[6][1], v[6][2]}, {v[7][0], v[7][1], v[7][2]}, {v[8][0], v[8][1], v[8][2]}}, + {{v[7][0], v[7][1], v[7][2]}, {v[9][0], v[9][1], v[9][2]}, {v[8][0], v[8][1], v[8][2]}}, + + {{v[8][0], v[8][1], v[8][2]}, {v[9][0], v[9][1], v[9][2]}, {v[10][0], v[10][1], v[10][2]}}, + + // bottom + {{v[0][0], v[0][1], v[0][2]}, {v[12][0], v[12][1], v[12][2]}, {v[1][0], v[1][1], v[1][2]}}, + {{v[11][0], v[11][1], v[11][2]}, {v[12][0], v[12][1], v[12][2]}, {v[0][0], v[0][1], v[0][2]}}, + + {{v[11][0], v[11][1], v[11][2]}, {v[14][0], v[14][1], v[14][2]}, {v[12][0], v[12][1], v[12][2]}}, + {{v[13][0], v[13][1], v[13][2]}, {v[14][0], v[14][1], v[14][2]}, {v[11][0], v[11][1], v[11][2]}}, + + {{v[13][0], v[13][1], v[13][2]}, {v[16][0], v[16][1], v[16][2]}, {v[14][0], v[14][1], v[14][2]}}, + {{v[15][0], v[15][1], v[15][2]}, {v[16][0], v[16][1], v[16][2]}, {v[13][0], v[13][1], v[13][2]}}, + + {{v[15][0], v[15][1], v[15][2]}, {v[18][0], v[18][1], v[18][2]}, {v[16][0], v[16][1], v[16][2]}}, + {{v[17][0], v[17][1], v[17][2]}, {v[18][0], v[18][1], v[18][2]}, {v[15][0], v[15][1], v[15][2]}}, + + {{v[18][0], v[18][1], v[18][2]}, {v[17][0], v[17][1], v[17][2]}, {v[10][0], v[10][1], v[10][2]}}, + + // one side + {{v[11][0], v[11][1], v[11][2]}, {v[0][0], v[0][1], v[0][2]}, {v[2][0], v[2][1], v[2][2]}}, + + {{v[11][0], v[11][1], v[11][2]}, {v[2][0], v[2][1], v[2][2]}, {v[4][0], v[4][1], v[4][2]}}, + {{v[4][0], v[4][1], v[4][2]}, {v[13][0], v[13][1], v[13][2]}, {v[11][0], v[11][1], v[11][2]}}, + + {{v[13][0], v[13][1], v[13][2]}, {v[4][0], v[4][1], v[4][2]}, {v[6][0], v[6][1], v[6][2]}}, + {{v[6][0], v[6][1], v[6][2]}, {v[15][0], v[15][1], v[15][2]}, {v[13][0], v[13][1], v[13][2]}}, + + {{v[15][0], v[15][1], v[15][2]}, {v[6][0], v[6][1], v[6][2]}, {v[8][0], v[8][1], v[8][2]}}, + {{v[8][0], v[8][1], v[8][2]}, {v[17][0], v[17][1], v[17][2]}, {v[15][0], v[15][1], v[15][2]}}, + + {{v[17][0], v[17][1], v[17][2]}, {v[8][0], v[8][1], v[8][2]}, {v[10][0], v[10][1], v[10][2]}}, + + // other side + {{v[3][0], v[3][1], v[3][2]}, {v[1][0], v[1][1], v[1][2]}, {v[12][0], v[12][1], v[12][2]}}, + + {{v[3][0], v[3][1], v[3][2]}, {v[12][0], v[12][1], v[12][2]}, {v[14][0], v[14][1], v[14][2]}}, + {{v[14][0], v[14][1], v[14][2]}, {v[5][0], v[5][1], v[5][2]}, {v[3][0], v[3][1], v[3][2]}}, + + {{v[5][0], v[5][1], v[5][2]}, {v[14][0], v[14][1], v[14][2]}, {v[16][0], v[16][1], v[16][2]}}, + {{v[16][0], v[16][1], v[16][2]}, {v[7][0], v[7][1], v[7][2]}, {v[5][0], v[5][1], v[5][2]}}, + + {{v[7][0], v[7][1], v[7][2]}, {v[16][0], v[16][1], v[16][2]}, {v[18][0], v[18][1], v[18][2]}}, + {{v[18][0], v[18][1], v[18][2]}, {v[9][0], v[9][1], v[9][2]}, {v[7][0], v[7][1], v[7][2]}}, + + {{v[9][0], v[9][1], v[9][2]}, {v[18][0], v[18][1], v[18][2]}, {v[10][0], v[10][1], v[10][2]}} + + }; + + + // Normals defined so as to make each face of the brush a flat surface + const GLfloat norms[34][3][3] = { + // top + {{0.0f, 0.93f, -0.37f}, {0.0f, 0.93f, -0.37f}, {0.0f, 0.93f, -0.37f}}, + {{0.0f, 0.93f, -0.37f}, {0.0f, 0.93f, -0.37f}, {0.0f, 0.93f, -0.37f}}, + + {{0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}}, + {{0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}}, + + {{0.0f, 0.988f, 0.158f}, {0.0f, 0.988f, 0.158f}, {0.0f, 0.988f, 0.158f}}, + {{0.0f, 0.988f, 0.158f}, {0.0f, 0.988f, 0.158f}, {0.0f, 0.988f, 0.158f}}, + + {{0.0f, 0.999f, -0.0533f}, {0.0f, 0.999f, -0.0533f}, {0.0f, 0.999f, -0.0533f}}, + {{0.0f, 0.999f, -0.0533f}, {0.0f, 0.999f, -0.0533f}, {0.0f, 0.999f, -0.0533f}}, + + {{0.0f, 0.709f, 0.709f}, {0.0f, 0.709f, 0.709f}, {0.0f, 0.709f, 0.709f}}, + + // bottom + {{0.0f, -0.93f, -0.37f}, {0.0f, -0.93f, -0.37f}, {0.0f, -0.93f, -0.37f}}, + {{0.0f, -0.93f, -0.37f}, {0.0f, -0.93f, -0.37f}, {0.0f, -0.93f, -0.37f}}, + + {{0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}}, + {{0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, -1.0f, 0.0f}}, + + {{0.0f, -0.988f, 0.158f}, {0.0f, -0.988f, 0.158f}, {0.0f, -0.988f, 0.158f}}, + {{0.0f, -0.988f, 0.158f}, {0.0f, -0.988f, 0.158f}, {0.0f, -0.988f, 0.158f}}, + + {{0.0f, -0.999f, -0.0533f}, {0.0f, -0.999f, -0.0533f}, {0.0f, -0.999f, -0.0533f}}, + {{0.0f, -0.999f, -0.0533f}, {0.0f, -0.999f, -0.0533f}, {0.0f, -0.999f, -0.0533f}}, + + {{0.0f, -0.709f, 0.709f}, {0.0f, -0.709f, 0.709f}, {0.0f, -0.709f, 0.709f}}, + + // one side + {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}}, + + {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}}, + {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}}, + + {{0.848f, 0.0f, 0.530f}, {0.848f, 0.0f, 0.530f}, {0.848f, 0.0f, 0.530f}}, + {{0.848f, 0.0f, 0.530f}, {0.848f, 0.0f, 0.530f}, {0.848f, 0.0f, 0.530f}}, + + {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}}, + {{1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}}, + + {{0.709f, 0.0f, 0.709f}, {0.709f, 0.0f, 0.709f}, {0.709f, 0.0f, 0.709f}}, + + // other side + {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}}, + + {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}}, + {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}}, + + {{-0.848f, 0.0f, 0.530f}, {-0.848f, 0.0f, 0.530f}, {-0.848f, 0.0f, 0.530f}}, + {{-0.848f, 0.0f, 0.530f}, {-0.848f, 0.0f, 0.530f}, {-0.848f, 0.0f, 0.530f}}, + + {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}}, + {{-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}}, + + {{-0.709f, 0.0f, 0.709f}, {-0.709f, 0.0f, 0.709f}, {-0.709f, 0.0f, 0.709f}} + }; + + brushMesh_.SetVertices((float*)verts, 102); + brushMesh_.SetNormals((float*)norms, 102); + brushMesh_.UpdateGPUMemory(); +} + + +void QuickShapes::DrawBrush(const Matrix4 &modelMatrix, const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, const Color &color) +{ + if (brushMesh_.num_vertices() == 0) { + initBrush(); + } + defaultMaterial_.ambient_reflectance = color; + defaultMaterial_.diffuse_reflectance = color; + defaultMaterial_.surface_texture = emptyTex_; + defaultShader_.Draw(modelMatrix, viewMatrix, projectionMatrix, &brushMesh_, defaultMaterial_); +} + + + +// ---------------- + + +void QuickShapes::DrawLineSegment(const Matrix4 &modelMatrix, + const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, + const Color &color, + const Point3 &p1, + const Point3 &p2, + float radius) +{ + Matrix4 S = Matrix4::Scale(Vector3(radius, 0.5f*(p2-p1).Length(), radius)); + Vector3 y = (p2-p1).ToUnit(); + Vector3 z = Vector3(1,0,0).Cross(y).ToUnit(); + if (z == Vector3(0,0,0)) { + z = Vector3(0,0,1).Cross(y).ToUnit(); + } + Vector3 x = y.Cross(z); + Matrix4 R = Matrix4::FromRowMajorElements( + x[0], y[0], z[0], 0, + x[1], y[1], z[1], 0, + x[2], y[2], z[2], 0, + 0, 0, 0, 1 + ); + Matrix4 T = Matrix4::Translation(0.5 * Vector3(p1[0]+p2[0], p1[1]+p2[1], p1[2]+p2[2])); + + Matrix4 M = T * R * S; + + DrawCylinder(modelMatrix * M, viewMatrix, projectionMatrix, color); +} + + + +void QuickShapes::DrawLines(const Matrix4 &modelMatrix, + const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, + const Color &color, + const std::vector<Point3> &points, + LinesType ltype, + float radius) +{ + if (ltype == LinesType::LINES) { + for (size_t i=0; i<points.size(); i+=2) { + DrawLineSegment(modelMatrix, viewMatrix, projectionMatrix, color, points[i], points[i+1], radius); + } + } + else { + for (size_t i=0; i<points.size()-1; i++) { + DrawLineSegment(modelMatrix, viewMatrix, projectionMatrix, color, points[i], points[i+1], radius); + } + if (ltype == LinesType::LINE_LOOP) { + DrawLineSegment(modelMatrix, viewMatrix, projectionMatrix, color, points[points.size()-1], points[0], radius); + } + } +} + + + +void QuickShapes::DrawArrow(const Matrix4 &modelMatrix, + const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix, + const Color &color, + Point3 p, Vector3 dir, float radius) +{ + float d = dir.Length() - 8.0f*radius; + DrawLineSegment(modelMatrix, viewMatrix, projectionMatrix, color, p, p + d*dir.ToUnit(), radius); + + Matrix4 S = Matrix4::Scale(Vector3(radius*3.0f, radius*4.0f, radius*3.0f)); + Vector3 y = dir.ToUnit(); + Vector3 z = Vector3(1,0,0).Cross(y).ToUnit(); + if (z == Vector3(0,0,0)) { + z = Vector3(0,0,1).Cross(y).ToUnit(); + } + Vector3 x = y.Cross(z); + Matrix4 R = Matrix4::FromRowMajorElements( + x[0], y[0], z[0], 0, + x[1], y[1], z[1], 0, + x[2], y[2], z[2], 0, + 0, 0, 0, 1 + ); + Matrix4 T = Matrix4::Translation((p + d*dir.ToUnit()) - Point3::Origin()); + + Matrix4 M = T * R * S * Matrix4::Translation(Vector3(0,1,0)); + + DrawCone(modelMatrix * M, viewMatrix, projectionMatrix, color); +} + + +void QuickShapes::DrawAxes(const Matrix4 &modelMatrix, + const Matrix4 &viewMatrix, + const Matrix4 &projectionMatrix) +{ + DrawArrow(modelMatrix, viewMatrix, projectionMatrix, Color(1.0f, 0.6f, 0.6f), Point3::Origin(), Vector3::UnitX(), 0.02f); + DrawArrow(modelMatrix, viewMatrix, projectionMatrix, Color(0.6f, 1.0f, 0.6f), Point3::Origin(), Vector3::UnitY(), 0.02f); + DrawArrow(modelMatrix, viewMatrix, projectionMatrix, Color(0.6f, 0.6f, 1.0f), Point3::Origin(), Vector3::UnitZ(), 0.02f); + +} + + +void QuickShapes::initFull() { + GLfloat vertices[] = { + -1, -1, 0, 1, -1, 0, 1, 1, 0, + -1, -1, 0, 1, 1, 0, -1, 1, 0 + }; + + GLfloat normals[] = { + 0, 0, 1, 0, 0, 1, 0, 0, 1, + 0, 0, 1, 0, 0, 1, 0, 0, 1 + }; + + GLfloat texcoords[] = { + 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f + }; + + fullMesh_.SetVertices(vertices, 6); + fullMesh_.SetNormals(normals, 6); + fullMesh_.SetTexCoords(0, texcoords, 6); + fullMesh_.UpdateGPUMemory(); +} + + +void QuickShapes::DrawFullscreenTexture(const Color &color, const Texture2D &tex) { + if (fullMesh_.num_vertices() == 0) { + initFull(); + } + DrawWithFullscreen(color, &fullMesh_, tex); +} + + + + + +void QuickShapes::DrawWithFullscreen(const Color &color, Mesh *mesh, const Texture2D &tex) { + if (!fullscreenShader_.initialized()) { + fullscreenShader_.AddVertexShaderFromFile(Platform::FindMinGfxShaderFile("fullscreen.vert")); + fullscreenShader_.AddFragmentShaderFromFile(Platform::FindMinGfxShaderFile("fullscreen.frag")); + fullscreenShader_.LinkProgram(); + } + + glDisable(GL_DEPTH_TEST); + glDepthMask(GL_FALSE); + + // Activate the shader program + fullscreenShader_.UseProgram(); + + // Pass uniforms and textures from C++ to the GPU Shader Program + fullscreenShader_.SetUniform("TintColor", color); + fullscreenShader_.BindTexture("SurfaceTexture", tex); + + // Draw the mesh using the shader program + mesh->Draw(); + + // Deactivate the shader program + fullscreenShader_.StopProgram(); + + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); +} + + +DefaultShader* QuickShapes::default_shader() { + return &defaultShader_; +} + + +DefaultShader::MaterialProperties* QuickShapes::material() { + return &defaultMaterial_; +} + + +} // end namespace diff --git a/dev/MinGfx/src/ray.h b/dev/MinGfx/src/ray.h index 7374d86..d1b41b6 100644 --- a/dev/MinGfx/src/ray.h +++ b/dev/MinGfx/src/ray.h @@ -1,167 +1,166 @@ -/*
- This file is part of the MinGfx Project.
-
- Copyright (c) 2017,2018 Regents of the University of Minnesota.
- All Rights Reserved.
-
- Original Author(s) of this File:
- Dan Keefe, 2018, University of Minnesota
-
- Author(s) of Significant Updates/Modifications to the File:
- ...
- */
-
-#ifndef SRC_RAY_H_
-#define SRC_RAY_H_
-
-#include <iostream>
-
-#include "aabb.h"
-#include "point3.h"
-#include "vector3.h"
-#include "mesh.h"
-
-
-namespace mingfx {
-
-
-/** Stores the mathematical object of a ray that begins at an origin (a 3D
- point) and points in a direction (a unit 3D vector). Rays can intersect
- a variety of other computer graphics objects, such as planes, triangles,
- spheres, 3D meshes, etc. These intersections can be tested with the
- Intersect...() methods. The Ray can also be transformed by a Matrix4.
- Example:
- ~~~
- // Create a pick ray from the mouse position
- void MyGraphicsApp::OnLeftMouseDown(const Point2 &pos) {
- Point2 mouse_xy = PixelsToNormalizedDeviceCoords(pos);
- float mouse_z = ReadZValueAtPixel(pos);
- Point3 mouse_3d = GfxMath::ScreenToNearPlane(view_matrix, proj_matrix, mouse_xy, mouse_z);
- Matrix4 camera_matrix = view_matrix.Inverse();
- Point3 eye = camera_matrix.ColumnToPoint3(3);
-
- Ray pick_ray(eye, mouse_3d - eye);
-
- // check to see if the ray intersects a sphere
- float t;
- Point3 p;
- if (pick_ray.IntersectSphere(Point3(0,0,0), 2.0, &t, &p)) {
- std::cout << "Mouse pointing at sphere! Intersection point = " << p << std::endl;
- }
- }
- ~~~
- */
-class Ray {
-public:
-
- /// Defaults to a ray at the origin and pointing in the -Z direction
- Ray();
-
- /// Creates a ray from a 3D origin and direction
- Ray(const Point3 &origin, const Vector3 &direction);
-
- /// Ray destructor
- virtual ~Ray();
-
- /// Check for "equality", taking floating point imprecision into account
- bool operator==(const Ray& other) const;
-
- /// Check for "inequality", taking floating point imprecision into account
- bool operator!=(const Ray& other) const;
-
- /// Returns the length of the direction vector
- float Length() const;
-
- /** Checks to see if the ray intersects a plane defined by a point and a normal.
- If there was an intersection, true is returned, iTime is set to the intersection
- time, and iPoint is set to the intersection point. The plane is considered
- to be 1-sided. That is the intersection will only occur if the ray hits the
- plane from its front side as determined by the plane's normal.
- */
- bool IntersectPlane(const Point3 &planePt, const Vector3 &planeNormal,
- float *iTime, Point3 *iPoint) const;
-
- /** Checks to see if the ray intersects a triangle defined by the vertices v1, v2, and v3.
- The vertices must be provided in counter-clockwise order so that the normal of the
- triangle can be determined via the right-hand rule. The intersection will only happen
- if the ray hits the front side of the triangle. If there was an intersection,
- true is returned, iTime is set to the intersection time, and iPoint is set to the intersection point.
- */
- bool IntersectTriangle(const Point3 &v1, const Point3 &v2, const Point3 &v3,
- float *iTime, Point3 *iPoint) const;
-
- /** Checks to see if the ray intersects a quad defined by the vertices v1, v2, v3, and v4.
- The vertices must be provided in counter-clockwise order so that the normal of the
- triangle can be determined via the right-hand rule. The intersection will only happen
- if the ray hits the front side of the triangle. If there was an intersection,
- true is returned, iTime is set to the intersection time, and iPoint is set to the intersection point.
- */
- bool IntersectQuad(const Point3 &v1, const Point3 &v2, const Point3 &v3, const Point3 &v4,
- float *iTime, Point3 *iPoint) const;
-
- /** Checks to see if the ray intersects a sphere defined by a center point and a radius.
- If there was an intersection, true is returned, iTime is set to the intersection time,
- and iPoint is set to the intersection point.
- */
- bool IntersectSphere(const Point3 ¢er, float radius,
- float *iTime, Point3 *iPoint) const;
-
- /** Checks to see if the ray intersects a triangle mesh. This is a brute-force
- check over each triangle in the mesh. If there was an intersection, true is returned,
- iTime is set to the intersection time, iPoint is set to the intersection point,
- and iTriangleID is set to the ID of the closest intersected triangle along the ray.
- */
- bool IntersectMesh(const Mesh &mesh, float *iTime,
- Point3 *iPoint, int *iTriangleID) const;
-
- /** Checks to see if the ray intersects a triangle mesh. This uses a BVH
- (Bounding Volume Hierarchy) to accelerate the ray-triangle intersection tests.
- Each mesh can optionally store a BVH. If a BVH has already been calculated
- for the mesh (done with Mesh::CalculateBVH()), then this function will be
- much faster than the brute-force IntersectMesh() function. If a BVH has
- not already been calculated for the mesh, the first call to FastIntersectMesh()
- will trigger the mesh to create a BVH (not a fast operation) but then
- subsequent calls to FastIntersectMesh() will be fast.
- */
- bool FastIntersectMesh(Mesh *mesh, float *iTime,
- Point3 *iPoint, int *iTriangleID) const;
-
- /** Checks to see if the ray intersects an AABB (Axis-Aligned Bounding Box).
- Typically, this is the first step of a more detailed intersection test and
- we don't care about the actual point of intersection, just whether it
- intersects or not. So, we don't bother calculating the iPoint. We get the
- iTime for free though, so we do return that. You can calc the iPoint if
- you want using:
- ~~~
- float t;
- if (ray.IntersectAABB(box, &t)) {
- Point3 iPoint = ray.origin() + t*ray.direction();
- }
- ~~~
- */
- bool IntersectAABB(const AABB &box, float *iTime) const;
-
- /// Returns the origin
- Point3 origin() const;
-
- /// Returns the direction
- Vector3 direction() const;
-
- /// Sets a new origin and direction
- void set(Point3 newOrigin, Vector3 newDir);
-
-private:
- Point3 p_;
- Vector3 d_;
-};
-
-
-// --- Stream operators ---
-
-std::ostream & operator<< ( std::ostream &os, const Ray &r);
-std::istream & operator>> ( std::istream &is, Ray &r);
-
-
-} // end namespace
-
-#endif
+/* + This file is part of the MinGfx Project. + + Copyright (c) 2017,2018 Regents of the University of Minnesota. + All Rights Reserved. + + Original Author(s) of this File: + Dan Keefe, 2018, University of Minnesota + + Author(s) of Significant Updates/Modifications to the File: + ... + */ + +#ifndef SRC_RAY_H_ +#define SRC_RAY_H_ + +#include <iostream> + +#include "aabb.h" +#include "point3.h" +#include "vector3.h" +#include "mesh.h" + + +namespace mingfx { + + +/** Stores the mathematical object of a ray that begins at an origin (a 3D + point) and points in a direction (a unit 3D vector). Rays can intersect + a variety of other computer graphics objects, such as planes, triangles, + spheres, 3D meshes, etc. These intersections can be tested with the + Intersect...() methods. The Ray can also be transformed by a Matrix4. + Example: + ~~~ + // Create a pick ray from the mouse position + void MyGraphicsApp::OnLeftMouseDown(const Point2 &mouse_in_2d_pixels) { + Point2 mouse_in_2d_ndc = PixelsToNormalizedDeviceCoords(mouse_in_2d_pixels); + Point3 mouse_in_3d = GfxMath::ScreenToNearPlane(view_matrix, proj_matrix, mouse_in_2d_ndc); + Matrix4 camera_matrix = view_matrix.Inverse(); + Point3 eye = camera_matrix.ColumnToPoint3(3); + + Ray pick_ray(eye, mouse_in_3d - eye); + + // check to see if the ray intersects a sphere + float t; + Point3 p; + if (pick_ray.IntersectSphere(Point3(0,0,0), 2.0, &t, &p)) { + std::cout << "Mouse pointing at sphere! Intersection point = " << p << std::endl; + } + } + ~~~ + */ +class Ray { +public: + + /// Defaults to a ray at the origin and pointing in the -Z direction + Ray(); + + /// Creates a ray from a 3D origin and direction + Ray(const Point3 &origin, const Vector3 &direction); + + /// Ray destructor + virtual ~Ray(); + + /// Check for "equality", taking floating point imprecision into account + bool operator==(const Ray& other) const; + + /// Check for "inequality", taking floating point imprecision into account + bool operator!=(const Ray& other) const; + + /// Returns the length of the direction vector + float Length() const; + + /** Checks to see if the ray intersects a plane defined by a point and a normal. + If there was an intersection, true is returned, iTime is set to the intersection + time, and iPoint is set to the intersection point. The plane is considered + to be 1-sided. That is the intersection will only occur if the ray hits the + plane from its front side as determined by the plane's normal. + */ + bool IntersectPlane(const Point3 &planePt, const Vector3 &planeNormal, + float *iTime, Point3 *iPoint) const; + + /** Checks to see if the ray intersects a triangle defined by the vertices v1, v2, and v3. + The vertices must be provided in counter-clockwise order so that the normal of the + triangle can be determined via the right-hand rule. The intersection will only happen + if the ray hits the front side of the triangle. If there was an intersection, + true is returned, iTime is set to the intersection time, and iPoint is set to the intersection point. + */ + bool IntersectTriangle(const Point3 &v1, const Point3 &v2, const Point3 &v3, + float *iTime, Point3 *iPoint) const; + + /** Checks to see if the ray intersects a quad defined by the vertices v1, v2, v3, and v4. + The vertices must be provided in counter-clockwise order so that the normal of the + triangle can be determined via the right-hand rule. The intersection will only happen + if the ray hits the front side of the triangle. If there was an intersection, + true is returned, iTime is set to the intersection time, and iPoint is set to the intersection point. + */ + bool IntersectQuad(const Point3 &v1, const Point3 &v2, const Point3 &v3, const Point3 &v4, + float *iTime, Point3 *iPoint) const; + + /** Checks to see if the ray intersects a sphere defined by a center point and a radius. + If there was an intersection, true is returned, iTime is set to the intersection time, + and iPoint is set to the intersection point. + */ + bool IntersectSphere(const Point3 ¢er, float radius, + float *iTime, Point3 *iPoint) const; + + /** Checks to see if the ray intersects a triangle mesh. This is a brute-force + check over each triangle in the mesh. If there was an intersection, true is returned, + iTime is set to the intersection time, iPoint is set to the intersection point, + and iTriangleID is set to the ID of the closest intersected triangle along the ray. + */ + bool IntersectMesh(const Mesh &mesh, float *iTime, + Point3 *iPoint, int *iTriangleID) const; + + /** Checks to see if the ray intersects a triangle mesh. This uses a BVH + (Bounding Volume Hierarchy) to accelerate the ray-triangle intersection tests. + Each mesh can optionally store a BVH. If a BVH has already been calculated + for the mesh (done with Mesh::CalculateBVH()), then this function will be + much faster than the brute-force IntersectMesh() function. If a BVH has + not already been calculated for the mesh, the first call to FastIntersectMesh() + will trigger the mesh to create a BVH (not a fast operation) but then + subsequent calls to FastIntersectMesh() will be fast. + */ + bool FastIntersectMesh(Mesh *mesh, float *iTime, + Point3 *iPoint, int *iTriangleID) const; + + /** Checks to see if the ray intersects an AABB (Axis-Aligned Bounding Box). + Typically, this is the first step of a more detailed intersection test and + we don't care about the actual point of intersection, just whether it + intersects or not. So, we don't bother calculating the iPoint. We get the + iTime for free though, so we do return that. You can calc the iPoint if + you want using: + ~~~ + float t; + if (ray.IntersectAABB(box, &t)) { + Point3 iPoint = ray.origin() + t*ray.direction(); + } + ~~~ + */ + bool IntersectAABB(const AABB &box, float *iTime) const; + + /// Returns the origin + Point3 origin() const; + + /// Returns the direction + Vector3 direction() const; + + /// Sets a new origin and direction + void set(Point3 newOrigin, Vector3 newDir); + +private: + Point3 p_; + Vector3 d_; +}; + + +// --- Stream operators --- + +std::ostream & operator<< ( std::ostream &os, const Ray &r); +std::istream & operator>> ( std::istream &is, Ray &r); + + +} // end namespace + +#endif diff --git a/dev/a2-carsoccer/.gitignore b/dev/a2-carsoccer/.gitignore new file mode 100644 index 0000000..dd1a9a8 --- /dev/null +++ b/dev/a2-carsoccer/.gitignore @@ -0,0 +1,2 @@ +config.h +build diff --git a/dev/a2-carsoccer/CMakeLists.txt b/dev/a2-carsoccer/CMakeLists.txt new file mode 100644 index 0000000..0d67553 --- /dev/null +++ b/dev/a2-carsoccer/CMakeLists.txt @@ -0,0 +1,176 @@ +# Original Author(s) of this File: +# Daniel Keefe, 2017, University of Minnesota +# +# Author(s) of Significant Updates/Modifications to the File: +# ... + + + +# You are encouraged to copy this example, move it outside of the MinGfx directory, and use +# it as a starting point for your project. When you do this, you'll have to edit the +# following line as needed to point to the MinGfx install prefix used on your system. + +# !!!!!!!!!!!!! EDIT THE FOLLOWING LINE AS NEEDED !!!!!!!!!!!!! +list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../build/install ../..) + + + + + +#### BASIC PROJECT SETUP #### + +project(a2-carsoccer) + +# Using 3.9 to get a modern version of FindOpenGL.cmake +cmake_minimum_required (VERSION 3.9) + +# Dependencies that are auto-downloaded, built, and installed for you will go in the +# directory pointed to by the CMAKE_INSTALL_PREFIX. It defaults to a location inside +# the build directory. +if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OR "${CMAKE_INSTALL_PREFIX}" STREQUAL "") + set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "default install path" FORCE ) +endif() + +# Add to paths cmake uses to search for scripts, modules, and config packages +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_INSTALL_PREFIX}) +list(INSERT CMAKE_PREFIX_PATH 0 ${CMAKE_INSTALL_PREFIX}) + +include(MessageMacros) +h1("Building ${PROJECT_NAME}") +h2("Configuring paths") + +message(STATUS "Module path: ${CMAKE_MODULE_PATH}") +message(STATUS "Prefix path: ${CMAKE_PREFIX_PATH}") +message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}") + +set(DATA_DIR_BUILD ${CMAKE_CURRENT_SOURCE_DIR}/data) +set(DATA_DIR_INSTALL ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/data) + +message(STATUS "Data dir (in build tree): ${DATA_DIR_BUILD}") +message(STATUS "Data dir (in install tree): ${DATA_DIR_INSTALL}") + +# Configure a header file to pass some of the CMake settings to the source code +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in + ${CMAKE_CURRENT_SOURCE_DIR}/config.h +) + +#### SOURCE FOR THIS PROJECT #### +h2("Configuring source files") + +set(SOURCEFILES + ball.cc + car.cc + car_soccer.cc + main.cc +) + +set(HEADERFILES + ball.h + car_soccer.h + car.h + config.h +) + +set(EXTRAFILES + config.h.in + README.md +) + +set_source_files_properties(${EXTRAFILES} PROPERTIES HEADER_FILE_ONLY TRUE) + + + +#### COMPILE OPTIONS #### + +h2("Configuring Compiler Options") + + + +message(STATUS "Building for " ${CMAKE_SYSTEM_NAME} ".") + +# Linux specific +if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") + add_definitions(-DLINUX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +endif() + + +# Apple specific +if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + add_definitions(-DOSX) + + # RPATH settings, see https://cmake.org/Wiki/CMake_RPATH_handling + set(CMAKE_MACOSX_RPATH ON) + + # use, i.e. don't skip the full RPATH for the build tree + SET(CMAKE_SKIP_BUILD_RPATH FALSE) + + # when building, don't use the install RPATH already + # (but later on when installing) + SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) + + SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") + + # add the automatically determined parts of the RPATH + # which point to directories outside the build tree to the install RPATH + SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + + # the RPATH to be used when installing, but only if it's not a system directory + LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir) + IF("${isSystemDir}" STREQUAL "-1") + SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") + ENDIF("${isSystemDir}" STREQUAL "-1") + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +endif() + + +# Windows specific +if (WIN32) + add_definitions(-DWIN32) +endif() + + + + +#### DEFINE TARGET(S) #### + +h2("Defining Target(s)") + +add_executable(${PROJECT_NAME} ${SOURCEFILES} ${HEADERFILES} ${EXTRAFILES}) + + + +#### FIND AND ADD DEPENDENCIES #### + +h2("Adding Dependencies") +set(EXTERNAL_DIR external) + + +# MinGfx (linked with an imported cmake target so no need to specify include dirs) +# This will try to find MinGfxConfig.cmake, which should have been installed under +# CMAKE_INSTALL_PREFIX/lib/cmake/MinGfx when you installed the MinGfx Toolkit. +find_package(MinGfx REQUIRED) +target_link_libraries(${PROJECT_NAME} PUBLIC MinGfx::MinGfx) + + +# Add dependency on OpenGL +include(UseOpenGL) +UseOpenGL(${PROJECT_NAME} PUBLIC ${EXTERNAL_DIR}) + + + +#### INSTALL TARGET(S) #### + +h2("Configuring Install Target") + +# The install locations are relative to the CMAKE_INSTALL_PREFIX variable +install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin) + +install( + DIRECTORY data/ + DESTINATION ${DATA_DIR_INSTALL} + OPTIONAL +) diff --git a/dev/a2-carsoccer/README.md b/dev/a2-carsoccer/README.md new file mode 100644 index 0000000..9c30158 --- /dev/null +++ b/dev/a2-carsoccer/README.md @@ -0,0 +1 @@ +# My solution to Assignment 2 diff --git a/dev/a2-carsoccer/ball.cc b/dev/a2-carsoccer/ball.cc new file mode 100644 index 0000000..8289905 --- /dev/null +++ b/dev/a2-carsoccer/ball.cc @@ -0,0 +1,58 @@ +#include "ball.h" + +/// The constructor sets the radius and calls Reset() to start the ball at +/// the center of the field +Ball::Ball() : radius_(2.6f) { + Reset(); +} + +Ball::~Ball() { + +} + + +float Ball::radius() { + return radius_; +} + +Point3 Ball::position() { + return position_; +} + +void Ball::set_position(const Point3 &p) { + position_ = p; +} + +Vector3 Ball::velocity() { + return velocity_; +} + +void Ball::set_velocity(const Vector3 &v) { + velocity_ = v; +} + +void Ball::Reset() { + position_ = Point3(0, radius_, 0); + + float a = GfxMath::PI * rand()/RAND_MAX; + velocity_ = Vector3(25*cos(a), 10, 25*sin(a)); +} + +void Ball::Draw(QuickShapes quickShapes, Matrix4 modelMatrix, Matrix4 viewMatrix, Matrix4 projMatrix) { + Color ballcol(1,1,1); + Matrix4 Mball = + Matrix4::Translation(position_- Point3(0,0,0)) * + Matrix4::Scale(Vector3(radius_, radius_, radius_)); + quickShapes.DrawSphere(modelMatrix * Mball, viewMatrix, projMatrix, ballcol); + + // Draw the ball's shadow -- this is a bit of a hack, scaling Y by zero + // flattens the sphere into a pancake, which we then draw just a bit + // above the ground plane. + Color shadowcol(0.2f, 0.4f, 0.15f); + Matrix4 Mshadow = + Matrix4::Translation(Vector3(position_[0], -0.1f, position_[2])) * + Matrix4::Scale(Vector3(radius_, 0, radius_)) * + Matrix4::RotationX(90); + quickShapes.DrawSphere(modelMatrix * Mshadow, viewMatrix, projMatrix, shadowcol); +} + diff --git a/dev/a2-carsoccer/ball.h b/dev/a2-carsoccer/ball.h new file mode 100644 index 0000000..0fa107e --- /dev/null +++ b/dev/a2-carsoccer/ball.h @@ -0,0 +1,39 @@ +/** CSci-4611 Assignment 2: Car Soccer + */ + +#ifndef BALL_H_ +#define BALL_H_ + +#include <mingfx.h> +using namespace mingfx; + +/// Small class representing the ball. Feel free to add additional member variables and functions if you wish. +class Ball { +public: + + Ball(); + virtual ~Ball(); + + // The same radius is used to draw the ball and to calculate physics for the ball + float radius(); + + // Current 3D position + Point3 position(); + void set_position(const Point3 &p); + + // Current 3D velocity + Vector3 velocity(); + void set_velocity(const Vector3 &v); + + // Resets the ball's position and velocity to initial values. + void Reset(); + + void Draw(QuickShapes quickShapes, Matrix4 modelMatrix, Matrix4 viewMatrix, Matrix4 projMatrix); + +private: + Point3 position_; + Vector3 velocity_; + float radius_; +}; + +#endif diff --git a/dev/a2-carsoccer/car.cc b/dev/a2-carsoccer/car.cc new file mode 100644 index 0000000..3b9bb5c --- /dev/null +++ b/dev/a2-carsoccer/car.cc @@ -0,0 +1,66 @@ +#include "car.h" + +/// The constructor sets the static properties of the car, like its size, +/// and then calls Reset() to reset the position, velocity, and any other +/// dynamic variables that change during game play. +Car::Car() : size_(3,2,4), collision_radius_(2.5) { + Reset(); +} + +Car::~Car() { +} + + +float Car::collision_radius() { + return collision_radius_; + +} + +Vector3 Car::size() { + return size_; +} + +Point3 Car::position() { + return position_; +} + +void Car::set_position(const Point3 &p) { + position_ = p; +} + +Vector3 Car::forward() { + return forward_; +} + +void Car::set_forward(const Vector3 &v) { + forward_ = v; +} + +float Car::speed() { + return speed_; +} + +void Car::set_speed(float s) { + speed_ = s; +} + +Vector3 Car::velocity() { + return speed_ * forward_; +} + + +void Car::Reset() { + position_ = Point3(0, size_[1]/2, 45); + forward_ = Vector3(0, 0, -1); + speed_ = 0.0f; +} + +void Car::Draw(QuickShapes quickShapes, Matrix4 modelMatrix, Matrix4 viewMatrix, Matrix4 projMatrix) { + Color carcol(0.8f, 0.2f, 0.2f); + Matrix4 Mcar = + Matrix4::Translation(position_ - Point3(0,0,0)) * + Matrix4::LookAt(Point3(0,0,0), Point3(0,0,0) + forward_, Vector3(0,1,0)).Inverse() * + Matrix4::Scale(size_) * + Matrix4::Scale(Vector3(0.5f, 0.5f, 0.5f)); + quickShapes.DrawCube(modelMatrix * Mcar, viewMatrix, projMatrix, carcol); +} diff --git a/dev/a2-carsoccer/car.h b/dev/a2-carsoccer/car.h new file mode 100644 index 0000000..6746a56 --- /dev/null +++ b/dev/a2-carsoccer/car.h @@ -0,0 +1,52 @@ +/** CSci-4611 Assignment 2: Car Soccer + */ + +#ifndef CAR_H_ +#define CAR_H_ + +#include <mingfx.h> +using namespace mingfx; + +/// Small class representing the car. Feel free to add additional member variables and functions if you wish. +class Car { +public: + Car(); + virtual ~Car(); + + // Size is the size of the box drawn to represent the car in the X, Y, and Z dimensions + Vector3 size(); + + // Radius of the bounding sphere used to calculate approximate physics + float collision_radius(); + + // 3D position of the car + Point3 position(); + void set_position(const Point3 &p); + + // The unit vector direction the car is pointing. Since the car drives around on the + // Y=0 plane, this vector will always have a y coordinate = 0 + Vector3 forward(); + void set_forward(const Vector3 &v); + + // The speed of the car can be positive (moving forward) or negative (moving in reverse) + float speed(); + void set_speed(float s); + + // Current 3D velocity, computed as speed_ * forward_ + Vector3 velocity(); + + // Resets the position, speed, and forward direction to the car's starting point + void Reset(); + + // Draws a simple box shape for the car at the proper position and rotated to face forward + void Draw(QuickShapes quickShapes, Matrix4 modelMatrix, Matrix4 viewMatrix, Matrix4 projMatrix); + +private: + Vector3 size_; + float collision_radius_; + Point3 position_; + Vector3 forward_; + float speed_; +}; + +#endif diff --git a/dev/a2-carsoccer/car_soccer.cc b/dev/a2-carsoccer/car_soccer.cc new file mode 100644 index 0000000..c5619a8 --- /dev/null +++ b/dev/a2-carsoccer/car_soccer.cc @@ -0,0 +1,134 @@ +/** CSci-4611 Assignment 2: Car Soccer + */ + +#include "car_soccer.h" +#include "config.h" + + +// Remember in C++, the .h file list all the functions and member variables that are part of the class! +// Look there first to understand what is part of the CarSoccer class, then look below to see how each +// function is implemented. + + +CarSoccer::CarSoccer() : GraphicsApp(1024,768, "Car Soccer") { + // If you are having trouble driving the car with the keybaord, you can set this to true to use + // the mouse instead. The mouse controls are based on the postion of the mouse cursor on the window. + // There is a "dead zone" in the middle of the window, and if you move the mouse up/down or left/right + // outside of that zone, it is like pushing the up/down and/or left/right keys on the keyboard + use_mouse_ = false; + + // Define a search path for finding data files (images and shaders) + searchPath_.push_back("."); + searchPath_.push_back("./data"); + searchPath_.push_back(DATA_DIR_INSTALL); + searchPath_.push_back(DATA_DIR_BUILD); +} + +CarSoccer::~CarSoccer() { +} + + +void CarSoccer::OnMouseMove(const Point2& pos, const Vector2& delta) +{ + mouse_pos_ = PixelsToNormalizedDeviceCoords(pos); +} + +void CarSoccer::OnSpecialKeyDown(int key, int scancode, int modifiers) { + if (key == GLFW_KEY_SPACE) { + // Here's where you could call some form of launch_ball(); + ball_.Reset(); + } +} + +/// This is a little utility function that is helpful. It treats the arrow keys like a joystick or D-pad on a game controller +/// and returns the direction you are pressing as a 2D vector, taking into account the fact that you might be holding +/// down more than one key at a time. +Vector2 CarSoccer::joystick_direction() { + Vector2 dir; + + if (use_mouse_) { + // threshold defines the size of the "dead zone" in the middle of the screen + // if the mouse's x,y position falls outside of this, then it is like pushing + // the corresponding key on the keyboard + const float threshold = 0.2f; + dir[0] = 0; + if (mouse_pos_[0] < -threshold) { + dir[0] = -1; + } + else if (mouse_pos_[0] > threshold) { + dir[0] = 1; + } + dir[1] = 0; + if (mouse_pos_[1] < -threshold) { + dir[1] = -1; + } + else if (mouse_pos_[1] > threshold) { + dir[1] = 1; + } + } + else { + // the default user interface is to use the arrow keys on the keyboard. + // like a D-pad on a game controller, you can hold more than one key down at a time if you want. + if (IsKeyDown(GLFW_KEY_LEFT)) + dir[0]--; + if (IsKeyDown(GLFW_KEY_RIGHT)) + dir[0]++; + if (IsKeyDown(GLFW_KEY_UP)) + dir[1]++; + if (IsKeyDown(GLFW_KEY_DOWN)) + dir[1]--; + } + + return dir; +} + +// dt is for "Delta Time", the elapsed time in seconds since the last frame +void CarSoccer::UpdateSimulation(double dt) { + Vector2 dpad_dir = joystick_direction(); + std::cout << "D-Pad Direction: " << dpad_dir << std::endl; + + // Here's where you shound do your "simulation", updating the positions of the + // car and ball based on the elapsed time and checking for collisions. Filling + // in this routine is the main part of the assignment. + + // Example: This is not the "correct way" to drive the car, but this code + // will at least move the car around for testing + float metersPerSec = 10.0f; + car_.set_position(car_.position() + metersPerSec * Vector3(dpad_dir[0], 0, -dpad_dir[1]) * dt); + +} + + +void CarSoccer::InitOpenGL() { + // Set up the camera in a good position to see the entire field + projMatrix_ = Matrix4::Perspective(60, aspect_ratio(), 1, 1000); + modelMatrix_ = Matrix4::LookAt(Point3(0,60,70), Point3(0,0,10), Vector3(0,1,0)); + + // Set a background color for the screen (don't worry if you get a depricated warning on this line in OSX) + glClearColor(0.8f, 0.8f, 0.8f, 1.0f); + + // Load some image files we'll use + fieldTex_.InitFromFile(Platform::FindFile("pitch.png", searchPath_)); + crowdTex_.InitFromFile(Platform::FindFile("crowd.png", searchPath_)); +} + + +void CarSoccer::DrawUsingOpenGL() { + // Draw the crowd as a fullscreen background image + quickShapes_.DrawFullscreenTexture(Color(1,1,1), crowdTex_); + + // Draw the car and the ball + car_.Draw(quickShapes_, modelMatrix_, viewMatrix_, projMatrix_); + ball_.Draw(quickShapes_, modelMatrix_, viewMatrix_, projMatrix_); + + // Draw the field with the field texture on it. + Color col(16.0f/255.0f, 46.0f/255.0f, 9.0f/255.0f); + Matrix4 M = Matrix4::Translation(Vector3(0.0f, -0.201f, 0.0f)) * Matrix4::Scale(Vector3(50.0f, 1.0f, 60.0f)); + quickShapes_.DrawSquare(modelMatrix_ * M, viewMatrix_, projMatrix_, col); + M = Matrix4::Translation(Vector3(0.0f, -0.2f, 0.0f)) * Matrix4::Scale(Vector3(40.0f, 1.0f, 50.0f)); + quickShapes_.DrawSquare(modelMatrix_ * M, viewMatrix_, projMatrix_, Color(1,1,1), fieldTex_); + + // You should add drawing the goals and the boundary of the playing area + // using quickShapes_.DrawLines() + +} diff --git a/dev/a2-carsoccer/car_soccer.h b/dev/a2-carsoccer/car_soccer.h new file mode 100644 index 0000000..9982238 --- /dev/null +++ b/dev/a2-carsoccer/car_soccer.h @@ -0,0 +1,84 @@ +/** CSci-4611 Assignment 2: Car Soccer + */ + +#ifndef CAR_SOCCER_H_ +#define CAR_SOCCER_H_ + +#include <mingfx.h> +using namespace mingfx; + +#include "ball.h" +#include "car.h" + + +// The main class for the Car Soccer application +class CarSoccer : public GraphicsApp { +public: + CarSoccer(); + virtual ~CarSoccer(); + + /// Called whenever the mouse moves + void OnMouseMove(const Point2& pos, const Vector2& delta); + + /// This is called when special keys like SPACEBAR are pressed + void OnSpecialKeyDown(int key, int scancode, int modifiers); + + /// This is called once each frame. dt is "delta time", the time elapsed + /// since the last call. + void UpdateSimulation(double dt); + + /// This is called when it is time to initialize graphics objects, like + /// texture files. + void InitOpenGL(); + + /// This is called once each frame, and you should draw the scene inside + /// this function. + void DrawUsingOpenGL(); + + /// This is a little utility function that is helpful. It treats the + /// arrow keys like a joystick and returns the direction you are pressing + /// as a 2D vector, taking into account the fact that you might be holding + /// down more than one key at a time. + Vector2 joystick_direction(); + + // Feel free to add more functions here as needed. + + +private: + + // Simulation objects/parameters: + + // We suggest you start with the Car and Ball objects provided, adding new + // member variables to those classes if you need to. You'll probably want + // to store some other data for the simulation here too, like some value + // for gravity. + Car car_; + Ball ball_; + + + // Support for drawing some simple shapes: + QuickShapes quickShapes_; + + // Images to use as textures: + Texture2D fieldTex_; + Texture2D crowdTex_; + + // Control the computer graphics camera (we'll learn about this in a few weeks): + Matrix4 modelMatrix_; + Matrix4 viewMatrix_; + Matrix4 projMatrix_; + + // A list of paths to search for data files (images): + std::vector<std::string> searchPath_; + + // Set this to true if you want to use the mouse to control the car rather than the keyboard + bool use_mouse_; + + // Mouse position in Normalized Device Coordinates, + // meaning -1 to +1 in both X and Y. (0,0) is the + // center of the screen + Point2 mouse_pos_; +}; + + +#endif diff --git a/dev/a2-carsoccer/cmake/DownloadHelper.txt.in b/dev/a2-carsoccer/cmake/DownloadHelper.txt.in new file mode 100644 index 0000000..fb29bff --- /dev/null +++ b/dev/a2-carsoccer/cmake/DownloadHelper.txt.in @@ -0,0 +1,26 @@ +# This file is part of the MinGfx cmake build system. +# See the main MinGfx/CMakeLists.txt file for authors, copyright, and license info. + +# This is a "helper" cmake project -- the only thing this project does is download +# the external project. So, the configure, build, install, and test commands for +# ExternalProject_Add() are intentionally set as NOPs. + +cmake_minimum_required (VERSION 3.9) + +project(@EXT_PROJECT_NAME@-download) + +include(ExternalProject) +ExternalProject_Add( + @EXT_PROJECT_NAME@ + SOURCE_DIR "@DOWNLOAD_DIR@/@EXT_PROJECT_NAME@/src" + BINARY_DIR "@DOWNLOAD_DIR@/@EXT_PROJECT_NAME@/download-helper" + @DOWNLOAD_OPTIONS@ + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" + LOG_DOWNLOAD ON + GIT_PROGRESS 1 +) + + diff --git a/dev/a2-carsoccer/cmake/ExternalProjectDownloadBuildInstall.cmake b/dev/a2-carsoccer/cmake/ExternalProjectDownloadBuildInstall.cmake new file mode 100644 index 0000000..ce12d1d --- /dev/null +++ b/dev/a2-carsoccer/cmake/ExternalProjectDownloadBuildInstall.cmake @@ -0,0 +1,98 @@ +# This file is part of the MinGfx cmake build system. +# See the main MinGfx/CMakeLists.txt file for authors, copyright, and license info. + + +# Calling CMAKE_CURRENT_LIST_DIR inside a function returns the list dir of the calling script +# but we want the list dir of this file in order to find the DownloadHelper.txt.in file, which +# should be stored right next to this one. So, defining this variable outside the scope of the +# functions below. +set(DIR_OF_THIS_FILE ${CMAKE_CURRENT_LIST_DIR}) + + + +# Usage: +# ExternalProject_Download( +# # This first argument is the name of the project to download. It is required: +# glm +# +# # Additional arguments specify how to download the project using GIT, SVN, CVS, or URL. +# # These can be any of the arguments used for the downloading step of the cmake builtin +# # ExternalProject_Add command. +# GIT_REPOSITORY "https://github.com/g-truc/glm.git" +# GIT_TAG master +# etc.. +# ) +function(ExternalProject_Download EXT_PROJECT_NAME DOWNLOAD_DIR) + + include(MessageMacros) + h1("BEGIN EXTERNAL PROJECT DOWNLOAD (${EXT_PROJECT_NAME}).") + + h2("Creating a download helper project for ${EXT_PROJECT_NAME}.") + + set(DOWNLOAD_OPTIONS ${ARGN}) + string (REGEX REPLACE "(^|[^\\\\]);" "\\1 " DOWNLOAD_OPTIONS "${DOWNLOAD_OPTIONS}") + + + file(MAKE_DIRECTORY ${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}) + configure_file( + ${DIR_OF_THIS_FILE}/DownloadHelper.txt.in + ${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}/download-helper/CMakeLists.txt + ) + + h2("Generating build files for the ${EXT_PROJECT_NAME} download helper project.") + execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY "${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}/download-helper") + + h2("Building the ${EXT_PROJECT_NAME} download helper project. (This actually performs the download and may take some time...)") + execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}/download-helper") + + h2("Completed download of external project ${EXT_PROJECT_NAME}.") + +endfunction() + + +# Usage: +# ExternalProject_BuildAndInstallNow( +# # This first argument is the name of the external project to download. It is required: +# VRPN +# # This second argument is the relative path from ${EXTERNAL_DIR_NAME}/projectname/ to the project's +# # main CMakeLists.txt file: +# src +# +# # Additional arguments are passed on as options to the cmake build file generator +# -DVRPN_BUILD_DIRECTSHOW_VIDEO_SERVER=OFF +# -DVRPN_BUILD_HID_GUI=OFF +# etc.. +# ) +function(ExternalProject_BuildAndInstallNow EXT_PROJECT_NAME DOWNLOAD_DIR RELPATH_TO_CMAKELISTS) + + include(MessageMacros) + h1("BEGIN EXTERNAL PROJECT BUILD AND INSTALL (${EXT_PROJECT_NAME}).") + + # any extra args to the function are interpreted as arguments for the cmake config process + set(CMAKE_CONFIG_OPTIONS ${ARGN}) + + # always set the install prefix to be the same as for the main project + list(APPEND CMAKE_CONFIG_OPTIONS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}) + + #string (REGEX REPLACE "(^|[^\\\\]);" "\\1 " CMAKE_CONFIG_OPTIONS "${CMAKE_CONFIG_OPTIONS}") + + + set(SRC_DIR "${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}/${RELPATH_TO_CMAKELISTS}") + set(BUILD_DIR "${CMAKE_BINARY_DIR}/external/${EXT_PROJECT_NAME}") + + file(MAKE_DIRECTORY ${BUILD_DIR}) + + h2("Generating build files for external project ${EXT_PROJECT_NAME}.") + message(STATUS "Using source dir: ${SRC_DIR}") + message(STATUS "Using build dir: ${BUILD_DIR}") + message(STATUS "Config options: ${CMAKE_CONFIG_OPTIONS}") + + execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" ${SRC_DIR} ${CMAKE_CONFIG_OPTIONS} WORKING_DIRECTORY ${BUILD_DIR}) + + h2("Building external project ${EXT_PROJECT_NAME}. (This may take some time...)") + execute_process(COMMAND "${CMAKE_COMMAND}" --build ${BUILD_DIR} --target install) + + h2("Completed external build of ${EXT_PROJECT_NAME}.") + +endfunction() + diff --git a/dev/a2-carsoccer/cmake/MessageMacros.cmake b/dev/a2-carsoccer/cmake/MessageMacros.cmake new file mode 100644 index 0000000..4628e5c --- /dev/null +++ b/dev/a2-carsoccer/cmake/MessageMacros.cmake @@ -0,0 +1,17 @@ +# This file is part of the MinVR cmake build system. +# See the main MinVR/CMakeLists.txt file for authors, copyright, and license info. + + +macro(h1 TITLE) + string(TOUPPER ${TITLE} TITLE) + message(STATUS "\n\n==== ${TITLE} ====") +endmacro() + +macro(h2 TITLE) + message(STATUS "\n* ${TITLE}") +endmacro() + +macro(h3 TITLE) + message(STATUS "- ${TITLE}") +endmacro() + diff --git a/dev/a2-carsoccer/cmake/UseOpenGL.cmake b/dev/a2-carsoccer/cmake/UseOpenGL.cmake new file mode 100644 index 0000000..2ec5ffb --- /dev/null +++ b/dev/a2-carsoccer/cmake/UseOpenGL.cmake @@ -0,0 +1,52 @@ +# This file is part of the MinGfx cmake build system. +# See the main MinGfx/CMakeLists.txt file for authors, copyright, and license info. + +# Either finds a pre-installed version or complains. + +# Usage: In your CMakeLists.txt, somewhere after you define the target that depends +# on the OpenGL library (typical with something like add_executable(${PROJECT_NAME} ...) +# or add_library(${PROJECT_NAME} ...)), add the following two lines: + +# include(UseOpenGL) +# UseOpenGL(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external) + +# The second argument can be either PUBLIC, PRIVATE, or INTERFACE, following the keyword +# usage described here: +# https://cmake.org/cmake/help/latest/command/target_include_directories.html + +# The third argument is the directory to use for downloading the external project if +# autobuild is used. + + + +macro(UseOpenGL YOUR_TARGET INTERFACE_PUBLIC_OR_PRIVATE DOWNLOAD_DIR) + + message(STATUS "Searching for OpenGL...") + + # Check to see if the library is already installed on the system + # CMake ships with FindOpenGL.cmake and in CMake 3.9+ it defines + # the imported targets OpenGL::GL and OpenGL::GLU. Using these is + # now the preferred way to link with OpenGL and all of its dependencies. + # See https://cmake.org/cmake/help/v3.9/module/FindOpenGL.html + find_package(OpenGL) + + if (NOT ${OPENGL_FOUND}) + message(FATAL_ERROR "OpenGL was not found on the system. MinGfx can auto-download and build many dependencies for you, but not OpenGL. It should come pre-installed on your system.") + endif() + + message(STATUS "Ok: OpenGL Found.") + message(STATUS "OpenGL headers: ${OPENGL_INCLUDE_DIR}") + message(STATUS "OpenGL libs: ${OPENGL_LIBRARIES}") + + + message(STATUS "Linking target ${YOUR_TARGET} with ${INTERFACE_PUBLIC_OR_PRIVATE} dependency OpenGL::GL.") + target_link_libraries(${YOUR_TARGET} ${INTERFACE_PUBLIC_OR_PRIVATE} OpenGL::GL) + + if (${OPENGL_GLU_FOUND}) + message(STATUS "Linking target ${YOUR_TARGET} with ${INTERFACE_PUBLIC_OR_PRIVATE} dependency OpenGL::GLU.") + target_link_libraries(${YOUR_TARGET} ${INTERFACE_PUBLIC_OR_PRIVATE} OpenGL::GLU) + endif() + + target_compile_definitions(${YOUR_TARGET} ${INTERFACE_PUBLIC_OR_PRIVATE} -DUSE_OPENGL) + +endmacro() diff --git a/dev/a2-carsoccer/config.h.in b/dev/a2-carsoccer/config.h.in new file mode 100644 index 0000000..bf66666 --- /dev/null +++ b/dev/a2-carsoccer/config.h.in @@ -0,0 +1,13 @@ +/** CSci-4611 Assignment 2: Car Soccer +*/ + + +// The file config.h.in is processed by cmake to produce config.h. This +// replaces strings of the form "at"CMAKE_VARIABLE_NAME"at" with the value +// of the corresponding cmake variable, allowing us to pass directory paths +// and other information configured with cmake into our C++ code. + + +#define DATA_DIR_BUILD "@DATA_DIR_BUILD@" +#define DATA_DIR_INSTALL "@DATA_DIR_INSTALL@" + diff --git a/dev/a2-carsoccer/data/crowd.png b/dev/a2-carsoccer/data/crowd.png Binary files differnew file mode 100644 index 0000000..e6f14f3 --- /dev/null +++ b/dev/a2-carsoccer/data/crowd.png diff --git a/dev/a2-carsoccer/data/crowd.psd b/dev/a2-carsoccer/data/crowd.psd Binary files differnew file mode 100644 index 0000000..f16a761 --- /dev/null +++ b/dev/a2-carsoccer/data/crowd.psd diff --git a/dev/a2-carsoccer/data/pitch.png b/dev/a2-carsoccer/data/pitch.png Binary files differnew file mode 100644 index 0000000..bf36a3f --- /dev/null +++ b/dev/a2-carsoccer/data/pitch.png diff --git a/dev/a2-carsoccer/data/pitch.psd b/dev/a2-carsoccer/data/pitch.psd Binary files differnew file mode 100644 index 0000000..04970c8 --- /dev/null +++ b/dev/a2-carsoccer/data/pitch.psd diff --git a/dev/a2-carsoccer/main.cc b/dev/a2-carsoccer/main.cc new file mode 100644 index 0000000..cc6c086 --- /dev/null +++ b/dev/a2-carsoccer/main.cc @@ -0,0 +1,11 @@ +/** CSci-4611 Assignment 2: Car Soccer + */ + + +#include "car_soccer.h" + +int main(int argc, const char *argv[]) { + CarSoccer app; + app.Run(); + return 0; +} diff --git a/dev/texture-demo/.gitignore b/dev/texture-demo/.gitignore new file mode 100644 index 0000000..dd1a9a8 --- /dev/null +++ b/dev/texture-demo/.gitignore @@ -0,0 +1,2 @@ +config.h +build diff --git a/dev/texture-demo/CMakeLists.txt b/dev/texture-demo/CMakeLists.txt new file mode 100644 index 0000000..0c7fce9 --- /dev/null +++ b/dev/texture-demo/CMakeLists.txt @@ -0,0 +1,193 @@ +# Original Author(s) of this File: +# Daniel Keefe, 2017, University of Minnesota +# +# Author(s) of Significant Updates/Modifications to the File: +# ... + + + +# You are encouraged to copy this example, move it outside of the MinGfx directory, and use +# it as a starting point for your project. When you do this, you'll have to edit the +# following line as needed to point to the MinGfx install prefix used on your system. + +# !!!!!!!!!!!!! EDIT THE FOLLOWING LINE AS NEEDED !!!!!!!!!!!!! +list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../build/install ../..) + + +#### BASIC PROJECT SETUP #### + +project(example) + +# Using 3.9 to get a modern version of FindOpenGL.cmake +cmake_minimum_required (VERSION 3.9) + +# Dependencies that are auto-downloaded, built, and installed for you will go in the +# directory pointed to by the CMAKE_INSTALL_PREFIX. It defaults to a location inside +# the build directory. +if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OR "${CMAKE_INSTALL_PREFIX}" STREQUAL "") + set (CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "default install path" FORCE ) +endif() + +# Add to paths cmake uses to search for scripts, modules, and config packages +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_INSTALL_PREFIX}) +list(INSERT CMAKE_PREFIX_PATH 0 ${CMAKE_INSTALL_PREFIX}) + +include(MessageMacros) +h1("Building ${PROJECT_NAME}") +h2("Configuring paths") + +message(STATUS "Module path: ${CMAKE_MODULE_PATH}") +message(STATUS "Prefix path: ${CMAKE_PREFIX_PATH}") +message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}") + +set(DATA_DIR_BUILD ${CMAKE_CURRENT_SOURCE_DIR}/data) +set(DATA_DIR_INSTALL ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/data) + +message(STATUS "Data dir (in build tree): ${DATA_DIR_BUILD}") +message(STATUS "Data dir (in install tree): ${DATA_DIR_INSTALL}") + +set(SHADERS_DIR_BUILD ${CMAKE_CURRENT_SOURCE_DIR}/shaders) +set(SHADERS_DIR_INSTALL ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/shaders) + +message(STATUS "Shaders dir (in build tree): ${SHADERS_DIR_BUILD}") +message(STATUS "Shaders dir (in install tree): ${SHADERS_DIR_INSTALL}") + + +# Configure a header file to pass some of the CMake settings to the source code +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in + ${CMAKE_CURRENT_SOURCE_DIR}/config.h +) + + +#### SOURCE FOR THIS PROJECT #### +h2("Configuring source files") + +set(SOURCEFILES + example.cc + main.cc +) + +set(HEADERFILES + config.h + example.h +) + +set(EXTRAFILES + README.md +) + +set(SHADERFILES +) + +set_source_files_properties(${EXTRAFILES} PROPERTIES HEADER_FILE_ONLY TRUE) +set_source_files_properties(${SHADERFILES} PROPERTIES HEADER_FILE_ONLY TRUE) + +source_group("Shaders" FILES ${SHADERFILES}) + + +#### COMPILE OPTIONS #### + +h2("Configuring Compiler Options") + + + +message(STATUS "Building for " ${CMAKE_SYSTEM_NAME} ".") + +# Linux specific +if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") + add_definitions(-DLINUX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +endif() + + +# Apple specific +if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + add_definitions(-DOSX) + + # RPATH settings, see https://cmake.org/Wiki/CMake_RPATH_handling + set(CMAKE_MACOSX_RPATH ON) + + # use, i.e. don't skip the full RPATH for the build tree + SET(CMAKE_SKIP_BUILD_RPATH FALSE) + + # when building, don't use the install RPATH already + # (but later on when installing) + SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) + + SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") + + # add the automatically determined parts of the RPATH + # which point to directories outside the build tree to the install RPATH + SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + + # the RPATH to be used when installing, but only if it's not a system directory + LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir) + IF("${isSystemDir}" STREQUAL "-1") + SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") + ENDIF("${isSystemDir}" STREQUAL "-1") + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") +endif() + + +# Windows specific +if (WIN32) + add_definitions(-DWIN32) + + if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) + message(FATAL_ERROR + "You must use the 64 bit version of the compiler. Be sure to set the correct generator when configuring through CMake.") + endif() +endif() + + + + + +#### DEFINE TARGET(S) #### + +h2("Defining Target(s)") + +add_executable(${PROJECT_NAME} ${SOURCEFILES} ${HEADERFILES} ${EXTRAFILES} ${SHADERFILES}) + + + +#### FIND AND ADD DEPENDENCIES #### + +h2("Adding Dependencies") +set(EXTERNAL_DIR external) + + +# MinGfx (linked with an imported cmake target so no need to specify include dirs) +# This will try to find MinGfxConfig.cmake, which should have been installed under +# CMAKE_INSTALL_PREFIX/lib/cmake/MinGfx when you installed the MinGfx Toolkit. +find_package(MinGfx REQUIRED) +target_link_libraries(${PROJECT_NAME} PUBLIC MinGfx::MinGfx) + + +# Add dependency on OpenGL +include(UseOpenGL) +UseOpenGL(${PROJECT_NAME} PUBLIC ${EXTERNAL_DIR}) + + + +#### INSTALL TARGET(S) #### + +h2("Configuring Install Target") + +# The install locations are relative to the CMAKE_INSTALL_PREFIX variable +install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin) + +install( + DIRECTORY data/ + DESTINATION ${DATA_DIR_INSTALL} + OPTIONAL +) + +install( + DIRECTORY shaders/ + DESTINATION ${SHADERS_DIR_INSTALL} + OPTIONAL +) diff --git a/dev/texture-demo/README.md b/dev/texture-demo/README.md new file mode 100644 index 0000000..ee42984 --- /dev/null +++ b/dev/texture-demo/README.md @@ -0,0 +1 @@ +# Angry Vectors CSci-4611 In-Class Example of Visual Debugging diff --git a/dev/texture-demo/cmake/DownloadHelper.txt.in b/dev/texture-demo/cmake/DownloadHelper.txt.in new file mode 100644 index 0000000..fb29bff --- /dev/null +++ b/dev/texture-demo/cmake/DownloadHelper.txt.in @@ -0,0 +1,26 @@ +# This file is part of the MinGfx cmake build system. +# See the main MinGfx/CMakeLists.txt file for authors, copyright, and license info. + +# This is a "helper" cmake project -- the only thing this project does is download +# the external project. So, the configure, build, install, and test commands for +# ExternalProject_Add() are intentionally set as NOPs. + +cmake_minimum_required (VERSION 3.9) + +project(@EXT_PROJECT_NAME@-download) + +include(ExternalProject) +ExternalProject_Add( + @EXT_PROJECT_NAME@ + SOURCE_DIR "@DOWNLOAD_DIR@/@EXT_PROJECT_NAME@/src" + BINARY_DIR "@DOWNLOAD_DIR@/@EXT_PROJECT_NAME@/download-helper" + @DOWNLOAD_OPTIONS@ + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" + LOG_DOWNLOAD ON + GIT_PROGRESS 1 +) + + diff --git a/dev/texture-demo/cmake/ExternalProjectDownloadBuildInstall.cmake b/dev/texture-demo/cmake/ExternalProjectDownloadBuildInstall.cmake new file mode 100644 index 0000000..ce12d1d --- /dev/null +++ b/dev/texture-demo/cmake/ExternalProjectDownloadBuildInstall.cmake @@ -0,0 +1,98 @@ +# This file is part of the MinGfx cmake build system. +# See the main MinGfx/CMakeLists.txt file for authors, copyright, and license info. + + +# Calling CMAKE_CURRENT_LIST_DIR inside a function returns the list dir of the calling script +# but we want the list dir of this file in order to find the DownloadHelper.txt.in file, which +# should be stored right next to this one. So, defining this variable outside the scope of the +# functions below. +set(DIR_OF_THIS_FILE ${CMAKE_CURRENT_LIST_DIR}) + + + +# Usage: +# ExternalProject_Download( +# # This first argument is the name of the project to download. It is required: +# glm +# +# # Additional arguments specify how to download the project using GIT, SVN, CVS, or URL. +# # These can be any of the arguments used for the downloading step of the cmake builtin +# # ExternalProject_Add command. +# GIT_REPOSITORY "https://github.com/g-truc/glm.git" +# GIT_TAG master +# etc.. +# ) +function(ExternalProject_Download EXT_PROJECT_NAME DOWNLOAD_DIR) + + include(MessageMacros) + h1("BEGIN EXTERNAL PROJECT DOWNLOAD (${EXT_PROJECT_NAME}).") + + h2("Creating a download helper project for ${EXT_PROJECT_NAME}.") + + set(DOWNLOAD_OPTIONS ${ARGN}) + string (REGEX REPLACE "(^|[^\\\\]);" "\\1 " DOWNLOAD_OPTIONS "${DOWNLOAD_OPTIONS}") + + + file(MAKE_DIRECTORY ${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}) + configure_file( + ${DIR_OF_THIS_FILE}/DownloadHelper.txt.in + ${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}/download-helper/CMakeLists.txt + ) + + h2("Generating build files for the ${EXT_PROJECT_NAME} download helper project.") + execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY "${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}/download-helper") + + h2("Building the ${EXT_PROJECT_NAME} download helper project. (This actually performs the download and may take some time...)") + execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}/download-helper") + + h2("Completed download of external project ${EXT_PROJECT_NAME}.") + +endfunction() + + +# Usage: +# ExternalProject_BuildAndInstallNow( +# # This first argument is the name of the external project to download. It is required: +# VRPN +# # This second argument is the relative path from ${EXTERNAL_DIR_NAME}/projectname/ to the project's +# # main CMakeLists.txt file: +# src +# +# # Additional arguments are passed on as options to the cmake build file generator +# -DVRPN_BUILD_DIRECTSHOW_VIDEO_SERVER=OFF +# -DVRPN_BUILD_HID_GUI=OFF +# etc.. +# ) +function(ExternalProject_BuildAndInstallNow EXT_PROJECT_NAME DOWNLOAD_DIR RELPATH_TO_CMAKELISTS) + + include(MessageMacros) + h1("BEGIN EXTERNAL PROJECT BUILD AND INSTALL (${EXT_PROJECT_NAME}).") + + # any extra args to the function are interpreted as arguments for the cmake config process + set(CMAKE_CONFIG_OPTIONS ${ARGN}) + + # always set the install prefix to be the same as for the main project + list(APPEND CMAKE_CONFIG_OPTIONS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}) + + #string (REGEX REPLACE "(^|[^\\\\]);" "\\1 " CMAKE_CONFIG_OPTIONS "${CMAKE_CONFIG_OPTIONS}") + + + set(SRC_DIR "${DOWNLOAD_DIR}/${EXT_PROJECT_NAME}/${RELPATH_TO_CMAKELISTS}") + set(BUILD_DIR "${CMAKE_BINARY_DIR}/external/${EXT_PROJECT_NAME}") + + file(MAKE_DIRECTORY ${BUILD_DIR}) + + h2("Generating build files for external project ${EXT_PROJECT_NAME}.") + message(STATUS "Using source dir: ${SRC_DIR}") + message(STATUS "Using build dir: ${BUILD_DIR}") + message(STATUS "Config options: ${CMAKE_CONFIG_OPTIONS}") + + execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" ${SRC_DIR} ${CMAKE_CONFIG_OPTIONS} WORKING_DIRECTORY ${BUILD_DIR}) + + h2("Building external project ${EXT_PROJECT_NAME}. (This may take some time...)") + execute_process(COMMAND "${CMAKE_COMMAND}" --build ${BUILD_DIR} --target install) + + h2("Completed external build of ${EXT_PROJECT_NAME}.") + +endfunction() + diff --git a/dev/texture-demo/cmake/MessageMacros.cmake b/dev/texture-demo/cmake/MessageMacros.cmake new file mode 100644 index 0000000..4628e5c --- /dev/null +++ b/dev/texture-demo/cmake/MessageMacros.cmake @@ -0,0 +1,17 @@ +# This file is part of the MinVR cmake build system. +# See the main MinVR/CMakeLists.txt file for authors, copyright, and license info. + + +macro(h1 TITLE) + string(TOUPPER ${TITLE} TITLE) + message(STATUS "\n\n==== ${TITLE} ====") +endmacro() + +macro(h2 TITLE) + message(STATUS "\n* ${TITLE}") +endmacro() + +macro(h3 TITLE) + message(STATUS "- ${TITLE}") +endmacro() + diff --git a/dev/texture-demo/cmake/UseOpenGL.cmake b/dev/texture-demo/cmake/UseOpenGL.cmake new file mode 100644 index 0000000..2ec5ffb --- /dev/null +++ b/dev/texture-demo/cmake/UseOpenGL.cmake @@ -0,0 +1,52 @@ +# This file is part of the MinGfx cmake build system. +# See the main MinGfx/CMakeLists.txt file for authors, copyright, and license info. + +# Either finds a pre-installed version or complains. + +# Usage: In your CMakeLists.txt, somewhere after you define the target that depends +# on the OpenGL library (typical with something like add_executable(${PROJECT_NAME} ...) +# or add_library(${PROJECT_NAME} ...)), add the following two lines: + +# include(UseOpenGL) +# UseOpenGL(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/external) + +# The second argument can be either PUBLIC, PRIVATE, or INTERFACE, following the keyword +# usage described here: +# https://cmake.org/cmake/help/latest/command/target_include_directories.html + +# The third argument is the directory to use for downloading the external project if +# autobuild is used. + + + +macro(UseOpenGL YOUR_TARGET INTERFACE_PUBLIC_OR_PRIVATE DOWNLOAD_DIR) + + message(STATUS "Searching for OpenGL...") + + # Check to see if the library is already installed on the system + # CMake ships with FindOpenGL.cmake and in CMake 3.9+ it defines + # the imported targets OpenGL::GL and OpenGL::GLU. Using these is + # now the preferred way to link with OpenGL and all of its dependencies. + # See https://cmake.org/cmake/help/v3.9/module/FindOpenGL.html + find_package(OpenGL) + + if (NOT ${OPENGL_FOUND}) + message(FATAL_ERROR "OpenGL was not found on the system. MinGfx can auto-download and build many dependencies for you, but not OpenGL. It should come pre-installed on your system.") + endif() + + message(STATUS "Ok: OpenGL Found.") + message(STATUS "OpenGL headers: ${OPENGL_INCLUDE_DIR}") + message(STATUS "OpenGL libs: ${OPENGL_LIBRARIES}") + + + message(STATUS "Linking target ${YOUR_TARGET} with ${INTERFACE_PUBLIC_OR_PRIVATE} dependency OpenGL::GL.") + target_link_libraries(${YOUR_TARGET} ${INTERFACE_PUBLIC_OR_PRIVATE} OpenGL::GL) + + if (${OPENGL_GLU_FOUND}) + message(STATUS "Linking target ${YOUR_TARGET} with ${INTERFACE_PUBLIC_OR_PRIVATE} dependency OpenGL::GLU.") + target_link_libraries(${YOUR_TARGET} ${INTERFACE_PUBLIC_OR_PRIVATE} OpenGL::GLU) + endif() + + target_compile_definitions(${YOUR_TARGET} ${INTERFACE_PUBLIC_OR_PRIVATE} -DUSE_OPENGL) + +endmacro() diff --git a/dev/texture-demo/config.h.in b/dev/texture-demo/config.h.in new file mode 100644 index 0000000..16d824b --- /dev/null +++ b/dev/texture-demo/config.h.in @@ -0,0 +1,13 @@ +/** CSci-4611 In-Class Example +*/ + + +// The file config.h.in is processed by cmake to produce config.h. This +// replaces strings of the form "at"CMAKE_VARIABLE_NAME"at" with the value +// of the corresponding cmake variable, allowing us to pass directory paths +// and other information configured with cmake into our C++ code. + + +#define DATA_DIR_BUILD "@DATA_DIR_BUILD@" +#define DATA_DIR_INSTALL "@DATA_DIR_INSTALL@" + diff --git a/dev/texture-demo/data/campbells.png b/dev/texture-demo/data/campbells.png Binary files differnew file mode 100644 index 0000000..ed4b245 --- /dev/null +++ b/dev/texture-demo/data/campbells.png diff --git a/dev/texture-demo/data/monalisa.png b/dev/texture-demo/data/monalisa.png Binary files differnew file mode 100644 index 0000000..91e5498 --- /dev/null +++ b/dev/texture-demo/data/monalisa.png diff --git a/dev/texture-demo/example.cc b/dev/texture-demo/example.cc new file mode 100644 index 0000000..b114da5 --- /dev/null +++ b/dev/texture-demo/example.cc @@ -0,0 +1,113 @@ +/** CSci-4611 In-Class Example */ + +#include "example.h" + +#include "config.h" + +#include <iostream> +#include <sstream> + + + +Example::Example() : GraphicsApp(1024,768, "MinGfx Example") { + // Define a search path for finding data files (images and shaders) + search_path_.push_back("."); + search_path_.push_back("./data"); + search_path_.push_back(DATA_DIR_INSTALL); + search_path_.push_back(DATA_DIR_BUILD); +} + + +Example::~Example() { +} + + +void Example::UpdateSimulation(double dt) { +} + + +void Example::InitOpenGL() { + // Set up the camera in a good position to see the entire scene + proj_matrix_ = Matrix4::Perspective(60.0f, aspect_ratio(), 0.01f, 100.0f); + view_matrix_ = Matrix4::LookAt(Point3(2,1,4), Point3(2,1,0), Vector3(0,1,0)); + glClearColor(1,1,1,1); + + + // Load the texture we will use + texture_.InitFromFile(Platform::FindFile("monalisa.png", search_path_)); + + + // Create the mesh by setting the vertex and index arrays directly + std::vector<unsigned int> indices; + std::vector<Point3> vertices; + std::vector<Vector3> normals; + std::vector<Point2> tex_coords; + + // four vertices, each requires 3 floats: (x,y,z) + vertices.push_back(Point3(0,0,0)); + vertices.push_back(Point3(1,0,0)); + vertices.push_back(Point3(1,1,-1)); + vertices.push_back(Point3(0,1,-1)); + + // four normals, each requires 3 floats: (x,y,z) + normals.push_back(Vector3(0,1,1).ToUnit()); + normals.push_back(Vector3(0,1,1).ToUnit()); + normals.push_back(Vector3(0,1,1).ToUnit()); + normals.push_back(Vector3(0,1,1).ToUnit()); + + // TODO: YOU ADD TEXTURE COORDINATES TO THE MESH + + + + // indices into the arrays above for the first triangle + indices.push_back(0); + indices.push_back(1); + indices.push_back(2); + + // indices for the second triangle, note some are reused + indices.push_back(0); + indices.push_back(2); + indices.push_back(3); + + mesh_.SetVertices(vertices); + mesh_.SetNormals(normals); + mesh_.SetIndices(indices); + + // TODO: ALSO REMEMBER TO CALL mesh_.SetTexCoords(..) HERE ONCE YOU HAVE THEM DEFINED. + // USE TEXTURE UNIT = 0 SINCE WE HAVE ONLY ONE TEXTURE APPLIED TO THE MESH. + + mesh_.UpdateGPUMemory(); + +} + + +void Example::DrawUsingOpenGL() { + // draws a set of axes at the world origin, since we are passing the identity + // matrix for the "model" matrix. + Matrix4 identity; + quick_shapes_.DrawAxes(identity, view_matrix_, proj_matrix_); + + + // We're already learned about how to use transformation matrices to move + // an individual model around within the scene. + Matrix4 model_matrix = Matrix4::Scale(Vector3(4,4,4)); + + + // Since we want to texture the mesh we will define a custom material for the + // mesh. The material property we are interested in is called "surface_texture". + // We'll set that to point to the Texture2D that we loaded earlier. You can + // also set other properties of the material to define how it reflects light. + DefaultShader::MaterialProperties mesh_material; + mesh_material.surface_texture = texture_; + mesh_material.ambient_reflectance = Color(0.8f, 0.8f, 0.8f); + mesh_material.diffuse_reflectance = Color(0.8f, 0.8f, 0.8f); + + + // Use a shader program to draw the mesh with the given model, view, and projection + // matrices and with the material properties defined in mesh_material. + shader_.Draw(model_matrix, view_matrix_, proj_matrix_, &mesh_, mesh_material); +} + + + + diff --git a/dev/texture-demo/example.h b/dev/texture-demo/example.h new file mode 100644 index 0000000..afecff6 --- /dev/null +++ b/dev/texture-demo/example.h @@ -0,0 +1,65 @@ +/** CSci-4611 In-Class Example */ + +#ifndef SNOWMAN_H_ +#define SNOWMAN_H_ + +#include <mingfx.h> +using namespace mingfx; + +#include <string> +#include <vector> + +class Example : public GraphicsApp { +public: + + // Creates the App + Example(); + + // Cleans up when the App shuts down + virtual ~Example(); + + // Note a Run() function is inherited from GraphicsApp, that's what + // actually starts up the App. + + // This is a callback, a function that gets called when the user presses + // the Pause button in the GUI. + void OnPauseBtnPressed(); + + // This gets called once each frame. Note that dt (a.k.a., "delta time") is + // the amount of time (in seconds) that has passed since the last frame. + void UpdateSimulation(double dt); + + // This is where we initialize any OpenGL data, like textures or meshes that + // need to be loaded from files and setup in OpenGL. It gets called once + // when the program starts up. + void InitOpenGL(); + + // This gets called once each frame, and this is where you draw the latest + // version of your 3D graphics scene. + void DrawUsingOpenGL(); + + +private: + + // Texture loaded from a file + Texture2D texture_; + + // Triangle mesh data structure + Mesh mesh_; + + // A shader is required to draw meshes, MinGfx provides a default shader + // that does some simple lighting. + DefaultShader shader_; + + // Sets up the computer graphics camera + Matrix4 view_matrix_; + Matrix4 proj_matrix_; + + // A helper class for drawing some simple shapes (cubes, spheres, 3D arrows) + QuickShapes quick_shapes_; + + // Paths to search for data files, like images + std::vector<std::string> search_path_; +}; + +#endif
\ No newline at end of file diff --git a/dev/texture-demo/main.cc b/dev/texture-demo/main.cc new file mode 100644 index 0000000..3bbc8b8 --- /dev/null +++ b/dev/texture-demo/main.cc @@ -0,0 +1,9 @@ +/** CSci-4611 In-Class Example */ + +#include "example.h" + +int main(int argc, const char *argv[]) { + Example app; + app.Run(); + return 0; +} diff --git a/worksheets/a2_carsoccer.md b/worksheets/a2_carsoccer.md new file mode 100644 index 0000000..95170c4 --- /dev/null +++ b/worksheets/a2_carsoccer.md @@ -0,0 +1,156 @@ +# Assignment 2 (Car Soccer) Worksheet + +## Definitions + +Use the following C++ style pseudocode definitions for Q1 and Q2: + +``` +/* Use this Point3 class to store x,y,z values that define a mathematical + * point (i.e., a position) in 3-space. + */ +class Point3 { + float x; + float y; + float z; +}; + +/* Use this Vector3 class to store x,y,z values that define a vector in + * 3-space. Remember, mathematically, a vector is quite different than + * a point. It has a direction and a magnitude but no position! + * For vectors it is often useful to be able to compute the length, + * also known as the magnitude, of the vector. + */ +class Vector3 { + float x; + float y; + float z; + + // returns the length (i.e., magnitude) of the vector + float Length() { + return sqrt(x*x + y*y + z*z); + } +}; + + +/* In C++ and other languages we can define operators so we can use + * the +, -, =, *, / operations on custom classes. Like many graphics + * libraries, this is what MinGfx does to make it easy to work with + * points and vectors in code. For example, recall from class that + * if we have a point A (Coffman Union) and we add a vector (direction + * and magnitude) to this, we arrive at a new point B (e.g., Murphy Hall). + * Conceptually, a point + a vector = a new point. Mathematically, it + * does not make sense to add two points, but it does make sense to + * subtract two points. The "difference" between the Murphy and Coffman + * points is a vector that tells us the direction and magnitude we would + * need to walk from Coffman to get to Murphy. Here's how we can write + * that in code using Point3, Vector3, and operators like + and -. + * + * Point3 murphy = Point3(5, 8, 0); + * Point3 coffman = Point3(4, 6, 0); + * Vector3 toMurphy = murphy - coffman; + * + * // or, if we were given coffman and toMurphy we could find + * // the point "murphy" by starting at point "coffman" and adding + * // the vector "toMurphy". + * Point3 murphy2 = coffman + toMurhpy; + * + * The code that defines these opertors looks something like this: +*/ + +// a point + a vector = a new point +Point3 operator+(Point3 p, Vector3 v) { + return Point3(p.x + v.x, p.y + v.y, p.z + v.z); +} + +// a point - a point = a vector +// the dir and magnitude needed to go from point point B to point A +Vector3 operator-(Point3 A, Point3 B) { + return Vector3(A.x - B.x, A.y - B.y, A.z - B.z); +} + +// a vector * a scalar = a new vector with scaled magnitude +Vector3 operator*(Vector3 v, float s) { + return Vector3(v.x * s, v.y * s, v.z * s); +} + + + +/* Given all these tools, we can define additional classes for geometries + * that are useful in graphics. For example, we can represent a sphere + * using a Point3 for the position of the center point of the sphere and + * a float for the sphere's radius. + */ +class Sphere { + Point3 position; + float radius; +}; +``` + +## Q1: Eulerian Integration + +In computer graphics and animation, there are many forms of integration that +are used. For simple physics models like we have in Car Soccer, Eulerian +Integration is good enough. Eulerian Integration uses velocity and position +information from the current frame, and the elapsed time to produce a position +for the next frame. Write pseudocode for determining the position of the sphere in the +next frame: + +*Hint: think back to the motion equations from introductory physics. Or, look +around in the assignment handout.* + +``` +Vector3 velocity = Vector3(1.0, 1.0, 1.0); +float dt = 20; // milliseconds + +Sphere s = Sphere { + position: Point3(0.0, 0.0, 0.0), + radius: 5.0, +}; + +s.position = /* --- Fill in the next frame position computation here --- */ +``` + + + +## Q2: Sphere Intersection + +In this assignment, you will need to test intersections between spheres and +other objects. Using the information contained within each sphere class, +write pseudocode to determine whether or not two spheres are intersecting +(which you can use for car/ball intersections): + +``` +bool sphereIntersection(Sphere s1, Sphere s2) { + /* --- Fill in your sphere intersection code here --- */ + + +} +``` + +To check that your intersections work, try working through the math by hand for the +following two cases. You can write out the math on a scrap piece of paper. You do +not need to include that detail in this worksheet. But, do change the lines below where +it says "Fill in expected output" to indicate whether True or False would be returned: + +``` +Sphere s1 = Sphere { + position: Point3(0.0, 1.0, 0.0), + radius: 1.0, +}; + +Sphere s2 = Sphere { + position: Point3(3.0, 0.0, 0.0), + radius: 1.0, +}; + +Sphere s3 = Sphere { + position: Point3(1.0, 1.0, 0.0), + radius: 2.0, +}; + +print(sphereIntersection(s1, s2)); +/* --- Fill in expected output (True or False) --- */ + +print(sphereIntersection(s1, s3)); +/* --- Fill in expected output (True or False) --- */ +``` |