xref: /haiku/src/tests/kits/opengl/direct_mode/GLDirectMode.cpp (revision e2947e53448c4645a4a996b6819a3855fcf04ef0)
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>
10895eb46bSPhilippe Houdoin #include <GL/glu.h>
11895eb46bSPhilippe Houdoin 
12895eb46bSPhilippe 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);
20895eb46bSPhilippe Houdoin    virtual void   MessageReceived(BMessage * msg);
21a8251cf6SPhilippe Houdoin 
22a8251cf6SPhilippe Houdoin    void         Render(void);
23a8251cf6SPhilippe Houdoin 
24a8251cf6SPhilippe Houdoin private:
25a8251cf6SPhilippe Houdoin    void         gInit(void);
26895eb46bSPhilippe Houdoin    void         gDraw(float rotation = 0);
27a8251cf6SPhilippe Houdoin    void         gReshape(int width, int height);
28a8251cf6SPhilippe Houdoin 
29a8251cf6SPhilippe Houdoin    float        width;
30a8251cf6SPhilippe Houdoin    float        height;
31895eb46bSPhilippe 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);
40895eb46bSPhilippe Houdoin    ~SampleGLWindow();
41895eb46bSPhilippe Houdoin 
42a8251cf6SPhilippe Houdoin    virtual bool QuitRequested();
43a8251cf6SPhilippe Houdoin    virtual void DirectConnected( direct_buffer_info *info );
44a8251cf6SPhilippe Houdoin 
45a8251cf6SPhilippe Houdoin private:
46a8251cf6SPhilippe Houdoin    SampleGLView   *theView;
47895eb46bSPhilippe 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()
61*e2947e53SStephan Aßmus    : BApplication("application/x-vnd.Haiku-GLDirectMode")
62a8251cf6SPhilippe Houdoin {
63a8251cf6SPhilippe Houdoin    BRect windowRect;
64895eb46bSPhilippe 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)
740795ca4aSPhilippe Houdoin    : BDirectWindow(frame, "GLDirectMode", B_TITLED_WINDOW, 0)
75a8251cf6SPhilippe Houdoin {
760795ca4aSPhilippe Houdoin    float minWidth = 0.0f;
770795ca4aSPhilippe Houdoin    float maxWidth = 0.0f;
780795ca4aSPhilippe Houdoin    float minHeight = 0.0f;
790795ca4aSPhilippe Houdoin    float maxHeight = 0.0f;
800795ca4aSPhilippe Houdoin 
810795ca4aSPhilippe Houdoin    GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight);
820795ca4aSPhilippe Houdoin    SetSizeLimits(50.0f, maxWidth, 50.0f, maxHeight);
830795ca4aSPhilippe Houdoin 
84a8251cf6SPhilippe Houdoin    BRect r = Bounds();
85a8251cf6SPhilippe Houdoin 
86a8251cf6SPhilippe Houdoin    r.InsetBy(10, 10);
87a8251cf6SPhilippe Houdoin    theView = new SampleGLView(r, type);
88a8251cf6SPhilippe Houdoin    AddChild(theView);
89a8251cf6SPhilippe Houdoin    Show();
90895eb46bSPhilippe Houdoin 
91895eb46bSPhilippe Houdoin    updateRunner = new BMessageRunner(BMessenger(theView),
92895eb46bSPhilippe Houdoin    		new BMessage(REDRAW_MSG), 1000000/60 /* 60 fps */);
93895eb46bSPhilippe Houdoin 
94a8251cf6SPhilippe Houdoin    theView->Render();
95a8251cf6SPhilippe Houdoin }
96a8251cf6SPhilippe Houdoin 
97a8251cf6SPhilippe Houdoin 
98895eb46bSPhilippe Houdoin SampleGLWindow::~SampleGLWindow()
99895eb46bSPhilippe Houdoin {
100895eb46bSPhilippe Houdoin 	delete updateRunner;
101895eb46bSPhilippe Houdoin }
102895eb46bSPhilippe Houdoin 
103895eb46bSPhilippe Houdoin 
104a8251cf6SPhilippe Houdoin bool SampleGLWindow::QuitRequested()
105a8251cf6SPhilippe Houdoin {
106a8251cf6SPhilippe Houdoin 	theView->EnableDirectMode(false);
107a8251cf6SPhilippe Houdoin 	be_app->PostMessage(B_QUIT_REQUESTED);
108a8251cf6SPhilippe Houdoin 	return true;
109a8251cf6SPhilippe Houdoin }
110a8251cf6SPhilippe Houdoin 
111a8251cf6SPhilippe Houdoin 
112a8251cf6SPhilippe Houdoin void SampleGLWindow::DirectConnected(direct_buffer_info *info)
113a8251cf6SPhilippe Houdoin {
114a8251cf6SPhilippe Houdoin 	theView->DirectConnected(info);
115a8251cf6SPhilippe Houdoin 	theView->EnableDirectMode(true);
116a8251cf6SPhilippe Houdoin }
117a8251cf6SPhilippe Houdoin 
118895eb46bSPhilippe Houdoin // ----
119a8251cf6SPhilippe Houdoin 
120a8251cf6SPhilippe Houdoin SampleGLView::SampleGLView(BRect frame, uint32 type)
121895eb46bSPhilippe Houdoin    : BGLView(frame, "SampleGLView", B_FOLLOW_ALL_SIDES, 0, type), rotate(0)
122a8251cf6SPhilippe Houdoin {
123a8251cf6SPhilippe Houdoin    width = frame.right-frame.left;
124a8251cf6SPhilippe Houdoin    height = frame.bottom-frame.top;
125a8251cf6SPhilippe Houdoin }
126a8251cf6SPhilippe Houdoin 
127a8251cf6SPhilippe Houdoin 
128a8251cf6SPhilippe Houdoin void SampleGLView::AttachedToWindow(void)
129a8251cf6SPhilippe Houdoin {
130a8251cf6SPhilippe Houdoin    BGLView::AttachedToWindow();
1316dfe2237SStefano Ceccherini    LockGL();
132a8251cf6SPhilippe Houdoin    gInit();
133a8251cf6SPhilippe Houdoin    gReshape(width, height);
134a8251cf6SPhilippe Houdoin    UnlockGL();
135a8251cf6SPhilippe Houdoin }
136a8251cf6SPhilippe Houdoin 
137a8251cf6SPhilippe Houdoin 
138a8251cf6SPhilippe Houdoin void SampleGLView::FrameResized(float newWidth, float newHeight)
139a8251cf6SPhilippe Houdoin {
140a8251cf6SPhilippe Houdoin    BGLView::FrameResized(newWidth, newHeight);
141a8251cf6SPhilippe Houdoin 
142a8251cf6SPhilippe Houdoin    LockGL();
143a8251cf6SPhilippe Houdoin 
144a8251cf6SPhilippe Houdoin    width = newWidth;
145a8251cf6SPhilippe Houdoin    height = newHeight;
146a8251cf6SPhilippe Houdoin 
147a8251cf6SPhilippe Houdoin    gReshape(width,height);
148a8251cf6SPhilippe Houdoin 
149a8251cf6SPhilippe Houdoin    UnlockGL();
150a8251cf6SPhilippe Houdoin    Render();
151a8251cf6SPhilippe Houdoin }
152a8251cf6SPhilippe Houdoin 
153a8251cf6SPhilippe Houdoin 
154a8251cf6SPhilippe Houdoin void SampleGLView::gInit(void)
155a8251cf6SPhilippe Houdoin {
156a8251cf6SPhilippe Houdoin    glClearColor(0.0, 0.0, 0.0, 0.0);
157895eb46bSPhilippe Houdoin    glEnable(GL_DEPTH_TEST);
158895eb46bSPhilippe Houdoin    glDepthMask(GL_TRUE);
159a8251cf6SPhilippe Houdoin }
160a8251cf6SPhilippe Houdoin 
161a8251cf6SPhilippe Houdoin 
162a8251cf6SPhilippe Houdoin 
163895eb46bSPhilippe Houdoin void SampleGLView::gDraw(float rotation)
164a8251cf6SPhilippe Houdoin {
165895eb46bSPhilippe Houdoin   /* Clear the buffer, clear the matrix */
166895eb46bSPhilippe Houdoin   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
167895eb46bSPhilippe Houdoin   glLoadIdentity();
168a8251cf6SPhilippe Houdoin 
169895eb46bSPhilippe Houdoin   /* A step backward, then spin the cube */
170895eb46bSPhilippe Houdoin   glTranslatef(0, 0, -5);
171895eb46bSPhilippe Houdoin   glRotatef(rotation, 0, 0, 1);
172895eb46bSPhilippe Houdoin   glRotatef(rotation, 1, 0.6, 0);
173a8251cf6SPhilippe Houdoin 
174895eb46bSPhilippe Houdoin   /* We tell we want to draw quads */
175895eb46bSPhilippe Houdoin   glBegin (GL_QUADS);
176a8251cf6SPhilippe Houdoin 
177895eb46bSPhilippe Houdoin   /* Every four calls to glVertex, a quad is drawn */
178895eb46bSPhilippe Houdoin   glColor3f (0, 0, 0); glVertex3f (-1, -1, -1);
179895eb46bSPhilippe Houdoin   glColor3f (0, 0, 1); glVertex3f (-1, -1,  1);
180895eb46bSPhilippe Houdoin   glColor3f (0, 1, 1); glVertex3f (-1,  1,  1);
181895eb46bSPhilippe Houdoin   glColor3f (0, 1, 0); glVertex3f (-1,  1, -1);
182a8251cf6SPhilippe Houdoin 
183895eb46bSPhilippe Houdoin   glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1);
184895eb46bSPhilippe Houdoin   glColor3f (1, 0, 1); glVertex3f ( 1, -1,  1);
185895eb46bSPhilippe Houdoin   glColor3f (1, 1, 1); glVertex3f ( 1,  1,  1);
186895eb46bSPhilippe Houdoin   glColor3f (1, 1, 0); glVertex3f ( 1,  1, -1);
187a8251cf6SPhilippe Houdoin 
188895eb46bSPhilippe Houdoin   glColor3f (0, 0, 0); glVertex3f (-1, -1, -1);
189895eb46bSPhilippe Houdoin   glColor3f (0, 0, 1); glVertex3f (-1, -1,  1);
190895eb46bSPhilippe Houdoin   glColor3f (1, 0, 1); glVertex3f ( 1, -1,  1);
191895eb46bSPhilippe Houdoin   glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1);
192a8251cf6SPhilippe Houdoin 
193895eb46bSPhilippe Houdoin   glColor3f (0, 1, 0); glVertex3f (-1,  1, -1);
194895eb46bSPhilippe Houdoin   glColor3f (0, 1, 1); glVertex3f (-1,  1,  1);
195895eb46bSPhilippe Houdoin   glColor3f (1, 1, 1); glVertex3f ( 1,  1,  1);
196895eb46bSPhilippe Houdoin   glColor3f (1, 1, 0); glVertex3f ( 1,  1, -1);
197a8251cf6SPhilippe Houdoin 
198895eb46bSPhilippe Houdoin   glColor3f (0, 0, 0); glVertex3f (-1, -1, -1);
199895eb46bSPhilippe Houdoin   glColor3f (0, 1, 0); glVertex3f (-1,  1, -1);
200895eb46bSPhilippe Houdoin   glColor3f (1, 1, 0); glVertex3f ( 1,  1, -1);
201895eb46bSPhilippe Houdoin   glColor3f (1, 0, 0); glVertex3f ( 1, -1, -1);
202a8251cf6SPhilippe Houdoin 
203895eb46bSPhilippe Houdoin   glColor3f (0, 0, 1); glVertex3f (-1, -1,  1);
204895eb46bSPhilippe Houdoin   glColor3f (0, 1, 1); glVertex3f (-1,  1,  1);
205895eb46bSPhilippe Houdoin   glColor3f (1, 1, 1); glVertex3f ( 1,  1,  1);
206895eb46bSPhilippe Houdoin   glColor3f (1, 0, 1); glVertex3f ( 1, -1,  1);
207a8251cf6SPhilippe Houdoin 
208895eb46bSPhilippe Houdoin   /* No more quads */
209a8251cf6SPhilippe Houdoin   glEnd ();
210a8251cf6SPhilippe Houdoin 
211895eb46bSPhilippe Houdoin   /* End */
212895eb46bSPhilippe Houdoin   glFlush ();
213a8251cf6SPhilippe Houdoin }
214a8251cf6SPhilippe Houdoin 
215a8251cf6SPhilippe Houdoin 
216a8251cf6SPhilippe Houdoin void SampleGLView::gReshape(int width, int height)
217a8251cf6SPhilippe Houdoin {
218a8251cf6SPhilippe Houdoin    glViewport(0, 0, width, height);
2190795ca4aSPhilippe Houdoin    if (height) {  // Avoid Divide By Zero error...
220a8251cf6SPhilippe Houdoin       glMatrixMode(GL_PROJECTION);
221a8251cf6SPhilippe Houdoin       glLoadIdentity();
222895eb46bSPhilippe Houdoin       gluPerspective(45, width / (float) height, 1, 500);
223a8251cf6SPhilippe Houdoin       glMatrixMode(GL_MODELVIEW);
224a8251cf6SPhilippe Houdoin    }
2250795ca4aSPhilippe Houdoin }
226a8251cf6SPhilippe Houdoin 
227a8251cf6SPhilippe Houdoin 
228a8251cf6SPhilippe Houdoin void SampleGLView::Render(void)
229a8251cf6SPhilippe Houdoin {
230a8251cf6SPhilippe Houdoin    LockGL();
231895eb46bSPhilippe Houdoin    gDraw(rotate);
232a8251cf6SPhilippe Houdoin    SwapBuffers();
233a8251cf6SPhilippe Houdoin    UnlockGL();
234a8251cf6SPhilippe Houdoin }
235a8251cf6SPhilippe Houdoin 
236895eb46bSPhilippe Houdoin void SampleGLView::MessageReceived(BMessage * msg)
237895eb46bSPhilippe Houdoin {
238895eb46bSPhilippe Houdoin 	switch (msg->what) {
239895eb46bSPhilippe Houdoin 	case REDRAW_MSG:
240895eb46bSPhilippe Houdoin 		Render();
241895eb46bSPhilippe Houdoin 		/* Rotate a bit more */
242895eb46bSPhilippe Houdoin 		rotate++;
243895eb46bSPhilippe Houdoin 		break;
244895eb46bSPhilippe Houdoin 
245895eb46bSPhilippe Houdoin 	default:
246895eb46bSPhilippe Houdoin 		BGLView::MessageReceived(msg);
247895eb46bSPhilippe Houdoin 	}
248895eb46bSPhilippe Houdoin }
249895eb46bSPhilippe Houdoin 
250a8251cf6SPhilippe Houdoin 
251a8251cf6SPhilippe Houdoin 
252a8251cf6SPhilippe Houdoin int main(int argc, char *argv[])
253a8251cf6SPhilippe Houdoin {
254a8251cf6SPhilippe Houdoin    SampleGLApp *app = new SampleGLApp;
255a8251cf6SPhilippe Houdoin    app->Run();
256a8251cf6SPhilippe Houdoin    delete app;
257a8251cf6SPhilippe Houdoin    return 0;
258a8251cf6SPhilippe Houdoin }
259