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