Sign in to follow this  
Soulstormgr

OpenGL My .obj model loader

Recommended Posts

&bHello. This is my first post in this forum. I hope I find all the help I need here... This is the problem I have. I am trying to create a .obj model loader (from .obj files). Searching the internet, I found the specification, and I started programming based on that. here is what I have done so far: ObjLoaders.h
#ifndef OBJLOADERS_H
#define OBJLOADERS_H


#include <iostream>
#include <vector>
#include <string>
#include <OpenGL/OpenGL.h>
#include <OpenGL/glu.h>

using namespace std;

typedef float vector3f[3];

class OBJFileInfo{
public:
	unsigned numVertices;
	unsigned numTriangles;
	unsigned numVertexNormals;
	unsigned numTextureVertex;
	
	OBJFileInfo(){
		numVertices = 0;
		numTriangles = 0;
		numVertexNormals = 0;
		numTextureVertex = 0;
	}
};

class Triangle{
public:
	unsigned TriangleEdges[3];
	unsigned TriangleTextureCoords[3];
	unsigned TriangleNormals[3];
	
	Triangle(const Triangle& otherTriangle){
		for (int i=0; i<3; i++) {
			TriangleEdges[i] = otherTriangle.TriangleEdges[i];
			TriangleTextureCoords[i] = otherTriangle.TriangleTextureCoords[i];
			TriangleNormals[i] = otherTriangle.TriangleNormals[i];
		}
	}
	Triangle(){
		reset();
	}
	void reset(){
		for (int i=0; i<3; i++) {
			TriangleEdges[i] = 0;
			TriangleTextureCoords[i] = 0;
			TriangleNormals[i] = 0;
		}
	}
};

class OBJModel{
	OBJFileInfo fileinfo;
public:
	
	vector3f* vertices;
	vector3f* vertexNormals;
	vector3f* textureVertices;
	//Triangle* triangles;
	vector<Triangle> triangles;
	OBJModel(OBJFileInfo info){
		fileinfo = info;
		vertices	=		new vector3f[info.numVertices];
		vertexNormals =		new vector3f[info.numVertexNormals];
		textureVertices =		new vector3f[info.numTextureVertex];
		//triangles =			new Triangle[info.numTriangles];
	}
	
	void vertexAtIndex(unsigned index, vector3f resultBuffer){
		resultBuffer[0] = vertices[index][0];
		resultBuffer[1] = vertices[index][1];
		resultBuffer[1] = vertices[index][1];
	}
	
	void drawVertexAtIndex(unsigned index){
		vector3f vertexToDraw;
		vertexToDraw[0] = vertices[index][0];
		vertexToDraw[1] = vertices[index][1];
		vertexToDraw[1] = vertices[index][1];
		
		glVertex3f(vertexToDraw[0], vertexToDraw[1], vertexToDraw[2]);
		
	}
	
	//TO BE COMPLETED!!!
	void drawFaceAtIndex(unsigned index){
		unsigned triangleVertexPositions[3];
		triangleVertexPositions[0] = triangles[index].TriangleEdges[0];
		triangleVertexPositions[1] = triangles[index].TriangleEdges[1];
		triangleVertexPositions[2] = triangles[index].TriangleEdges[2];
		
		//cout << triangles[index].TriangleEdges[0] << " " << triangles[index].TriangleEdges[0] << " " << triangles[index].TriangleEdges[0] << '\n';
		
		vector3f v1, v2, v3;
		v1[0] = vertices[triangleVertexPositions[0]][0];
		v1[1] = vertices[triangleVertexPositions[0]][1];
		v1[2] = vertices[triangleVertexPositions[0]][2];
		
		v2[0] = vertices[triangleVertexPositions[1]][0];
		v2[1] = vertices[triangleVertexPositions[1]][1];
		v2[2] = vertices[triangleVertexPositions[1]][2];
		
		v3[0] = vertices[triangleVertexPositions[2]][0];
		v3[1] = vertices[triangleVertexPositions[2]][1];
		v3[2] = vertices[triangleVertexPositions[2]][2];
		
		cout << v1[0] << " " << v1[1] << " " << v1[2] << '\n';
		cout << v2[0] << " " << v2[1] << " " << v2[2] << '\n';
		cout << v3[0] << " " << v3[1] << " " << v3[2] << '\n';
		
		glBegin(GL_TRIANGLES);
			glVertex3f(v1[0], v1[1], v1[2]);
			glVertex3f(v2[0], v2[1], v2[2]);
			glVertex3f(v3[0], v3[1], v3[2]);
		glEnd();
	}
	
	~OBJModel()
	{
		delete [] vertices;
		delete [] vertexNormals;
		delete [] textureVertices;
		//delete [] triangles;
	}
	
	unsigned numVertices(){return fileinfo.numVertices;}
	unsigned numTriangles(){return fileinfo.numTriangles;}
	unsigned numVertexNormals(){return fileinfo.numVertexNormals;}
	unsigned numTextureVertex(){return fileinfo.numTextureVertex;}
};




void firstPass(char *filename, OBJFileInfo& info);
void secondPass(char *filename, OBJModel *model);

#endif


ObjLoaders.cpp
#include "ObjLoaders.h"


void firstPass(char *filename, OBJFileInfo& info)
{
	char buf[128];
	FILE *filePointer = fopen(filename, "r");	
	
	while ( fscanf(filePointer, "%s", buf) != EOF){
		switch (buf[0]) {
			case 'v':
				switch (buf[1]) {
					case 't':
						fgets(buf, sizeof(buf), filePointer);
						info.numTextureVertex++;
						break;
					case '\0':
						fgets(buf, sizeof(buf), filePointer);
						info.numVertices++;
						break;
					case 'n':
						fgets(buf, sizeof(buf), filePointer);
						info.numVertexNormals++;
						break;
					default:
						break;
				}
				break;
			case '#':
				fgets(buf, sizeof(buf), filePointer);
				break;
			case 'f':
				info.numTriangles++;
				//NSLog(@"not implemented yet! %i", info.numTriangles);
				break;
			default:
				fgets(buf, sizeof(buf), filePointer);
				break;
		}
	}
	fclose(filePointer);
	info.numTriangles--;
}

void secondPass(char *filename, OBJModel *model){
	char buf[128];
	FILE *filePointer = fopen(filename, "r");
	
	unsigned currentVertexIndex = 0;
	unsigned currentvertexNormalIndex = 0;
	unsigned currentTextureVertexIndex = 0;
	unsigned currentTriangleIndex = 0;
	
	while ( fscanf(filePointer, "%s", buf) != EOF ) {
		switch (buf[0]) {
			case 'v':{
				switch (buf[1]) {
					case '\0':{
						float a,b,c;
						fscanf(filePointer, "%f %f %f", &a, &b, &c);						
						model->vertices[currentVertexIndex][0] = a;
						model->vertices[currentVertexIndex][1] = b;
						model->vertices[currentVertexIndex][2] = c;
						//cout << a << " " << b << " " << c << '\n';
						currentVertexIndex++;
					}
						break;
					case 'n':{
						float a,b,c;
						fscanf(filePointer, "%f %f %f", &a, &b, &c);
						//cout << a << ' ' << b << ' ' << c << '\n';
						model->vertexNormals[currentvertexNormalIndex][0] = a;
						model->vertexNormals[currentvertexNormalIndex][1] = b;
						model->vertexNormals[currentvertexNormalIndex][2] = c;
						currentvertexNormalIndex++;
					}
						break;
					case 't':{
						float a,b,c;
						fscanf(filePointer, "%f %f %f", &a, , &c);
						model->textureVertices[currentTextureVertexIndex][0] = a;
						model->textureVertices[currentTextureVertexIndex][1] = b;
						model->textureVertices[currentTextureVertexIndex][2] = c;
						currentTextureVertexIndex++;
					}
						break;
					default:
						break;
				}
			}
				break;
			case 'f':{
				Triangle currentTriangle;
				int v,t,n = 0;
				fscanf(filePointer, "%s", buf);
				if ( strstr(buf, "//") ) {
					sscanf(buf, "%d//%d", &v, &n);
					currentTriangle.TriangleEdges[0] = v;
					currentTriangle.TriangleNormals[0] = n;
					fscanf(filePointer, "%d//%d", &v, &n);
					currentTriangle.TriangleEdges[1] = v;
					currentTriangle.TriangleNormals[1] = n;
					fscanf(filePointer, "%d//%d", &v, &n);
					currentTriangle.TriangleEdges[2] = v;
					currentTriangle.TriangleNormals[2] = n;
				}
				else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3){
					currentTriangle.TriangleEdges[0] = v;
					currentTriangle.TriangleTextureCoords[0] = t;
					currentTriangle.TriangleNormals[0] = n;
					fscanf(filePointer, "%d/%d%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1], &currentTriangle.TriangleTextureCoords[1]);
					fscanf(filePointer, "%d/%d%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1], &currentTriangle.TriangleNormals[1]);
				}
				else if (sscanf(buf, "%d/%d", &v, &t) == 2){
					currentTriangle.TriangleTextureCoords[0] = t;
					currentTriangle.TriangleEdges[0] = v;
					fscanf(filePointer, "%d/%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1]);
					fscanf(filePointer, "%d/%d", &currentTriangle.TriangleEdges[2], &currentTriangle.TriangleTextureCoords[2]);
				}
				else {
					sscanf(buf, "%d", &v);
					currentTriangle.TriangleEdges[0] = v;
					fscanf(filePointer, "%d", currentTriangle.TriangleEdges[1]);
					fscanf(filePointer, "%d", currentTriangle.TriangleEdges[2]);
				}
				model->triangles.push_back(currentTriangle);
				currentTriangleIndex++;
			}
				break;	
			default:
				fgets(buf, sizeof(buf), filePointer);
				break;
		}
	}
}



the code to draw:
OBJFileInfo objFile;
	firstPass(FILENAME2, objFile);
	OBJModel model(objFile);
	secondPass(FILENAME2, &model);
	
	model.drawFaceAtIndex(1);


Although something is drawn to the screen, I only see garbage! The file is parsed correctly, and all classes end up in having the correct variables stored in them. So it must be a logic error, and not a programming one (and logic errors are hard to spot!). For simplicity reasons I haven't implemented texture or material support, and I am trying to load a file that only contains vertex and vertex normals data. This is the file, in case you want to run the code yourself:
#  
#  Wavefront OBJ generated by GLM library
#  
#  GLM library
#  Nate Robins
#  ndr@pobox.com
#  http://www.pobox.com/~ndr
#  

mtllib lightcycle.mtl

# 91 vertices
v -0.675745 -4.181760 0.828977
v -0.675746 -0.260560 1.854730
v 0.677798 -2.070860 -0.100226
v 0.669888 -0.260560 1.854730
v 0.669888 -4.181760 0.828977
v 0.203011 3.536160 -0.847399
v 0.203011 3.851470 -0.511977
v 0.203011 3.207430 -0.525406
v 0.203011 3.536160 -0.150241
v -0.200718 3.536160 -0.150241
v -0.200718 3.851470 -0.511977
v -0.200718 3.207430 -0.538833
v -0.200718 3.536160 -0.847399
v 1.257510 -0.361758 0.887388
v 0.610035 -0.187286 1.704330
v 0.610035 -3.311140 0.887388
v -1.257510 -0.361758 0.888085
v -0.602038 -3.311140 0.888085
v -0.602039 -0.187286 1.705020
v 0.571984 -3.371230 -0.150160
v 0.571984 -3.692720 -0.507515
v 0.571984 -3.054550 -0.512684
v 0.571984 -3.371230 -0.847315
v -0.569690 -3.371230 -0.150160
v -0.569690 -3.054550 -0.512684
v -0.569690 -3.692720 -0.507515
v -0.569690 -3.371230 -0.847315
v -0.675746 2.560710 1.165440
v -0.675504 1.051360 -0.101380
v 0.669887 2.561520 1.165440
v 0.677798 1.051100 -0.101111
v 0.301519 2.480440 -0.547424
v 0.301519 3.536160 0.565185
v 0.301519 3.536160 -1.562820
v 0.301519 4.538160 -0.493712
v 0.002783 3.536160 -1.987540
v 0.002783 5.000000 -0.491226
v 0.002783 3.536160 0.989902
v -0.299225 3.536160 -1.562820
v -0.299225 4.538160 -0.493712
v -0.299225 3.536160 0.565185
v -0.299225 2.480440 -0.533995
v 0.002783 1.978340 -0.547779
v -0.545389 1.646580 -1.553560
v -0.545389 3.002350 -0.509387
v -0.544671 3.530940 -1.150170
v -0.545389 3.530570 -1.559590
v 0.548401 3.530670 -1.150440
v 0.547683 3.532990 -1.559590
v 0.547683 3.002350 -0.508849
v -1.074710 -3.371230 0.597294
v -1.074710 -4.529150 -0.143543
v 0.011553 -5.000000 -0.143341
v 0.011553 -3.371230 0.989982
v 1.077010 -3.371230 0.597294
v 1.077010 -4.529150 -0.143543
v 1.077010 -2.298130 -0.853935
v 0.011553 -1.807680 -0.867211
v -1.074710 -2.298130 -0.853935
v 1.077010 -3.840490 -1.664290
v 0.011553 -3.840490 -2.056980
v -1.074710 -3.840490 -1.664290
v -0.545389 3.529330 0.160689
v 0.547683 3.529330 0.160689
v 0.547683 1.646580 -1.553560
v -0.389670 3.132820 1.122540
v 0.392881 3.134730 1.125790
v 0.393451 2.943360 0.805744
v -0.389097 2.941480 0.802474
v 0.392881 -0.187721 2.056980
v 0.393451 -0.187888 1.694550
v -0.389669 -0.189631 2.053730
v -0.389096 -0.189771 1.691110
v 0.392881 -3.121660 1.598650
v 0.393451 -2.951540 1.251250
v -0.389669 -3.123570 1.595410
v -0.389096 -2.953420 1.247980
v 0.392881 -4.460940 0.882128
v 0.393451 -4.109510 0.815935
v -0.389669 -4.462850 0.878881
v -0.389096 -4.109460 0.815965
v -0.545389 -2.722760 -1.553560
v -0.542968 -2.722760 1.299390
v -0.545389 1.273530 0.348329
v 0.547683 1.273530 0.348329
v 0.547683 -2.722760 1.299390
v 0.549168 -2.723830 -1.553400
v -0.545389 1.905600 1.092650
v -0.545389 3.529330 1.092650
v 0.547683 3.529330 1.092650
v 0.547683 1.905600 1.092650

# 161 normals
vn -1.000000 0.000043 -0.000164
vn -0.695529 0.228198 0.681296
vn 0.999989 -0.000698 0.004692
vn 0.999989 -0.000071 0.004740
vn 0.999985 -0.001397 0.005339
vn 1.000000 0.000000 0.000000
vn -0.727607 0.485071 0.485071
vn -1.000000 -0.000000 -0.000000
vn -0.762493 0.457496 0.457496
vn -0.816497 0.408248 0.408248
vn 0.755312 -0.165813 0.634042
vn -0.650458 0.433360 0.623782
vn -0.751311 -0.166972 0.638477
vn -0.689812 0.328116 0.645368
vn -0.870411 0.348168 0.348085
vn -1.000000 -0.000013 -0.000146
vn -1.000000 -0.000000 -0.000191
vn -0.000037 -0.008133 0.999967
vn -0.000143 0.237274 0.971443
vn -0.000071 0.237306 0.971435
vn 0.999990 0.000592 0.004441
vn 0.999988 0.001182 0.004837
vn 0.990735 0.135771 0.003072
vn 0.990950 0.134093 0.006023
vn 0.991671 -0.000341 -0.128797
vn 0.990653 -0.000193 -0.136404
vn 0.989251 -0.053024 0.136276
vn 0.990060 0.038876 0.135166
vn 0.988948 -0.142679 -0.040308
vn 0.988451 -0.142925 0.050369
vn 0.718758 0.502329 -0.480679
vn 0.739721 0.481013 -0.470574
vn 0.697080 0.523144 -0.490306
vn 0.728037 0.492626 0.476741
vn 0.706889 0.503081 0.497209
vn 0.748475 0.481689 0.455807
vn -0.722445 0.499236 -0.478369
vn -0.743007 0.488333 -0.457681
vn -0.701182 0.509654 -0.498594
vn -0.716825 0.501381 0.484540
vn -0.694993 0.522259 0.494196
vn -0.737935 0.479999 0.474397
vn -0.989280 -0.052461 0.136279
vn -0.989474 0.041017 0.138778
vn -0.988451 -0.142924 0.050369
vn -0.988948 -0.142678 -0.040308
vn -0.991921 -0.001821 -0.126842
vn -0.990653 -0.001044 -0.136403
vn -0.990692 0.135907 0.007691
vn -0.990815 0.134357 0.015289
vn -0.734282 -0.467560 -0.492156
vn -0.709626 -0.491743 -0.504598
vn -0.757948 -0.442747 -0.479052
vn 0.740924 -0.460565 -0.488786
vn 0.722193 -0.469473 -0.507969
vn 0.759063 -0.451290 -0.469212
vn -1.000000 0.000107 0.000214
vn -1.000000 0.000176 -0.000228
vn -1.000000 0.000267 0.000534
vn -0.999998 0.000006 0.001754
vn -0.000984 0.999997 0.002385
vn -0.002214 0.999981 0.005674
vn 0.000247 1.000000 -0.000905
vn 0.000017 0.771683 0.636008
vn 0.000348 0.771406 0.636343
vn -0.000313 0.771959 0.635672
vn -0.256207 -0.536497 0.804070
vn -0.227626 -0.524786 0.820235
vn 0.002409 -0.555019 0.831834
vn 0.002659 -0.571153 0.820840
vn 0.260864 -0.535801 0.803036
vn 0.231834 -0.524251 0.819399
vn 0.270297 0.756003 0.596153
vn -0.082418 0.775548 0.625886
vn 0.107381 0.774012 0.623999
vn 0.214051 0.785420 0.580773
vn -0.281550 0.752851 0.594933
vn -0.335186 0.757543 0.560159
vn 0.261686 0.468109 -0.844034
vn 0.002771 0.505124 -0.863042
vn 0.002409 0.485013 -0.874503
vn 0.219633 0.453754 -0.863637
vn -0.257025 0.468725 -0.845124
vn -0.215624 0.454170 -0.864428
vn -0.249256 -0.857477 -0.450116
vn -0.147496 -0.900986 -0.408006
vn -0.105259 -0.870728 -0.480367
vn 0.074871 -0.872314 -0.483180
vn 0.282281 -0.847936 -0.448689
vn 0.373382 -0.845068 -0.382683
vn -0.613902 -0.614155 -0.495921
vn -0.660614 -0.603626 -0.446346
vn -0.564274 -0.621767 -0.543140
vn -0.826467 0.357975 -0.434519
vn -0.863654 0.374751 -0.337140
vn -0.780066 0.337209 -0.527055
vn 0.613898 -0.614158 -0.495923
vn 0.660610 -0.603629 -0.446349
vn 0.564270 -0.621769 -0.543142
vn 0.774472 0.417297 -0.475454
vn 0.831994 0.298993 -0.467322
vn 0.703497 0.528353 -0.475326
vn 0.620156 -0.494198 0.609241
vn 0.791443 -0.284296 0.541104
vn 0.400472 -0.665533 0.629832
vn -0.782366 -0.365083 0.504597
vn -0.840669 -0.393338 0.372237
vn -0.707072 -0.328899 0.625998
vn -0.828812 0.471259 0.301639
vn -0.872628 0.354766 0.335650
vn -0.771197 0.579907 0.262607
vn 0.828809 0.471263 0.301642
vn 0.872626 0.354769 0.335653
vn 0.771194 0.579911 0.262609
vn 0.000152 0.785917 -0.618331
vn 0.000304 0.786038 -0.618178
vn 0.000000 0.785797 -0.618485
vn 1.000000 -0.000267 -0.000535
vn 1.000000 -0.000105 0.000137
vn 1.000000 -0.000048 -0.000251
vn 0.999998 -0.000006 -0.001755
vn 0.000060 0.858295 -0.513157
vn 0.000037 0.858270 -0.513198
vn 0.000082 0.858319 -0.513116
vn 0.999999 0.000437 0.001549
vn 0.999999 0.000446 0.001573
vn 0.999999 0.000428 0.001526
vn -0.999999 -0.000439 -0.001557
vn -0.999999 -0.000430 -0.001533
vn -0.999999 -0.000449 -0.001580
vn 0.999999 -0.000245 0.001547
vn 0.999999 -0.000244 0.001521
vn 0.999999 -0.000246 0.001573
vn -0.999999 0.000246 -0.001555
vn -0.999999 0.000247 -0.001580
vn -0.999999 0.000245 -0.001529
vn 0.999997 -0.000888 0.002087
vn 0.999995 -0.001081 0.002875
vn 0.999999 -0.000695 0.001300
vn -0.999997 0.000899 -0.002123
vn -0.999999 0.000699 -0.001307
vn -0.999995 0.001098 -0.002939
vn 0.002240 -0.180194 -0.983629
vn -0.000049 -0.175279 -0.984519
vn 0.004529 -0.185104 -0.982709
vn -1.000000 -0.000202 0.000424
vn -1.000000 -0.000404 0.000849
vn -1.000000 -0.000135 0.000283
vn 1.000000 0.000116 0.000147
vn 1.000000 0.000124 0.000520
vn 1.000000 0.000232 0.000294
vn -0.003720 -0.154342 0.988010
vn -0.003714 -0.154344 0.988010
vn -0.003726 -0.154340 0.988011
vn 0.000000 -0.253076 0.967446
vn -0.429191 0.320573 0.844410
vn 0.000000 1.000000 0.000000
vn -0.004658 0.269870 0.962886
vn -0.002503 -0.471736 0.881736
vn -0.002507 -0.471734 0.881737
vn -0.002499 -0.471738 0.881735

# 126 faces (triangles)
g  default

usemtl Hull
f 1//1 1//1 2//2
f 3//3 4//4 5//5
usemtl Chassis
f 6//6 7//6 8//6
f 9//6 8//6 7//6
f 10//7 11//8 12//9
f 12//9 11//8 13//10
usemtl Window
f 14//11 15//11 16//11
f 17//12 18//13 19//14
usemtl Chassis
f 20//6 21//6 22//6
f 21//6 23//6 22//6
f 24//7 25//8 26//9
f 26//9 25//8 27//10
usemtl Hull
f 1//15 2//2 28//16
f 28//16 29//17 1//15
f 4//18 30//19 28//20
f 28//20 2//2 4//18
f 31//21 30//22 4//4
f 4//4 3//3 31//21
usemtl Window
f 32//23 8//24 9//25
f 9//25 33//26 32//23
f 32//23 34//27 6//28
f 6//28 8//24 32//23
f 34//27 35//29 7//30
f 7//30 6//28 34//27
f 35//29 33//26 9//25
f 9//25 7//30 35//29
usemtl Hull
f 36//31 37//32 35//31
f 35//31 34//33 36//31
f 37//34 38//35 33//34
f 33//34 35//36 37//34
f 39//37 40//38 37//37
f 37//37 36//39 39//37
f 40//40 41//41 38//40
f 38//40 37//42 40//40
usemtl Window
f 39//43 13//44 11//45
f 11//45 40//46 39//43
f 40//46 11//45 10//47
f 10//47 41//48 40//46
f 42//49 41//48 10//47
f 10//47 12//50 42//49
f 42//49 12//50 13//44
f 13//44 39//43 42//49
usemtl Hull
f 42//51 39//52 36//51
f 36//51 43//53 42//51
f 43//54 36//55 34//54
f 34//54 32//56 43//54
usemtl Chassis
f 44//57 45//58 46//59
f 46//59 47//60 44//57
f 48//61 49//62 47//61
f 47//61 46//63 48//61
f 48//64 46//65 45//64
f 45//64 50//66 48//64
usemtl Hull
f 51//67 52//68 53//69
f 53//69 54//70 51//67
f 55//71 54//70 53//69
f 53//69 56//72 55//71
f 57//73 58//74 54//75
f 54//75 55//76 57//73
f 51//77 54//75 58//74
f 58//74 59//78 51//77
f 60//79 61//80 58//81
f 58//81 57//82 60//79
f 62//83 59//84 58//81
f 58//81 61//80 62//83
f 52//85 62//86 61//87
f 61//87 53//88 52//85
f 60//89 56//90 53//88
f 53//88 61//87 60//89
usemtl Window
f 24//91 51//92 59//91
f 59//91 25//93 24//91
f 24//94 26//95 52//94
f 52//94 51//96 24//94
f 57//97 55//98 20//97
f 20//97 22//99 57//97
f 55//100 56//101 21//100
f 21//100 20//102 55//100
f 60//103 57//104 22//103
f 22//103 23//105 60//103
f 27//106 25//107 59//106
f 59//106 62//108 27//106
f 26//109 27//110 62//109
f 62//109 52//111 26//109
f 60//112 23//113 21//112
f 21//112 56//114 60//112
usemtl Chassis
f 50//115 45//116 63//115
f 63//115 64//117 50//115
f 48//118 50//119 65//120
f 65//120 49//121 48//118
usemtl Hull
f 66//122 67//123 68//122
f 68//122 69//124 66//122
f 70//125 71//126 68//125
f 68//125 67//127 70//125
f 72//128 66//129 69//128
f 69//128 73//130 72//128
usemtl Window
f 74//131 75//132 71//131
f 71//131 70//133 74//131
f 76//134 72//135 73//134
f 73//134 77//136 76//134
usemtl Hull
f 78//137 79//138 75//137
f 75//137 74//139 78//137
f 80//140 76//141 77//140
f 77//140 81//142 80//140
f 80//143 81//144 79//143
f 79//143 78//145 80//143
usemtl Chassis
f 82//146 83//147 84//148
f 84//148 44//57 82//146
f 85//149 86//150 87//151
f 87//151 65//120 85//149
usemtl Window
f 76//152 74//153 70//152
f 70//152 72//154 76//152
usemtl Hull
f 4//18 2//2 1//155
f 1//155 5//156 4//18
usemtl Chassis
f 88//8 45//58 44//57
f 44//57 84//148 88//8
f 64//157 63//157 89//157
f 89//157 90//157 64//157
f 88//8 89//8 63//8
f 63//8 45//58 88//8
f 50//119 64//6 90//6
f 90//6 91//6 50//119
f 85//149 65//120 50//119
f 50//119 91//6 85//149
usemtl Hull
f 72//158 70//158 67//158
f 67//158 66//158 72//158
f 80//159 78//160 74//159
f 74//159 76//161 80//159


The result should be a lightcycle (remember the movie "Tron"?) or at least something that looks like it. But instead I see garbage... Can anyone help me?

Share this post


Link to post
Share on other sites
Step through the program with the debugger and make sure the values are read in correctly. This will at least ensure that your loading code is correct. Then you can move on to the drawing code.

Also, your code can be made more simple by using std::vector and std::ifstream. It will also make it less error-prone (it will reduce the chance for memory leaks, buffer overruns, etc.).

Share this post


Link to post
Share on other sites
I haven't review your code deeply yet. But perhaps you should know that OBJ face indices start from 1, so the vertex at position 0 is referenced with 1 and so on. May your code starts the index counting from 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by Soulstormgr
Anyone?


Did you follow my suggestion?

Also:

void vertexAtIndex(unsigned index, vector3f resultBuffer){
resultBuffer[0] = vertices[index][0];
resultBuffer[1] = vertices[index][1];
resultBuffer[1] = vertices[index][1];
}

void drawVertexAtIndex(unsigned index){
vector3f vertexToDraw;
vertexToDraw[0] = vertices[index][0];
vertexToDraw[1] = vertices[index][1];
vertexToDraw[1] = vertices[index][1];

glVertex3f(vertexToDraw[0], vertexToDraw[1], vertexToDraw[2]);
}


Shouldn't the third index be 2?

Share this post


Link to post
Share on other sites
Quote:
Did you follow my suggestion?
To be honest, no. I didn't. But I am pretty sure (after extensive tests) that all values are read in correctly, and there is no memory leak so the problem lies elsewhere. I corrected the last mistake you pointed out, though. To no effect.

A guy in [url=http://forums.macrumors.com/showthread.php?t=545362] another forum[/url] has managed to have the correct output, but when I use his/her code, I am not getting the same result at all.

Here is my latest code.

ObjLoaders.h
#ifndef OBJLOADERS_H
#define OBJLOADERS_H

#include <iostream>
#include <vector>
#include <string>
#include <OpenGL/OpenGL.h>
#include <OpenGL/glu.h>

using namespace std;

typedef float vector3f[3];

class OBJFileInfo{
public:
unsigned numVertices;
unsigned numTriangles;
unsigned numVertexNormals;
unsigned numTextureVertex;

OBJFileInfo(){
numVertices = 0;
numTriangles = 0;
numVertexNormals = 0;
numTextureVertex = 0;
}
};

class Triangle{
public:
unsigned TriangleEdges[3];
unsigned TriangleTextureCoords[3];
unsigned TriangleNormals[3];

Triangle(const Triangle& otherTriangle){
for (int i=0; i<3; i++) {
TriangleEdges[i] = otherTriangle.TriangleEdges[i];
TriangleTextureCoords[i] = otherTriangle.TriangleTextureCoords[i];
TriangleNormals[i] = otherTriangle.TriangleNormals[i];
}
}
Triangle(){
reset();
}
void reset(){
for (int i=0; i<3; i++) {
TriangleEdges[i] = 0;
TriangleTextureCoords[i] = 0;
TriangleNormals[i] = 0;
}
}
};

class OBJModel{
OBJFileInfo fileinfo;
public:

vector3f* vertices;
vector3f* vertexNormals;
vector3f* textureVertices;
// Triangle* triangles;
vector<Triangle> triangles;

OBJModel(OBJFileInfo info){
setInfo(info);
}

void setInfo(OBJFileInfo info){
fileinfo = info;
vertices = new vector3f[info.numVertices];
vertexNormals = new vector3f[info.numVertexNormals];
textureVertices = new vector3f[info.numTextureVertex];
// triangles = new Triangle[info.numTriangles];
}

void vertexAtIndex(unsigned index, vector3f resultBuffer){
resultBuffer[0] = vertices[index][0];
resultBuffer[1] = vertices[index][1];
resultBuffer[2] = vertices[index][2];
}

void drawVertexAtIndex(unsigned index){
vector3f vertexToDraw;
vertexToDraw[0] = vertices[index][0];
vertexToDraw[1] = vertices[index][1];
vertexToDraw[2] = vertices[index][2];

glVertex3f(vertexToDraw[0], vertexToDraw[1], vertexToDraw[2]);

}

//TO BE COMPLETED!!!
void drawFaceAtIndex(unsigned index){
unsigned triangleVertexPositions[3];
triangleVertexPositions[0] = triangles[index].TriangleEdges[0];
triangleVertexPositions[1] = triangles[index].TriangleEdges[1];
triangleVertexPositions[2] = triangles[index].TriangleEdges[2];

unsigned triangleNormalPositions[3];
triangleNormalPositions[0] = triangles[index].TriangleNormals[0];
triangleNormalPositions[1] = triangles[index].TriangleNormals[1];
triangleNormalPositions[2] = triangles[index].TriangleNormals[2];

//cout << triangles[index].TriangleEdges[0] << " " << triangles[index].TriangleEdges[0] << " " << triangles[index].TriangleEdges[0] << '\n';

vector3f v1, v2, v3;
vector3f n1, n2, n3;
for(unsigned i=0; i<3; ++i){
v1[i] = vertices[triangleVertexPositions[0]][i];
v2[i] = vertices[triangleVertexPositions[1]][i];
v3[i] = vertices[triangleVertexPositions[2]][i];
n1[i] = vertexNormals[triangleNormalPositions[0]][i];
n2[i] = vertexNormals[triangleNormalPositions[1]][i];
n3[i] = vertexNormals[triangleNormalPositions[2]][i];
}

#define normalize(n) {float mag = sqrtf(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]); for(unsigned i=0; i<3;++i)n[i]/=mag;}
normalize(n1);
normalize(n2);
normalize(n3);

glBegin(GL_TRIANGLES);
glNormal3fv(n1);
glVertex3fv(v1);
glNormal3fv(n2);
glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
glEnd();
}

~OBJModel()
{
delete [] vertices;
delete [] vertexNormals;
delete [] textureVertices;
// delete [] triangles;
}

unsigned numVertices(){return fileinfo.numVertices;}
unsigned numTriangles(){return fileinfo.numTriangles;}
unsigned numVertexNormals(){return fileinfo.numVertexNormals;}
unsigned numTextureVertex(){return fileinfo.numTextureVertex;}
};




//void firstPass(char *filename, OBJFileInfo& info);
//void secondPass(char *filename, OBJModel *model);


static inline void firstPass(char *filename, OBJFileInfo& info)
{
char buf[128];
FILE *filePointer = fopen(filename, "r");

while ( fscanf(filePointer, "%s", buf) != EOF){
switch (buf[0]) {
case 'v':
switch (buf[1]) {
case 't':
fgets(buf, sizeof(buf), filePointer);
info.numTextureVertex++;
break;
case '\0':
fgets(buf, sizeof(buf), filePointer);
info.numVertices++;
break;
case 'n':
fgets(buf, sizeof(buf), filePointer);
info.numVertexNormals++;
break;
default:
break;
}
break;
case '#':
fgets(buf, sizeof(buf), filePointer);
break;
case 'f':
info.numTriangles++;
//NSLog(@"not implemented yet! %i", info.numTriangles);
break;
default:
fgets(buf, sizeof(buf), filePointer);
break;
}
}
fclose(filePointer);
info.numTriangles;//--;
}

static inline void secondPass(char *filename, OBJModel *model){
char buf[128];
FILE *filePointer = fopen(filename, "r");

unsigned currentVertexIndex = 0;
unsigned currentvertexNormalIndex = 0;
unsigned currentTextureVertexIndex = 0;
unsigned currentTriangleIndex = 0;

while ( fscanf(filePointer, "%s", buf) != EOF ) {
switch (buf[0]) {
case 'v':{
switch (buf[1]) {
case '\0':{
float a,b,c;
fscanf(filePointer, "%f %f %f", &a, &b, &c);
model->vertices[currentVertexIndex][0] = a;
model->vertices[currentVertexIndex][1] = b;
model->vertices[currentVertexIndex][2] = c;
//cout << a << " " << b << " " << c << '\n';
currentVertexIndex++;
}
break;
case 'n':{
float a,b,c;
fscanf(filePointer, "%f %f %f", &a, &b, &c);
cout << a << ' ' << b << ' ' << c << '\n';
model->vertexNormals[currentvertexNormalIndex][0] = a;
model->vertexNormals[currentvertexNormalIndex][1] = b;
model->vertexNormals[currentvertexNormalIndex][2] = c;
currentvertexNormalIndex++;
}
break;
case 't':{
float a,b,c;
fscanf(filePointer, "%f %f %f", &a, &b, &c);
model->textureVertices[currentTextureVertexIndex][0] = a;
model->textureVertices[currentTextureVertexIndex][1] = b;
model->textureVertices[currentTextureVertexIndex][2] = c;
currentTextureVertexIndex++;
}
break;
default:
break;
}
}
break;
case 'f':{
Triangle currentTriangle;
int v,t,n = 0;
fscanf(filePointer, "%s", buf);
if ( strstr(buf, "//") ) {
sscanf(buf, "%d//%d", &v, &n);
printf("%d %d\n", v, n);
--v; --n;
currentTriangle.TriangleEdges[0] = v;
currentTriangle.TriangleNormals[0] = n;
fscanf(filePointer, "%d//%d", &v, &n);
printf("%d %d\n", v, n);
--v; --n;
currentTriangle.TriangleEdges[1] = v;
currentTriangle.TriangleNormals[1] = n;
fscanf(filePointer, "%d//%d", &v, &n);
printf("%d %d\n", v, n);
--v; --n;
currentTriangle.TriangleEdges[2] = v;
currentTriangle.TriangleNormals[2] = n;
}
else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3){
currentTriangle.TriangleEdges[0] = v;
currentTriangle.TriangleTextureCoords[0] = t;
currentTriangle.TriangleNormals[0] = n;
fscanf(filePointer, "%d/%d%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1], &currentTriangle.TriangleTextureCoords[1]);
fscanf(filePointer, "%d/%d%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1], &currentTriangle.TriangleNormals[1]);
}
else if (sscanf(buf, "%d/%d", &v, &t) == 2){
currentTriangle.TriangleTextureCoords[0] = t;
currentTriangle.TriangleEdges[0] = v;
fscanf(filePointer, "%d/%d", &currentTriangle.TriangleEdges[1], &currentTriangle.TriangleTextureCoords[1]);
fscanf(filePointer, "%d/%d", &currentTriangle.TriangleEdges[2], &currentTriangle.TriangleTextureCoords[2]);
}
else {
sscanf(buf, "%d", &v);
currentTriangle.TriangleEdges[0] = v;
fscanf(filePointer, "%d", currentTriangle.TriangleEdges[1]);
fscanf(filePointer, "%d", currentTriangle.TriangleEdges[2]);
}
//cout << currentTriangleIndex << ": ";
//cout << "f " << currentTriangle.TriangleEdges[0] << "/" << currentTriangle.TriangleTextureCoords[0] << "/" << currentTriangle.TriangleNormals[0];
//cout << currentTriangle.TriangleEdges[1] << "/" << currentTriangle.TriangleTextureCoords[1] << "/" << currentTriangle.TriangleNormals[0];
//cout << currentTriangle.TriangleEdges[2] << "/" << currentTriangle.TriangleTextureCoords[2] << "/" << currentTriangle.TriangleNormals[2] << '\n';

//cout << "f " << currentTriangle.TriangleEdges[0] << "//" << currentTriangle.TriangleNormals[0] << " ";
//cout << currentTriangle.TriangleEdges[1] << "//" << currentTriangle.TriangleNormals[1] << " ";
//cout << currentTriangle.TriangleEdges[2] << "//" << currentTriangle.TriangleNormals[2] << "\n";
// model->triangles[currentTriangleIndex++] = currentTriangle;
model->triangles.push_back(currentTriangle);
currentTriangleIndex++;
}
break;
default:
fgets(buf, sizeof(buf), filePointer);
break;
}
}
}

#endif



For your facilitation, I include a .obj file that represents a solid cone using only a few vertices. It is simpler to debug.

cone.obj:

g object
usemtl Texture
v -21.2132 0 21.2132
v -21.2132 0 21.2132
v -15.9099 0 15.9099
v -30 0 2.62268e-06
v -30 0 2.62268e-06
v 3.57746e-07 0 30
v 3.57746e-07 0 30
v -18.5616 7.5 18.5616
v -22.5 0 1.96701e-06
v 2.6831e-07 0 22.5
v -10.6066 0 10.6066
v -26.25 7.5 2.29485e-06
v 3.13028e-07 7.5 26.25
v -15.9099 15 15.9099
v 1.78873e-07 0 15
v -15 0 1.31134e-06
v -5.3033 0 5.3033
v -22.5 15 1.96701e-06
v -7.5 0 6.55671e-07
v 8.94366e-08 0 7.5
v 2.6831e-07 15 22.5
v -13.2582 22.5 13.2583
v 15.9099 0 15.9099
v 21.2132 0 21.2132
v 21.2132 0 21.2132
v 10.6066 0 10.6066
v -21.2132 0 -21.2132
v -10.6066 0 -10.6066
v 0 0 -0
v -5.3033 0 -5.3033
v 5.3033 0 5.3033
v -15.9099 0 -15.9099
v -21.2132 0 -21.2132
v -18.75 22.5 1.63918e-06
v 2.23592e-07 22.5 18.75
v 18.5616 7.5 18.5616
v -18.5616 7.5 -18.5616
v 7.5 0 -0
v -3.27835e-07 0 -7.5
v -10.6066 30 10.6066
v 5.3033 0 -5.3033
v 15.9099 15 15.9099
v -15 30 1.31134e-06
v -15.9099 15 -15.9099
v -6.55671e-07 0 -15
v 15 0 -0
v 1.78873e-07 30 15
v 10.6066 0 -10.6066
v -7.95495 37.5 7.95495
v 13.2582 22.5 13.2583
v -9.83506e-07 0 -22.5
v -13.2583 22.5 -13.2583
v 22.5 0 -0
v -11.25 37.5 9.83506e-07
v 1.34155e-07 37.5 11.25
v 10.6066 30 10.6066
v -1.31134e-06 0 -30
v -1.31134e-06 0 -30
v -10.6066 30 -10.6066
v 30 0 -0
v 30 0 -0
v 15.9099 0 -15.9099
v -1.14742e-06 7.5 -26.25
v 26.25 7.5 -0
v -5.3033 45 5.3033
v 7.95495 37.5 7.95495
v -9.83506e-07 15 -22.5
v -7.5 45 6.55671e-07
v -7.95495 37.5 -7.95495
v 8.94366e-08 45 7.5
v 22.5 15 -0
v -8.19589e-07 22.5 -18.75
v 18.75 22.5 -0
v 21.2132 0 -21.2132
v 21.2132 0 -21.2132
v 18.5616 7.5 -18.5616
v 5.3033 45 5.3033
v -6.55671e-07 30 -15
v -5.3033 45 -5.3033
v 15 30 -0
v 15.9099 15 -15.9099
v -2.65165 52.5 2.65165
v -3.75 52.5 3.27835e-07
v -4.91753e-07 37.5 -11.25
v 4.47183e-08 52.5 3.75
v 11.25 37.5 -0
v 13.2583 22.5 -13.2583
v 10.6066 30 -10.6066
v -2.65165 52.5 -2.65165
v -3.27835e-07 45 -7.5
v 2.65165 52.5 2.65165
v 7.5 45 -0
v 7.95495 37.5 -7.95495
v 5.3033 45 -5.3033
v -1.63918e-07 52.5 -3.75
v 3.75 52.5 -0
v 2.65165 52.5 -2.65165
v 0 60 -0
vn -0.632455 0.447214 0.632456
vn 0 -1 -0
vn 0 -1 -0
vn -0.894427 0.447214 7.81933e-08
vn 0 -1 -0
vn 0 -1 -0
vn 1.06659e-08 0.447214 0.894427
vn -0.632455 0.447214 0.632456
vn 0 -1 -0
vn 0 -1 -0
vn 0 -1 -0
vn -0.894427 0.447214 7.81933e-08
vn 1.06659e-08 0.447214 0.894427
vn -0.632455 0.447214 0.632456
vn 0 -1 -0
vn 0 -1 -0
vn 0 -1 -0
vn -0.894427 0.447214 7.81933e-08
vn 0 -1 -0
vn 0 -1 -0
vn 1.06659e-08 0.447214 0.894427
vn -0.632455 0.447214 0.632456
vn 0 -1 -0
vn 0 -1 -0
vn 0.632455 0.447214 0.632456
vn 0 -1 -0
vn -0.632456 0.447214 -0.632456
vn 0 -1 -0
vn 0 -1 -0
vn 0 -1 -0
vn 0 -1 -0
vn 0 -1 -0
vn 0 -1 -0
vn -0.894427 0.447214 7.81933e-08
vn 1.06659e-08 0.447214 0.894427
vn 0.632455 0.447214 0.632456
vn -0.632456 0.447214 -0.632456
vn 0 -1 -0
vn 0 -1 -0
vn -0.632455 0.447214 0.632456
vn 0 -1 -0
vn 0.632455 0.447214 0.632456
vn -0.894427 0.447214 7.81933e-08
vn -0.632456 0.447214 -0.632456
vn 0 -1 -0
vn 0 -1 -0
vn 1.06659e-08 0.447214 0.894427
vn 0 -1 -0
vn -0.632455 0.447214 0.632456
vn 0.632455 0.447214 0.632456
vn 0 -1 -0
vn -0.632456 0.447214 -0.632456
vn 0 -1 -0
vn -0.894427 0.447214 7.81933e-08
vn 1.06659e-08 0.447214 0.894427
vn 0.632455 0.447214 0.632456
vn -3.90967e-08 0.447214 -0.894427
vn 0 -1 -0
vn -0.632456 0.447214 -0.632456
vn 0 -1 -0
vn 0.894427 0.447214 -0
vn 0 -1 -0
vn -3.90967e-08 0.447214 -0.894427
vn 0.894427 0.447214 -0
vn -0.632455 0.447214 0.632456
vn 0.632455 0.447214 0.632456
vn -3.90967e-08 0.447214 -0.894427
vn -0.894427 0.447214 7.81933e-08
vn -0.632456 0.447214 -0.632456
vn 1.06659e-08 0.447214 0.894427
vn 0.894427 0.447214 -0
vn -3.90967e-08 0.447214 -0.894427
vn 0.894427 0.447214 -0
vn 0 -1 -0
vn 0.632456 0.447214 -0.632456
vn 0.632456 0.447214 -0.632456
vn 0.632455 0.447214 0.632456
vn -3.90967e-08 0.447214 -0.894427
vn -0.632456 0.447214 -0.632456
vn 0.894427 0.447214 -0
vn 0.632456 0.447214 -0.632456
vn -0.632455 0.447214 0.632456
vn -0.894427 0.447214 7.81933e-08
vn -3.90967e-08 0.447214 -0.894427
vn 1.06659e-08 0.447214 0.894427
vn 0.894427 0.447214 -0
vn 0.632456 0.447214 -0.632456
vn 0.632456 0.447214 -0.632456
vn -0.632456 0.447214 -0.632456
vn -3.90967e-08 0.447214 -0.894427
vn 0.632455 0.447214 0.632456
vn 0.894427 0.447214 -0
vn 0.632456 0.447214 -0.632456
vn 0.632456 0.447214 -0.632456
vn -3.90967e-08 0.447214 -0.894427
vn 0.894427 0.447214 -0
vn 0.632456 0.447214 -0.632456
vn 0 1 -0
f 98//98 96//96 97//97
f 98//98 97//97 95//95
f 98//98 95//95 89//89
f 98//98 89//89 83//83
f 98//98 83//83 82//82
f 98//98 82//82 85//85
f 98//98 85//85 91//91
f 98//98 91//91 96//96
f 96//96 92//92 94//94
f 96//96 94//94 97//97
f 97//97 94//94 90//90
f 97//97 90//90 95//95
f 95//95 90//90 79//79
f 95//95 79//79 89//89
f 89//89 79//79 68//68
f 89//89 68//68 83//83
f 83//83 68//68 65//65
f 83//83 65//65 82//82
f 82//82 65//65 70//70
f 82//82 70//70 85//85
f 85//85 70//70 77//77
f 85//85 77//77 91//91
f 91//91 77//77 92//92
f 91//91 92//92 96//96
f 92//92 86//86 93//93
f 92//92 93//93 94//94
f 94//94 93//93 84//84
f 94//94 84//84 90//90
f 90//90 84//84 69//69
f 90//90 69//69 79//79
f 79//79 69//69 54//54
f 79//79 54//54 68//68
f 68//68 54//54 49//49
f 68//68 49//49 65//65
f 65//65 49//49 55//55
f 65//65 55//55 70//70
f 70//70 55//55 66//66
f 70//70 66//66 77//77
f 77//77 66//66 86//86
f 77//77 86//86 92//92
f 86//86 80//80 88//88
f 86//86 88//88 93//93
f 93//93 88//88 78//78
f 93//93 78//78 84//84
f 84//84 78//78 59//59
f 84//84 59//59 69//69
f 69//69 59//59 43//43
f 69//69 43//43 54//54
f 54//54 43//43 40//40
f 54//54 40//40 49//49
f 49//49 40//40 47//47
f 49//49 47//47 55//55
f 55//55 47//47 56//56
f 55//55 56//56 66//66
f 66//66 56//56 80//80
f 66//66 80//80 86//86
f 80//80 73//73 87//87
f 80//80 87//87 88//88
f 88//88 87//87 72//72
f 88//88 72//72 78//78
f 78//78 72//72 52//52
f 78//78 52//52 59//59
f 59//59 52//52 34//34
f 59//59 34//34 43//43
f 43//43 34//34 22//22
f 43//43 22//22 40//40
f 40//40 22//22 35//35
f 40//40 35//35 47//47
f 47//47 35//35 50//50
f 47//47 50//50 56//56
f 56//56 50//50 73//73
f 56//56 73//73 80//80
f 73//73 71//71 81//81
f 73//73 81//81 87//87
f 87//87 81//81 67//67
f 87//87 67//67 72//72
f 72//72 67//67 44//44
f 72//72 44//44 52//52
f 52//52 44//44 18//18
f 52//52 18//18 34//34
f 34//34 18//18 14//14
f 34//34 14//14 22//22
f 22//22 14//14 21//21
f 22//22 21//21 35//35
f 35//35 21//21 42//42
f 35//35 42//42 50//50
f 50//50 42//42 71//71
f 50//50 71//71 73//73
f 71//71 64//64 76//76
f 71//71 76//76 81//81
f 81//81 76//76 63//63
f 81//81 63//63 67//67
f 67//67 63//63 37//37
f 67//67 37//37 44//44
f 44//44 37//37 12//12
f 44//44 12//12 18//18
f 18//18 12//12 8//8
f 18//18 8//8 14//14
f 14//14 8//8 13//13
f 14//14 13//13 21//21
f 21//21 13//13 36//36
f 21//21 36//36 42//42
f 42//42 36//36 64//64
f 42//42 64//64 71//71
f 64//64 61//61 75//75
f 64//64 75//75 76//76
f 76//76 75//75 57//57
f 76//76 57//57 63//63
f 63//63 57//57 27//27
f 63//63 27//27 37//37
f 37//37 27//27 4//4
f 37//37 4//4 12//12
f 12//12 4//4 1//1
f 12//12 1//1 8//8
f 8//8 1//1 7//7
f 8//8 7//7 13//13
f 13//13 7//7 25//25
f 13//13 25//25 36//36
f 36//36 25//25 61//61
f 36//36 61//61 64//64
f 29//29 31//31 20//20
f 29//29 20//20 17//17
f 29//29 17//17 19//19
f 29//29 19//19 30//30
f 29//29 30//30 39//39
f 29//29 39//39 41//41
f 29//29 41//41 38//38
f 29//29 38//38 31//31
f 31//31 26//26 15//15
f 31//31 15//15 20//20
f 20//20 15//15 11//11
f 20//20 11//11 17//17
f 17//17 11//11 16//16
f 17//17 16//16 19//19
f 19//19 16//16 28//28
f 19//19 28//28 30//30
f 30//30 28//28 45//45
f 30//30 45//45 39//39
f 39//39 45//45 48//48
f 39//39 48//48 41//41
f 41//41 48//48 46//46
f 41//41 46//46 38//38
f 38//38 46//46 26//26
f 38//38 26//26 31//31
f 26//26 23//23 10//10
f 26//26 10//10 15//15
f 15//15 10//10 3//3
f 15//15 3//3 11//11
f 11//11 3//3 9//9
f 11//11 9//9 16//16
f 16//16 9//9 32//32
f 16//16 32//32 28//28
f 28//28 32//32 51//51
f 28//28 51//51 45//45
f 45//45 51//51 62//62
f 45//45 62//62 48//48
f 48//48 62//62 53//53
f 48//48 53//53 46//46
f 46//46 53//53 23//23
f 46//46 23//23 26//26
f 23//23 24//24 6//6
f 23//23 6//6 10//10
f 10//10 6//6 2//2
f 10//10 2//2 3//3
f 3//3 2//2 5//5
f 3//3 5//5 9//9
f 9//9 5//5 33//33
f 9//9 33//33 32//32
f 32//32 33//33 58//58
f 32//32 58//58 51//51
f 51//51 58//58 74//74
f 51//51 74//74 62//62
f 62//62 74//74 60//60
f 62//62 60//60 53//53
f 53//53 60//60 24//24
f 53//53 24//24 23//23



Any ideas?

Share this post


Link to post
Share on other sites
maybe try even something simpler than a cone, a cube for instance, draw its vertices as billboard quads, draw the edges as lines as well as the polygons? this way you can visually see where the vert's should be.

This is what I did when loading in '.3ds' meshes for the first time once the cube worked so did everything else :D

Share this post


Link to post
Share on other sites
Thank you all for your answers, you have been very helpful. I found the problem.

The problem was the one that superoptimo said. I fixed that, and I changed some other variables that concerned the way I have my OpenGL set up in my project. Sorry I could give you my entire project file, but I thought you would be using Xcode anyway...

Thanks a lot!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628316
    • Total Posts
      2982033
  • Similar Content

    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
    • By Abecederia
      So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
      Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
      Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!
    • By test opty
      Hi,
      I'm trying to learn OpenGL through a website and have proceeded until this page of it. The output is a simple triangle. The problem is the complexity.
      I have read that page several times and tried to analyse the code but I haven't understood the code properly and completely yet. This is the code:
       
      #include <glad/glad.h> #include <GLFW/glfw3.h> #include <C:\Users\Abbasi\Desktop\std_lib_facilities_4.h> using namespace std; //****************************************************************************** void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); // settings const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 600; const char *vertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "void main()\n" "{\n" " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" "}\0"; const char *fragmentShaderSource = "#version 330 core\n" "out vec4 FragColor;\n" "void main()\n" "{\n" " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" "}\n\0"; //******************************* int main() { // glfw: initialize and configure // ------------------------------ glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // glfw window creation GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "My First Triangle", nullptr, nullptr); if (window == nullptr) { cout << "Failed to create GLFW window" << endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // glad: load all OpenGL function pointers if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { cout << "Failed to initialize GLAD" << endl; return -1; } // build and compile our shader program // vertex shader int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr); glCompileShader(vertexShader); // check for shader compile errors int success; char infoLog[512]; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog); cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << endl; } // fragment shader int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr); glCompileShader(fragmentShader); // check for shader compile errors glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog); cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << endl; } // link shaders int shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); // check for linking errors glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog); cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << endl; } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); // set up vertex data (and buffer(s)) and configure vertex attributes float vertices[] = { -0.5f, -0.5f, 0.0f, // left 0.5f, -0.5f, 0.0f, // right 0.0f, 0.5f, 0.0f // top }; unsigned int VBO, VAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); // bind the Vertex Array Object first, then bind and set vertex buffer(s), //and then configure vertex attributes(s). glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // note that this is allowed, the call to glVertexAttribPointer registered VBO // as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind glBindBuffer(GL_ARRAY_BUFFER, 0); // You can unbind the VAO afterwards so other VAO calls won't accidentally // modify this VAO, but this rarely happens. Modifying other // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind // VAOs (nor VBOs) when it's not directly necessary. glBindVertexArray(0); // uncomment this call to draw in wireframe polygons. //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // render loop while (!glfwWindowShouldClose(window)) { // input // ----- processInput(window); // render // ------ glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // draw our first triangle glUseProgram(shaderProgram); glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to // bind it every time, but we'll do so to keep things a bit more organized glDrawArrays(GL_TRIANGLES, 0, 3); // glBindVertexArray(0); // no need to unbind it every time // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) glfwSwapBuffers(window); glfwPollEvents(); } // optional: de-allocate all resources once they've outlived their purpose: glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); // glfw: terminate, clearing all previously allocated GLFW resources. glfwTerminate(); return 0; } //************************************************** // process all input: query GLFW whether relevant keys are pressed/released // this frame and react accordingly void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); } //******************************************************************** // glfw: whenever the window size changed (by OS or user resize) this callback function executes void framebuffer_size_callback(GLFWwindow* window, int width, int height) { // make sure the viewport matches the new window dimensions; note that width and // height will be significantly larger than specified on retina displays. glViewport(0, 0, width, height); } As you see, about 200 lines of complicated code only for a simple triangle. 
      I don't know what parts are necessary for that output. And also, what the correct order of instructions for such an output or programs is, generally. That start point is too complex for a beginner of OpenGL like me and I don't know how to make the issue solved. What are your ideas please? What is the way to figure both the code and the whole program out correctly please?
      I wish I'd read a reference that would teach me OpenGL through a step-by-step method. 
  • Popular Now