1*a8251cf6SPhilippe Houdoin // sample BGLView app from the Be Book, modified to stress direct mode support. 2*a8251cf6SPhilippe Houdoin 3*a8251cf6SPhilippe Houdoin 4*a8251cf6SPhilippe Houdoin #include <stdio.h> 5*a8251cf6SPhilippe Houdoin 6*a8251cf6SPhilippe Houdoin #include <Application.h> 7*a8251cf6SPhilippe Houdoin #include <DirectWindow.h> 8*a8251cf6SPhilippe Houdoin #include <CheckBox.h> 9*a8251cf6SPhilippe Houdoin #include <GLView.h> 10*a8251cf6SPhilippe Houdoin 11*a8251cf6SPhilippe Houdoin class SampleGLView : public BGLView 12*a8251cf6SPhilippe Houdoin { 13*a8251cf6SPhilippe Houdoin public: 14*a8251cf6SPhilippe Houdoin SampleGLView(BRect frame, uint32 type); 15*a8251cf6SPhilippe Houdoin virtual void AttachedToWindow(void); 16*a8251cf6SPhilippe Houdoin virtual void FrameResized(float newWidth, float newHeight); 17*a8251cf6SPhilippe Houdoin virtual void ErrorCallback(GLenum which); 18*a8251cf6SPhilippe Houdoin 19*a8251cf6SPhilippe Houdoin void Render(void); 20*a8251cf6SPhilippe Houdoin 21*a8251cf6SPhilippe Houdoin private: 22*a8251cf6SPhilippe Houdoin void gInit(void); 23*a8251cf6SPhilippe Houdoin void gDraw(void); 24*a8251cf6SPhilippe Houdoin void gReshape(int width, int height); 25*a8251cf6SPhilippe Houdoin 26*a8251cf6SPhilippe Houdoin float width; 27*a8251cf6SPhilippe Houdoin float height; 28*a8251cf6SPhilippe Houdoin }; 29*a8251cf6SPhilippe Houdoin 30*a8251cf6SPhilippe Houdoin 31*a8251cf6SPhilippe Houdoin 32*a8251cf6SPhilippe Houdoin class SampleGLWindow : public BDirectWindow 33*a8251cf6SPhilippe Houdoin { 34*a8251cf6SPhilippe Houdoin public: 35*a8251cf6SPhilippe Houdoin SampleGLWindow(BRect frame, uint32 type); 36*a8251cf6SPhilippe Houdoin virtual bool QuitRequested(); 37*a8251cf6SPhilippe Houdoin virtual void DirectConnected( direct_buffer_info *info ); 38*a8251cf6SPhilippe Houdoin 39*a8251cf6SPhilippe Houdoin private: 40*a8251cf6SPhilippe Houdoin SampleGLView *theView; 41*a8251cf6SPhilippe Houdoin }; 42*a8251cf6SPhilippe Houdoin 43*a8251cf6SPhilippe Houdoin 44*a8251cf6SPhilippe Houdoin class SampleGLApp : public BApplication 45*a8251cf6SPhilippe Houdoin { 46*a8251cf6SPhilippe Houdoin public: 47*a8251cf6SPhilippe Houdoin SampleGLApp(); 48*a8251cf6SPhilippe Houdoin private: 49*a8251cf6SPhilippe Houdoin SampleGLWindow *theWindow; 50*a8251cf6SPhilippe Houdoin }; 51*a8251cf6SPhilippe Houdoin 52*a8251cf6SPhilippe Houdoin 53*a8251cf6SPhilippe Houdoin SampleGLApp::SampleGLApp() 54*a8251cf6SPhilippe Houdoin : BApplication("application/x-vnd.sample") 55*a8251cf6SPhilippe Houdoin { 56*a8251cf6SPhilippe Houdoin BRect windowRect; 57*a8251cf6SPhilippe Houdoin uint32 type = BGL_RGB|BGL_DOUBLE; 58*a8251cf6SPhilippe Houdoin 59*a8251cf6SPhilippe Houdoin windowRect.Set(50, 50, 350, 350); 60*a8251cf6SPhilippe Houdoin 61*a8251cf6SPhilippe Houdoin theWindow = new SampleGLWindow(windowRect, type); 62*a8251cf6SPhilippe Houdoin } 63*a8251cf6SPhilippe Houdoin 64*a8251cf6SPhilippe Houdoin 65*a8251cf6SPhilippe Houdoin 66*a8251cf6SPhilippe Houdoin SampleGLWindow::SampleGLWindow(BRect frame, uint32 type) 67*a8251cf6SPhilippe Houdoin : BDirectWindow(frame, "OpenGL Test", B_TITLED_WINDOW, 0) 68*a8251cf6SPhilippe Houdoin { 69*a8251cf6SPhilippe Houdoin BRect r = Bounds(); 70*a8251cf6SPhilippe Houdoin 71*a8251cf6SPhilippe Houdoin r.InsetBy(10, 10); 72*a8251cf6SPhilippe Houdoin theView = new SampleGLView(r, type); 73*a8251cf6SPhilippe Houdoin AddChild(theView); 74*a8251cf6SPhilippe Houdoin Show(); 75*a8251cf6SPhilippe Houdoin theView->Render(); 76*a8251cf6SPhilippe Houdoin } 77*a8251cf6SPhilippe Houdoin 78*a8251cf6SPhilippe Houdoin 79*a8251cf6SPhilippe Houdoin bool SampleGLWindow::QuitRequested() 80*a8251cf6SPhilippe Houdoin { 81*a8251cf6SPhilippe Houdoin theView->EnableDirectMode(false); 82*a8251cf6SPhilippe Houdoin be_app->PostMessage(B_QUIT_REQUESTED); 83*a8251cf6SPhilippe Houdoin return true; 84*a8251cf6SPhilippe Houdoin } 85*a8251cf6SPhilippe Houdoin 86*a8251cf6SPhilippe Houdoin 87*a8251cf6SPhilippe Houdoin void SampleGLWindow::DirectConnected(direct_buffer_info *info) 88*a8251cf6SPhilippe Houdoin { 89*a8251cf6SPhilippe Houdoin theView->DirectConnected(info); 90*a8251cf6SPhilippe Houdoin theView->EnableDirectMode(true); 91*a8251cf6SPhilippe Houdoin } 92*a8251cf6SPhilippe Houdoin 93*a8251cf6SPhilippe Houdoin 94*a8251cf6SPhilippe Houdoin 95*a8251cf6SPhilippe Houdoin SampleGLView::SampleGLView(BRect frame, uint32 type) 96*a8251cf6SPhilippe Houdoin : BGLView(frame, "SampleGLView", B_FOLLOW_ALL_SIDES, 0, type) 97*a8251cf6SPhilippe Houdoin { 98*a8251cf6SPhilippe Houdoin width = frame.right-frame.left; 99*a8251cf6SPhilippe Houdoin height = frame.bottom-frame.top; 100*a8251cf6SPhilippe Houdoin } 101*a8251cf6SPhilippe Houdoin 102*a8251cf6SPhilippe Houdoin 103*a8251cf6SPhilippe Houdoin void SampleGLView::AttachedToWindow(void) 104*a8251cf6SPhilippe Houdoin { 105*a8251cf6SPhilippe Houdoin LockGL(); 106*a8251cf6SPhilippe Houdoin BGLView::AttachedToWindow(); 107*a8251cf6SPhilippe Houdoin gInit(); 108*a8251cf6SPhilippe Houdoin gReshape(width, height); 109*a8251cf6SPhilippe Houdoin UnlockGL(); 110*a8251cf6SPhilippe Houdoin } 111*a8251cf6SPhilippe Houdoin 112*a8251cf6SPhilippe Houdoin 113*a8251cf6SPhilippe Houdoin void SampleGLView::FrameResized(float newWidth, float newHeight) 114*a8251cf6SPhilippe Houdoin { 115*a8251cf6SPhilippe Houdoin BGLView::FrameResized(newWidth, newHeight); 116*a8251cf6SPhilippe Houdoin 117*a8251cf6SPhilippe Houdoin LockGL(); 118*a8251cf6SPhilippe Houdoin 119*a8251cf6SPhilippe Houdoin width = newWidth; 120*a8251cf6SPhilippe Houdoin height = newHeight; 121*a8251cf6SPhilippe Houdoin 122*a8251cf6SPhilippe Houdoin gReshape(width,height); 123*a8251cf6SPhilippe Houdoin 124*a8251cf6SPhilippe Houdoin UnlockGL(); 125*a8251cf6SPhilippe Houdoin Render(); 126*a8251cf6SPhilippe Houdoin } 127*a8251cf6SPhilippe Houdoin 128*a8251cf6SPhilippe Houdoin 129*a8251cf6SPhilippe Houdoin void SampleGLView::ErrorCallback(GLenum whichError) 130*a8251cf6SPhilippe Houdoin { 131*a8251cf6SPhilippe Houdoin // fprintf(stderr, "Unexpected error occured (%d):\\n", whichError); 132*a8251cf6SPhilippe Houdoin // fprintf(stderr, " %s\\n", gluErrorString(whichError)); 133*a8251cf6SPhilippe Houdoin } 134*a8251cf6SPhilippe Houdoin 135*a8251cf6SPhilippe Houdoin 136*a8251cf6SPhilippe Houdoin 137*a8251cf6SPhilippe Houdoin // globals 138*a8251cf6SPhilippe Houdoin GLenum use_stipple_mode; // GL_TRUE to use dashed lines 139*a8251cf6SPhilippe Houdoin GLenum use_smooth_mode; // GL_TRUE to use anti-aliased lines 140*a8251cf6SPhilippe Houdoin GLint linesize; // Line width 141*a8251cf6SPhilippe Houdoin GLint pointsize; // Point diameter 142*a8251cf6SPhilippe Houdoin 143*a8251cf6SPhilippe Houdoin float pntA[3] = { 144*a8251cf6SPhilippe Houdoin -160.0, 0.0, 0.0 145*a8251cf6SPhilippe Houdoin }; 146*a8251cf6SPhilippe Houdoin float pntB[3] = { 147*a8251cf6SPhilippe Houdoin -130.0, 0.0, 0.0 148*a8251cf6SPhilippe Houdoin }; 149*a8251cf6SPhilippe Houdoin 150*a8251cf6SPhilippe Houdoin 151*a8251cf6SPhilippe Houdoin 152*a8251cf6SPhilippe Houdoin void SampleGLView::gInit(void) 153*a8251cf6SPhilippe Houdoin { 154*a8251cf6SPhilippe Houdoin glClearColor(0.0, 0.0, 0.0, 0.0); 155*a8251cf6SPhilippe Houdoin glLineStipple(1, 0xF0E0); 156*a8251cf6SPhilippe Houdoin glBlendFunc(GL_SRC_ALPHA, GL_ONE); 157*a8251cf6SPhilippe Houdoin use_stipple_mode = GL_FALSE; 158*a8251cf6SPhilippe Houdoin use_smooth_mode = GL_TRUE; 159*a8251cf6SPhilippe Houdoin linesize = 2; 160*a8251cf6SPhilippe Houdoin pointsize = 6; 161*a8251cf6SPhilippe Houdoin } 162*a8251cf6SPhilippe Houdoin 163*a8251cf6SPhilippe Houdoin 164*a8251cf6SPhilippe Houdoin 165*a8251cf6SPhilippe Houdoin void SampleGLView::gDraw(void) 166*a8251cf6SPhilippe Houdoin { 167*a8251cf6SPhilippe Houdoin GLint i; 168*a8251cf6SPhilippe Houdoin 169*a8251cf6SPhilippe Houdoin glClear(GL_COLOR_BUFFER_BIT); 170*a8251cf6SPhilippe Houdoin glLineWidth(linesize); 171*a8251cf6SPhilippe Houdoin 172*a8251cf6SPhilippe Houdoin /* 173*a8251cf6SPhilippe Houdoin 174*a8251cf6SPhilippe Houdoin if (use_stipple_mode) { 175*a8251cf6SPhilippe Houdoin glEnable(GL_LINE_STIPPLE); 176*a8251cf6SPhilippe Houdoin } else { 177*a8251cf6SPhilippe Houdoin glDisable(GL_LINE_STIPPLE); 178*a8251cf6SPhilippe Houdoin } 179*a8251cf6SPhilippe Houdoin */ 180*a8251cf6SPhilippe Houdoin 181*a8251cf6SPhilippe Houdoin glDisable(GL_POINT_SMOOTH); 182*a8251cf6SPhilippe Houdoin 183*a8251cf6SPhilippe Houdoin 184*a8251cf6SPhilippe Houdoin glPushMatrix(); 185*a8251cf6SPhilippe Houdoin 186*a8251cf6SPhilippe Houdoin glPointSize(pointsize); // Set size for point 187*a8251cf6SPhilippe Houdoin 188*a8251cf6SPhilippe Houdoin for (i = 0; i < 360; i += 5) { 189*a8251cf6SPhilippe Houdoin glRotatef(5.0, 0,0,1); // Rotate right 5 degrees 190*a8251cf6SPhilippe Houdoin 191*a8251cf6SPhilippe Houdoin if (use_smooth_mode) { 192*a8251cf6SPhilippe Houdoin glEnable(GL_LINE_SMOOTH); 193*a8251cf6SPhilippe Houdoin glEnable(GL_BLEND); 194*a8251cf6SPhilippe Houdoin } else { 195*a8251cf6SPhilippe Houdoin glDisable(GL_LINE_SMOOTH); 196*a8251cf6SPhilippe Houdoin glDisable(GL_BLEND); 197*a8251cf6SPhilippe Houdoin } 198*a8251cf6SPhilippe Houdoin 199*a8251cf6SPhilippe Houdoin glColor3f(1.0, 1.0, 0.0); // Set color for line 200*a8251cf6SPhilippe Houdoin glBegin(GL_LINE_STRIP); // And create the line 201*a8251cf6SPhilippe Houdoin glVertex3fv(pntA); 202*a8251cf6SPhilippe Houdoin glVertex3fv(pntB); 203*a8251cf6SPhilippe Houdoin glEnd(); 204*a8251cf6SPhilippe Houdoin 205*a8251cf6SPhilippe Houdoin glDisable(GL_POINT_SMOOTH); 206*a8251cf6SPhilippe Houdoin glDisable(GL_BLEND); 207*a8251cf6SPhilippe Houdoin 208*a8251cf6SPhilippe Houdoin glColor3f(0.0, 1.0, 0.0); // Set color for point 209*a8251cf6SPhilippe Houdoin glBegin(GL_POINTS); 210*a8251cf6SPhilippe Houdoin glVertex3fv(pntA); // Draw point at one end 211*a8251cf6SPhilippe Houdoin glVertex3fv(pntB); // Draw point at other end 212*a8251cf6SPhilippe Houdoin glEnd(); 213*a8251cf6SPhilippe Houdoin } 214*a8251cf6SPhilippe Houdoin 215*a8251cf6SPhilippe Houdoin glPopMatrix(); // Done with matrix 216*a8251cf6SPhilippe Houdoin } 217*a8251cf6SPhilippe Houdoin 218*a8251cf6SPhilippe Houdoin 219*a8251cf6SPhilippe Houdoin void SampleGLView::gReshape(int width, int height) 220*a8251cf6SPhilippe Houdoin { 221*a8251cf6SPhilippe Houdoin glViewport(0, 0, width, height); 222*a8251cf6SPhilippe Houdoin glMatrixMode(GL_PROJECTION); 223*a8251cf6SPhilippe Houdoin glLoadIdentity(); 224*a8251cf6SPhilippe Houdoin glOrtho(-175, 175, -175, 175, -1, 1); 225*a8251cf6SPhilippe Houdoin glMatrixMode(GL_MODELVIEW); 226*a8251cf6SPhilippe Houdoin } 227*a8251cf6SPhilippe Houdoin 228*a8251cf6SPhilippe Houdoin 229*a8251cf6SPhilippe Houdoin void SampleGLView::Render(void) 230*a8251cf6SPhilippe Houdoin { 231*a8251cf6SPhilippe Houdoin LockGL(); 232*a8251cf6SPhilippe Houdoin gDraw(); 233*a8251cf6SPhilippe Houdoin SwapBuffers(); 234*a8251cf6SPhilippe Houdoin UnlockGL(); 235*a8251cf6SPhilippe Houdoin } 236*a8251cf6SPhilippe Houdoin 237*a8251cf6SPhilippe Houdoin 238*a8251cf6SPhilippe Houdoin 239*a8251cf6SPhilippe Houdoin int main(int argc, char *argv[]) 240*a8251cf6SPhilippe Houdoin { 241*a8251cf6SPhilippe Houdoin SampleGLApp *app = new SampleGLApp; 242*a8251cf6SPhilippe Houdoin app->Run(); 243*a8251cf6SPhilippe Houdoin delete app; 244*a8251cf6SPhilippe Houdoin return 0; 245*a8251cf6SPhilippe Houdoin } 246