2
0

main2.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Crown.h"
  24. #include "FPSSystem.h"
  25. using namespace crown;
  26. Renderer* r;
  27. IndexBufferId monkey_ib;
  28. VertexBufferId monkey_vb;
  29. IndexBufferId cube_ib;
  30. VertexBufferId cube_vb;
  31. IndexBufferId cone_ib;
  32. VertexBufferId cone_vb;
  33. GPUProgramId default_program;
  34. GPUProgramId texture_program;
  35. Camera* camera;
  36. FPSSystem* fps;
  37. TextureId grass_texture;
  38. TextureId lightmap_texture;
  39. IndexBufferId quad_ib;
  40. VertexBufferId quad_vb;
  41. UniformId u_albedo_0;
  42. UniformId u_lightmap_0;
  43. UniformId u_brightness;
  44. static float quad_vertices[] =
  45. {
  46. -1.0f, -1.0f, 0.0f,
  47. 0.0f, 0.0f, 1.0f,
  48. 1.0f, 1.0f, 1.0f, 1.0f,
  49. 0.0f, 0.0f,
  50. 1.0f, -1.0f, 0.0f,
  51. 0.0f, 0.0f, 1.0f,
  52. 1.0f, 1.0f, 1.0f, 1.0f,
  53. 1.0f, 0.0f,
  54. 1.0f, 1.0f, 0.0f,
  55. 0.0f, 0.0f, 1.0f,
  56. 1.0f, 1.0f, 1.0f, 1.0f,
  57. 1.0f, 1.0f,
  58. -1.0f, 1.0f, 0.0f,
  59. 0.0f, 0.0f, 1.0f,
  60. 1.0f, 1.0f, 1.0f, 1.0f,
  61. 0.0f, 1.0f
  62. };
  63. static uint16_t quad_indices[] =
  64. {
  65. 0, 1, 3,
  66. 1, 2, 3
  67. };
  68. static const char* default_vertex =
  69. "in vec4 a_position;"
  70. "in vec4 a_normal;"
  71. "in vec2 a_tex_coord0;"
  72. "in vec4 a_color;"
  73. "uniform mat4 u_model;"
  74. "uniform mat4 u_model_view_projection;"
  75. "varying out vec2 tex_coord0;"
  76. "varying out vec4 color;"
  77. "void main(void)"
  78. "{"
  79. " tex_coord0 = a_tex_coord0;"
  80. " color = a_color;"
  81. " gl_Position = u_model_view_projection * a_position;"
  82. "}";
  83. static const char* default_fragment =
  84. "void main(void)"
  85. "{"
  86. " gl_FragColor = vec4(1, 0, 0, 0);"
  87. "}";
  88. static const char* texture_fragment =
  89. "in vec2 tex_coord0;"
  90. "in vec4 color;"
  91. "uniform sampler2D u_albedo_0;"
  92. "uniform sampler2D u_lightmap_0;"
  93. "uniform float u_brightness;"
  94. "void main(void)"
  95. "{"
  96. " gl_FragColor = texture(u_albedo_0, tex_coord0);"
  97. "}";
  98. void draw(float dt)
  99. {
  100. Mat4 pose; pose.load_identity();
  101. fps->update(dt);
  102. fps->set_view_by_cursor();
  103. //-----------------------
  104. r->set_layer_view(0, camera->view_matrix());
  105. r->set_layer_projection(0, camera->projection_matrix());
  106. r->set_layer_viewport(0, 0, 0, 1000, 625);
  107. r->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
  108. r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CCW);
  109. r->commit(0);
  110. static uint64_t prim = STATE_PRIMITIVE_TRIANGLES;
  111. if (device()->keyboard()->key_pressed(KC_z))
  112. {
  113. prim = STATE_PRIMITIVE_TRIANGLES;
  114. }
  115. else if (device()->keyboard()->key_pressed(KC_x))
  116. {
  117. prim = STATE_PRIMITIVE_POINTS;
  118. }
  119. else if (device()->keyboard()->key_pressed(KC_c))
  120. {
  121. prim = STATE_PRIMITIVE_LINES;
  122. }
  123. static uint32_t filter = TEXTURE_FILTER_NEAREST;
  124. if (device()->keyboard()->key_pressed(KC_1))
  125. {
  126. filter = TEXTURE_FILTER_NEAREST;
  127. }
  128. else if (device()->keyboard()->key_pressed(KC_2))
  129. {
  130. filter = TEXTURE_FILTER_LINEAR;
  131. }
  132. else if (device()->keyboard()->key_pressed(KC_3))
  133. {
  134. filter = TEXTURE_FILTER_BILINEAR;
  135. }
  136. else if (device()->keyboard()->key_pressed(KC_4))
  137. {
  138. filter = TEXTURE_FILTER_TRILINEAR;
  139. }
  140. static float brightness = 1.0f;
  141. if (device()->keyboard()->key_pressed(KC_UP))
  142. {
  143. brightness += 0.01f;
  144. }
  145. else if (device()->keyboard()->key_pressed(KC_DOWN))
  146. {
  147. brightness += -0.01f;
  148. }
  149. if (brightness > 1.0f)
  150. brightness = 1.0f;
  151. //-----------------------
  152. // r->set_state(prim | STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CW);
  153. // r->set_vertex_buffer(monkey_vb);
  154. // r->set_index_buffer(monkey_ib);
  155. // r->set_program(default_program);
  156. // pose.set_translation(Vec3(-3, 0, -3));
  157. // r->set_pose(pose);
  158. // r->commit(0);
  159. //-----------------------
  160. r->set_state(prim | STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CW);
  161. r->set_vertex_buffer(cube_vb);
  162. r->set_index_buffer(cube_ib);
  163. r->set_program(texture_program);
  164. r->set_texture(0, u_albedo_0, grass_texture, filter | TEXTURE_WRAP_CLAMP_EDGE);
  165. r->set_texture(1, u_lightmap_0, lightmap_texture, filter | TEXTURE_WRAP_CLAMP_EDGE);
  166. r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);
  167. pose.set_translation(Vec3(0, 0, -3));
  168. r->set_pose(pose);
  169. r->commit(0);
  170. //-----------------------
  171. // r->set_state(prim | STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
  172. // r->set_vertex_buffer(cone_vb);
  173. // r->set_index_buffer(cone_ib);
  174. // r->set_program(default_program);
  175. // pose.set_translation(Vec3(3, 0, -3));
  176. // r->set_pose(pose);
  177. // r->commit(0);
  178. //-----------------------
  179. // r->set_state(prim | STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
  180. // r->set_vertex_buffer(quad_vb);
  181. // r->set_index_buffer(quad_ib);
  182. // r->set_program(texture_program);
  183. // r->set_texture(0, u_albedo_0, grass_texture, filter | TEXTURE_WRAP_CLAMP_EDGE);
  184. // r->set_texture(1, u_lightmap_0, lightmap_texture, filter | TEXTURE_WRAP_CLAMP_EDGE);
  185. // r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);
  186. // pose.set_translation(Vec3(0, 0, -1));
  187. // r->set_pose(pose);
  188. // r->commit(0);
  189. }
  190. int main(int argc, char** argv)
  191. {
  192. crown::init();
  193. Device* engine = device();
  194. engine->init(argc, argv);
  195. // Load resources
  196. ResourceManager* resman = engine->resource_manager();
  197. ResourceId texture = resman->load("texture", "ship");
  198. ResourceId lightmap = resman->load("texture", "lightmap");
  199. ResourceId monkey = resman->load("mesh", "monkey");
  200. ResourceId cube = resman->load("mesh", "ship");
  201. ResourceId cone = resman->load("mesh", "cone");
  202. resman->flush();
  203. TextureResource* texture_resource = (TextureResource*)resman->data(texture);
  204. TextureResource* lightmap_resource = (TextureResource*)resman->data(lightmap);
  205. MeshResource* monkey_resource = (MeshResource*)resman->data(monkey);
  206. MeshResource* cube_resource = (MeshResource*)resman->data(cube);
  207. MeshResource* cone_resource = (MeshResource*)resman->data(cone);
  208. Log::d("monkey format = %d", monkey_resource->vertex_format());
  209. Log::d("monkey vcount = %d", monkey_resource->num_vertices());
  210. Log::d("cube format = %d", cube_resource->vertex_format());
  211. Log::d("cube vcount = %d", cube_resource->num_vertices());
  212. Log::d("cone format = %d", cone_resource->vertex_format());
  213. Log::d("cone vcount = %d", cone_resource->num_vertices());
  214. // Create vb/ib
  215. r = engine->renderer();
  216. monkey_vb = r->create_vertex_buffer(monkey_resource->num_vertices(), monkey_resource->vertex_format(), monkey_resource->vertices());
  217. monkey_ib = r->create_index_buffer(monkey_resource->num_indices(), monkey_resource->indices());
  218. cube_vb = r->create_vertex_buffer(cube_resource->num_vertices(), cube_resource->vertex_format(), cube_resource->vertices());
  219. cube_ib = r->create_index_buffer(cube_resource->num_indices(), cube_resource->indices());
  220. cone_vb = r->create_vertex_buffer(cone_resource->num_vertices(), cone_resource->vertex_format(), cone_resource->vertices());
  221. cone_ib = r->create_index_buffer(cone_resource->num_indices(), cone_resource->indices());
  222. // Create texture
  223. grass_texture = r->create_texture(texture_resource->width(), texture_resource->height(), texture_resource->format(),
  224. texture_resource->data());
  225. lightmap_texture = r->create_texture(lightmap_resource->width(), lightmap_resource->height(), lightmap_resource->format(),
  226. lightmap_resource->data());
  227. quad_vb = r->create_vertex_buffer(4, VERTEX_P3_N3_C4_T2, quad_vertices);
  228. quad_ib = r->create_index_buffer(6, quad_indices);
  229. ShaderId default_vs = r->create_shader(SHADER_VERTEX, default_vertex);
  230. ShaderId default_fs = r->create_shader(SHADER_FRAGMENT, default_fragment);
  231. ShaderId texture_fs = r->create_shader(SHADER_FRAGMENT, texture_fragment);
  232. u_albedo_0 = r->create_uniform("u_albedo_0", UNIFORM_INTEGER_1, 1);
  233. u_lightmap_0 = r->create_uniform("u_lightmap_0", UNIFORM_INTEGER_1, 1);
  234. u_brightness = r->create_uniform("u_brightness", UNIFORM_FLOAT_1, 1);
  235. default_program = r->create_gpu_program(default_vs, default_fs);
  236. texture_program = r->create_gpu_program(default_vs, texture_fs);
  237. // Create camera
  238. TempAllocator2048 alloc;
  239. camera = CE_NEW(alloc, Camera)(Vec3(0, 0, 3), 90.0f, 16.0f/9.0f);
  240. fps = CE_NEW(alloc, FPSSystem)(camera, 3.0f, 2.5f);
  241. while (engine->is_running())
  242. {
  243. engine->frame(draw);
  244. }
  245. //resman->unload(mesh);
  246. resman->unload(texture);
  247. resman->unload(lightmap);
  248. resman->unload(monkey);
  249. resman->unload(cube);
  250. resman->unload(cone);
  251. r->destroy_index_buffer(monkey_ib);
  252. r->destroy_vertex_buffer(monkey_vb);
  253. r->destroy_index_buffer(cube_ib);
  254. r->destroy_vertex_buffer(cube_vb);
  255. r->destroy_index_buffer(cone_ib);
  256. r->destroy_vertex_buffer(cone_vb);
  257. r->destroy_shader(default_vs);
  258. r->destroy_shader(default_fs);
  259. r->destroy_shader(texture_fs);
  260. r->destroy_gpu_program(default_program);
  261. r->destroy_gpu_program(texture_program);
  262. r->destroy_vertex_buffer(quad_vb);
  263. r->destroy_index_buffer(quad_ib);
  264. r->destroy_uniform(u_albedo_0);
  265. r->destroy_uniform(u_lightmap_0);
  266. r->destroy_uniform(u_brightness);
  267. engine->shutdown();
  268. crown::shutdown();
  269. }