world_test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "dd_filetomesh.h"
  2. #include "dd_world.h"
  3. #include <stdio.h>
  4. #include <GL/glew.h>
  5. #include "dd_world.h"
  6. #include "dd_object.h"
  7. /* test world
  8. */
  9. struct dd_world_test2 {
  10. struct dd_world parent;
  11. // contains a rotating object
  12. struct dd_object o;
  13. float rotation;
  14. };
  15. // constructor - destructor
  16. void dd_world_test2_create(struct dd_world_test2 *);
  17. void dd_world_test2_clean(struct dd_world_test2 *);
  18. /* update
  19. * keep rotating
  20. */
  21. void dd_world_test2_update(struct dd_world *w) {
  22. struct dd_world_test2 *world = (struct dd_world_test2 *) w;
  23. world->rotation -= 1;
  24. if (world->rotation < 0) {
  25. world->rotation += 360;
  26. }
  27. }
  28. /* draw
  29. * move back, rotate the world, then draw the object
  30. */
  31. void dd_world_test2_draw(struct dd_world *w) {
  32. struct dd_world_test2 *world = (struct dd_world_test2 *) w;
  33. // move and and rotate
  34. glPushMatrix();
  35. glTranslatef(0, 0, -5);
  36. glRotatef(world->rotation, 0, 1, 0);
  37. // draw object
  38. dd_object_draw(&world->o);
  39. // cancel world's transformations
  40. glPopMatrix();
  41. }
  42. // constructor
  43. void dd_world_test2_create(struct dd_world_test2 *w) {
  44. struct dd_world *parent = (struct dd_world *) w;
  45. // pass update and draw functions to world
  46. parent->update = dd_world_test2_update;
  47. parent->draw = dd_world_test2_draw;
  48. // create the object from "untitled.ply"
  49. dd_object_create(&w->o);
  50. dd_filetomesh(&w->o.p, "untitled.ply", 0, 0, DD_PLY);
  51. // initialise rotation
  52. w->rotation = 0;
  53. }
  54. void dd_world_test2_clean(struct dd_world_test2 *w) {
  55. }
  56. /* test world
  57. */
  58. struct dd_world_test {
  59. struct dd_world parent;
  60. // contains a rotating object
  61. struct dd_object o;
  62. float rotation;
  63. };
  64. // constructor - destructor
  65. void dd_world_test_create(struct dd_world_test *);
  66. void dd_world_test_clean(struct dd_world_test *);
  67. /* update
  68. * keep rotating
  69. */
  70. void dd_world_test_update(struct dd_world *w) {
  71. struct dd_world_test *world = (struct dd_world_test *) w;
  72. world->rotation += 0.5;
  73. if (world->rotation >= 360) {
  74. world->rotation -= 360;
  75. dd_world_change(sizeof(struct dd_world_test2), (void (*) (struct dd_world *)) dd_world_test2_create);
  76. }
  77. }
  78. /* draw
  79. * move back, rotate the world, then draw the object
  80. */
  81. void dd_world_test_draw(struct dd_world *w) {
  82. struct dd_world_test *world = (struct dd_world_test *) w;
  83. // move and and rotate
  84. glPushMatrix();
  85. glTranslatef(0, 0, -5);
  86. glRotatef(world->rotation, 0, 1, 0);
  87. // draw object
  88. dd_object_draw(&world->o);
  89. // cancel world's transformations
  90. glPopMatrix();
  91. }
  92. // constructor
  93. void dd_world_test_create(struct dd_world_test *w) {
  94. struct dd_world *parent = (struct dd_world *) w;
  95. // pass update and draw functions to world
  96. parent->update = dd_world_test_update;
  97. parent->draw = dd_world_test_draw;
  98. // create the object from "untitled.ply"
  99. dd_object_create(&w->o);
  100. dd_filetomesh(&w->o.p, "untitled.ply", 0, 0, DD_PLY);
  101. // initialise rotation
  102. w->rotation = 0;
  103. }
  104. void dd_world_test_clean(struct dd_world_test *w) {
  105. }
  106. // mark this as the default world
  107. void dd_world_init() {
  108. dd_world_change(sizeof(struct dd_world_test), (void (*) (struct dd_world *)) dd_world_test_create);
  109. }