2 * Copyright 2008 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
11 * Original Be Sample source modified to use a quaternion for the object's orientation
15 Copyright 1999, Be Incorporated. All Rights Reserved.
16 This file may be used under the terms of the Be Sample Code License.
22 #include <Application.h>
24 #include <InterfaceKit.h>
25 #include <Resources.h>
31 float ambient
[3], diffuse
[3], specular
[3];
34 float *colors
[] = {NULL
, white
, yellow
, blue
, red
, green
};
36 material materials
[] = {
39 {0.1745, 0.03175, 0.03175},
40 {0.61424, 0.10136, 0.10136},
41 {0.727811, 0.626959, 0.626959}
45 {0.1745, 0.1745, 0.1745},
46 {0.61424, 0.61424, 0.61424},
47 {0.727811, 0.727811, 0.727811}
51 {0.1745, 0.1745, 0.03175},
52 {0.61424, 0.61424, 0.10136},
53 {0.727811, 0.727811, 0.626959}
57 {0.03175, 0.03175, 0.1745},
58 {0.10136, 0.10136, 0.61424},
59 {0.626959, 0.626959, 0.727811}
63 {0.1745, 0.03175, 0.03175},
64 {0.61424, 0.10136, 0.10136},
65 {0.727811, 0.626959, 0.626959}
69 {0.03175, 0.1745, 0.03175},
70 {0.10136, 0.61424, 0.10136},
71 {0.626959, 0.727811, 0.626959}
75 #define USE_QUAD_STRIPS 1
77 extern long setEvent(sem_id event
);
80 GLObject::GLObject(ObjectView
* ov
)
85 fRotation(0.0f
, 0.0f
, 0.0f
, 1.0f
),
102 GLObject::MenuInvoked(BPoint point
)
104 BPopUpMenu
* m
= new BPopUpMenu("Object",false,false);
108 m
->AddItem(i
= new BMenuItem("White",NULL
));
111 m
->AddItem(i
= new BMenuItem("Yellow",NULL
));
114 m
->AddItem(i
= new BMenuItem("Blue",NULL
));
117 m
->AddItem(i
= new BMenuItem("Red",NULL
));
120 m
->AddItem(i
= new BMenuItem("Green",NULL
));
123 m
->AddSeparatorItem();
126 m
->AddItem(i
= new BMenuItem("Solid",NULL
));
129 m
->AddItem(i
= new BMenuItem("Translucent",NULL
));
132 m
->AddItem(i
= new BMenuItem("Transparent",NULL
));
137 int32 index
= m
->IndexOf(i
);
142 } else if (index
> 5) {
146 setEvent(fObjView
->drawEvent
);
151 GLObject::Solidity() const
162 c
= c
|| ((spinX
!= 0.0f
) || (spinY
!= 0.0f
));
165 RotateWorldSpace(spinY
, spinX
);
172 GLObject::Spin(float rx
, float ry
)
180 GLObject::RotateWorldSpace(float rx
, float ry
)
182 fRotation
= Quaternion(Vector3(0.0f
, 1.0f
, 0.0f
), 0.01f
* rx
) * fRotation
;
183 fRotation
= Quaternion(Vector3(1.0f
, 0.0f
, 0.0f
), 0.01f
* ry
) * fRotation
;
189 GLObject::Draw(bool forID
, float IDcolor
[])
192 glTranslatef(x
, y
, z
);
195 fRotation
.toOpenGLMatrix(mat
);
196 glMultMatrixf((GLfloat
*)mat
);
210 TriangleObject::TriangleObject(ObjectView
* ov
)
215 fTriangles(100, 100),
218 BResources
*res
= BApplication::AppResources();
224 = (int32
*)res
->LoadResource(B_RAW_TYPE
, "points", &size
);
225 if (arrayOfPoints
== NULL
)
229 size_t numPt
= size
/ sizeof(int32
);
230 for (size_t i
= 0; i
< numPt
; i
+= 6) {
232 p
.x
= 1e-6 * arrayOfPoints
[i
];
233 p
.y
= 1e-6 * arrayOfPoints
[i
+ 1];
234 p
.z
= 1e-6 * arrayOfPoints
[i
+ 2];
235 p
.nx
= 1e-6 * arrayOfPoints
[i
+ 3];
236 p
.ny
= 1e-6 * arrayOfPoints
[i
+ 4];
237 p
.nz
= 1e-6 * arrayOfPoints
[i
+ 5];
239 if (fabs(p
.x
) > maxp
)
241 if (fabs(p
.y
) > maxp
)
243 if (fabs(p
.z
) > maxp
)
249 for (int i
= 0; i
< fPoints
.num_items
; i
++) {
250 fPoints
[i
].x
/= maxp
;
251 fPoints
[i
].y
/= maxp
;
252 fPoints
[i
].z
/= maxp
;
255 int32
*arrayOfTriangles
256 = (int32
*)res
->LoadResource(B_RAW_TYPE
, "triangles", &size
);
257 if (arrayOfTriangles
== NULL
)
260 size_t numTriPoints
= size
/ sizeof(int32
);
261 for (size_t i
= 0; i
< numTriPoints
; i
+= 3) {
263 t
.p1
= arrayOfTriangles
[i
];
264 t
.p2
= arrayOfTriangles
[i
+ 1];
265 t
.p3
= arrayOfTriangles
[i
+ 2];
269 size_t numTri
= numTriPoints
/ 3;
276 q
.pts
[2] = fTriangles
[0].p1
;
277 q
.pts
[0] = fTriangles
[0].p2
;
278 q
.pts
[1] = fTriangles
[0].p3
;
279 q
.pts
[3] = fTriangles
[1].p3
;
281 for (size_t i
= 2; i
< numTri
; i
+= 2) {
282 if ((fTriangles
[i
- 1].p1
== fTriangles
[i
].p2
) &&
283 (fTriangles
[i
- 1].p3
== fTriangles
[i
].p3
)) {
284 q
.pts
[q
.numpts
++] = fTriangles
[i
+ 1].p1
;
285 q
.pts
[q
.numpts
++] = fTriangles
[i
+ 1].p3
;
288 int *np
= (int*)malloc(sizeof(int)*q
.numpts
);
289 memcpy(np
, qp
, q
.numpts
* sizeof(int));
291 nqs
.numpts
= q
.numpts
;
297 q
.pts
[2] = fTriangles
[i
].p1
;
298 q
.pts
[0] = fTriangles
[i
].p2
;
299 q
.pts
[1] = fTriangles
[i
].p3
;
300 q
.pts
[3] = fTriangles
[i
+ 1].p3
;
304 int* np
= (int*)malloc(sizeof(int)*q
.numpts
);
305 memcpy(np
, qp
, q
.numpts
* sizeof(int));
307 nqs
.numpts
= q
.numpts
;
315 TriangleObject::~TriangleObject()
317 for (int i
= 0; i
< fQs
.num_items
; i
++) {
324 TriangleObject::InitCheck() const
331 TriangleObject::DoDrawing(bool forID
)
335 c
[0][0] = materials
[color
].ambient
[0];
336 c
[0][1] = materials
[color
].ambient
[1];
337 c
[0][2] = materials
[color
].ambient
[2];
338 c
[1][0] = materials
[color
].diffuse
[0];
339 c
[1][1] = materials
[color
].diffuse
[1];
340 c
[1][2] = materials
[color
].diffuse
[2];
341 c
[2][0] = materials
[color
].specular
[0];
342 c
[2][1] = materials
[color
].specular
[1];
343 c
[2][2] = materials
[color
].specular
[2];
348 else if (solidity
== 1)
350 else if (solidity
== 2)
352 c
[0][3] = c
[1][3] = c
[2][3] = alpha
;
354 glBlendFunc(GL_SRC_ALPHA
,GL_ONE
);
356 glDepthMask(GL_FALSE
);
357 glDisable(GL_CULL_FACE
);
360 glDepthMask(GL_TRUE
);
362 glMaterialfv(GL_FRONT
, GL_AMBIENT
, c
[0]);
363 glMaterialfv(GL_FRONT
, GL_DIFFUSE
, c
[1]);
364 glMaterialfv(GL_FRONT
, GL_SPECULAR
, c
[2]);
367 glDepthMask(GL_TRUE
);
371 for (int i
= 0; i
< fQs
.num_items
; i
++) {
372 glBegin(GL_QUAD_STRIP
);
373 for (int j
= 0; j
< fQs
[i
].numpts
; j
++) {
375 fPoints
[fQs
[i
].pts
[j
]].nx
,
376 fPoints
[fQs
[i
].pts
[j
]].ny
,
377 fPoints
[fQs
[i
].pts
[j
]].nz
380 fPoints
[fQs
[i
].pts
[j
]].x
,
381 fPoints
[fQs
[i
].pts
[j
]].y
,
382 fPoints
[fQs
[i
].pts
[j
]].z
388 glBegin(GL_TRIANGLES
);
389 for (int i
= 0; i
< fTriangles
.num_items
; i
++) {
390 int v3
= fTriangles
[i
].p1
;
391 int v1
= fTriangles
[i
].p2
;
392 int v2
= fTriangles
[i
].p3
;