blob: c25eb93075cf3f2bb3d467525e02838f447356ca [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001// $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
15class MyWindow : public BWindow
16{
17public:
18 MyWindow(BRect frame);
19 virtual bool QuitRequested();
20};
21
22
23MyWindow::MyWindow(BRect frame)
24 : BWindow(frame, "demo", B_TITLED_WINDOW, B_NOT_ZOOMABLE)
25{
26 // no-op
27}
28
29bool MyWindow::QuitRequested()
30{
31 be_app->PostMessage(B_QUIT_REQUESTED);
32 return true;
33}
34
35
36class MyGL : public BGLView
37{
38public:
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);
45private:
46 float mAngle;
47};
48
49
50MyGL::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
58void MyGL::AttachedToWindow()
59{
60 BGLView::AttachedToWindow();
61 LockGL();
62 glClearColor(.7, .7, 0, 0);
63 UnlockGL();
64}
65#endif
66
67
68void 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
87void 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
107void 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
130int 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}