2 * Copyright 2012, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
6 * Aaron Hill <serac@hillvisions.com>
7 * Alexander von Gluck <kallisti5@unixzen.com>
11 #include "GLifeView.h"
18 #include "GLifeGrid.h"
19 #include "GLifeState.h"
22 // ------------------------------------------------------
23 // GLifeView Class Constructor Definition
24 GLifeView::GLifeView(BRect rect
, const char* name
, ulong resizingMode
,
25 ulong options
, GLifeState
* pglsState
)
27 BGLView(rect
, name
, resizingMode
, B_FRAME_EVENTS
| B_WILL_DRAW
, options
),
28 m_pglsState(pglsState
)
31 m_pglgGrid
= new GLifeGrid(pglsState
->GridWidth(), pglsState
->GridHeight());
35 // ------------------------------------------------------
36 // GLifeView Class Destructor Definition
37 GLifeView::~GLifeView(void)
43 // ------------------------------------------------------
44 // GLifeView Class AttachedToWindow Definition
46 GLifeView::AttachedToWindow(void)
49 BGLView::AttachedToWindow();
53 glEnable(GL_DEPTH_TEST
);
56 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
58 glShadeModel(GL_SMOOTH
);
60 glMatrixMode(GL_PROJECTION
);
62 gluPerspective(45.0, Bounds().Width() / Bounds().Height(), 2.0, 20000.0);
63 glTranslatef(0.0, 0.0, -50.0);
64 glMatrixMode(GL_MODELVIEW
);
66 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
72 // ------------------------------------------------------
73 // GLifeView Class Draw Definition
75 GLifeView::Draw(BRect updateRect
)
77 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
79 // TODO: Dynamic colors or user-specified coloring
80 GLfloat glfGreen
[] = {0.05, 0.8, 0.15, 1.0};
81 GLfloat glfOrange
[] = {0.65, 0.3, 0.05, 1.0};
86 int32 iWidth
= m_pglsState
->GridWidth();
87 int32 iHeight
= m_pglsState
->GridHeight();
88 int32 iBorder
= m_pglsState
->GridBorder();
92 glRotatef(m_glfDelta
* 3, 1.0, 0.0, 0.0);
93 glRotatef(m_glfDelta
* 1, 0.0, 0.0, 1.0);
94 glRotatef(m_glfDelta
* 2, 0.0, 1.0, 0.0);
96 for(int32 iRow
= (0 - iBorder
); iRow
< (iHeight
+ iBorder
); ++iRow
) {
97 GLfloat glfY
= (GLfloat
)iRow
- ((GLfloat
)iHeight
/ 2);
99 for(int32 iColumn
= (0 - iBorder
); iColumn
< (iWidth
+ iBorder
); ++iColumn
) {
101 GLfloat glfX
= (GLfloat
)iColumn
- ((GLfloat
) iWidth
/ 2);
103 bColor
= (iColumn
< 0) || (iColumn
>= iWidth
) || (iRow
< 0) || (iRow
>= iHeight
);
105 if (m_pglgGrid
->Occupied(iRow
, iColumn
)) {
108 glTranslatef(glfX
, glfY
, 0.0);
109 glScalef(0.45, 0.45, 0.45);
112 glBegin(GL_QUAD_STRIP
);
114 glColor3f( 0.65, 0.3, 0.05 );
116 glColor3f( 0.05, 0.8, 0.15 );
118 glMaterialfv(GL_FRONT
, GL_DIFFUSE
, bColor
? glfOrange
: glfGreen
);
119 glNormal3f(0.0, 1.0, 0.0);
120 glVertex3f(1.0, 1.0, -1.0);
121 glVertex3f(-1.0, 1.0, -1.0);
122 glVertex3f(1.0, 1.0, 1.0);
123 glVertex3f(-1.0, 1.0, 1.0);
125 glNormal3f(0.0, 0.0, 1.0);
126 glVertex3f(-1.0, 1.0, 1.0);
127 glVertex3f(1.0, 1.0, 1.0);
128 glVertex3f(-1.0, -1.0, 1.0);
129 glVertex3f(1.0, -1.0, 1.0);
131 glNormal3f(0.0, -1.0, 0.0);
132 glVertex3f(-1.0, -1.0, 1.0);
133 glVertex3f(1.0, -1.0, 1.0);
134 glVertex3f(-1.0, -1.0, -1.0);
135 glVertex3f(1.0, -1.0, -1.0);
140 glBegin( GL_QUAD_STRIP
);
142 glColor3f(0.65, 0.3, 0.05);
144 glColor3f(0.05, 0.8, 0.15);
146 glMaterialfv(GL_FRONT
, GL_DIFFUSE
, bColor
? glfOrange
: glfGreen
);
147 glNormal3f(-1.0, 0.0, 0.0);
148 glVertex3f(-1.0, 1.0, 1.0);
149 glVertex3f(-1.0, -1.0, 1.0);
150 glVertex3f(-1.0, 1.0, -1.0);
151 glVertex3f(-1.0, -1.0, -1.0);
153 glNormal3f(0.0, 0.0, -1.0);
154 glVertex3f(-1.0, 1.0, -1.0);
155 glVertex3f(-1.0, -1.0, -1.0);
156 glVertex3f(1.0, 1.0, -1.0);
157 glVertex3f(1.0, -1.0, -1.0);
159 glNormal3f(1.0, 0.0, 0.0);
160 glVertex3f(1.0, 1.0, -1.0);
161 glVertex3f(1.0, -1.0, -1.0);
162 glVertex3f(1.0, 1.0, 1.0);
163 glVertex3f(1.0, -1.0, 1.0);
176 // ------------------------------------------------------
177 // GLifeView Class Advance Definition
179 GLifeView::Advance(void)
181 if (m_glfDelta
++ > 360.0)
184 int32 gridDelay
= m_pglsState
->GridDelay();
185 if (m_iStep
++ > gridDelay
) {
187 m_pglgGrid
->Generation();
191 BRect
location(0,0,0,0);