Implement ViewROI and base classes (#287)

* Implement ViewROI and base classes

* Clean up Orientable header

* Move tgl to tgl subdirectory, and use target_include_directories

* Move classes to submodules

* Fix some missed references

* Fix/match UpdateWorldData

* Renaming / removing MxTypes / refactoring

* Consistent naming for Matrix

* Adjust format action

* Add Vector3/Vector4 to Data vector

* Add TGL comment

* Add a comment about Matrix4Impl

* Add ROI comment

---------

Co-authored-by: Anonymous Maarten <anonymous.maarten@gmail.com>
Co-authored-by: Christian Semmler <mail@csemmler.com>
This commit is contained in:
Nathan M Gilbert
2023-11-19 09:38:07 -05:00
committed by GitHub
parent 17b0eeddb4
commit 7fc1f8019f
27 changed files with 1645 additions and 316 deletions

365
LEGO1/tgl/tgl.h Normal file
View File

@@ -0,0 +1,365 @@
#ifndef TGL_H
#define TGL_H
#ifdef _WIN32
#define NOMINMAX // to avoid conflict with STL
#include <d3d.h>
#include <ddraw.h>
#include <windows.h> // HWND
#endif /* _WIN32 */
#include "tglVector.h"
namespace Tgl
{
// ???
enum ColorModel {
Ramp,
RGB
};
// ???
enum ShadingModel {
Wireframe,
UnlitFlat,
Flat,
Gouraud,
Phong
};
// ?????
enum LightType {
Ambient,
Point,
Spot,
Directional,
ParallelPoint
};
// ???
enum ProjectionType {
Perspective,
Orthographic
};
enum TextureMappingMode {
Linear,
PerspectiveCorrect
};
struct PaletteEntry {
unsigned char m_red;
unsigned char m_green;
unsigned char m_blue;
};
#ifdef _WIN32
struct DeviceDirectDrawCreateData {
const GUID* m_driverGUID;
HWND m_hWnd; // ??? derive from m_pDirectDraw
IDirectDraw* m_pDirectDraw;
IDirectDrawSurface* m_pFrontBuffer; // ??? derive from m_pDirectDraw
IDirectDrawSurface* m_pBackBuffer;
IDirectDrawPalette* m_pPalette; // ??? derive from m_pDirectDraw
int m_isFullScreen; // ??? derive from m_pDirectDraw
};
struct DeviceDirect3DCreateData {
IDirect3D* m_pDirect3D;
IDirect3DDevice* m_pDirect3DDevice;
};
#else
struct DeviceDirectDrawCreateData {};
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Result (return value type)
enum Result {
Error = 0,
Success = 1
};
inline int Succeeded(Result result)
{
return (result == Success);
}
//////////////////////////////////////////////////////////////////////////////
//
// Forward declarations
class Renderer;
class Object;
class Device;
class View;
class Light;
class Camera;
class Group;
class Mesh;
class Texture;
//////////////////////////////////////////////////////////////////////////////
//
// Object
class Object {
public:
virtual ~Object() {}
// returns pointer to implementation data
virtual void* ImplementationDataPtr() = 0;
};
//////////////////////////////////////////////////////////////////////////////
//
// Renderer
// ??? for now until we figured out how an app should pass the Renderer around
Renderer* CreateRenderer();
class Renderer : public Object {
public:
virtual Device* CreateDevice(const DeviceDirectDrawCreateData&) = 0;
virtual Device* CreateDevice(const DeviceDirect3DCreateData&) = 0;
virtual View* CreateView(
const Device*,
const Camera*,
unsigned long x,
unsigned long y,
unsigned long width,
unsigned long height
) = 0;
virtual Camera* CreateCamera() = 0;
virtual Light* CreateLight(LightType, double r, double g, double b) = 0;
virtual Group* CreateGroup(const Group* pParent = 0) = 0;
// pTextureCoordinates is pointer to array of vertexCount elements
// (each element being two floats), or NULL
// pFaceData is faceCount tuples, each of format
// [vertex1index, ... vertexNindex], where N = vertexPerFaceCount
virtual Mesh* CreateMesh(
unsigned long vertexCount,
const float (*pVertices)[3],
const float (*pTextureCoordinates)[2],
unsigned long faceCount,
unsigned long vertexPerFaceCount,
unsigned long* pFaceData
) = 0;
// pTextureCoordinates is pointer to array of vertexCount elements
// (each element being two floats), or NULL
// pFaceData is:
// [face1VertexCount face1Vertex1index, ... face1VertexMindex
// face2VertexCount face2Vertex1index, ... face2VertexNindex
// ...
// 0]
virtual Mesh* CreateMesh(
unsigned long vertexCount,
const float (*pVertices)[3],
const float (*pTextureCoordinates)[2],
unsigned long* pFaceData
) = 0;
virtual Texture* CreateTexture(
int width,
int height,
int bitsPerTexel,
const void* pTexels,
int pTexelsArePersistent,
int paletteEntryCount,
const PaletteEntry* pEntries
) = 0;
virtual Texture* CreateTexture() = 0;
virtual Result SetTextureDefaultShadeCount(unsigned long) = 0;
virtual Result SetTextureDefaultColorCount(unsigned long) = 0;
};
//////////////////////////////////////////////////////////////////////////////
//
// Device
class Device : public Object {
public:
virtual unsigned long GetWidth() = 0;
virtual unsigned long GetHeight() = 0;
virtual Result SetColorModel(ColorModel) = 0;
virtual Result SetShadingModel(ShadingModel) = 0;
virtual Result SetShadeCount(unsigned long) = 0;
virtual Result SetDither(int) = 0;
virtual Result Update() = 0;
// ??? should this be handled by app ???
// ??? this needs to be called when the window on which the device is ...
// is being activated
virtual void HandleActivate(int bActivate) = 0;
// ??? this needs to be called when the window on which this device is based
// needs to be repainted
virtual void HandlePaint(void*) = 0;
#ifdef _DEBUG
virtual unsigned long GetDrawnTriangleCount() = 0;
#endif
};
//////////////////////////////////////////////////////////////////////////////
//
// View
class View : public Object {
public:
virtual Result Add(const Light*) = 0;
virtual Result Remove(const Light*) = 0;
virtual Result SetCamera(const Camera*) = 0;
virtual Result SetProjection(ProjectionType) = 0;
virtual Result SetFrustrum(double frontClippingDistance, double backClippingDistance, double degrees) = 0;
virtual Result SetBackgroundColor(double r, double g, double b) = 0;
virtual Result Clear() = 0;
virtual Result Render(const Group*) = 0;
// ??? needed for fine grain control when using DirectDraw/D3D ???
virtual Result ForceUpdate(unsigned long x, unsigned long y, unsigned long width, unsigned long height) = 0;
// ??? for now: used by Mesh Cost calculation
virtual Result TransformWorldToScreen(const double world[3], double screen[4]) = 0;
// Pick():
// x, y:
// view coordinates
//
// ppGroupsToPickFrom:
// array of (Group*) in any order
// Groups to pick from
//
// groupsToPickFromCount:
// size of ppGroupsToPickFrom
//
// rppPickedGroups:
// output parameter
// array of (Group*) representing a Group hierarchy
// top-down order (element 0 is root/scene)
// caller must deallocate array
// ref count of each element (Group*) has not been increased
// an element will be 0, if a corresponding Group was not found in ppGroupsToPickFrom
//
// rPickedGroupCount:
// output parameter
// size of rppPickedGroups
virtual Result Pick(
unsigned long x,
unsigned long y,
const Group** ppGroupsToPickFrom,
int groupsToPickFromCount,
const Group**& rppPickedGroups,
int& rPickedGroupCount
) = 0;
};
//////////////////////////////////////////////////////////////////////////////
//
// Camera
class Camera : public Object {
public:
#if 0
virtual Result SetPosition(const double[3]) = 0;
virtual Result SetOrientation(const double direction[3],
const double up[3]) = 0;
#endif
virtual Result SetTransformation(const FloatMatrix4&) = 0;
};
//////////////////////////////////////////////////////////////////////////////
//
// Light
class Light : public Object {
public:
#if 0
virtual Result SetPosition(const double[3]) = 0;
virtual Result SetOrientation(const double direction[3],
const double up[3]) = 0;
#endif
virtual Result SetTransformation(const FloatMatrix4&) = 0;
};
//////////////////////////////////////////////////////////////////////////////
//
// Group
class Group : public Object {
public:
#if 0
virtual Result SetPosition(const double[3]) = 0;
virtual Result SetOrientation(const double direction[3],
const double up[3]) = 0;
#endif
// TODO: The type was changed from `FloatMatrix` to `Matrix` to make code in UpdateWorldData match.
// However, this is unlikely to be correct and will have to be figured out at some point.
virtual Result SetTransformation(const Matrix4&) = 0;
// ??? not yet fully implemented
virtual Result SetColor(double r, double g, double b) = 0;
virtual Result SetTexture(const Texture*) = 0;
virtual Result Add(const Group*) = 0;
virtual Result Add(const Mesh*) = 0;
virtual Result Remove(const Group*) = 0;
virtual Result Remove(const Mesh*) = 0;
virtual Result RemoveAll() = 0;
// ??? for now: used by Mesh Cost calculation
virtual Result TransformLocalToWorld(const double local[3], double world[3]) = 0;
};
//////////////////////////////////////////////////////////////////////////////
//
// Mesh
class Mesh : public Object {
public:
// ??? also on Group
virtual Result SetColor(double r, double g, double b) = 0;
virtual Result SetTexture(const Texture*) = 0;
virtual Result SetTextureMappingMode(TextureMappingMode) = 0;
virtual Result SetShadingModel(ShadingModel) = 0;
#ifdef _DEBUG
virtual Result GetBoundingBox(float min[3], float max[3]) = 0;
virtual unsigned long GetFaceCount() = 0;
virtual unsigned long GetVertexCount() = 0;
#endif
};
//////////////////////////////////////////////////////////////////////////////
//
// Texture
class Texture : public Object {
public:
virtual Result SetTexels(
int width,
int height,
int bitsPerTexel,
const void* pTexels,
int pTexelsArePersistent
) = 0;
virtual Result SetPalette(int entryCount, const PaletteEntry* pEntries) = 0;
};
//////////////////////////////////////////////////////////////////////////////
} // namespace Tgl
#endif // TGL_H

277
LEGO1/tgl/tglvector.h Normal file
View File

@@ -0,0 +1,277 @@
#ifndef TGLVECTOR_H
#define TGLVECTOR_H
#include "math.h" // ??? sin() in RotateAroundY()
#include <stddef.h> // offsetof()
namespace Tgl
{
namespace Constant
{
const float Pi = 3.14159265358979323846;
};
inline float DegreesToRadians(float degrees)
{
return Constant::Pi * (degrees / 180.0);
}
inline float RadiansToDegrees(float radians)
{
return (radians / Constant::Pi) * 180.0;
}
//////////////////////////////////////////////////////////////////////////////
//
// Array<T, N>
template <class T, int N>
class Array {
public:
Array() {}
Array(const Array& rArray) { *this = rArray; }
~Array() {}
const T& operator[](int i) const { return m_elements[i]; };
T& operator[](int i) { return m_elements[i]; };
Array<T, N>& operator=(const Array<T, N>&);
void operator+=(const Array<T, N>&);
protected:
T m_elements[N];
};
//////////////////////////////////////////////////////////////////////////////
//
// Array<T, N> implementation
template <class T, int N>
inline Array<T, N>& Array<T, N>::operator=(const Array<T, N>& rArray)
{
int i;
for (i = 0; i < N; i++) {
m_elements[i] = rArray.m_elements[i];
}
return *this;
}
template <class T, int N>
inline void Array<T, N>::operator+=(const Array<T, N>& rArray)
{
int i;
for (i = 0; i < N; i++) {
m_elements[i] += rArray.m_elements[i];
}
}
//////////////////////////////////////////////////////////////////////////////
//
// FloatMatrix4
class FloatMatrix4 : public Array<Array<float, 4>, 4> {
public:
FloatMatrix4() {}
FloatMatrix4(const FloatMatrix4& rMatrix) { *this = rMatrix; }
FloatMatrix4(const FloatMatrix4&, const FloatMatrix4&);
void operator*=(const FloatMatrix4&);
};
//////////////////////////////////////////////////////////////////////////////
//
// FloatMatrix4 implementation
inline FloatMatrix4::FloatMatrix4(const FloatMatrix4& rMatrix1, const FloatMatrix4& rMatrix2)
{
for (int row = 0; row < 4; row++) {
for (int column = 0; column < 4; column++) {
float element = 0;
for (int i = 0; i < 4; i++) {
element += rMatrix1[row][i] * rMatrix2[i][column];
}
m_elements[row][column] = element;
}
}
}
inline void FloatMatrix4::operator*=(const FloatMatrix4& rMatrix)
{
FloatMatrix4 temp(*this, rMatrix);
// *this = FloatMatrix4(*this, rMatrix);
*this = temp;
}
//////////////////////////////////////////////////////////////////////////////
//
// Transformation matrices
class Translation : public FloatMatrix4 {
public:
Translation(const float[3]);
Translation(float x, float y, float z);
protected:
void Init(float x, float y, float z);
};
class Scale : public FloatMatrix4 {
public:
Scale(const float[3]);
Scale(float x, float y, float z);
Scale(float);
protected:
void Init(float x, float y, float z);
};
class RotationX : public FloatMatrix4 {
public:
RotationX(float radians);
};
class RotationY : public FloatMatrix4 {
public:
RotationY(float radians);
};
//////////////////////////////////////////////////////////////////////////////
//
// Transformation matrices implementation
inline Translation::Translation(const float vector[3])
{
Init(vector[0], vector[1], vector[2]);
}
inline Translation::Translation(float x, float y, float z)
{
Init(x, y, z);
}
inline void Translation::Init(float x, float y, float z)
{
m_elements[0][0] = 1;
m_elements[0][1] = 0;
m_elements[0][2] = 0;
m_elements[0][3] = 0;
m_elements[1][0] = 0;
m_elements[1][1] = 1;
m_elements[1][2] = 0;
m_elements[1][3] = 0;
m_elements[2][0] = 0;
m_elements[2][1] = 0;
m_elements[2][2] = 1;
m_elements[2][3] = 0;
m_elements[3][0] = x;
m_elements[3][1] = y;
m_elements[3][2] = z;
m_elements[3][3] = 1;
}
inline Scale::Scale(const float vector[3])
{
Init(vector[0], vector[1], vector[2]);
}
inline Scale::Scale(float x, float y, float z)
{
Init(x, y, z);
}
inline Scale::Scale(float scale)
{
Init(scale, scale, scale);
}
inline void Scale::Init(float x, float y, float z)
{
m_elements[0][0] = x;
m_elements[0][1] = 0;
m_elements[0][2] = 0;
m_elements[0][3] = 0;
m_elements[1][0] = 0;
m_elements[1][1] = y;
m_elements[1][2] = 0;
m_elements[1][3] = 0;
m_elements[2][0] = 0;
m_elements[2][1] = 0;
m_elements[2][2] = z;
m_elements[2][3] = 0;
m_elements[3][0] = 0;
m_elements[3][1] = 0;
m_elements[3][2] = 0;
m_elements[3][3] = 1;
}
inline RotationX::RotationX(float radians)
{
float cosRadians = cos(radians);
float sinRadians = sin(radians);
m_elements[0][0] = 1;
m_elements[0][1] = 0;
m_elements[0][2] = 0;
m_elements[0][3] = 0;
m_elements[1][0] = 0;
m_elements[1][1] = cosRadians;
m_elements[1][2] = -sinRadians;
m_elements[1][3] = 0;
m_elements[2][0] = 0;
m_elements[2][1] = sinRadians;
m_elements[2][2] = cosRadians;
m_elements[2][3] = 0;
m_elements[3][0] = 0;
m_elements[3][1] = 0;
m_elements[3][2] = 0;
m_elements[3][3] = 1;
}
inline RotationY::RotationY(float radians)
{
float cosRadians = cos(radians);
float sinRadians = sin(radians);
m_elements[0][0] = cosRadians;
m_elements[0][1] = 0;
m_elements[0][2] = sinRadians;
m_elements[0][3] = 0;
m_elements[1][0] = 0;
m_elements[1][1] = 1;
m_elements[1][2] = 0;
m_elements[1][3] = 0;
m_elements[2][0] = -sinRadians;
m_elements[2][1] = 0;
m_elements[2][2] = cosRadians;
m_elements[2][3] = 0;
m_elements[3][0] = 0;
m_elements[3][1] = 0;
m_elements[3][2] = 0;
m_elements[3][3] = 1;
}
//////////////////////////////////////////////////////////////////////////////
} // namespace Tgl
#endif // TLGVECTOR_H