-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
208 lines (159 loc) · 5.01 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stdio.h>
#include <fstream>
#include <bx/math.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
SDL_Window* window = NULL;
const int WIDTH = 640;
const int HEIGHT = 480;
bgfx::ShaderHandle loadShader(const char* _name) {
char* data = new char[2048];
std::ifstream file;
size_t fileSize;
file.open(_name, std::ios::binary);
if(file.is_open()) {
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
file.read(data, fileSize);
file.close();
}
const bgfx::Memory* mem = bgfx::copy(data,fileSize+1);
delete [] data;
mem->data[mem->size-1] = '\0';
bgfx::ShaderHandle handle = bgfx::createShader(mem);
bgfx::setName(handle, _name);
return handle;
}
struct PosColorVertex {
float m_x;
float m_y;
float m_z;
uint32_t m_abgr;
static void init() {
ms_decl
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
};
static bgfx::VertexLayout ms_decl;
};
bgfx::VertexLayout PosColorVertex::ms_decl;
static PosColorVertex s_cubeVertices[] =
{
{ 0.5f, 0.5f, 0.0f, 0xff0000ff },
{ 0.5f, -0.5f, 0.0f, 0xff0000ff },
{ -0.5f, -0.5f, 0.0f, 0xff00ff00 },
{ -0.5f, 0.5f, 0.0f, 0xff00ff00 }
};
static const uint16_t s_cubeTriList[] =
{
0,1,3,
1,2,3
};
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
bgfx::ProgramHandle m_program;
int main ( int argc, char* args[] ) {
// Initialize SDL systems
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else {
//Create a window
window = SDL_CreateWindow( "BGFX Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL ) {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
}
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(window, &wmi)) {
return 1;
}
bgfx::PlatformData pd;
// and give the pointer to the window to pd
pd.ndt = wmi.info.x11.display;
pd.nwh = (void*)(uintptr_t)wmi.info.x11.window;
// Tell bgfx about the platform and window
bgfx::setPlatformData(pd);
// Render an empty frame
bgfx::renderFrame();
// Initialize bgfx
bgfx::init();
PosColorVertex::init();
m_vbh = bgfx::createVertexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices)),
PosColorVertex::ms_decl
);
m_ibh = bgfx::createIndexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_cubeTriList, sizeof(s_cubeTriList))
);
bgfx::ShaderHandle vsh = loadShader("v_simple.bin");
bgfx::ShaderHandle fsh = loadShader("f_simple.bin");
m_program = bgfx::createProgram(vsh,fsh, true);
// Reset window
bgfx::reset(WIDTH, HEIGHT, BGFX_RESET_VSYNC);
// Enable debug text.
bgfx::setDebug(BGFX_DEBUG_TEXT /*| BGFX_DEBUG_STATS*/);
// Set view rectangle for 0th view
bgfx::setViewRect(0, 0, 0, uint16_t(WIDTH), uint16_t(HEIGHT));
// Clear the view rect
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f, 0);
// Set empty primitive on screen
bgfx::touch(0);
// Poll for events and wait till user closes window
bool quit = false;
SDL_Event currentEvent;
while(!quit) {
while(SDL_PollEvent(¤tEvent) != 0) {
if(currentEvent.type == SDL_QUIT) {
quit = true;
}
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, 10.0f };
// Set view and projection matrix for view 0.
float view[16];
bx::mtxLookAt(view, eye, at);
float proj[16];
bx::mtxProj(proj,
60.0f,
float(WIDTH)/float(HEIGHT),
0.1f, 100.0f,
bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(0, view, proj);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0,
WIDTH,
HEIGHT);
bgfx::touch(0);
float mtx[16];
bx::mtxRotateY(mtx, 0.0f);
// position x,y,z
mtx[12] = 0.0f;
mtx[13] = 0.0f;
mtx[14] = 0.0f;
// Set model matrix for rendering.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
// Set render states.
bgfx::setState(BGFX_STATE_DEFAULT);
// Submit primitive for rendering to view 0.
bgfx::submit(0, m_program);
bgfx::frame();
}
}
bgfx::shutdown();
// Free up window
SDL_DestroyWindow(window);
// Shutdown SDL
SDL_Quit();
return 0;
}