Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sample.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // sample BGLView app from the Be Book
  2. #include <stdio.h>
  3. #include <Application.h>
  4. #include <Window.h>
  5. #include <GLView.h>
  6. class SampleGLView : public BGLView
  7. {
  8. public:
  9. SampleGLView(BRect frame, uint32 type);
  10. virtual void AttachedToWindow(void);
  11. virtual void FrameResized(float newWidth, float newHeight);
  12. virtual void ErrorCallback(GLenum which);
  13. void Render(void);
  14. private:
  15. void gInit(void);
  16. void gDraw(void);
  17. void gReshape(int width, int height);
  18. float width;
  19. float height;
  20. };
  21. class SampleGLWindow : public BWindow
  22. {
  23. public:
  24. SampleGLWindow(BRect frame, uint32 type);
  25. virtual bool QuitRequested() { be_app->PostMessage(B_QUIT_REQUESTED); return true; }
  26. private:
  27. SampleGLView *theView;
  28. };
  29. class SampleGLApp : public BApplication
  30. {
  31. public:
  32. SampleGLApp();
  33. private:
  34. SampleGLWindow *theWindow;
  35. };
  36. SampleGLApp::SampleGLApp()
  37. : BApplication("application/x-vnd.sample")
  38. {
  39. BRect windowRect;
  40. uint32 type = BGL_RGB|BGL_DOUBLE;
  41. windowRect.Set(50, 50, 350, 350);
  42. theWindow = new SampleGLWindow(windowRect, type);
  43. }
  44. SampleGLWindow::SampleGLWindow(BRect frame, uint32 type)
  45. : BWindow(frame, "OpenGL Test", B_TITLED_WINDOW, 0)
  46. {
  47. theView = new SampleGLView(Bounds(), type);
  48. AddChild(theView);
  49. Show();
  50. theView->Render();
  51. }
  52. SampleGLView::SampleGLView(BRect frame, uint32 type)
  53. : BGLView(frame, "SampleGLView", B_FOLLOW_ALL_SIDES, 0, type)
  54. {
  55. width = frame.right-frame.left;
  56. height = frame.bottom-frame.top;
  57. }
  58. void SampleGLView::AttachedToWindow(void)
  59. {
  60. LockGL();
  61. BGLView::AttachedToWindow();
  62. gInit();
  63. gReshape(width, height);
  64. UnlockGL();
  65. }
  66. void SampleGLView::FrameResized(float newWidth, float newHeight)
  67. {
  68. LockGL();
  69. BGLView::FrameResized(width, height);
  70. width = newWidth;
  71. height = newHeight;
  72. gReshape(width,height);
  73. UnlockGL();
  74. Render();
  75. }
  76. void SampleGLView::ErrorCallback(GLenum whichError)
  77. {
  78. // fprintf(stderr, "Unexpected error occured (%d):\\n", whichError);
  79. // fprintf(stderr, " %s\\n", gluErrorString(whichError));
  80. }
  81. // globals
  82. GLenum use_stipple_mode; // GL_TRUE to use dashed lines
  83. GLenum use_smooth_mode; // GL_TRUE to use anti-aliased lines
  84. GLint linesize; // Line width
  85. GLint pointsize; // Point diameter
  86. float pntA[3] = {
  87. -160.0, 0.0, 0.0
  88. };
  89. float pntB[3] = {
  90. -130.0, 0.0, 0.0
  91. };
  92. void SampleGLView::gInit(void)
  93. {
  94. glClearColor(0.0, 0.0, 0.0, 0.0);
  95. glLineStipple(1, 0xF0E0);
  96. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  97. use_stipple_mode = GL_FALSE;
  98. use_smooth_mode = GL_TRUE;
  99. linesize = 2;
  100. pointsize = 6;
  101. }
  102. void SampleGLView::gDraw(void)
  103. {
  104. GLint i;
  105. glClear(GL_COLOR_BUFFER_BIT);
  106. glLineWidth(linesize);
  107. /*
  108. if (use_stipple_mode) {
  109. glEnable(GL_LINE_STIPPLE);
  110. } else {
  111. glDisable(GL_LINE_STIPPLE);
  112. }
  113. */
  114. glDisable(GL_POINT_SMOOTH);
  115. glPushMatrix();
  116. glPointSize(pointsize); // Set size for point
  117. for (i = 0; i < 360; i += 5) {
  118. glRotatef(5.0, 0,0,1); // Rotate right 5 degrees
  119. if (use_smooth_mode) {
  120. glEnable(GL_LINE_SMOOTH);
  121. glEnable(GL_BLEND);
  122. } else {
  123. glDisable(GL_LINE_SMOOTH);
  124. glDisable(GL_BLEND);
  125. }
  126. glColor3f(1.0, 1.0, 0.0); // Set color for line
  127. glBegin(GL_LINE_STRIP); // And create the line
  128. glVertex3fv(pntA);
  129. glVertex3fv(pntB);
  130. glEnd();
  131. glDisable(GL_POINT_SMOOTH);
  132. glDisable(GL_BLEND);
  133. glColor3f(0.0, 1.0, 0.0); // Set color for point
  134. glBegin(GL_POINTS);
  135. glVertex3fv(pntA); // Draw point at one end
  136. glVertex3fv(pntB); // Draw point at other end
  137. glEnd();
  138. }
  139. glPopMatrix(); // Done with matrix
  140. }
  141. void SampleGLView::gReshape(int width, int height)
  142. {
  143. glViewport(0, 0, width, height);
  144. glMatrixMode(GL_PROJECTION);
  145. glLoadIdentity();
  146. glOrtho(-175, 175, -175, 175, -1, 1);
  147. glMatrixMode(GL_MODELVIEW);
  148. }
  149. void SampleGLView::Render(void)
  150. {
  151. LockGL();
  152. gDraw();
  153. SwapBuffers();
  154. UnlockGL();
  155. }
  156. int main(int argc, char *argv[])
  157. {
  158. SampleGLApp *app = new SampleGLApp;
  159. app->Run();
  160. delete app;
  161. return 0;
  162. }