jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame^] | 1 | // $Id: demo.cpp,v 1.1 1999/08/19 00:55:40 jtg Exp $ |
| 2 | |
| 3 | // Simple BeOS GLView demo |
| 4 | // Written by Brian Paul |
| 5 | // This file is in the public domain. |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <Application.h> |
| 11 | #include <Window.h> |
| 12 | #include <GLView.h> |
| 13 | |
| 14 | |
| 15 | class MyWindow : public BWindow |
| 16 | { |
| 17 | public: |
| 18 | MyWindow(BRect frame); |
| 19 | virtual bool QuitRequested(); |
| 20 | }; |
| 21 | |
| 22 | |
| 23 | MyWindow::MyWindow(BRect frame) |
| 24 | : BWindow(frame, "demo", B_TITLED_WINDOW, B_NOT_ZOOMABLE) |
| 25 | { |
| 26 | // no-op |
| 27 | } |
| 28 | |
| 29 | bool MyWindow::QuitRequested() |
| 30 | { |
| 31 | be_app->PostMessage(B_QUIT_REQUESTED); |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | class MyGL : public BGLView |
| 37 | { |
| 38 | public: |
| 39 | MyGL(BRect rect, char *name, ulong options); |
| 40 | |
| 41 | // virtual void AttachedToWindow(); |
| 42 | virtual void Draw(BRect updateRect); |
| 43 | virtual void Pulse(); |
| 44 | virtual void FrameResized(float w, float h); |
| 45 | private: |
| 46 | float mAngle; |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | MyGL::MyGL(BRect rect, char *name, ulong options) |
| 51 | : BGLView(rect, name, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP_BOTTOM, 0, options) |
| 52 | { |
| 53 | mAngle = 0.0; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | #if 0 |
| 58 | void MyGL::AttachedToWindow() |
| 59 | { |
| 60 | BGLView::AttachedToWindow(); |
| 61 | LockGL(); |
| 62 | glClearColor(.7, .7, 0, 0); |
| 63 | UnlockGL(); |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | |
| 68 | void MyGL::FrameResized(float w, float h) |
| 69 | { |
| 70 | BGLView::FrameResized(w, h); |
| 71 | |
| 72 | printf("FrameResized\n"); |
| 73 | LockGL(); |
| 74 | BGLView::FrameResized(w,h); |
| 75 | glViewport(0, 0, (int) (w + 1), (int) (h + 1)); |
| 76 | glMatrixMode(GL_PROJECTION); |
| 77 | glLoadIdentity(); |
| 78 | glFrustum(-1, 1, -1, 1, 10, 30); |
| 79 | glMatrixMode(GL_MODELVIEW); |
| 80 | glLoadIdentity(); |
| 81 | glTranslatef(0, 0, -18); |
| 82 | UnlockGL(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | void MyGL::Draw(BRect r) |
| 88 | { |
| 89 | printf("MyGL::Draw\n"); |
| 90 | BGLView::Draw(r); |
| 91 | LockGL(); |
| 92 | glClear(GL_COLOR_BUFFER_BIT); |
| 93 | glPushMatrix(); |
| 94 | glRotatef(mAngle, 0, 0, 1); |
| 95 | glColor3f(0, 0, 1); |
| 96 | glBegin(GL_POLYGON); |
| 97 | glVertex2f(-1, -1); |
| 98 | glVertex2f( 1, -1); |
| 99 | glVertex2f( 1, 1); |
| 100 | glVertex2f(-1, 1); |
| 101 | glEnd(); |
| 102 | SwapBuffers(); |
| 103 | UnlockGL(); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void MyGL::Pulse() |
| 108 | { |
| 109 | printf("pulse\n"); |
| 110 | BGLView::Pulse(); |
| 111 | mAngle += 1.0; |
| 112 | |
| 113 | LockGL(); |
| 114 | glClear(GL_COLOR_BUFFER_BIT); |
| 115 | glPushMatrix(); |
| 116 | glRotatef(mAngle, 0, 0, 1); |
| 117 | glColor3f(0, 0, 1); |
| 118 | glBegin(GL_POLYGON); |
| 119 | glVertex2f(-1, -1); |
| 120 | glVertex2f( 1, -1); |
| 121 | glVertex2f( 1, 1); |
| 122 | glVertex2f(-1, 1); |
| 123 | glEnd(); |
| 124 | SwapBuffers(); |
| 125 | UnlockGL(); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | |
| 130 | int main(int argc, char *argv[]) |
| 131 | { |
| 132 | BApplication *app = new BApplication("application/demo"); |
| 133 | |
| 134 | // make top-level window |
| 135 | int x = 500, y = 500; |
| 136 | int w = 400, h = 400; |
| 137 | MyWindow *win = new MyWindow(BRect(x, y, x + w, y + h)); |
| 138 | // win->Lock(); |
| 139 | // win->Unlock(); |
| 140 | win->Show(); |
| 141 | |
| 142 | // Make OpenGL view and put it in the window |
| 143 | MyGL *gl = new MyGL(BRect(5, 5, w-10, h-10), "GL", BGL_RGB | BGL_DOUBLE); |
| 144 | // MyGL *gl = new MyGL(BRect(5, 5, w-10, h-10), "GL", BGL_RGB ); |
| 145 | win->AddChild(gl); |
| 146 | |
| 147 | printf("calling app->Run\n"); |
| 148 | app->Run(); |
| 149 | |
| 150 | delete app; |
| 151 | |
| 152 | return 0; |
| 153 | } |