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