Custom file format ripped from a .3DS. Trouble displaying object and texture mapping.

Started by
0 comments, last by Klendathu 11 years, 9 months ago
Basically, my problem is this:
I've created one program that rips out only the relevent info from a .3ds file and stores them in a new condensed format.
My DX program then loads the info into vectors and buffers them.

Now I had this working before I tried to use U,V data, and it displayed the object fine. But my object has two texture maps. Its basically a sphere, half with a planar map for one texture and the other half with another planar map for the other texture.

Now when it try and display it, the object is messed. A jumble of polys, and the texture is highly tiled.

I'm looking for advice, not only on how to solve my problem, but also any tips in general for the program.
I'm intermediate with cpp, so please bear with me.

First the 3DS Converter.

sdObj.h

#ifndef _SDOBJ_H_
#define _SDOBJ_H_
#include <string>
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <vector>
typedef struct chunk
{
uint16_t ID;
long size;
} chunk;
typedef struct vertex
{
float x, y, z;
} vertex;
typedef struct face
{
uint16_t p1, p2, p3;
} face;
typedef struct facedesc
{
std::string materialName;
uint32_t numFacesAppliesTo;
std::vector< uint32_t > FaceNum;
} facedesc;
typedef struct uvcoord
{
float u, v;
} uvcoord;
typedef struct material
{
std::string materialName;
std::string materialFileName;
} material;
typedef struct object
{
} object;
class sdObj
{
public:
uint32_t filesize;
std::vector< uint8_t > data;
std::string objectName;
std::vector< vertex > vlist;
std::vector< uvcoord > uvlist;
std::vector< face > flist;
std::vector< facedesc > fdlist;
std::vector< material > mlist;
std::vector< face > reorder;
uint16_t matnamecount;
uint16_t matfilecount;
uint16_t nMaterials;
uint16_t fdNum;
uint16_t numFaces;
uint16_t numVerts;
uint16_t numUVs;
bool firstScan;
sdObj();
~sdObj();
bool load3DS(std::string const &filename);
bool scan3DS(std::vector< uint8_t > &data);
void fixFloats(uint32_t nVerts);
bool fileOutput();
void reorderFaces();
chunk getChunkInfo(uint32_t offset, std::vector< uint8_t > &data);
protected:
enum
{
CHUNK_VERSION = 0x0002,
CHUNK_MAIN = 0x4D4D,
CHUNK_EDIT = 0x3D3D,
CHUNK_OBJ = 0x4000,
CHUNK_TRIMESH = 0x4100,
CHUNK_VERTEX = 0x4110,
CHUNK_FACELIST = 0x4120,
CHUNK_FACEDESC = 0x4130,
CHUNK_MAPCOORDLIST = 0x4140,
CHUNK_SMOOTHGRP = 0x4150,
CHUNK_LOCALCOORD = 0x4160,
CHUNK_OBJCOLOR = 0x4165,
CHUNK_LIGHT = 0x4600,
CHUNK_SPOTLIGHT = 0x4610,
CHUNK_CAMERA = 0x4700,
CHUNK_MAT = 0xAFFF,
CHUNK_MATNAME = 0xA000,
CHUNK_AMB = 0xA010,
CHUNK_DIFF = 0xA020,
CHUNK_SPEC = 0xA030,
CHUNK_TEXMAP1 = 0xA200,
CHUNK_BUMP = 0xA230,
CHUNK_REFL = 0xA220,
CHUNK_MAPFNAME = 0xA300,
CHUNK_MAPPARAMS = 0xA351,
CHUNK_RGB1 = 0x0011,
CHUNK_KEYFRAMER = 0xB000,
CHUNK_MESHINFO = 0xB002,
CHUNK_SPOTINFO = 0xB007,
CHUNK_FRAMES = 0xB008,
CHUNK_OBJNAME = 0xB010,
CHUNK_OBJPIVOT = 0xB013,
CHUNK_POSITRACK = 0xB020,
CHUNK_ROTTRACK = 0xB021,
CHUNK_SCALETRACK = 0xB022,
CHUNK_HIPOS = 0xB030
};
};
#endif


sdObj.cpp

#include "sdObj.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <vector>
using namespace std;
sdObj::sdObj()
{
filesize = 0;
firstScan = true;
fdNum = 0;
matnamecount = 0;
matfilecount = 0;
}
sdObj::~sdObj()
{
}
bool sdObj::load3DS(string const &filename)
{

ifstream infile(filename.c_str(), ios::in | ios::binary);
if (infile.is_open())
{
infile.seekg(0, ios::end);
filesize = (long)infile.tellg();
infile.seekg(0, ios::beg);
data.resize(filesize);
infile.read((char*)&data[0], filesize);
infile.close();
if(infile.is_open())
return false;
}
return scan3DS(data);
}
void sdObj::fixFloats(uint32_t nVerts)
{
stringstream ss (stringstream::in | stringstream::out);
string check;
for(uint32_t i=0; i < nVerts; i++)
{
ss << vlist.x;
check = ss.str();
string::size_type pos = check.find("e");
if (string::npos != pos)
{
vlist.x = 0.00f;
}
ss.clear();
ss.str("");
ss << vlist.y;
check = ss.str();

pos = check.find("e");
if (string::npos != pos)
{
vlist.y = 0.00f;
}
ss.clear();
ss.str("");
ss << vlist.z;
check = ss.str();

pos = check.find("e");
if (string::npos != pos)
{
vlist.z = 0.00f;
}
ss.clear();
ss.str("");
}
}
bool sdObj::scan3DS(vector< uint8_t > &data)
{
chunk info;
bool scanDone = false;
uint32_t off = 0;
nMaterials = 0;
memcpy(&info.ID, &data[off], 2);
memcpy(&info.size, &data[off+2], 4);
if (info.ID != CHUNK_MAIN)
return false;
if (info.size != filesize)
return false;
off = 6;
while(!scanDone)
{
while(off < filesize)
{
info = getChunkInfo(off, data);
switch(info.ID)
{
case CHUNK_EDIT:
{
off += 6;
break;
}
case CHUNK_OBJ:
{
int nameoff = off + 6;
char c = 0;
string name;
do
{
memcpy (&c, &data[nameoff] , 1);
name += c;
nameoff++;
}
while(c != 0x00);

if(firstScan)
{

} else
{
objectName = name;
}
off = nameoff;
break;
}
case CHUNK_TRIMESH:
{
off += 6;
break;
}
case CHUNK_VERTEX:
{
vertex storevert;
short nVert;
int vertoff = off + 6;
memcpy (&nVert, &data[vertoff], 2);
if(firstScan)
{

} else
{
vlist.resize(nVert);
}
vertoff += 2;
int vertplus = sizeof(float);
float a = 0;
for(int i = 0; i < nVert; i++)
{
memcpy(&storevert.x, &data[vertoff], sizeof(float));
vertoff += vertplus;
memcpy(&storevert.y, &data[vertoff], sizeof(float));
vertoff += vertplus;
memcpy(&storevert.z, &data[vertoff], sizeof(float));
vertoff += vertplus;
if(firstScan)
{}
else
{
vlist.x = storevert.x;
vlist.y = storevert.y;
vlist.z = storevert.z;
}
}

if(firstScan) {}
else
{
numVerts = nVert;
fixFloats(nVert);
}
off = vertoff;
break;
}
case CHUNK_MAPCOORDLIST:
{
short nVert;
uvcoord vertmap;
int mapoff = off + 6;
int mapplus = sizeof(float);
vertmap.u = 0;
vertmap.v = 0;
memcpy(&nVert, &data[mapoff], 2);
if(firstScan)
{}
else
{
numUVs = nVert;
uvlist.resize(nVert);
}
mapoff += 2;
for(int i = 0; i < nVert; i++)
{
memcpy(&vertmap.u, &data[mapoff], sizeof(float));
mapoff += mapplus;
memcpy(&vertmap.v, &data[mapoff], sizeof(float));
mapoff += mapplus;
if(firstScan)
{}
else
{
uvlist.u = vertmap.u;
uvlist.v = vertmap.v;
}
}
off = mapoff;
break;
}
case CHUNK_FACELIST:
{
face facelist;
facelist.p1 = 0;
facelist.p2 = 0;
facelist.p3 = 0;
short nFaces;
int faceoff = off + 6;
short faceinfo = 0;
int faceplus = sizeof(unsigned short);
memcpy(&nFaces, &data[faceoff], 2);
numFaces = nFaces;
if(firstScan)
{}
else
{
flist.resize(nFaces);
}
faceoff += 2;
for(int i = 0; i < nFaces; i++)
{
memcpy(&facelist.p1, &data[faceoff], sizeof(unsigned short));
faceoff += sizeof(unsigned short);
memcpy(&facelist.p2, &data[faceoff], sizeof(unsigned short));
faceoff += sizeof(unsigned short);
memcpy(&facelist.p3, &data[faceoff], sizeof(unsigned short));
faceoff += sizeof(unsigned short);
faceoff += sizeof(short);
if(firstScan)
{}
else
{
flist.p1 = facelist.p1;
flist.p2 = facelist.p2;
flist.p3 = facelist.p3;
}
}
off = faceoff;
break;
}
case CHUNK_FACEDESC:
{
int nameoff = off + 6;
short nMats = 0;
short faceNum = 0;
char c = 0;
string name;
do
{
memcpy (&c, &data[nameoff] , 1);
name += c;
nameoff++;
}
while(c != 0x00);
memcpy(&nMats, &data[nameoff], 2);
nameoff += 2;
if(firstScan)
{}
else
{
fdlist.resize(nMaterials);
fdlist[fdNum].materialName = name;
fdlist[fdNum].numFacesAppliesTo = nMats;
fdlist[fdNum].FaceNum.resize(nMats);
}
for(int i=0; i < nMats; i++)
{
memcpy(&faceNum, &data[nameoff], 2);
nameoff += 2;
if(firstScan)
{}
else
{
fdlist[fdNum].FaceNum = faceNum;
}
}
if(firstScan)
{
}
else
{
fdNum++;
}
off = nameoff;
break;
}
case CHUNK_OBJCOLOR:
{
off += info.size;
break;
}
case CHUNK_MAT:
{
if(firstScan)
{
nMaterials++;
}
else
{
mlist.resize(nMaterials);
}
off += 6;
break;
}
case CHUNK_MATNAME:
{
unsigned char* objmatname;
objmatname = new unsigned char;
int nameoff = off + 6;
char c = 0;
string name;
do
{
memcpy (&c, &data[nameoff] , 1);
name += c;
nameoff++;
}
while(c != 0x00);
if(firstScan)
{
}
else
{
mlist[matnamecount].materialName = name;
matnamecount++;
}

off = nameoff;
break;
}
case CHUNK_MAPFNAME:
{
unsigned char* objmatfname;
objmatfname = new unsigned char;
int nameoff = off + 6;
char c = 0;
string name;
do
{
memcpy (&c, &data[nameoff] , 1);
name += c;
nameoff++;
}
while(c != 0x00);
if(firstScan)
{

}
else
{
mlist[matfilecount].materialFileName = name;
matfilecount++;
}
off = nameoff;
break;
}
case CHUNK_TEXMAP1:
{
off += 6;
break;
}
default:
{
off += info.size;
break;
}
}
}
if(firstScan)
{
firstScan = false;
off = 6;
}
else
{
scanDone = true;
//reorderFaces();
}
}
return fileOutput();
}
chunk sdObj::getChunkInfo(uint32_t offset, vector< uint8_t > &data)
{
chunk tc;
memcpy(&tc.ID, &data[offset], 2);
memcpy(&tc.size, &data[offset+2], 4);
return tc;
}
bool sdObj::fileOutput()
{
uint32_t totalsize;
uint32_t i;
string filename = "c:\\dawnstar.sd1";
ofstream outfile(filename.c_str(), ios::out | ios::binary | ios::ate);
if(outfile.is_open())
{
outfile.write((char *)&numVerts, sizeof(uint16_t));
for(i = 0; i < numVerts; i++)
{
outfile.write((char *)&vlist.x, sizeof(float));
outfile.write((char *)&vlist.y, sizeof(float));
outfile.write((char *)&vlist.z, sizeof(float));
outfile.write((char *)&uvlist.u, sizeof(float));
outfile.write((char *)&uvlist.v, sizeof(float));
}
outfile.write((char *)&nMaterials, sizeof(uint16_t));
string switchcheck;
for(i = 0; i < nMaterials; i++)
{
outfile.write(mlist.materialName.c_str(), mlist.materialName.size());
outfile.write((char *)"0x00", sizeof(uint8_t));
outfile.write(mlist.materialFileName.c_str(), mlist.materialFileName.size());
outfile.write((char *)"0x00", sizeof(uint8_t));
outfile.write((char *)&fdlist.numFacesAppliesTo, sizeof(uint16_t));
}
outfile.write((char *)&numFaces, sizeof(uint16_t));
for(i = 0; i < numFaces; i++)
{
//outfile.write((char *)&reorder.p1, sizeof(uint16_t));
//outfile.write((char *)&reorder.p2, sizeof(uint16_t));
//outfile.write((char *)&reorder.p3, sizeof(uint16_t));
outfile.write((char *)&flist.p1, sizeof(uint16_t));
outfile.write((char *)&flist.p2, sizeof(uint16_t));
outfile.write((char *)&flist.p3, sizeof(uint16_t));
}
}
outfile.close();
return true;
}
void sdObj::reorderFaces()
{
uint32_t facenumber = 0;
reorder.resize(numFaces);
for(uint32_t i = 0; i < nMaterials; i++)
{
for(uint32_t j = 0; j < fdlist.numFacesAppliesTo; j++)
{
reorder[facenumber].p1 = flist[fdlist.FaceNum[j]].p1;
reorder[facenumber].p2 = flist[fdlist.FaceNum[j]].p2;
reorder[facenumber].p3 = flist[fdlist.FaceNum[j]].p3;
facenumber++;
}
}
}



And now, the dx program.
sdImport.h

#ifndef _SDIMPORT_H_
#define _SDIMPORT_H_
#define _CRT_SECURE_NO_DEPRECATE_
#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <vector>
#include <stdint.h>
typedef struct chunk
{
uint16_t ID;
long size;
} chunk;
typedef struct vertex
{
float x, y, z;
DWORD color;
} vertex;
typedef struct face
{
uint16_t p1, p2, p3;
} face;
typedef struct texface
{
uint32_t faceid;
std::string matname;
} texface;
typedef struct uvcoord
{
float u, v;
} uvcoord;
typedef struct material
{
std::string materialName, materialFileName;
uint16_t numFacesAppliesTo;
} material;
typedef struct texTri
{
float x, y, z;
float u, v;
}texTri;
class sdObj
{
public:
sdObj();
~sdObj();
bool load( std::string const &filename );
chunk getChunkInfo( int32_t offset, std::vector< uint8_t >& data );
void testvert();
bool scan( std::vector< uint8_t >& data );
int16_t numVerts;
int16_t numFaces;
int16_t numMats;
long filesize;
std::string gObjName;
int32_t objNameSize;
uint8_t *objData;
uint16_t nMatPObj; // number of materials per object
std::vector< uvcoord > uv;
std::vector< vertex > vert;
std::vector< material > mat;
std::vector< texTri > texvert; // texture vertex object
std::vector< face > facelist;
std::vector< texface > texface; // texture face list
std::vector< face > faceload;
std::vector< vertex > vertconv;
std::string installdrive;
std::string basepath;
std::string modelpath;
std::string texturepath;
void setPaths();
};
#endif


sdImport.cpp

#include <string>
#include <fstream>
#include <vector>
#include <d3d9.h>
#include <d3dx9.h>
#include <sstream>
#include <stdint.h>
#include "sdImport.h"
using namespace std;
sdObj::sdObj()
{
nMatPObj = 0;
}
sdObj::~sdObj()
{
}
void sdObj::setPaths()
{
installdrive = "c:\\";
basepath = "3dproject\\";
modelpath = "models\\";
texturepath = "textures\\";
}
bool sdObj::load(string const &filename)
{
vector <uint8_t> funcObjData;
string fn;
setPaths();
fn.append(installdrive);
fn.append(basepath);
fn.append(modelpath);
fn.append(filename);
ifstream infile(fn.c_str(), ios::binary | ios::in);
if(!infile.is_open())
return false;
infile.seekg(0, ios::end);
filesize = (long)infile.tellg();
infile.seekg(0, ios::beg);
funcObjData.resize(filesize);
infile.read((char*)&funcObjData[0], filesize);
infile.close();
if(infile.is_open())
return false;
return scan(funcObjData);
}
chunk sdObj::getChunkInfo(int32_t offset, vector< uint8_t >& data)
{
chunk tc;
memcpy(&tc.ID, &data[offset], 2);
memcpy(&tc.size, &data[offset+2], 4);
return tc;
}
bool sdObj::scan( vector< uint8_t >& data )
{
int offset = 0;
int16_t i;
memcpy(&numVerts, &data[offset], sizeof(int16_t));
offset += sizeof(int16_t);
texvert.resize(numVerts);
int uvcount = 1;
for(i = 0; i < numVerts; i++)
{
memcpy(&texvert.x, &data[offset], sizeof(float));
offset += sizeof(float);
memcpy(&texvert.y, &data[offset], sizeof(float));
offset += sizeof(float);
memcpy(&texvert.z, &data[offset], sizeof(float));
offset += sizeof(float);
memcpy(&texvert.u, &data[offset], sizeof(float));
offset += sizeof(float);
memcpy(&texvert.v, &data[offset], sizeof(float));
offset += sizeof(float);
}

memcpy(&numMats, &data[offset], sizeof(int16_t));
offset += sizeof(int16_t);
mat.resize(numMats);
char c = 0;
short tempshort = 0;
for(i = 0; i < numMats; i++)
{
string name;
do
{
memcpy(&c, &data[offset], 1);
name += c;
offset++;
}while(c != 0x00);

offset++;
string filename;
do
{
memcpy(&c, &data[offset], 1);
filename += c;
offset++;
}while(c != 0x00);
offset++;
memcpy(&tempshort, &data[offset], sizeof(int16_t));
offset += sizeof(int16_t);
mat.numFacesAppliesTo = tempshort;
mat.materialName = name;
mat.materialFileName = filename;
}
memcpy(&numFaces, &data[offset], sizeof(int16_t));
offset += sizeof(int16_t);
facelist.resize(numFaces);
for(i = 0; i < numFaces; i++)
{
memcpy(&facelist.p1, &data[offset], sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(&facelist.p2, &data[offset], sizeof(uint16_t));
offset += sizeof(uint16_t);
memcpy(&facelist.p3, &data[offset], sizeof(uint16_t));
offset += sizeof(uint16_t);
}
return true;
}
void sdObj::testvert()
{
string filename = "c:\\vertout.txt";
ofstream outfile(filename.c_str(), ios::out | ios::ate);
if(outfile.is_open())
{
stringstream ss (stringstream::in | stringstream::out);
string check;
for(int32_t i=0; i < numVerts; i++)
{
ss << texvert.x;
check = ss.str();
string::size_type pos = check.find("e");
if (string::npos != pos)
{
texvert.x = 0.00f;
}
ss.clear();
ss.str("");
outfile << texvert.x << " _ ";
ss << texvert.y;
check = ss.str();

pos = check.find("e");
if (string::npos != pos)
{
texvert.y = 0.00f;
}
ss.clear();
ss.str("");
outfile << texvert.y << " _ ";
ss << texvert.z;
check = ss.str();

pos = check.find("e");
if (string::npos != pos)
{
texvert.z = 0.00f;
}
ss.clear();
ss.str("");
outfile << texvert.z << "\n ";
}
}
outfile.close();
}


winClude.h

#ifndef _WINCLUDE_H_
#define _WINCLUDE_H_
#pragma once
#include <stdint.h>
int32_t winStart (HINSTANCE hInstance, int32_t nShowCmd);
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

#endif


winClude.cpp

#include <Windows.h>
#include <string>
#include <stdint.h>
#include "winClude.h"
#include "dxInit.h"
using namespace std;
int winStart (HINSTANCE hInstance, int32_t nShowCmd)
{
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
dxInit dx;
string filename = "dawnstar.sd1";
int32_t screenWidth = 640;
int32_t screenHeight = 480;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "myWinClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
"myWinClass",
"Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, screenWidth, screenHeight, NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
dx.sd.load(filename);
bool devSet = dx.setDev(screenWidth, screenHeight, hwnd, 0);
while(true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if (devSet)
{
dx.render();
}
}
}
return msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
{
DestroyWindow(hwnd);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}



dxInit.h

#ifndef _DX_INIT_H_
#define _DX_INIT_H_
#pragma once
#include <d3d9.h>
#include <d3dx9.h>
#include <vector>
#include <string>
#include <stdint.h>
#include "sdImport.h"
#define _CRT_SECURE_NO_DEPRECATE_
class dxInit
{
public:
dxInit();
~dxInit();
bool setDev(int32_t width, int32_t height, HWND hwnd, bool bFullscreen);
bool render();
bool setVBuffer();
bool setIBuffer();

bool setViewMatrix();
bool setProjectionMatrix();
bool setWorldMatrix();
LPDIRECT3DTEXTURE9 loadTexture(std::string const &filename);
sdObj sd;
private:
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pDevice;
LPDIRECT3DVERTEXBUFFER9 pVBuffer;
LPDIRECT3DINDEXBUFFER9 pIBuffer;
std::vector< LPDIRECT3DTEXTURE9 > pTex;
std::vector< vertex > tempvert;
std::vector< face > faceload;
int32_t dxScreenW, dxScreenH;
};
#endif


dxInit.cpp

#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#include <stdint.h>
#include "dxInit.h"
using namespace std;
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define CUSTOM_FVF (D3DFVF_XYZ | D3DFVF_TEX1)
dxInit::dxInit()
{
}
dxInit::~dxInit()
{
for(int e = 0; e < sd.numMats; e++)
{
pTex[e]->Release();
}
pIBuffer->Release();
pVBuffer->Release();
pDevice->Release();
pD3D->Release();
}
bool dxInit::setDev(int32_t width, int32_t height, HWND hwnd, bool bFullscreen)
{
dxScreenW = width;
dxScreenH = height;
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS pD3Dpp;
ZeroMemory(&pD3Dpp, sizeof(pD3Dpp));
pD3Dpp.Windowed = !bFullscreen;
pD3Dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
pD3Dpp.hDeviceWindow = hwnd;
pD3Dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
pD3Dpp.BackBufferWidth = dxScreenW;
pD3Dpp.BackBufferHeight = dxScreenH;
pD3Dpp.EnableAutoDepthStencil = TRUE;
pD3Dpp.AutoDepthStencilFormat = D3DFMT_D16;
if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&pD3Dpp,
&pDevice)))
{
return false;
}
setVBuffer();
setIBuffer();
pTex.resize(sd.numMats);
for(int t = 0; t < sd.numMats; t++)
{
pTex[t] = loadTexture(sd.mat[t].materialFileName);
}
pDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
return true;
}
bool dxInit::render()
{
if(FAILED(pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0)))
return false;
if(FAILED(pDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0)))
return false;
pDevice->BeginScene();
pDevice->SetFVF(CUSTOM_FVF);
setViewMatrix();
setProjectionMatrix();
setWorldMatrix();

if(FAILED(pDevice->SetStreamSource(0, pVBuffer, 0, sizeof(vertex))))
return false;
if(FAILED(pDevice->SetIndices(pIBuffer)))
return false;
int16_t tempvsize, tempfsize;
int16_t basevert = 0;
int16_t minvert = 0;
tempfsize = sd.numFaces;

for(int i = 0; i < sd.numMats; i++)
{
pDevice->SetTexture(0, pTex);
minvert += sd.mat.numFacesAppliesTo - 1;
tempvsize = sd.mat.numFacesAppliesTo;

if(FAILED(pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, basevert, minvert, tempvsize, 0, tempfsize)))
return false;

basevert += minvert;
}
pDevice->EndScene();
pDevice->Present(NULL, NULL, NULL, NULL);
return true;
}
LPDIRECT3DTEXTURE9 dxInit::loadTexture(string const &filename)
{
LPDIRECT3DTEXTURE9 temp;
string fn;
fn.append(sd.installdrive);
fn.append(sd.basepath);
fn.append(sd.texturepath);
fn.append(filename);
D3DXCreateTextureFromFile(pDevice, fn.c_str(), &temp);
if(FAILED(pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR)))
return false;
if(FAILED(pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR)))
return false;
return temp;
}
bool dxInit::setVBuffer()
{
int32_t vertsize = sd.numVerts;
if(FAILED(pDevice->CreateVertexBuffer(vertsize * sizeof(texTri),
0,
CUSTOM_FVF,
D3DPOOL_MANAGED,
&pVBuffer,
NULL)))
return false;
void *pVoid;

if(FAILED(pVBuffer->Lock(0, 0, (void**)&pVoid, 0)))
return false;
memcpy(pVoid, &(sd.texvert[0]), sd.texvert.size() * sizeof(texTri));
if(FAILED(pVBuffer->Unlock()))
return false;
return true;
}
bool dxInit::setIBuffer()
{
int32_t facesize = sd.numFaces;
if(FAILED(pDevice->CreateIndexBuffer(facesize * 3 * sizeof(face),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&pIBuffer,
NULL)))
return false;
void *pvoid;
if(FAILED(pIBuffer->Lock(0,0, (void**)&pvoid, 0)))
return false;

memcpy(pvoid, &(sd.facelist[0]), sd.facelist.size() * sizeof(face));
if(FAILED(pIBuffer->Unlock()))
return false;
return true;
}
bool dxInit::setViewMatrix()
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3(0.0f, 8.0f, 110.0f), //camera
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), // look at
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)); // up direction
if(FAILED(pDevice->SetTransform(D3DTS_VIEW, &matView)))
return false;
return true;
}
bool dxInit::setProjectionMatrix()
{
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45),
(float)dxScreenW / (float)dxScreenH,
1.0f,
1000.0f);
if(FAILED(pDevice->SetTransform(D3DTS_PROJECTION, &matProjection)))
return false;
return true;
}
bool dxInit::setWorldMatrix()
{
static float index = 0.0f;
D3DXMATRIX matRotateY;
D3DXMatrixRotationY(&matRotateY, index);
if(FAILED(pDevice->SetTransform(D3DTS_WORLD, &(matRotateY))))
return false;
return true;
}



Now, when I try to render the object with just one texture, it looks like the following. Incidentally, if I don't set a texture and just try to render a simple diffuse object, the object shape is the same.

sd1output.jpg

When I try to load only the faces the related to each texture, I don't even get that, it flickers between two indistinct shapes:
sd1output1.jpg

and just so you know what the object is meant to look like:
http://www.yunaco.com/3dsoutput.jpg

And incase you need it, the .sd1 file.
http://www.yunaco.com/dawnstar.sd1


So, where am I going wrong here? I've stepped through and I can't see any faulty data. Everything seems as if I should work, but clearly, no.
Any help would be greatfully recieved.
Advertisement
Right, solved it.
I remmed out the lines for the multi textures, and instead just used one of them, and then changed sizeof(vertex) on the setstreamsource to sizeof(texTri), because i'm not using the vertex struct anymore. Renders fine (well, except it's still z-up, but that's 3dsmax for you).

I guess my method of using multitextures is wrong. I'll figure out another way. Sorry to have bothered you with such a simple mistake... really didn't see it =/

This topic is closed to new replies.

Advertisement