-
Notifications
You must be signed in to change notification settings - Fork 6
/
gfxprogram.cpp
187 lines (143 loc) · 5.07 KB
/
gfxprogram.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2010-2011 Christoph Kubisch
// This file is part of the "Luxinia Engine".
// See copyright notice in luxplatform.h
#include "../_project/project.hpp"
#include <luxgfx/luxgfx.h>
class GfxProgram : public Project
{
static const int PARAMSETS = 4;
enum Params{
PARAM_MATRICES,
PARAM_XCOLORPTR,
PARAM_YCOLORBUFFER,
PARAM_ZTEX,
PARAM_SHUFFLEFUNC,
PARAMS,
};
struct ParamSet{
lxgProgramParameterPTR params[PARAMS];
const void* datas[PARAMS];
lxCVector4 vecX;
lxCVector4 vecY;
lxgBuffer_t bufferX;
lxgBuffer_t bufferY;
lxgBufferPTR bufferYptr;
lxgTexture_t textureZ;
lxgTexturePTR textureZptr;
lxCMatrix44 matrices[2];
lxgSubroutineKey shuffle;
void init(lxgContextPTR ctx, RenderProgram &prog) {
lxgTexture_init(&textureZ, ctx);
int w = 1;
int h = 1;
lxgTexture_setup(&textureZ, LUXGL_TEXTURE_2D, LUXGFX_TEXTURECHANNEL_RGB, LUXGFX_TEXTUREDATA_UNORM8,w,h,0,0,LUXGFX_TEXTUREFLAG_AUTOMIPMAP);
size_t buffersize = w * h * sizeof(lxCVector3);
lxCVector3* buffer = new lxCVector3[w * h];
buffer[0].Set(0,0,1.0);
lxgTextureUpdate_t update = {{0,0,0},{0,0,0},{w,h,0}};
lxgTexture_readData(&textureZ,&update,0,GL_FLOAT,GL_RGB,buffer,buffersize);
delete[] buffer;
textureZptr = &textureZ;
lxGLErrorCheck();
vecX.Set(1.0f,0.0f,0.0f,0.0f);
vecY.Set(0.0f,1.0f,0.0f,0.0f);
lxgBuffer_init(&bufferX, ctx, LUXGL_STATIC_DRAW,sizeof(lxCVector4),&vecX);
lxgBuffer_residentNV(&bufferX,LUXGFX_ACCESS_READ);
lxgBuffer_init(&bufferY, ctx, LUXGL_STATIC_DRAW,sizeof(lxCVector4),&vecY);
bufferYptr = &bufferY;
lxMatrix44Identity(matrices[0]);
lxMatrix44Identity(matrices[1]);
params[PARAM_MATRICES] = prog.getParameter("worldMatrix");
params[PARAM_XCOLORPTR] = prog.getParameter("xcolor");
params[PARAM_YCOLORBUFFER] = prog.getParameter("ybuffer");
params[PARAM_ZTEX] = prog.getParameter("ztex");
params[PARAM_SHUFFLEFUNC] = prog.getParameter("shuffleFunc");
shuffle = prog.getSubRoutine("shuffle_xyz");
datas[PARAM_MATRICES] = matrices;
datas[PARAM_XCOLORPTR] = &bufferX.address;
datas[PARAM_YCOLORBUFFER] = &bufferYptr;
datas[PARAM_ZTEX] = &textureZptr;
datas[PARAM_SHUFFLEFUNC] = &shuffle;
}
};
private:
RenderHelper m_rh;
GeometryBox m_box;
KeyTracker m_keys;
GLFWwindow m_window;
lxgContext_t m_ctx;
RenderProgram m_prog;
ParamSet m_paramsets[PARAMSETS];
public:
GfxProgram()
: Project("gfxprogram","../../backend/test/")
{
}
void updateGeometry(int x, int y, int z)
{
m_box.update(x,y,z);
}
int onInit(GLFWwindow win, int argc, const char** argv) {
m_window = win;
updateGeometry(4,4,4);
lxBoundingBox_t bbox;
lxVector3Set(bbox.min,-2,-2,-2);
lxVector3Set(bbox.max, 2, 2, 2);
lxCVector3 up(0,1,0);
m_rh.init(win,up);
m_rh.cameraPerspective(&bbox, 30.0f);
m_rh.cameraOrtho(&bbox);
lxgContext_init(&m_ctx);
m_prog.init(&m_ctx);
m_prog.addStageProgram(LUXGFX_STAGE_VERTEX,FILENAME("resources/gfxprogram.vert").c_str(),NULL);
m_prog.addStageProgram(LUXGFX_STAGE_FRAGMENT,FILENAME("resources/gfxprogram.frag").c_str(),NULL);
m_prog.finish();
// TODO init parmasets
int i = 0;
for (int y = 0; y < 2; y++){
for (int x = 0; x < 2; x++,i++){
lxCVector3 pos(-2.0f + 4.0f * (float)x, -2.0f + 4.0f * (float)y, 0.0f);
m_paramsets[i].init(&m_ctx,m_prog);
lxMatrix44SetTranslation(m_paramsets[i].matrices[0],pos);
if (i == PARAMSETS-1){
lxCVector3 rot(0,45,0);
lxMatrix44FromEulerZYXdeg(m_paramsets[i].matrices[0],rot);
}
lxMatrix44Invert(m_paramsets[i].matrices[1],m_paramsets[i].matrices[0]);
lxMatrix44Transpose1(m_paramsets[i].matrices[1]);
}
}
m_paramsets[0].shuffle = m_prog.getSubRoutine("shuffle_zyx");
return 0;
}
void logic(int width, int height)
{
m_keys.update(m_window);
}
int onDraw(int width, int height) {
logic(width,height);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
lxCVector4 lightpos(0,0,0,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
m_rh.updateProjection(width,height);
m_rh.updateCamera();
m_rh.setCameraGL();
lxgContext_clearTextureState(&m_ctx);
lxgContext_clearProgramState(&m_ctx);
lxgContext_applyProgram(&m_ctx, &m_prog.m_program);
for (int i = 0; i < PARAMSETS; i++){
lxgContext_applyProgramParameters(&m_ctx,&m_prog.m_program, PARAMS, m_paramsets[i].params, m_paramsets[i].datas);
m_box.drawVA();
}
lxgContext_applyProgram(&m_ctx, NULL);
return 0;
}
};
static GfxProgram test;