1 // sample BGLView app from the Be Book
5 #include <Application.h>
10 class SampleGLView
: public BGLView
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
);
23 void gReshape(int width
, int height
);
31 class SampleGLWindow
: public BWindow
34 SampleGLWindow(BRect frame
, uint32 type
);
35 virtual bool QuitRequested() { be_app
->PostMessage(B_QUIT_REQUESTED
); return true; }
38 SampleGLView
*theView
;
42 class SampleGLApp
: public BApplication
47 SampleGLWindow
*theWindow
;
51 SampleGLApp::SampleGLApp()
52 : BApplication("application/x-vnd.sample")
55 uint32 type
= BGL_RGB
|BGL_DOUBLE
;
57 windowRect
.Set(50, 50, 350, 350);
59 theWindow
= new SampleGLWindow(windowRect
, type
);
64 SampleGLWindow::SampleGLWindow(BRect frame
, uint32 type
)
65 : BWindow(frame
, "OpenGL Test", B_TITLED_WINDOW
, 0)
67 theView
= new SampleGLView(Bounds(), type
);
75 SampleGLView::SampleGLView(BRect frame
, uint32 type
)
76 : BGLView(frame
, "SampleGLView", B_FOLLOW_ALL_SIDES
, 0, type
)
78 width
= frame
.right
-frame
.left
;
79 height
= frame
.bottom
-frame
.top
;
83 void SampleGLView::AttachedToWindow(void)
86 BGLView::AttachedToWindow();
88 gReshape(width
, height
);
93 void SampleGLView::FrameResized(float newWidth
, float newHeight
)
95 BGLView::FrameResized(newWidth
, newHeight
);
102 gReshape(width
,height
);
109 void SampleGLView::ErrorCallback(GLenum whichError
)
111 // fprintf(stderr, "Unexpected error occured (%d):\\n", whichError);
112 // fprintf(stderr, " %s\\n", gluErrorString(whichError));
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
132 void SampleGLView::gInit(void)
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
;
145 void SampleGLView::gDraw(void)
149 glClear(GL_COLOR_BUFFER_BIT
);
150 glLineWidth(linesize
);
154 if (use_stipple_mode) {
155 glEnable(GL_LINE_STIPPLE);
157 glDisable(GL_LINE_STIPPLE);
161 glDisable(GL_POINT_SMOOTH
);
166 glPointSize(pointsize
); // Set size for point
168 for (i
= 0; i
< 360; i
+= 5) {
169 glRotatef(5.0, 0,0,1); // Rotate right 5 degrees
171 if (use_smooth_mode
) {
172 glEnable(GL_LINE_SMOOTH
);
175 glDisable(GL_LINE_SMOOTH
);
179 glColor3f(1.0, 1.0, 0.0); // Set color for line
180 glBegin(GL_LINE_STRIP
); // And create the line
185 glDisable(GL_POINT_SMOOTH
);
188 glColor3f(0.0, 1.0, 0.0); // Set color for point
190 glVertex3fv(pntA
); // Draw point at one end
191 glVertex3fv(pntB
); // Draw point at other end
195 glPopMatrix(); // Done with matrix
199 void SampleGLView::gReshape(int width
, int height
)
201 glViewport(0, 0, width
, height
);
202 glMatrixMode(GL_PROJECTION
);
204 glOrtho(-175, 175, -175, 175, -1, 1);
205 glMatrixMode(GL_MODELVIEW
);
209 void SampleGLView::Render(void)
219 int main(int argc
, char *argv
[])
221 SampleGLApp
*app
= new SampleGLApp
;