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
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#ifndef _SOFTSHADEROOM_DEVICE_H_
#define _SOFTSHADEROOM_DEVICE_H_
#include "../math/math.h"
#include "../util/type.h"
#include "mem.h"
#include "limits.h"
#include "shader.h"
#include "rasterizer.h"
#include "vert.h"
#include "framebuffer.h"
typedef enum {
MATRIX_MODEL = 0,
MATRIX_VIEW = 1,
MATRIX_PROJECTION = 2,
} ssr_MatrixMode;
typedef struct {
int width, height;
bool dbuffer;/* double buffer? */
Color* target; /* screen target buffer */
} ssr_Config;
typedef enum {
PRIMITIVE_POINT,
PRIMITIVE_LINE,
PRIMITIVE_TRIANGLE,
} ssr_PrimitiveType;
typedef enum {
ENABLE_BACKFACECULL = 1,
ENABLE_DEPTHTEST = 1 << 1,
ENABLE_MULTISAMPLE = 1 << 2,
ENABLE_BLEND = 1 << 3,
ENABLE_WRITEDEPTH = 1 << 4,
ENABLE_STENCILTEST = 1 << 5,
} ssr_EnableMask;
typedef enum {
DEPTHFUNC_ALWAYS,
DEPTHFUNC_NEVER,
DEPTHFUNC_LESS,
DEPTHFUNC_EQUAL,
DEPTHFUNC_LEQUAL,
DEPTHFUNC_GREATER,
DEPTHFUNC_NOTEQUAL,
DEPTHFUNC_GEQUAL,
} ssr_DepthFunc;
typedef enum {
BLEND_ONE,
BLEND_ZERO,
BLEND_SRC_COLOR,
BLEND_ONE_MINUS_SRC_COLOR,
BLEND_DST_COLOR,
BLEND_ONE_MINUS_DST_COLOR,
BLEND_SRC_ALPHA,
BLEND_ONE_MINUS_SRC_ALPHA,
BLEND_DST_ALPHA,
BLEND_ONE_MINUS_DST_ALPHA,
} ssr_BlendFactor;
typedef enum {
STENCILFUNC_ALWAYS,
STENCILFUNC_NEVER,
STENCILFUNC_LESS,
STENCILFUNC_EQUAL,
STENCILFUNC_LEQUAL,
STENCILFUNC_GREATER,
STENCILFUNC_NOTEQUAL,
STENCILFUNC_GEQUAL,
} ssr_StencilFunc;
typedef enum {
STENCILOP_KEEP,
STENCILOP_ZERO,
STENCILOP_REPLACE,
STENCILOP_INCR,
STENCILOP_INCR_WRAP,
STENCILOP_DECR,
STENCILOP_DECR_WRAP,
STENCILOP_INVERT,
} ssr_StencilOp;
void ssr_init(ssr_Config* config);
float ssr_getaspect();
int ssr_getframebufferw();
int ssr_getframebufferh();
void ssr_matrixmode(ssr_MatrixMode mode);
void ssr_loadidentity();
void ssr_pushmatrix();
void ssr_popmatrix();
void ssr_lookat(Vec3* pos, Vec3* target, Vec3* up);
/*后乘,意味着这些操作会被率先应用到物体上,应该按照反向顺序调用这些函数*/
void ssr_translate(float x, float y, float z);
void ssr_rotate(float angle, float x, float y, float z);
void ssr_scale(float sx, float sy, float sz);
void ssr_multmatrix(Mat4* m);/*右乘矩阵*/
void ssr_loadmatrix(Mat4* m);
void ssr_frustum(float l, float r, float b, float t, float n, float f);
void ssr_perspective(float fov, float aspect, float n, float f);
void ssr_ortho(float l, float r, float b, float t, float n, float f);
void ssr_viewport(float l, float r, float b, float t);
void ssr_getm(Mat4* out);
void ssr_getmv(Mat4* out);
void ssr_getmvp(Mat4* out);
void ssr_enable(uint mask);
void ssr_disable(uint mask);
bool ssr_isenable(uint mask);
/* 绑定顶点数据 */
void ssr_bindvertices(Vertex* verts, int vert_count, uint* indices, int prim_count);
void ssr_unbindvertices();
void ssr_useprogram(Program* program);
void ssr_unuseprogram();
void ssr_setuniformmat4(uint idx, Mat4* src);
void ssr_setuniformvec4(uint idx, Vec4* src);
void ssr_setuniformvec3(uint idx, Vec3* src);
void ssr_setuniformvec2(uint idx, Vec2* src);
void ssr_setuniformtex(uint idx, Texture* tex);
void ssr_setuniformu(void* userdata);
void ssr_draw(ssr_PrimitiveType primitive);
void ssr_clearcolor(Color color);
void ssr_cleardepth();
void ssr_clearstencil(byte val);
/*put point onto screen*/
void ssr_putpoint(uint screenx, uint screeny, Color color);
void ssr_putpoint32(uint screenx, uint screeny, Color32* c32);
void ssr_present();
Color ssr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
bool ssr_testdepth(uint x, uint y, float depth);
void ssr_writedepth(uint x, uint y, float depth);
void ssr_setdepthfunc(ssr_DepthFunc func);
void ssr_blend(Color32* src, Color32* dst, Color32* out);
void ssrU_viewport(Vec2* p, Vec2* out);
void ssrU_blend(ssr_BlendFactor sfactor, ssr_BlendFactor dfactor, Color32* src, Color32* dst, Color* out);
void ssr_bindframebuffer(FrameBuffer* fbo);
void ssr_unbindframebuffer();
void ssr_setblendfunc(ssr_BlendFactor sfactor, ssr_BlendFactor dfactor);
void ssrU_blendscreencolor(uint x, uint y, Color32* src);
/*将颜色写入对应的屏幕位置*/
void ssrU_putpointwithblend(uint x, uint y, Color32* color, bool blend);
bool ssr_teststencil(uint x, uint y, bool pass_depth_test);
void ssr_writestencil(uint x, uint y, bool pass_depth_test, bool pass_stencil_test);
void ssr_setstencilfunc(ssr_StencilFunc func, byte ref, uint mask);
void ssr_setstencilop(ssr_StencilOp fail, ssr_StencilOp dpfail, ssr_StencilOp pass);
void ssr_setstencilmask(byte mask);
bool ssr_iswritesencil();
void ssr_blendandputpoint(int x, int y, bool blend);
#endif
|