-
Notifications
You must be signed in to change notification settings - Fork 1
/
SDL_3D.hpp
161 lines (111 loc) · 3.08 KB
/
SDL_3D.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// SDL_3D.hpp
// SDL_3D
//
// Created by Matt Parsons on 22/07/2018.
// Copyright © 2018 Matt Parsons. All rights reserved.
//
#ifndef SDL_3D_hpp
#define SDL_3D_hpp
#include <stdio.h>
// SDL2 Headers
#include <SDL2/SDL.h>
struct Col{
float r;
float g;
float b;
float a;
};
struct TDPoint{
float x;
float y;
float z;
};
struct TDTriangle{
int a;
int b;
int c;
Col colour;
};
struct TDQuad{
int a;
int b;
int c;
int d;
Col colour;
};
class TDMesh{
public:
~TDMesh();
TDPoint* localVertex; //Vertex array, vertices are in local coordinates
int vertices; //Vertex count
TDTriangle* triangle; //Triangle array
int triangles; //Triangle count
//modelling
void init(int,int); //initilise object with memory for vertices and triangles
int addVertex(float,float,float); // takes x,y,z and returns the vertex index
int addTriangle(int,int,int); // takes vertex indices and retruns the triangle index
void setTriangleColour(int,float,float,float,float); //sets the r,g,b,a value for triangle at index
void loadPly(char*); //file name of a stanford Ply model (little endian binary and ASCII only).
};
class TDCamera{
public:
TDPoint position; //Camera position in world coordinates
TDPoint angle; //Camera rotion in world coordinates
void locate(float,float,float); //Absolute
void move(float,float,float); //Relative
void orient(float,float,float); //Absolute
void rotate(float,float,float); //Relative
};
class TDObject{
public:
TDObject(TDMesh*);
~TDObject();
float x();
float y();
float z();
//User variables
float vx;
float vy;
float vz;
TDMesh* mesh;
TDPoint* vertex; //Vertex array, vertices in world coordinates
//These operate on the object vertices
void locate(float,float,float); //Absolute
void move(float,float,float); //Relative
void orient(float,float,float); //Absolute
void rotate(float,float,float); //Relative
void size(float,float,float); //Absolute *
void scale(float,float,float); //Relative *
private:
TDPoint position; //object position in world coordinates
TDPoint angle; //object rotion in world coordinates
TDPoint proportion; //object scale factor
};
class Rasterizer{
public:
float aspectRatio;
float focalLength;
TDCamera camera;
int backfaceCull=0;
Col clearCol;
void drawTQuad(TDPoint*,TDQuad*);
void drawTriangle(TDPoint*, TDTriangle*);
void drawPoint(TDPoint*);
void drawLine(TDPoint*,TDPoint*);
void drawObject(TDObject*);
void setDrawColour(int,int,int,int);
void setDisplay(SDL_Renderer*,int,int);
void adjustDisplay(int,int);
void clearDisplay();
void updateDisplay();
SDL_Point projection(TDPoint*);
private:
SDL_Renderer* renderer;
float hWidth;
float hHeight;
};
//Scene
class TDScene{
};
#endif /* SDL_3D_hpp */