Skip to main content
gamedev.net
🔒 Locked

Model Rendering Problem

Started by hahaha Jul 30, 2007 at 2:15 PM 4 replies 700+ views
Original Post
hahaha
hahaha
Im busy creating my map format(named JDB(My Name :))). Ive got a lot of code for exporting compiling and rendering the map. Heres the 3ds max exporter script:

Rollout JDB_Exporter_Main "JDB Exporter"
(
   checkbox ExTex "Export Texture Coords" pos:[7,4] width:150 height:16 enabled:true checked:true
   checkbox ExVn "Export Vertex Normals" pos:[7,26] width:150 height:16 enabled:true checked:true
   comboBox cbVer "JDB Version:" pos:[7,48] width:129 height:3 items:#("1.0", "Other") selection:1
   groupBox grp1 "Filename:" pos:[7,125] width:150 height:50
   editText edt1 "" pos:[12,142] width:132 height:22
   button b_export "Export File" pos:[60,185]
      on b_export pressed do                 
	  (
	      out = createfile "c:/123.txt"
		  TotNumVerts = 0
		  TotNumFaces = 0
		  TotNumTVerts = 0
		  FacesX = #()
		  FacesY = #()
		  FacesZ = #()
		  FacesTX = #()
		  FacesTY = #()
		  FacesTZ = #()
		  Verts = #()
		  TVerts = #()
		  for child in rootnode.children do
		  (
		     local Mesh = copy child
			 convertToMesh Mesh
			 NumVerts = Mesh.numverts
			 NumFaces = Mesh.numfaces
			 NumTVerts = Mesh.numTVerts
			 for i = 1 to NumTVerts do
			 (
				 uvw = GetTVert Mesh i
			     append TVerts uvw
			 )
			 for v = 1 to NumVerts do
			 (
  				 vert = getVert Mesh v
  				 append Verts vert
			 )
			 for f = 1 to NumFaces do
			 (
				 face = getFace Mesh f
				 X = TotNumVerts+face.X
				 Y = TotNumVerts+face.Y
				 Z = TotNumVerts+face.Z
				 tv = getTVFace Mesh f
				 TX = tv.X
				 TY = tv.Y
				 TZ = tv.Z
				 append FacesTX TX
				 append FacesTY TZ
				 append FacesTZ TZ
				 append FacesX X
			     append FacesY Y
				 append FacesZ Z
             )
			 TotNumFaces = TotNumFaces+NumFaces
			 TotNumVerts = TotNumVerts+NumVerts
			 TotNumTVerts = TotNumTVerts+NumTVerts
		  )
		  format "%\n" "JDB" to:out				--output header id
		  format "%\n" "1.0" to:out				--output version
          format "%\n" "0" to:out				--output number of textures 	
          format "%\n" "0" to:out				--output number of normals 
		  format "%\n" TotNumVerts to:out	    --output number of verts
		  format "%\n" (TotNumFaces*3) to:out	--output number of faces
		  format "%\n" TotNumTVerts to:out	    --output number of tverts
		  format "%\n" "0" to:out				--outputnumber of spawn points 
		  for v = 1 to TotNumVerts do
		  (
		     format "% % %\n" Verts[v].X Verts[v].Z Verts[v].Y to:out
		  )
		  for t = 1 to TotNumTVerts do
		  (
		     format "% % %\n" (TVerts[t].x) (TVerts[t].z) (TVerts[t].y) to:out
		  )
		  for f = 1 to TotNumFaces do
		  (
		     format "% % %\n" (FacesTX[f]-1) (FacesTZ[f]-1) (FacesTY[f]-1) to:out
		     format "% % %\n" (FacesX[f]-1) (FacesZ[f]-1) (FacesY[f]-1) to:out
		  )
		  close out
      )
)
Rollout JDB_Exporter_About "About JDB Exporter"
(
   label lbl2 "Created by Jason D Baldwin." pos:[7,10] width:150 height:27
)

Window = newRolloutFloater  "JDB Exporter Utility"  210 310

addrollout JDB_Exporter_Main Window
addrollout JDB_Exporter_About Window
Heres the compiler code which converts the exported map to binary:

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

struct JDBVertex
{
    float x,y,z;
};

struct JDBTVertex
{
    float x,y,z;
};

struct JDBFace
{
    JDBTVertex tverts;
    float x,y,z;
};

struct JDBFacei
{
    JDBTVertex tverts;
    int x,y,z;
};

struct JDBHeader
{
    char Id[4];
    float Version;
    int NumTextures;
    int NumNormals;
    int NumVerts;
    int NumFaces;
    int NumTVerts;
    int NumSpawnPoints;
};

struct JDBModel
{
    JDBHeader  Header;   
    JDBVertex  *Verts;
    JDBTVertex *TVerts;
    JDBFace    *Faces;
};

int main(int argc, char *argv[])
{
   JDBModel Model;
   FILE *fp = fopen("123.txt", "rb");
   fscanf(fp,"%s\n",&Model.Header.Id);
   fscanf(fp,"%f\n",&Model.Header.Version);
   fscanf(fp,"%d\n",&Model.Header.NumTextures);
   fscanf(fp,"%d\n",&Model.Header.NumNormals);
	fscanf(fp,"%d\n",&Model.Header.NumVerts);
	fscanf(fp,"%d\n",&Model.Header.NumFaces);
	fscanf(fp,"%d\n",&Model.Header.NumTVerts);
	fscanf(fp,"%d\n",&Model.Header.NumSpawnPoints);
	
	Model.Verts = new JDBVertex[Model.Header.NumVerts];
	Model.Faces = new JDBFace[Model.Header.NumFaces];
	Model.TVerts = new JDBTVertex[Model.Header.NumTVerts];
	
	for(int i = 0; i < Model.Header.NumVerts; i++)
	{
		fscanf(fp,"%f %f %f\n",&Model.Verts.x,&Model.Verts.y, &Model.Verts.z);				 			
	}
	for(int i = 0; i < Model.Header.NumTVerts; i++)
	{
		fscanf(fp,"%f %f %f\n",&Model.TVerts.x,&Model.TVerts.y, &Model.TVerts.z);				 			
	}
	for(int i = 0; i < Model.Header.NumFaces; i++)
	{
      fscanf(fp, "%f %f %f\n", &Model.Faces.tverts.x,&Model.Faces.tverts.y,&Model.Faces.tverts.z);           
      fscanf(fp, "%f %f %f\n", &Model.Faces.x,&Model.Faces.y,&Model.Faces.z);      			
	}
	fclose(fp);

	ofstream out("123.jdb",ios::out|ios::binary);
	out.write((char*)&Model.Header,sizeof(JDBHeader));
	
	for(int i=0;i<Model.Header.NumVerts;i++)  
	{
       out.write((char*)&Model.Verts,sizeof(JDBVertex));
   }
   for(int i=0;i<Model.Header.NumTVerts;i++) 
	{
       out.write((char*)&Model.TVerts,sizeof(JDBTVertex));
   }
   for(int i=0;i<Model.Header.NumFaces;i++)  
	{
       JDBFacei Face;
       Face.x = (int)Model.Faces.x;
       Face.y = (int)Model.Faces.y;
       Face.z = (int)Model.Faces.z;   
       Face.tverts = Model.Faces.tverts;
       out.write((char*)&Face,sizeof(JDBFacei));
   }
	out.close();
	cout << "Finished Compiling Succcessfully!\n";
   system("PAUSE");
   return EXIT_SUCCESS;
}




Heres the model loader and renderer:

MyLoader.h

#ifndef _MYLOADER_H
#define _MYLOADER_H

#include "Main.h"

struct JDBVertex
{
    float x,y,z;
};

struct JDBTVertex
{
    float x,y,z;
};

struct JDBFace
{
    JDBTVertex tverts;
    int x,y,z;
};

struct JDBHeader
{
    char Id[4];
    float Version;
    int NumTextures;
    int NumNormals;
    int NumVerts;
    int NumFaces;
    int NumTVerts;
    int NumSpawnPoints;
};

struct JDBModel
{
    JDBHeader  Header;   
    JDBVertex  *Verts;
    JDBTVertex *TVerts;
    JDBFace    *Faces;
};

class MyLoader
{
private:
    JDBModel m_Model;
public:
    MyLoader();
    ~MyLoader(); 
    bool LoadModel(char *filename); 
    void DrawModel();     
};
#endif

MyLoader.cpp

#include "MyLoader.h"
#include "Image_Main.h"

UINT texid;

bool CreateTexture(UINT &texture, LPSTR strFileName)
{
	tImage *pImage = NULL;
	pImage = LoadBMP(strFileName);
	glGenTextures(1, &texture);
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
	glBindTexture(GL_TEXTURE_2D, texture);
	int textureType = GL_RGB;
	gluBuild2DMipmaps(GL_TEXTURE_2D, pImage->channels, pImage->sizeX, 
					  pImage->sizeY, textureType, GL_UNSIGNED_BYTE, pImage->data);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	if (pImage)										// If we loaded the image
	{
		if (pImage->data)							// If there is texture data
		{
			free(pImage->data);						// Free the texture data, we don't need it anymore
		}
		free(pImage);								// Free the image structure
	}
	return true;
}

using namespace std;

MyLoader::MyLoader()
{
}
MyLoader::~MyLoader()
{
}

bool MyLoader::LoadModel(char *filename)
{
   ifstream in(filename,ios::in|ios::binary);
   if(!in.is_open())return false;
   in.read((char*)&m_Model.Header,sizeof(JDBHeader)); //Read Header
   
   m_Model.Verts = new JDBVertex[m_Model.Header.NumVerts];     //Allocate Vertex Memory
	m_Model.Faces = new JDBFace[m_Model.Header.NumFaces];          //Allocate Face Memory
	m_Model.TVerts = new JDBTVertex[m_Model.Header.NumTVerts];     //Allocate Texture Vertex Memory
	
	for(int i = 0; i < m_Model.Header.NumVerts; i++)
	{
       in.read((char*)&m_Model.Verts,sizeof(JDBVertex)); //Read Vertices
   }
   for(int i = 0; i < m_Model.Header.NumTVerts; i++)
	{
       in.read((char*)&m_Model.TVerts,sizeof(JDBTVertex)); //Read Texture Vertices
   }
   for(int i = 0; i < m_Model.Header.NumFaces; i++)
	{
       in.read((char*)&m_Model.Faces,sizeof(JDBFace));      //Read Faces 
   }
   in.close();
   if(!CreateTexture(texid,"virus.bmp"))return false;
	return true;
}
void MyLoader::DrawModel()
{
     glColor3f(1,1,1);
     glBegin(GL_TRIANGLES); 
     glBindTexture(GL_TEXTURE_2D,texid); 
     for(int i=0;i<m_Model.Header.NumFaces;i++)
     {   
         glTexCoord2f(m_Model.TVerts[(int)m_Model.Faces.tverts.x].x,m_Model.TVerts[(int)m_Model.Faces.tverts.x].z);
         glVertex3f(m_Model.Verts[m_Model.Faces.x].x,m_Model.Verts[m_Model.Faces.x].y,m_Model.Verts[m_Model.Faces.x].z);
         glTexCoord2f(m_Model.TVerts[(int)m_Model.Faces.tverts.y].x,m_Model.TVerts[(int)m_Model.Faces.tverts.y].z);
         glVertex3f(m_Model.Verts[m_Model.Faces.y].x,m_Model.Verts[m_Model.Faces.y].y,m_Model.Verts[m_Model.Faces.y].z);
         glTexCoord2f(m_Model.TVerts[(int)m_Model.Faces.tverts.z].x,m_Model.TVerts[(int)m_Model.Faces.tverts.z].z);
         glVertex3f(m_Model.Verts[m_Model.Faces.z].x,m_Model.Verts[m_Model.Faces.z].y,m_Model.Verts[m_Model.Faces.z].z);
     }
     glEnd();
}





For some reason when i render this the texture coords are all messed up. They show but they just form lines for some reason. I know this is a lot of code to go through but i would really appreciate it if someone could help me out. Thanks for your help in advance:) [Edited by - hahaha on August 3, 2007 8:17:24 AM]
hoodedpaladin
hoodedpaladin
I find it weird that you're using the z coordinate instead of the y when you call glTexCoord2f. Since most texture coords are 2D, you might not have real values saved in your z coordinates, and then your rendered v coordinates would never get changed, causing stripes to show up because that one line of texture would be swept across your mesh.
hahaha
hahaha
Thanks hoodedpaladin
I tried using the Y coord but nothing shows up if i do
hahaha
hahaha
anyone??
hahaha
hahaha
Please??
hahaha
hahaha
No worries i found the solution

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.