1 // sample BGLView app from the Be Book, modified to stress direct mode support. 2 3 4 #include <stdio.h> 5 6 #include <Application.h> 7 #include <DirectWindow.h> 8 #include <CheckBox.h> 9 #include <GLView.h> 10 #include <GL/glu.h> 11 12 #define REDRAW_MSG 'rdrw' 13 14 class SampleGLView : public BGLView 15 { 16 public: 17 SampleGLView(BRect frame, uint32 type); 18 virtual void AttachedToWindow(void); 19 virtual void FrameResized(float newWidth, float newHeight); 20 virtual void MessageReceived(BMessage * msg); 21 22 void Render(void); 23 24 private: 25 void gInit(void); 26 void gDraw(float rotation = 0); 27 void gReshape(int width, int height); 28 29 float width; 30 float height; 31 float rotate; 32 }; 33 34 35 36 class SampleGLWindow : public BDirectWindow 37 { 38 public: 39 SampleGLWindow(BRect frame, uint32 type); 40 ~SampleGLWindow(); 41 42 virtual bool QuitRequested(); 43 virtual void DirectConnected( direct_buffer_info *info ); 44 45 private: 46 SampleGLView *theView; 47 BMessageRunner *updateRunner; 48 }; 49 50 51 class SampleGLApp : public BApplication 52 { 53 public: 54 SampleGLApp(); 55 private: 56 SampleGLWindow *theWindow; 57 }; 58 59 60 SampleGLApp::SampleGLApp() 61 : BApplication("application/x-vnd.Haiku-GLDirectMode") 62 { 63 BRect windowRect; 64 uint32 type = BGL_RGB|BGL_DOUBLE|BGL_DEPTH; 65 66 windowRect.Set(50, 50, 350, 350); 67 68 theWindow = new SampleGLWindow(windowRect, type); 69 } 70 71 72 73 SampleGLWindow::SampleGLWindow(BRect frame, uint32 type) 74 : BDirectWindow(frame, "GLDirectMode", B_TITLED_WINDOW, 0) 75 { 76 float minWidth = 0.0f; 77 float maxWidth = 0.0f; 78 float minHeight = 0.0f; 79 float maxHeight = 0.0f; 80 81 GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight); 82 SetSizeLimits(50.0f, maxWidth, 50.0f, maxHeight); 83 84 BRect r = Bounds(); 85 86 r.InsetBy(10, 10); 87 theView = new SampleGLView(r, type); 88 AddChild(theView); 89 Show(); 90 91 updateRunner = new BMessageRunner(BMessenger(theView), 92 new BMessage(REDRAW_MSG), 1000000/60 /* 60 fps */); 93 94 theView->Render(); 95 } 96 97 98 SampleGLWindow::~SampleGLWindow() 99 { 100 delete updateRunner; 101 } 102 103 104 bool SampleGLWindow::QuitRequested() 105 { 106 theView->EnableDirectMode(false); 107 be_app->PostMessage(B_QUIT_REQUESTED); 108 return true; 109 } 110 111 112 void SampleGLWindow::DirectConnected(direct_buffer_info *info) 113 { 114 theView->DirectConnected(info); 115 theView->EnableDirectMode(true); 116 } 117 118 // ---- 119 120 SampleGLView::SampleGLView(BRect frame, uint32 type) 121 : BGLView(frame, "SampleGLView", B_FOLLOW_ALL_SIDES, 0, type), rotate(0) 122 { 123 width = frame.right-frame.left; 124 height = frame.bottom-frame.top; 125 } 126 127 128 void SampleGLView::AttachedToWindow(void) 129 { 130 BGLView::AttachedToWindow(); 131 LockGL(); 132 gInit(); 133 gReshape(width, height); 134 UnlockGL(); 135 } 136 137 138 void SampleGLView::FrameResized(float newWidth, float newHeight) 139 { 140 BGLView::FrameResized(newWidth, newHeight); 141 142 LockGL(); 143 144 width = newWidth; 145 height = newHeight; 146 147 gReshape(width,height); 148 149 UnlockGL(); 150 Render(); 151 } 152 153 154 void SampleGLView::gInit(void) 155 { 156 glClearColor(0.0, 0.0, 0.0, 0.0); 157 glEnable(GL_DEPTH_TEST); 158 glDepthMask(GL_TRUE); 159 } 160 161 162 163 void SampleGLView::gDraw(float rotation) 164 { 165 /* Clear the buffer, clear the matrix */ 166 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 167 glLoadIdentity(); 168 169 /* A step backward, then spin the cube */ 170 glTranslatef(0, 0, -5); 171 glRotatef(rotation, 0, 0, 1); 172 glRotatef(rotation, 1, 0.6, 0); 173 174 /* We tell we want to draw quads */ 175 glBegin (GL_QUADS); 176 177 /* Every four calls to glVertex, a quad is drawn */ 178 glColor3f (0, 0, 0); glVertex3f (-1, -1, -1); 179 glColor3f (0, 0, 1); glVertex3f (-1, -1, 1); 180 glColor3f (0, 1, 1); glVertex3f (-1, 1, 1); 181 glColor3f (0, 1, 0); glVertex3f (-1, 1, -1); 182 183 glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1); 184 glColor3f (1, 0, 1); glVertex3f ( 1, -1, 1); 185 glColor3f (1, 1, 1); glVertex3f ( 1, 1, 1); 186 glColor3f (1, 1, 0); glVertex3f ( 1, 1, -1); 187 188 glColor3f (0, 0, 0); glVertex3f (-1, -1, -1); 189 glColor3f (0, 0, 1); glVertex3f (-1, -1, 1); 190 glColor3f (1, 0, 1); glVertex3f ( 1, -1, 1); 191 glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1); 192 193 glColor3f (0, 1, 0); glVertex3f (-1, 1, -1); 194 glColor3f (0, 1, 1); glVertex3f (-1, 1, 1); 195 glColor3f (1, 1, 1); glVertex3f ( 1, 1, 1); 196 glColor3f (1, 1, 0); glVertex3f ( 1, 1, -1); 197 198 glColor3f (0, 0, 0); glVertex3f (-1, -1, -1); 199 glColor3f (0, 1, 0); glVertex3f (-1, 1, -1); 200 glColor3f (1, 1, 0); glVertex3f ( 1, 1, -1); 201 glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1); 202 203 glColor3f (0, 0, 1); glVertex3f (-1, -1, 1); 204 glColor3f (0, 1, 1); glVertex3f (-1, 1, 1); 205 glColor3f (1, 1, 1); glVertex3f ( 1, 1, 1); 206 glColor3f (1, 0, 1); glVertex3f ( 1, -1, 1); 207 208 /* No more quads */ 209 glEnd (); 210 211 /* End */ 212 glFlush (); 213 } 214 215 216 void SampleGLView::gReshape(int width, int height) 217 { 218 glViewport(0, 0, width, height); 219 if (height) { // Avoid Divide By Zero error... 220 glMatrixMode(GL_PROJECTION); 221 glLoadIdentity(); 222 gluPerspective(45, width / (float) height, 1, 500); 223 glMatrixMode(GL_MODELVIEW); 224 } 225 } 226 227 228 void SampleGLView::Render(void) 229 { 230 LockGL(); 231 gDraw(rotate); 232 SwapBuffers(); 233 UnlockGL(); 234 } 235 236 void SampleGLView::MessageReceived(BMessage * msg) 237 { 238 switch (msg->what) { 239 case REDRAW_MSG: 240 Render(); 241 /* Rotate a bit more */ 242 rotate++; 243 break; 244 245 default: 246 BGLView::MessageReceived(msg); 247 } 248 } 249 250 251 252 int main(int argc, char *argv[]) 253 { 254 SampleGLApp *app = new SampleGLApp; 255 app->Run(); 256 delete app; 257 return 0; 258 } 259