Updated functions to use the new mesh class.

This commit is contained in:
leo 2012-04-14 02:20:27 +00:00
parent aa401846b6
commit 79ccca4296
6 changed files with 297 additions and 434 deletions

View file

@ -1,6 +1,8 @@
#include "lc_global.h"
#include "lc_mesh.h"
#include "lc_colors.h"
#include "lc_file.h"
#include "globals.h"
struct lcVertex
{
@ -37,6 +39,109 @@ void lcMesh::Create(int NumSections, int NumVertices, int NumIndices)
}
}
void lcMesh::CreateBox()
{
Create(2, 8, 36 + 24);
Vector3 Min(-0.4f, -0.4f, -0.96f);
Vector3 Max(0.4f, 0.4f, 0.16f);
float* Verts = (float*)mVertexBuffer.mData;
lcuint16* Indices = (lcuint16*)mIndexBuffer.mData;
*Verts++ = Min[0]; *Verts++ = Min[1]; *Verts++ = Min[2];
*Verts++ = Min[0]; *Verts++ = Max[1]; *Verts++ = Min[2];
*Verts++ = Max[0]; *Verts++ = Max[1]; *Verts++ = Min[2];
*Verts++ = Max[0]; *Verts++ = Min[1]; *Verts++ = Min[2];
*Verts++ = Min[0]; *Verts++ = Min[1]; *Verts++ = Max[2];
*Verts++ = Min[0]; *Verts++ = Max[1]; *Verts++ = Max[2];
*Verts++ = Max[0]; *Verts++ = Max[1]; *Verts++ = Max[2];
*Verts++ = Max[0]; *Verts++ = Min[1]; *Verts++ = Max[2];
lcMeshSection* Section = &mSections[0];
Section->ColorIndex = gDefaultColor;
Section->IndexOffset = 0;
Section->NumIndices = 36;
Section->PrimitiveType = GL_TRIANGLES;
*Indices++ = 0;
*Indices++ = 1;
*Indices++ = 2;
*Indices++ = 0;
*Indices++ = 2;
*Indices++ = 3;
*Indices++ = 7;
*Indices++ = 6;
*Indices++ = 5;
*Indices++ = 7;
*Indices++ = 5;
*Indices++ = 4;
*Indices++ = 0;
*Indices++ = 1;
*Indices++ = 5;
*Indices++ = 0;
*Indices++ = 5;
*Indices++ = 4;
*Indices++ = 2;
*Indices++ = 3;
*Indices++ = 7;
*Indices++ = 2;
*Indices++ = 7;
*Indices++ = 6;
*Indices++ = 0;
*Indices++ = 3;
*Indices++ = 7;
*Indices++ = 0;
*Indices++ = 7;
*Indices++ = 4;
*Indices++ = 1;
*Indices++ = 2;
*Indices++ = 6;
*Indices++ = 1;
*Indices++ = 6;
*Indices++ = 5;
Section = &mSections[1];
Section->ColorIndex = gEdgeColor;
Section->IndexOffset = 36 * 2;
Section->NumIndices = 24;
Section->PrimitiveType = GL_LINES;
*Indices++ = 0;
*Indices++ = 1;
*Indices++ = 1;
*Indices++ = 2;
*Indices++ = 2;
*Indices++ = 3;
*Indices++ = 3;
*Indices++ = 0;
*Indices++ = 4;
*Indices++ = 5;
*Indices++ = 5;
*Indices++ = 6;
*Indices++ = 6;
*Indices++ = 7;
*Indices++ = 7;
*Indices++ = 4;
*Indices++ = 0;
*Indices++ = 4;
*Indices++ = 1;
*Indices++ = 5;
*Indices++ = 2;
*Indices++ = 6;
*Indices++ = 3;
*Indices++ = 7;
UpdateBuffers();
}
void lcMesh::Render(int DefaultColorIdx, bool Selected, bool Focused)
{
char* ElementsOffset;
@ -144,6 +249,14 @@ bool lcMesh::MinIntersectDist(const Vector3& Start, const Vector3& End, float& M
return Hit;
}
bool lcMesh::MinIntersectDist(const Vector3& Start, const Vector3& End, float& MinDist, Vector3& Intersection)
{
if (mIndexType == GL_UNSIGNED_SHORT)
return MinIntersectDist<GLushort>(Start, End, MinDist, Intersection);
else
return MinIntersectDist<GLuint>(Start, End, MinDist, Intersection);
}
// Return true if a polygon intersects a set of planes.
static bool PolygonIntersectsPlanes(float* p1, float* p2, float* p3, float* p4, const Vector4* Planes, int NumPlanes) // TODO: move to lc_math
{
@ -231,3 +344,104 @@ bool lcMesh::IntersectsPlanes(const Vector4* Planes, int NumPlanes)
return false;
}
bool lcMesh::IntersectsPlanes(const Vector4* Planes, int NumPlanes)
{
if (mIndexType == GL_UNSIGNED_SHORT)
return IntersectsPlanes<GLushort>(Planes, NumPlanes);
else
return IntersectsPlanes<GLuint>(Planes, NumPlanes);
}
template<typename IndexType>
void lcMesh::ExportPOVRay(lcFile& File, const char* MeshName, char ColorTable[LC_MAXCOLORS][64])
{
char Line[1024];
sprintf(Line, "#declare lc_%s = union {\n", MeshName);
File.WriteLine(Line);
float* Verts = (float*)mVertexBuffer.mData;
for (int SectionIdx = 0; SectionIdx < mNumSections; SectionIdx++)
{
lcMeshSection* Section = &mSections[SectionIdx];
if (Section->PrimitiveType != GL_TRIANGLES)
continue;
IndexType* Indices = (IndexType*)mIndexBuffer.mData + Section->IndexOffset / sizeof(IndexType);
File.WriteLine(" mesh {\n");
for (int Idx = 0; Idx < Section->NumIndices; Idx += 3)
{
sprintf(Line, " triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n",
-Verts[Indices[Idx+0]*3+1], -Verts[Indices[Idx+0]*3], Verts[Indices[Idx+0]*3+2],
-Verts[Indices[Idx+1]*3+1], -Verts[Indices[Idx+1]*3], Verts[Indices[Idx+1]*3+2],
-Verts[Indices[Idx+2]*3+1], -Verts[Indices[Idx+2]*3], Verts[Indices[Idx+2]*3+2]);
File.WriteLine(Line);
}
if (Section->ColorIndex != gDefaultColor)
{
sprintf(Line, "material { texture { %s normal { bumps 0.1 scale 2 } } }", ColorTable[Section->ColorIndex]);
File.WriteLine(Line);
}
File.WriteLine(" }\n");
}
}
void lcMesh::ExportPOVRay(lcFile& File, const char* MeshName, char ColorTable[LC_MAXCOLORS][64])
{
if (mIndexType == GL_UNSIGNED_SHORT)
ExportPOVRay<GLushort>(File, MeshName, ColorTable);
else
ExportPOVRay<GLuint>(File, MeshName, ColorTable);
}
template<typename IndexType>
void lcMesh::ExportWavefrontIndices(lcFile& File, int DefaultColorIndex, int VertexOffset)
{
char Line[1024];
float* Verts = (float*)mVertexBuffer.mData;
for (int SectionIdx = 0; SectionIdx < mNumSections; SectionIdx++)
{
lcMeshSection* Section = &mSections[SectionIdx];
if (Section->PrimitiveType != GL_TRIANGLES)
continue;
IndexType* Indices = (IndexType*)mIndexBuffer.mData + Section->IndexOffset / sizeof(IndexType);
if (Section->ColorIndex == gDefaultColor)
sprintf(Line, "usemtl %s\n", altcolornames[DefaultColorIndex]);
else
sprintf(Line, "usemtl %s\n", altcolornames[Section->ColorIndex]);
File.WriteLine(Line);
for (int Idx = 0; Idx < Section->NumIndices; Idx += 3)
{
long int idx1 = Indices[Idx + 0] + VertexOffset;
long int idx2 = Indices[Idx + 1] + VertexOffset;
long int idx3 = Indices[Idx + 2] + VertexOffset;
if (idx1 != idx2 && idx1 != idx3 && idx2 != idx3)
sprintf(Line, "f %ld %ld %ld\n", idx1, idx2, idx3);
File.WriteLine(Line);
}
}
File.WriteLine("\n");
}
void lcMesh::ExportWavefrontIndices(lcFile& File, int DefaultColorIndex, int VertexOffset)
{
if (mIndexType == GL_UNSIGNED_SHORT)
ExportWavefrontIndices<GLushort>(File, DefaultColorIndex, VertexOffset);
else
ExportWavefrontIndices<GLuint>(File, DefaultColorIndex, VertexOffset);
}

View file

@ -112,28 +112,24 @@ public:
~lcMesh();
void Create(int NumSections, int NumVertices, int NumIndices);
void CreateBox();
void Render(int ColorIdx, bool Selected, bool Focused);
bool MinIntersectDist(const Vector3& Start, const Vector3& End, float& MinDist, Vector3& Intersection)
{
if (mIndexType == GL_UNSIGNED_SHORT)
return MinIntersectDist<GLushort>(Start, End, MinDist, Intersection);
else
return MinIntersectDist<GLuint>(Start, End, MinDist, Intersection);
}
template<typename IndexType>
void ExportPOVRay(lcFile& File, const char* MeshName, char ColorTable[LC_MAXCOLORS][64]);
void ExportPOVRay(lcFile& File, const char* MeshName, char ColorTable[LC_MAXCOLORS][64]);
bool IntersectsPlanes(const Vector4* Planes, int NumPlanes)
{
if (mIndexType == GL_UNSIGNED_SHORT)
return IntersectsPlanes<GLushort>(Planes, NumPlanes);
else
return IntersectsPlanes<GLuint>(Planes, NumPlanes);
}
template<typename IndexType>
void ExportWavefrontIndices(lcFile& File, int DefaultColorIndex, int VertexOffset);
void ExportWavefrontIndices(lcFile& File, int DefaultColorIndex, int VertexOffset);
template<typename IndexType>
bool MinIntersectDist(const Vector3& Start, const Vector3& End, float& MinDist, Vector3& Intersection);
bool MinIntersectDist(const Vector3& Start, const Vector3& End, float& MinDist, Vector3& Intersection);
template<typename IndexType>
bool IntersectsPlanes(const Vector4* Planes, int NumPlanes);
bool IntersectsPlanes(const Vector4* Planes, int NumPlanes);
void UpdateBuffers()
{

View file

@ -211,7 +211,6 @@ void PieceInfo::LoadIndex(lcFile& file)
// TODO: don't change ref. if we're reloading ?
m_nRef = 0;
m_nVertexCount = 0;
m_fVertexArray = NULL;
m_nGroupCount = 0;
m_pGroups = NULL;
m_nBoxList = 0;
@ -244,7 +243,6 @@ void PieceInfo::CreatePlaceholder(const char* Name)
{
m_nRef = 0;
m_nVertexCount = 0;
m_fVertexArray = NULL;
m_nGroupCount = 0;
m_pGroups = NULL;
m_nBoxList = 0;
@ -639,20 +637,11 @@ void PieceInfo::BuildMesh(void* Data, int* SectionIndices)
lcuint16 ConnectionCount = LCUINT16(*((lcuint16*)bytes));
bytes += 2 + (1 + 6 * 2) * ConnectionCount;
bytes++; // TextureCount
bytes += sizeof(lcuint16); // m_nGroupCount
bytes += sizeof(lcuint16); // GroupCount
for (DRAWGROUP* pGroup = m_pGroups; sh--; pGroup++)
while (sh--)
{
lcuint8 bt = *bytes;
bytes++;
pGroup->connections[bt] = 0xFFFF;
while (bt--)
{
pGroup->connections[bt] = LCUINT16(*((lcuint16*)bytes));
bytes += sizeof(lcuint16);
}
bytes += 1 + 2 * *bytes;
switch (*bytes)
{
@ -714,111 +703,7 @@ void PieceInfo::LoadInformation()
{
if (m_nFlags & LC_PIECE_PLACEHOLDER)
{
m_nGroupCount = 1;
m_pGroups = (DRAWGROUP*)malloc(sizeof(DRAWGROUP)*m_nGroupCount);
memset(m_pGroups, 0, sizeof(DRAWGROUP)*m_nGroupCount);
int verts = 8;
m_fVertexArray = (float*)malloc(3*sizeof(float)*verts);
m_nVertexCount = verts;
Vector3 Min(-0.4f, -0.4f, -0.96f);
Vector3 Max(0.4f, 0.4f, 0.16f);
m_fVertexArray[0*3+0] = Min[0]; m_fVertexArray[0*3+1] = Min[1]; m_fVertexArray[0*3+2] = Min[2];
m_fVertexArray[1*3+0] = Min[0]; m_fVertexArray[1*3+1] = Max[1]; m_fVertexArray[1*3+2] = Min[2];
m_fVertexArray[2*3+0] = Max[0]; m_fVertexArray[2*3+1] = Max[1]; m_fVertexArray[2*3+2] = Min[2];
m_fVertexArray[3*3+0] = Max[0]; m_fVertexArray[3*3+1] = Min[1]; m_fVertexArray[3*3+2] = Min[2];
m_fVertexArray[4*3+0] = Min[0]; m_fVertexArray[4*3+1] = Min[1]; m_fVertexArray[4*3+2] = Max[2];
m_fVertexArray[5*3+0] = Min[0]; m_fVertexArray[5*3+1] = Max[1]; m_fVertexArray[5*3+2] = Max[2];
m_fVertexArray[6*3+0] = Max[0]; m_fVertexArray[6*3+1] = Max[1]; m_fVertexArray[6*3+2] = Max[2];
m_fVertexArray[7*3+0] = Max[0]; m_fVertexArray[7*3+1] = Min[1]; m_fVertexArray[7*3+2] = Max[2];
m_pGroups->connections[0] = 0xFFFF;
m_pGroups->drawinfo = malloc((1 + 1 + 3 + 36 + 1 + 3 + 24) * 2);
lcuint16* drawinfo = (lcuint16*)m_pGroups->drawinfo;
*drawinfo++ = 2;
*drawinfo++ = LC_COL_DEFAULT;
*drawinfo++ = 0;
*drawinfo++ = 36;
*drawinfo++ = 0;
*drawinfo++ = 1;
*drawinfo++ = 2;
*drawinfo++ = 0;
*drawinfo++ = 2;
*drawinfo++ = 3;
*drawinfo++ = 7;
*drawinfo++ = 6;
*drawinfo++ = 5;
*drawinfo++ = 7;
*drawinfo++ = 5;
*drawinfo++ = 4;
*drawinfo++ = 0;
*drawinfo++ = 1;
*drawinfo++ = 5;
*drawinfo++ = 0;
*drawinfo++ = 5;
*drawinfo++ = 4;
*drawinfo++ = 2;
*drawinfo++ = 3;
*drawinfo++ = 7;
*drawinfo++ = 2;
*drawinfo++ = 7;
*drawinfo++ = 6;
*drawinfo++ = 0;
*drawinfo++ = 3;
*drawinfo++ = 7;
*drawinfo++ = 0;
*drawinfo++ = 7;
*drawinfo++ = 4;
*drawinfo++ = 1;
*drawinfo++ = 2;
*drawinfo++ = 6;
*drawinfo++ = 1;
*drawinfo++ = 6;
*drawinfo++ = 5;
*drawinfo++ = 0;
*drawinfo++ = LC_COL_EDGES;
*drawinfo++ = 0;
*drawinfo++ = 0;
*drawinfo++ = 24;
*drawinfo++ = 0;
*drawinfo++ = 1;
*drawinfo++ = 1;
*drawinfo++ = 2;
*drawinfo++ = 2;
*drawinfo++ = 3;
*drawinfo++ = 3;
*drawinfo++ = 0;
*drawinfo++ = 4;
*drawinfo++ = 5;
*drawinfo++ = 5;
*drawinfo++ = 6;
*drawinfo++ = 6;
*drawinfo++ = 7;
*drawinfo++ = 7;
*drawinfo++ = 4;
*drawinfo++ = 0;
*drawinfo++ = 4;
*drawinfo++ = 1;
*drawinfo++ = 5;
*drawinfo++ = 2;
*drawinfo++ = 6;
*drawinfo++ = 3;
*drawinfo++ = 7;
mMesh->CreateBox();
return;
}
@ -831,7 +716,6 @@ void PieceInfo::LoadInformation()
float scale, shift;
lcint16* shorts;
// We don't want memory leaks.
FreeInformation();
// Open pieces.bin and buffer the information we need.
@ -1013,7 +897,7 @@ void PieceInfo::LoadInformation()
mMesh = new lcMesh();
mMesh->Create(NumSections, NumVertices, NumIndices);
m_nVertexCount = verts;
m_nVertexCount = NumVertices;
float* OutVertex = (float*)mMesh->mVertexBuffer.mData;
@ -1058,9 +942,6 @@ void PieceInfo::FreeInformation()
free(m_pGroups);
m_pGroups = NULL;
}
if (m_nFlags & LC_PIECE_LONGDATA_INDICES)
m_nFlags &= ~LC_PIECE_LONGDATA_INDICES;
}
// Zoom extents for the preview window and print catalog
@ -1219,124 +1100,3 @@ void PieceInfo::RenderPiece(int nColor)
{
mMesh->Render(nColor, false, false);
}
void PieceInfo::WriteWavefront(FILE* file, unsigned char color, unsigned long* start)
{
unsigned short group;
const char* colname;
for (group = 0; group < m_nGroupCount; group++)
{
if (m_nFlags & LC_PIECE_LONGDATA_INDICES)
{
unsigned long* info = (unsigned long*)m_pGroups[group].drawinfo;
unsigned long count, colors = *info;
info++;
while (colors--)
{
if (*info == LC_COL_DEFAULT)
colname = altcolornames[color];
else
{
if (*info >= LC_MAXCOLORS)
{
info++;
info += *info + 1;
info += *info + 1;
info += *info + 1;
continue;
}
colname = altcolornames[*info];
}
info++;
// skip if color only has lines
if ((info[0] == 0) && (info[1] == 0))
{
info += 2;
info += *info + 1;
continue;
}
fprintf(file, "usemtl %s\n", colname);
for (count = *info, info++; count; count -= 4)
{
fprintf(file, "f %ld %ld %ld %ld\n", info[0] + *start, info[1] + *start, info[2] + *start, info[3] + *start);
info += 4;
}
for (count = *info, info++; count; count -= 3)
{
long int idx1 = info[0] + *start;
long int idx2 = info[1] + *start;
long int idx3 = info[2] + *start;
if (idx1 != idx2 && idx1 != idx3 && idx2 != idx3)
fprintf(file, "f %ld %ld %ld\n", idx1, idx2, idx3);
info += 3;
}
info += *info + 1;
}
}
else
{
unsigned short* info = (unsigned short*)m_pGroups[group].drawinfo;
unsigned short count, colors = *info;
info++;
while (colors--)
{
if (*info == LC_COL_DEFAULT)
colname = altcolornames[color];
else
{
if (*info >= LC_MAXCOLORS)
{
info++;
info += *info + 1;
info += *info + 1;
info += *info + 1;
continue;
}
colname = altcolornames[*info];
}
info++;
// skip if color only has lines
if ((info[0] == 0) && (info[1] == 0))
{
info += 2;
info += *info + 1;
continue;
}
fprintf(file, "usemtl %s\n", colname);
for (count = *info, info++; count; count -= 4)
{
fprintf(file, "f %ld %ld %ld %ld\n", info[0] + *start, info[1] + *start, info[2] + *start, info[3] + *start);
info += 4;
}
for (count = *info, info++; count; count -= 3)
{
long int idx1 = info[0] + *start;
long int idx2 = info[1] + *start;
long int idx3 = info[2] + *start;
if (idx1 != idx2 && idx1 != idx3 && idx2 != idx3)
fprintf(file, "f %ld %ld %ld\n", idx1, idx2, idx3);
info += 3;
}
info += *info + 1;
}
}
}
*start += m_nVertexCount;
fputs("\n", file);
}

View file

@ -17,29 +17,11 @@
#define LC_PIECE_NAME_LEN 256
class Texture;
struct CONNECTIONINFO
{
unsigned char type;
float center[3];
float normal[3];
};
struct DRAWGROUP
{
unsigned short connections[6];
void* drawinfo;
};
struct TEXTURE
{
Texture* texture;
unsigned char color;
float vertex[4][3];
float coords[4][2];
};
unsigned char ConvertColor(int c);
class PieceInfo
@ -82,7 +64,6 @@ class PieceInfo
void ZoomExtents(float Fov, float Aspect, float* EyePos = NULL) const;
void RenderOnce(int nColor);
void RenderPiece(int nColor);
void WriteWavefront(FILE* file, unsigned char color, unsigned long* start);
// Implementation
GLuint GetBoxDisplayList()
@ -112,7 +93,6 @@ public:
// Nobody should change these
unsigned char m_nFlags;
unsigned long m_nVertexCount;
float* m_fVertexArray;
unsigned short m_nGroupCount;
DRAWGROUP* m_pGroups;

View file

@ -3,6 +3,7 @@
#include "lc_global.h"
#include "lc_math.h"
#include "lc_mesh.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -3990,124 +3991,8 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
*Ptr = '_';
sprintf(PieceTable + Index * LC_PIECE_NAME_LEN, "lc_%s", Name);
sprintf(Line, "#declare lc_%s = union {\n", Name);
POVFile.WriteLine(Line);
for (lcuint16 g = 0; g < Info->m_nGroupCount; g++)
{
if (Info->m_nFlags & LC_PIECE_LONGDATA_INDICES)
{
lcuint32* info = (lcuint32*)Info->m_pGroups[g].drawinfo;
lcuint32 count, curcolor, colors = *info;
info++;
while (colors--)
{
curcolor = *info;
info++;
// Skip if color only has lines.
if ((*info == 0) && (info[1] == 0))
{
info += 2;
info += *info + 1;
continue;
}
POVFile.WriteLine(" mesh {\n");
for (count = *info, info++; count; count -= 4)
{
sprintf(Line, " triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n",
-Info->m_fVertexArray[info[0]*3+1], -Info->m_fVertexArray[info[0]*3], Info->m_fVertexArray[info[0]*3+2],
-Info->m_fVertexArray[info[1]*3+1], -Info->m_fVertexArray[info[1]*3], Info->m_fVertexArray[info[1]*3+2],
-Info->m_fVertexArray[info[2]*3+1], -Info->m_fVertexArray[info[2]*3], Info->m_fVertexArray[info[2]*3+2]);
POVFile.WriteLine(Line);
sprintf(Line, " triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n",
-Info->m_fVertexArray[info[2]*3+1], -Info->m_fVertexArray[info[2]*3], Info->m_fVertexArray[info[2]*3+2],
-Info->m_fVertexArray[info[3]*3+1], -Info->m_fVertexArray[info[3]*3], Info->m_fVertexArray[info[3]*3+2],
-Info->m_fVertexArray[info[0]*3+1], -Info->m_fVertexArray[info[0]*3], Info->m_fVertexArray[info[0]*3+2]);
POVFile.WriteLine(Line);
info += 4;
}
for (count = *info, info++; count; count -= 3)
{
sprintf(Line, " triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n",
-Info->m_fVertexArray[info[0]*3+1], -Info->m_fVertexArray[info[0]*3], Info->m_fVertexArray[info[0]*3+2],
-Info->m_fVertexArray[info[1]*3+1], -Info->m_fVertexArray[info[1]*3], Info->m_fVertexArray[info[1]*3+2],
-Info->m_fVertexArray[info[2]*3+1], -Info->m_fVertexArray[info[2]*3], Info->m_fVertexArray[info[2]*3+2]);
POVFile.WriteLine(Line);
info += 3;
}
info += *info + 1;
if (curcolor != LC_COL_DEFAULT && curcolor != LC_COL_EDGES)
{
sprintf(Line, "material { texture { %s normal { bumps 0.1 scale 2 } } }", ColorTable[curcolor]);
POVFile.WriteLine(Line);
}
POVFile.WriteLine(" }\n");
}
}
else
{
unsigned short* info = (unsigned short*)Info->m_pGroups[g].drawinfo;
unsigned short count, curcolor, colors = *info;
info++;
while (colors--)
{
curcolor = *info;
info++;
// Skip if color only has lines.
if ((*info == 0) && (info[1] == 0))
{
info += 2;
info += *info + 1;
continue;
}
POVFile.WriteLine(" mesh {\n");
for (count = *info, info++; count; count -= 4)
{
sprintf(Line, " triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n",
-Info->m_fVertexArray[info[0]*3+1], -Info->m_fVertexArray[info[0]*3], Info->m_fVertexArray[info[0]*3+2],
-Info->m_fVertexArray[info[1]*3+1], -Info->m_fVertexArray[info[1]*3], Info->m_fVertexArray[info[1]*3+2],
-Info->m_fVertexArray[info[2]*3+1], -Info->m_fVertexArray[info[2]*3], Info->m_fVertexArray[info[2]*3+2]);
POVFile.WriteLine(Line);
sprintf(Line, " triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n",
-Info->m_fVertexArray[info[2]*3+1], -Info->m_fVertexArray[info[2]*3], Info->m_fVertexArray[info[2]*3+2],
-Info->m_fVertexArray[info[3]*3+1], -Info->m_fVertexArray[info[3]*3], Info->m_fVertexArray[info[3]*3+2],
-Info->m_fVertexArray[info[0]*3+1], -Info->m_fVertexArray[info[0]*3], Info->m_fVertexArray[info[0]*3+2]);
POVFile.WriteLine(Line);
info += 4;
}
for (count = *info, info++; count; count -= 3)
{
sprintf(Line, " triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n",
-Info->m_fVertexArray[info[0]*3+1], -Info->m_fVertexArray[info[0]*3], Info->m_fVertexArray[info[0]*3+2],
-Info->m_fVertexArray[info[1]*3+1], -Info->m_fVertexArray[info[1]*3], Info->m_fVertexArray[info[1]*3+2],
-Info->m_fVertexArray[info[2]*3+1], -Info->m_fVertexArray[info[2]*3], Info->m_fVertexArray[info[2]*3+2]);
POVFile.WriteLine(Line);
info += 3;
}
info += *info + 1;
if (curcolor != LC_COL_DEFAULT && curcolor != LC_COL_EDGES)
{
sprintf(Line, "material { texture { %s normal { bumps 0.1 scale 2 } } }", ColorTable[curcolor]);
POVFile.WriteLine(Line);
}
POVFile.WriteLine(" }\n");
}
}
}
Info->mMesh->ExportPOVRay(POVFile, Name, ColorTable);
POVFile.WriteLine("}\n\n");
@ -4208,9 +4093,17 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
if (!SystemDoDialog(LC_DLG_WAVEFRONT, filename))
break;
lcDiskFile OBJFile;
char Line[1024];
if (!OBJFile.Open(filename, "wt"))
{
SystemDoMessageBox("Could not open file for writing.", LC_MB_OK|LC_MB_ICONERROR);
break;
}
char buf[LC_MAXPATH], *ptr;
FILE* stream = fopen(filename, "wt");
unsigned long vert = 1, i;
lcuint32 vert = 1, i;
Piece* pPiece;
const char* OldLocale = setlocale(LC_NUMERIC, "C");
@ -4226,11 +4119,20 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
else
ptr = buf;
}
fputs("# Model exported from LeoCAD\n", stream);
OBJFile.WriteLine("# Model exported from LeoCAD\n");
if (strlen(buf) != 0)
fprintf(stream,"# Original name: %s\n", ptr);
{
sprintf(Line, "# Original name: %s\n", ptr);
OBJFile.WriteLine(Line);
}
if (strlen(m_strAuthor))
fprintf(stream, "# Author: %s\n", m_strAuthor);
{
sprintf(Line, "# Author: %s\n", m_strAuthor);
OBJFile.WriteLine(Line);
}
strcpy(buf, filename);
ptr = strrchr(buf, '.');
@ -4250,7 +4152,8 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
ptr = buf;
}
fprintf(stream, "#\n\nmtllib %s\n\n", ptr);
sprintf(Line, "#\n\nmtllib %s\n\n", ptr);
OBJFile.WriteLine(Line);
FILE* mat = fopen(buf, "wt");
fputs("# Colors used by LeoCAD\n# You need to add transparency values\n#\n\n", mat);
@ -4265,28 +4168,35 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
pPiece->GetRotation(rot);
Matrix mat(rot, pos);
PieceInfo* pInfo = pPiece->GetPieceInfo();
float* Verts = (float*)pInfo->mMesh->mVertexBuffer.mData;
for (i = 0; i < pInfo->m_nVertexCount*3; i += 3)
{
mat.TransformPoint(tmp, &pInfo->m_fVertexArray[i]);
fprintf(stream, "v %.2f %.2f %.2f\n", tmp[0], tmp[1], tmp[2]);
mat.TransformPoint(tmp, &Verts[i]);
sprintf(Line, "v %.2f %.2f %.2f\n", tmp[0], tmp[1], tmp[2]);
OBJFile.WriteLine(Line);
}
fputs("#\n\n", stream);
OBJFile.WriteLine("#\n\n");
}
for (pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext)
{
PieceInfo* Info = pPiece->GetPieceInfo();
strcpy(buf, pPiece->GetName());
for (i = 0; i < strlen(buf); i++)
if ((buf[i] == '#') || (buf[i] == ' '))
buf[i] = '_';
fprintf(stream, "g %s\n", buf);
pPiece->GetPieceInfo()->WriteWavefront(stream, pPiece->mColorCode, &vert);
sprintf(Line, "g %s\n", buf);
OBJFile.WriteLine(Line);
Info->mMesh->ExportWavefrontIndices(OBJFile, pPiece->mColorCode, vert);
vert += Info->m_nVertexCount;
}
setlocale(LC_NUMERIC, OldLocale);
fclose(stream);
} break;
case LC_FILE_PROPERTIES:

View file

@ -1,4 +1,5 @@
#include "lc_global.h"
#include "lc_mesh.h"
#include "Tools.h"
#include "resource.h"
#include "PrefSht.h"
@ -484,9 +485,11 @@ void Export3DStudio()
pPiece->GetPosition(pos);
pPiece->GetRotation(rot);
Matrix mat(rot, pos);
float* Verts = (float*)pInfo->mMesh->mVertexBuffer.mData;
for (c = 0; c < pInfo->m_nVertexCount; c++)
{
mat.TransformPoint(tmp, &pInfo->m_fVertexArray[c*3]);
mat.TransformPoint(tmp, &Verts[c*3]);
mobj->vertexarray[c].x = tmp[0];
mobj->vertexarray[c].y = tmp[1];
mobj->vertexarray[c].z = tmp[2];