jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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() { 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 | LockGL(); |
| 96 | BGLView::FrameResized(width, height); |
| 97 | width = newWidth; |
| 98 | height = newHeight; |
| 99 | |
| 100 | gReshape(width,height); |
| 101 | |
| 102 | UnlockGL(); |
| 103 | Render(); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void SampleGLView::ErrorCallback(GLenum whichError) |
| 108 | { |
| 109 | // fprintf(stderr, "Unexpected error occured (%d):\\n", whichError); |
| 110 | // fprintf(stderr, " %s\\n", gluErrorString(whichError)); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | // globals |
| 116 | GLenum use_stipple_mode; // GL_TRUE to use dashed lines |
| 117 | GLenum use_smooth_mode; // GL_TRUE to use anti-aliased lines |
| 118 | GLint linesize; // Line width |
| 119 | GLint pointsize; // Point diameter |
| 120 | |
| 121 | float pntA[3] = { |
| 122 | -160.0, 0.0, 0.0 |
| 123 | }; |
| 124 | float pntB[3] = { |
| 125 | -130.0, 0.0, 0.0 |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | |
| 130 | void SampleGLView::gInit(void) |
| 131 | { |
| 132 | glClearColor(0.0, 0.0, 0.0, 0.0); |
| 133 | glLineStipple(1, 0xF0E0); |
| 134 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
| 135 | use_stipple_mode = GL_FALSE; |
| 136 | use_smooth_mode = GL_TRUE; |
| 137 | linesize = 2; |
| 138 | pointsize = 4; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | void SampleGLView::gDraw(void) |
| 144 | { |
| 145 | GLint i; |
| 146 | |
| 147 | glClear(GL_COLOR_BUFFER_BIT); |
| 148 | glLineWidth(linesize); |
| 149 | |
| 150 | if (use_stipple_mode) { |
| 151 | glEnable(GL_LINE_STIPPLE); |
| 152 | } else { |
| 153 | glDisable(GL_LINE_STIPPLE); |
| 154 | } |
| 155 | |
| 156 | if (use_smooth_mode) { |
| 157 | glEnable(GL_LINE_SMOOTH); |
| 158 | glEnable(GL_BLEND); |
| 159 | } else { |
| 160 | glDisable(GL_LINE_SMOOTH); |
| 161 | glDisable(GL_BLEND); |
| 162 | } |
| 163 | |
| 164 | glPushMatrix(); |
| 165 | |
| 166 | for (i = 0; i < 360; i += 5) { |
| 167 | glRotatef(5.0, 0,0,1); // Rotate right 5 degrees |
| 168 | glColor3f(1.0, 1.0, 0.0); // Set color for line |
| 169 | glBegin(GL_LINE_STRIP); // And create the line |
| 170 | glVertex3fv(pntA); |
| 171 | glVertex3fv(pntB); |
| 172 | glEnd(); |
| 173 | |
| 174 | glPointSize(pointsize); // Set size for point |
| 175 | glColor3f(0.0, 1.0, 0.0); // Set color for point |
| 176 | glBegin(GL_POINTS); |
| 177 | glVertex3fv(pntA); // Draw point at one end |
| 178 | glVertex3fv(pntB); // Draw point at other end |
| 179 | glEnd(); |
| 180 | } |
| 181 | |
| 182 | glPopMatrix(); // Done with matrix |
| 183 | } |
| 184 | |
| 185 | |
| 186 | void SampleGLView::gReshape(int width, int height) |
| 187 | { |
| 188 | glViewport(0, 0, width, height); |
| 189 | glMatrixMode(GL_PROJECTION); |
| 190 | glLoadIdentity(); |
| 191 | glOrtho(-175, 175, -175, 175, -1, 1); |
| 192 | glMatrixMode(GL_MODELVIEW); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void SampleGLView::Render(void) |
| 197 | { |
| 198 | LockGL(); |
| 199 | gDraw(); |
| 200 | SwapBuffers(); |
| 201 | UnlockGL(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | int main(int argc, char *argv[]) |
| 207 | { |
| 208 | SampleGLApp *app = new SampleGLApp; |
| 209 | app->Run(); |
| 210 | delete app; |
| 211 | return 0; |
| 212 | } |