Teapot 3D model data moved to app resources

* GLTeapot 3D model data moved from the text file "teapot.data" into
  Application resources;
* Fixes #4934 and completes GCI 2011 task made by Aleksas Pantechovskis.
  Thanks for contribution!!!

Signed-off-by: Siarzhuk Zharski <zharik@gmx.li>
This commit is contained in:
Aleksas Pantechovskis 2011-12-05 22:35:20 +01:00 committed by Siarzhuk Zharski
parent a2ef4fa88a
commit 1bc51c2392
4 changed files with 2531 additions and 58 deletions

View File

@ -16,12 +16,17 @@
This file may be used under the terms of the Be Sample Code License.
*/
#include "GLObject.h"
#include <Application.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <InterfaceKit.h>
#include <Resources.h>
#include "glob.h"
struct material {
float ambient[3], diffuse[3], specular[3];
};
@ -141,6 +146,7 @@ GLObject::MenuInvoked(BPoint point)
setEvent(fObjView->drawEvent);
}
int
GLObject::Solidity() const
{
@ -148,6 +154,7 @@ GLObject::Solidity() const
}
bool
GLObject::SpinIt()
{
@ -200,55 +207,67 @@ GLObject::Draw(bool forID, float IDcolor[])
}
TriangleObject::TriangleObject(ObjectView* ov, char* filename)
: GLObject(ov),
fPoints(100,100),
fTriangles(100,100),
fQs(50,50)
TriangleObject::TriangleObject(ObjectView* ov)
:
GLObject(ov),
fStatus(B_NO_INIT),
fPoints(100, 100),
fTriangles(100, 100),
fQs(50, 50)
{
BResources *res = BApplication::AppResources();
if (res == NULL)
return;
size_t size = 0;
int32 *arrayOfPoints
= (int32*)res->LoadResource(B_RAW_TYPE, "points", &size);
if (arrayOfPoints == NULL)
return;
float maxp = 0;
int numPt,numTri;
FILE* f = fopen(filename,"r");
fscanf(f,"%d",&numPt);
// printf("Points: %d\n",numPt);
for (int i = 0; i < numPt; i++) {
size_t numPt = size / sizeof(int32);
for (size_t i = 0; i < numPt; i += 6) {
point p;
fscanf(f,"%f %f %f %f %f %f",
&p.x,
&p.y,
&p.z,
&p.nx,
&p.ny,
&p.nz);
p.x = 1e-6 * arrayOfPoints[i];
p.y = 1e-6 * arrayOfPoints[i + 1];
p.z = 1e-6 * arrayOfPoints[i + 2];
p.nx = 1e-6 * arrayOfPoints[i + 3];
p.ny = 1e-6 * arrayOfPoints[i + 4];
p.nz = 1e-6 * arrayOfPoints[i + 5];
if (fabs(p.x) > maxp)
maxp = fabs(p.x);
if (fabs(p.y) > maxp)
maxp = fabs(p.y);
if (fabs(p.z) > maxp)
maxp = fabs(p.z);
fPoints.add(p);
}
for (int i = 0; i < fPoints.num_items; i++) {
fPoints[i].x /= maxp;
fPoints[i].y /= maxp;
fPoints[i].z /= maxp;
}
fscanf(f,"%d",&numTri);
// printf("Triangles: %d\n",numTri);
int tpts = 0;
for (int i = 0; i < numTri; i++) {
int32 *arrayOfTriangles
= (int32*)res->LoadResource(B_RAW_TYPE, "triangles", &size);
if (arrayOfTriangles == NULL)
return;
size_t numTriPoints = size / sizeof(int32);
for (size_t i = 0; i < numTriPoints; i += 3) {
tri t;
fscanf(f,"%d %d %d",
&t.p1,
&t.p2,
&t.p3);
t.p1 = arrayOfTriangles[i];
t.p2 = arrayOfTriangles[i + 1];
t.p3 = arrayOfTriangles[i + 2];
fTriangles.add(t);
tpts += 3;
}
size_t numTri = numTriPoints / 3;
int qpts = 4;
int qp[1024];
quadStrip q;
@ -259,37 +278,37 @@ TriangleObject::TriangleObject(ObjectView* ov, char* filename)
q.pts[1] = fTriangles[0].p3;
q.pts[3] = fTriangles[1].p3;
for (int i = 2; i < numTri; i += 2) {
if ((fTriangles[i-1].p1 == fTriangles[i].p2) &&
(fTriangles[i-1].p3 == fTriangles[i].p3)) {
q.pts[q.numpts++] = fTriangles[i+1].p1;
q.pts[q.numpts++] = fTriangles[i+1].p3;
for (size_t i = 2; i < numTri; i += 2) {
if ((fTriangles[i - 1].p1 == fTriangles[i].p2) &&
(fTriangles[i - 1].p3 == fTriangles[i].p3)) {
q.pts[q.numpts++] = fTriangles[i + 1].p1;
q.pts[q.numpts++] = fTriangles[i + 1].p3;
qpts+=2;
} else {
int *np = (int*)malloc(sizeof(int)*q.numpts);
memcpy(np,qp,q.numpts*sizeof(int));
memcpy(np, qp, q.numpts * sizeof(int));
quadStrip nqs;
nqs.numpts = q.numpts;
nqs.pts = np;
fQs.add(nqs);
qpts += 4;
q.numpts = 4;
q.pts[2] = fTriangles[i].p1;
q.pts[0] = fTriangles[i].p2;
q.pts[1] = fTriangles[i].p3;
q.pts[3] = fTriangles[i+1].p3;
q.pts[3] = fTriangles[i + 1].p3;
}
}
int* np = (int*)malloc(sizeof(int)*q.numpts);
memcpy(np,qp,q.numpts*sizeof(int));
memcpy(np, qp, q.numpts * sizeof(int));
quadStrip nqs;
nqs.numpts = q.numpts;
nqs.pts = np;
fQs.add(nqs);
fclose(f);
fStatus = B_OK;
}
@ -301,6 +320,13 @@ TriangleObject::~TriangleObject()
}
status_t
TriangleObject::InitCheck() const
{
return fStatus;
}
void
TriangleObject::DoDrawing(bool forID)
{

View File

@ -50,7 +50,7 @@ class GLObject {
void RotateWorldSpace(float rx, float ry);
virtual void MenuInvoked(BPoint point);
virtual void DoDrawing(bool forID) {};
int Solidity() const;
int Solidity() const;
float x, y, z;
Quaternion fRotation;
@ -68,11 +68,13 @@ class GLObject {
class TriangleObject : public GLObject {
public:
TriangleObject(ObjectView* ov, char* filename);
TriangleObject(ObjectView* ov);
virtual ~TriangleObject();
status_t InitCheck() const;
virtual void DoDrawing(bool forID);
private:
status_t fStatus;
BufferArray<point> fPoints;
BufferArray<tri> fTriangles;
BufferArray<quadStrip> fQs;

File diff suppressed because it is too large Load Diff

View File

@ -18,9 +18,8 @@
#include "ObjectView.h"
#include <stdio.h>
#include <stdlib.h>
#include <Application.h>
#include <Catalog.h>
#include <Cursor.h>
#include <InterfaceKit.h>
#include <FindDirectory.h>
@ -29,8 +28,8 @@
#include "GLObject.h"
#include "ResScroll.h"
#define teapotData "teapot.data"
char teapotPath[PATH_MAX];
#undef B_TRANSLATE_CONTEXT
#define B_TRANSLATE_CONTEXT "ObjectView"
float displayScale = 1.0;
float depthOfView = 30.0;
@ -50,6 +49,9 @@ float red[3] = {1.0, 0.0, 0.0};
float* bgColor = black;
const char *kNoResourceError = B_TRANSLATE("The Teapot 3D model was "
"not found in application resources. "
"Please repair the program installation.");
struct light {
float *ambient;
@ -166,12 +168,18 @@ ObjectView::ObjectView(BRect rect, const char *name, ulong resizingMode,
quittingSem = create_sem(1, "quitting sem");
drawEvent = create_sem(0, "draw event");
char findDir[PATH_MAX];
find_directory(B_SYSTEM_DATA_DIRECTORY, -1, true, findDir, PATH_MAX);
sprintf(teapotPath, "%s/%s", findDir, teapotData);
fObjListLock.Lock();
fObjects.AddItem(new TriangleObject(this, teapotPath));
fObjListLock.Unlock();
TriangleObject *Tri = new TriangleObject(this);
if (Tri->InitCheck() == B_OK) {
fObjListLock.Lock();
fObjects.AddItem(Tri);
fObjListLock.Unlock();
} else {
BAlert *NoResourceAlert = new BAlert(B_TRANSLATE("Error"),
kNoResourceError, B_TRANSLATE("OK"), NULL, NULL,
B_WIDTH_AS_USUAL, B_OFFSET_SPACING, B_STOP_ALERT);
NoResourceAlert->Go();
delete Tri;
}
}
@ -304,12 +312,23 @@ ObjectView::MessageReceived(BMessage* msg)
fForceRedraw = true;
setEvent(drawEvent);
break;
case kMsgAddModel:
fObjListLock.Lock();
fObjects.AddItem(new TriangleObject(this, teapotPath));
fObjListLock.Unlock();
case kMsgAddModel:
{
TriangleObject *Tri = new TriangleObject(this);
if (Tri->InitCheck() == B_OK) {
fObjListLock.Lock();
fObjects.AddItem(Tri);
fObjListLock.Unlock();
} else {
BAlert *NoResourceAlert = new BAlert(B_TRANSLATE("Error"),
kNoResourceError, B_TRANSLATE("OK"), NULL, NULL,
B_WIDTH_AS_USUAL, B_OFFSET_SPACING, B_STOP_ALERT);
NoResourceAlert->Go();
delete Tri;
}
setEvent(drawEvent);
break;
}
case kMsgLights:
{
msg->FindPointer("source", reinterpret_cast<void**>(&item));