/samples/3D/OpenGL/glEcereCamera.ec

https://github.com/redj/ecere-sdk · C · 155 lines · 129 code · 26 blank · 0 comment · 5 complexity · f32a2f0b304a43ec375b5a2b5f2ae0fb MD5 · raw file

  1. import "ecere"
  2. #include <GL/gl.h>
  3. class GLDemo : Window
  4. {
  5. displayDriver = "OpenGL";
  6. nativeDecorations = true;
  7. text = "GL+Ecere Camera Demo";
  8. background = black;
  9. hasClose = true;
  10. size = { 640, 480 };
  11. Camera camera { fixed, position = { 50, -50, -300 }, orientation = Euler { 25, 0, 0 }, fov = 53; };
  12. Light light { diffuse = white; specular = white; orientation = Euler { pitch = -45, yaw = 0 }; };
  13. Cube cube { };
  14. bool moving;
  15. Point lastMouse;
  16. Timer timer
  17. {
  18. this, 0.05, true;
  19. bool DelayExpired()
  20. {
  21. static Time lastTime = 0, time;
  22. time = GetTime();
  23. if(!lastTime) lastTime = time;
  24. UpdateObjects(time - lastTime);
  25. lastTime = time;
  26. Update(null);
  27. return true;
  28. }
  29. };
  30. void UpdateObjects(Time diffTime)
  31. {
  32. Quaternion orientation = cube.transform.orientation;
  33. orientation.RotateYawPitch((Degrees)diffTime * 0.5, (Degrees)diffTime * 0.5);
  34. orientation.RotateRoll((Degrees)diffTime * 0.5);
  35. cube.transform.orientation = orientation;
  36. cube.UpdateTransform();
  37. }
  38. bool OnLoadGraphics()
  39. {
  40. cube.Create(displaySystem);
  41. cube.transform.scaling = { 100, 100, 100 };
  42. cube.transform.position = { 0, 0, 0 };
  43. cube.transform.orientation = Euler { 50, 50 };
  44. cube.UpdateTransform();
  45. return true;
  46. }
  47. void OnResize(int w, int h)
  48. {
  49. camera.Setup(w, h, null);
  50. }
  51. void OnRedraw(Surface surface)
  52. {
  53. surface.Clear(depthBuffer);
  54. camera.Update();
  55. display.SetLight(0, light);
  56. display.fogDensity = 0;
  57. display.SetCamera(surface, camera);
  58. display.DrawObject(cube);
  59. glPushMatrix();
  60. glTranslated(
  61. -camera.cPosition.x,
  62. -camera.cPosition.y,
  63. -camera.cPosition.z);
  64. glDisable(GL_LIGHTING);
  65. glBegin(GL_LINES);
  66. glColor3f(1, 0, 0);
  67. glVertex3f(-300, 0, 0);
  68. glVertex3f(300, 0, 0);
  69. glColor3f(0, 1, 0);
  70. glVertex3f(0, -300, 0);
  71. glVertex3f(0, 300, 0);
  72. glColor3f(0, 0, 1);
  73. glVertex3f(0, 0, -300);
  74. glVertex3f(0, 0, 300);
  75. glEnd();
  76. glEnable(GL_LIGHTING);
  77. glPopMatrix();
  78. display.SetCamera(surface, null);
  79. }
  80. bool OnMouseMove(int x, int y, Modifiers mods)
  81. {
  82. if(moving)
  83. {
  84. int dx = (int)(x - lastMouse.x), dy = (int)(lastMouse.y - y);
  85. camera.RotateYaw(-dx, 0,0);
  86. camera.RotatePitch(dy, 0,0);
  87. Update(null);
  88. lastMouse.x = x;
  89. lastMouse.y = y;
  90. }
  91. return true;
  92. }
  93. bool OnLeftButtonDown(int x, int y, Modifiers mods)
  94. {
  95. if(!moving)
  96. {
  97. bool back;
  98. Capture();
  99. moving = true;
  100. lastMouse = { x, y };
  101. }
  102. return true;
  103. }
  104. bool OnLeftButtonUp(int x, int y, Modifiers mods)
  105. {
  106. if(moving)
  107. {
  108. ReleaseCapture();
  109. moving = false;
  110. }
  111. return true;
  112. }
  113. bool OnKeyHit(Key key, unichar ch)
  114. {
  115. switch(key)
  116. {
  117. case wheelUp:
  118. camera.Move({ 0,0,100 });
  119. Update(null);
  120. break;
  121. case wheelDown:
  122. camera.Move({ 0,0,-100 });
  123. Update(null);
  124. break;
  125. }
  126. return true;
  127. }
  128. }
  129. GLDemo glDemo { anchor = { horz = 160, vert = 120 } };