Jump to content
  • Advertisement
Sign in to follow this  
Soulstormgr

OpenGL My .obj model loader

This topic is 3565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = otherTriangle.TriangleEdges;
			TriangleTextureCoords = otherTriangle.TriangleTextureCoords;
			TriangleNormals = otherTriangle.TriangleNormals;
		}
	}
	Triangle(){
		reset();
	}
	void reset(){
		for (int i=0; i<3; i++) {
			TriangleEdges = 0;
			TriangleTextureCoords = 0;
			TriangleNormals = 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
Advertisement
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
I will look into it but I doubt this is the issue here. The result is sooo wrong that it can't be the fault of one misplaced vertex only...

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 another forum 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 = otherTriangle.TriangleEdges;
TriangleTextureCoords = otherTriangle.TriangleTextureCoords;
TriangleNormals = otherTriangle.TriangleNormals;
}
}
Triangle(){
reset();
}
void reset(){
for (int i=0; i<3; i++) {
TriangleEdges = 0;
TriangleTextureCoords = 0;
TriangleNormals = 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 = vertices[triangleVertexPositions[0]];
v2 = vertices[triangleVertexPositions[1]];
v3 = vertices[triangleVertexPositions[2]];
n1 = vertexNormals[triangleNormalPositions[0]];
n2 = vertexNormals[triangleNormalPositions[1]];
n3 = vertexNormals[triangleNormalPositions[2]];
}

#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/=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
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By mmmax3d
      Hi everyone,
      I would need some assistance from anyone who has a similar experience
      or a nice idea!
      I have created a skybox (as cube) and now I need to add a floor/ground.
      The skybox is created from cubemap and initially it was infinite.
      Now it is finite with a specific size. The floor is a quad in the middle
      of the skybox, like a horizon.
      I have two problems:
      When moving the skybox upwards or downwards, I need to
      sample from points even above the horizon while sampling
      from the botton at the same time.  I am trying to create a seamless blending of the texture
      at the points of the horizon, when the quad is connected
      to the skybox. However, I get skew effects. Does anybody has done sth similar?
      Is there any good practice?
      Thanks everyone!
    • By mmmax3d
      Hi everyone,
      I would need some assistance from anyone who has a similar experience
      or a nice idea!
      I have created a skybox (as cube) and now I need to add a floor/ground.
      The skybox is created from cubemap and initially it was infinite.
      Now it is finite with a specific size. The floor is a quad in the middle
      of the skybox, like a horizon.
      I have two problems:
      When moving the skybox upwards or downwards, I need to
      sample from points even above the horizon while sampling
      from the botton at the same time.  I am trying to create a seamless blending of the texture
      at the points of the horizon, when the quad is connected
      to the skybox. However, I get skew effects. Does anybody has done sth similar?
      Is there any good practice?
      Thanks everyone!
    • By iArtist93
      I'm trying to implement PBR into my simple OpenGL renderer and trying to use multiple lighting passes, I'm using one pass per light for rendering as follow:
      1- First pass = depth
      2- Second pass = ambient
      3- [3 .. n] for all the lights in the scene.
      I'm using the blending function glBlendFunc(GL_ONE, GL_ONE) for passes [3..n], and i'm doing a Gamma Correction at the end of each fragment shader.
      But i still have a problem with the output image it just looks noisy specially when i'm using texture maps.
      Is there anything wrong with those steps or is there any improvement to this process?
    • By babaliaris
      Hello Everyone!
      I'm learning openGL, and currently i'm making a simple 2D game engine to test what I've learn so far.  In order to not say to much, i made a video in which i'm showing you the behavior of the rendering.
      Video: 
       
      What i was expecting to happen, was the player moving around. When i render only the player, he moves as i would expect. When i add a second Sprite object, instead of the Player, this new sprite object is moving and finally if i add a third Sprite object the third one is moving. And the weird think is that i'm transforming the Vertices of the Player so why the transformation is being applied somewhere else?
       
      Take a look at my code:
      Sprite Class
      (You mostly need to see the Constructor, the Render Method and the Move Method)
      #include "Brain.h" #include <glm/gtc/matrix_transform.hpp> #include <vector> struct Sprite::Implementation { //Position. struct pos pos; //Tag. std::string tag; //Texture. Texture *texture; //Model matrix. glm::mat4 model; //Vertex Array Object. VertexArray *vao; //Vertex Buffer Object. VertexBuffer *vbo; //Layout. VertexBufferLayout *layout; //Index Buffer Object. IndexBuffer *ibo; //Shader. Shader *program; //Brains. std::vector<Brain *> brains; //Deconstructor. ~Implementation(); }; Sprite::Sprite(std::string image_path, std::string tag, float x, float y) { //Create Pointer To Implementaion. m_Impl = new Implementation(); //Set the Position of the Sprite object. m_Impl->pos.x = x; m_Impl->pos.y = y; //Set the tag. m_Impl->tag = tag; //Create The Texture. m_Impl->texture = new Texture(image_path); //Initialize the model Matrix. m_Impl->model = glm::mat4(1.0f); //Get the Width and the Height of the Texture. int width = m_Impl->texture->GetWidth(); int height = m_Impl->texture->GetHeight(); //Create the Verticies. float verticies[] = { //Positions //Texture Coordinates. x, y, 0.0f, 0.0f, x + width, y, 1.0f, 0.0f, x + width, y + height, 1.0f, 1.0f, x, y + height, 0.0f, 1.0f }; //Create the Indicies. unsigned int indicies[] = { 0, 1, 2, 2, 3, 0 }; //Create Vertex Array. m_Impl->vao = new VertexArray(); //Create the Vertex Buffer. m_Impl->vbo = new VertexBuffer((void *)verticies, sizeof(verticies)); //Create The Layout. m_Impl->layout = new VertexBufferLayout(); m_Impl->layout->PushFloat(2); m_Impl->layout->PushFloat(2); m_Impl->vao->AddBuffer(m_Impl->vbo, m_Impl->layout); //Create the Index Buffer. m_Impl->ibo = new IndexBuffer(indicies, 6); //Create the new shader. m_Impl->program = new Shader("Shaders/SpriteShader.shader"); } //Render. void Sprite::Render(Window * window) { //Create the projection Matrix based on the current window width and height. glm::mat4 proj = glm::ortho(0.0f, (float)window->GetWidth(), 0.0f, (float)window->GetHeight(), -1.0f, 1.0f); //Set the MVP Uniform. m_Impl->program->setUniformMat4f("u_MVP", proj * m_Impl->model); //Run All The Brains (Scripts) of this game object (sprite). for (unsigned int i = 0; i < m_Impl->brains.size(); i++) { //Get Current Brain. Brain *brain = m_Impl->brains[i]; //Call the start function only once! if (brain->GetStart()) { brain->SetStart(false); brain->Start(); } //Call the update function every frame. brain->Update(); } //Render. window->GetRenderer()->Draw(m_Impl->vao, m_Impl->ibo, m_Impl->texture, m_Impl->program); } void Sprite::Move(float speed, bool left, bool right, bool up, bool down) { if (left) { m_Impl->pos.x -= speed; m_Impl->model = glm::translate(m_Impl->model, glm::vec3(-speed, 0, 0)); } if (right) { m_Impl->pos.x += speed; m_Impl->model = glm::translate(m_Impl->model, glm::vec3(speed, 0, 0)); } if (up) { m_Impl->pos.y += speed; m_Impl->model = glm::translate(m_Impl->model, glm::vec3(0, speed, 0)); } if (down) { m_Impl->pos.y -= speed; m_Impl->model = glm::translate(m_Impl->model, glm::vec3(0, -speed, 0)); } } void Sprite::AddBrain(Brain * brain) { //Push back the brain object. m_Impl->brains.push_back(brain); } pos *Sprite::GetPos() { return &m_Impl->pos; } std::string Sprite::GetTag() { return m_Impl->tag; } int Sprite::GetWidth() { return m_Impl->texture->GetWidth(); } int Sprite::GetHeight() { return m_Impl->texture->GetHeight(); } Sprite::~Sprite() { delete m_Impl; } //Implementation Deconstructor. Sprite::Implementation::~Implementation() { delete texture; delete vao; delete vbo; delete layout; delete ibo; delete program; }  
      Renderer Class
      #include "Renderer.h" #include "Error.h" Renderer::Renderer() { } Renderer::~Renderer() { } void Renderer::Draw(VertexArray * vao, IndexBuffer * ibo, Texture *texture, Shader * program) { vao->Bind(); ibo->Bind(); program->Bind(); if (texture != NULL) texture->Bind(); GLCall(glDrawElements(GL_TRIANGLES, ibo->GetCount(), GL_UNSIGNED_INT, NULL)); } void Renderer::Clear(float r, float g, float b) { GLCall(glClearColor(r, g, b, 1.0)); GLCall(glClear(GL_COLOR_BUFFER_BIT)); } void Renderer::Update(GLFWwindow *window) { /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); }  
      Shader Code
      #shader vertex #version 330 core layout(location = 0) in vec4 aPos; layout(location = 1) in vec2 aTexCoord; out vec2 t_TexCoord; uniform mat4 u_MVP; void main() { gl_Position = u_MVP * aPos; t_TexCoord = aTexCoord; } #shader fragment #version 330 core out vec4 aColor; in vec2 t_TexCoord; uniform sampler2D u_Texture; void main() { aColor = texture(u_Texture, t_TexCoord); } Also i'm pretty sure that every time i'm hitting the up, down, left and right arrows on the keyboard, i'm changing the model Matrix of the Player and not the others.
       
      Window Class:
      #include "Window.h" #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Error.h" #include "Renderer.h" #include "Scene.h" #include "Input.h" //Global Variables. int screen_width, screen_height; //On Window Resize. void OnWindowResize(GLFWwindow *window, int width, int height); //Implementation Structure. struct Window::Implementation { //GLFW Window. GLFWwindow *GLFW_window; //Renderer. Renderer *renderer; //Delta Time. double delta_time; //Frames Per Second. int fps; //Scene. Scene *scnene; //Input. Input *input; //Deconstructor. ~Implementation(); }; //Window Constructor. Window::Window(std::string title, int width, int height) { //Initializing width and height. screen_width = width; screen_height = height; //Create Pointer To Implementation. m_Impl = new Implementation(); //Try initializing GLFW. if (!glfwInit()) { std::cout << "GLFW could not be initialized!" << std::endl; std::cout << "Press ENTER to exit..." << std::endl; std::cin.get(); exit(-1); } //Setting up OpenGL Version 3.3 Core Profile. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); /* Create a windowed mode window and its OpenGL context */ m_Impl->GLFW_window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL); if (!m_Impl->GLFW_window) { std::cout << "GLFW could not create a window!" << std::endl; std::cout << "Press ENTER to exit..." << std::endl; std::cin.get(); glfwTerminate(); exit(-1); } /* Make the window's context current */ glfwMakeContextCurrent(m_Impl->GLFW_window); //Initialize GLEW. if(glewInit() != GLEW_OK) { std::cout << "GLEW could not be initialized!" << std::endl; std::cout << "Press ENTER to exit..." << std::endl; std::cin.get(); glfwTerminate(); exit(-1); } //Enabling Blending. GLCall(glEnable(GL_BLEND)); GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); //Setting the ViewPort. GLCall(glViewport(0, 0, width, height)); //**********Initializing Implementation**********// m_Impl->renderer = new Renderer(); m_Impl->delta_time = 0.0; m_Impl->fps = 0; m_Impl->input = new Input(this); //**********Initializing Implementation**********// //Set Frame Buffer Size Callback. glfwSetFramebufferSizeCallback(m_Impl->GLFW_window, OnWindowResize); } //Window Deconstructor. Window::~Window() { delete m_Impl; } //Window Main Loop. void Window::MainLoop() { //Time Variables. double start_time = 0, end_time = 0, old_time = 0, total_time = 0; //Frames Counter. int frames = 0; /* Loop until the user closes the window */ while (!glfwWindowShouldClose(m_Impl->GLFW_window)) { old_time = start_time; //Total time of previous frame. start_time = glfwGetTime(); //Current frame start time. //Calculate the Delta Time. m_Impl->delta_time = start_time - old_time; //Get Frames Per Second. if (total_time >= 1) { m_Impl->fps = frames; total_time = 0; frames = 0; } //Clearing The Screen. m_Impl->renderer->Clear(0, 0, 0); //Render The Scene. if (m_Impl->scnene != NULL) m_Impl->scnene->Render(this); //Updating the Screen. m_Impl->renderer->Update(m_Impl->GLFW_window); //Increasing frames counter. frames++; //End Time. end_time = glfwGetTime(); //Total time after the frame completed. total_time += end_time - start_time; } //Terminate GLFW. glfwTerminate(); } //Load Scene. void Window::LoadScene(Scene * scene) { //Set the scene. m_Impl->scnene = scene; } //Get Delta Time. double Window::GetDeltaTime() { return m_Impl->delta_time; } //Get FPS. int Window::GetFPS() { return m_Impl->fps; } //Get Width. int Window::GetWidth() { return screen_width; } //Get Height. int Window::GetHeight() { return screen_height; } //Get Input. Input * Window::GetInput() { return m_Impl->input; } Renderer * Window::GetRenderer() { return m_Impl->renderer; } GLFWwindow * Window::GetGLFWindow() { return m_Impl->GLFW_window; } //Implementation Deconstructor. Window::Implementation::~Implementation() { delete renderer; delete input; } //OnWindowResize void OnWindowResize(GLFWwindow *window, int width, int height) { screen_width = width; screen_height = height; //Updating the ViewPort. GLCall(glViewport(0, 0, width, height)); }  
      Brain Class
      #include "Brain.h" #include "Sprite.h" #include "Window.h" struct Brain::Implementation { //Just A Flag. bool started; //Window Pointer. Window *window; //Sprite Pointer. Sprite *sprite; }; Brain::Brain(Window *window, Sprite *sprite) { //Create Pointer To Implementation. m_Impl = new Implementation(); //Initialize Implementation. m_Impl->started = true; m_Impl->window = window; m_Impl->sprite = sprite; } Brain::~Brain() { //Delete Pointer To Implementation. delete m_Impl; } void Brain::Start() { } void Brain::Update() { } Window * Brain::GetWindow() { return m_Impl->window; } Sprite * Brain::GetSprite() { return m_Impl->sprite; } bool Brain::GetStart() { return m_Impl->started; } void Brain::SetStart(bool value) { m_Impl->started = value; } Script Class (Its a Brain Subclass!!!)
      #include "Script.h" Script::Script(Window *window, Sprite *sprite) : Brain(window, sprite) { } Script::~Script() { } void Script::Start() { std::cout << "Game Started!" << std::endl; } void Script::Update() { Input *input = this->GetWindow()->GetInput(); Sprite *sp = this->GetSprite(); //Move this sprite. this->GetSprite()->Move(200 * this->GetWindow()->GetDeltaTime(), input->GetKeyDown("left"), input->GetKeyDown("right"), input->GetKeyDown("up"), input->GetKeyDown("down")); std::cout << sp->GetTag().c_str() << ".x = " << sp->GetPos()->x << ", " << sp->GetTag().c_str() << ".y = " << sp->GetPos()->y << std::endl; }  
      Main:
      #include "SpaceShooterEngine.h" #include "Script.h" int main() { Window w("title", 600,600); Scene *scene = new Scene(); Sprite *player = new Sprite("Resources/Images/player.png", "Player", 100,100); Sprite *other = new Sprite("Resources/Images/cherno.png", "Other", 400, 100); Sprite *other2 = new Sprite("Resources/Images/cherno.png", "Other", 300, 400); Brain *brain = new Script(&w, player); player->AddBrain(brain); scene->AddSprite(player); scene->AddSprite(other); scene->AddSprite(other2); w.LoadScene(scene); w.MainLoop(); return 0; }  
       
      I literally can't find what is wrong. If you need more code, ask me to post it. I will also attach all the source files.
      Brain.cpp
      Error.cpp
      IndexBuffer.cpp
      Input.cpp
      Renderer.cpp
      Scene.cpp
      Shader.cpp
      Sprite.cpp
      Texture.cpp
      VertexArray.cpp
      VertexBuffer.cpp
      VertexBufferLayout.cpp
      Window.cpp
      Brain.h
      Error.h
      IndexBuffer.h
      Input.h
      Renderer.h
      Scene.h
      Shader.h
      SpaceShooterEngine.h
      Sprite.h
      Texture.h
      VertexArray.h
      VertexBuffer.h
      VertexBufferLayout.h
      Window.h
    • By Cristian Decu
      Hello fellow programmers,
      For a couple of days now i've decided to build my own planet renderer just to see how floating point precision issues
      can be tackled. As you probably imagine, i've quickly faced FPP issues when trying to render absurdly large planets.
       
      I have used the classical quadtree LOD approach;
      I've generated my grids with 33 vertices, (x: -1 to 1, y: -1 to 1, z = 0).
      Each grid is managed by a TerrainNode class that, depending on the side it represents (top, bottom, left right, front, back),
      creates a special rotation-translation matrix that moves and rotates the grid away from the origin so that when i finally
      normalize all the vertices on my vertex shader i can get a perfect sphere.
      T = glm::translate(glm::dmat4(1.0), glm::dvec3(0.0, 0.0, 1.0)); R = glm::rotate(glm::dmat4(1.0), glm::radians(180.0), glm::dvec3(1.0, 0.0, 0.0)); sides[0] = new TerrainNode(1.0, radius, T * R, glm::dvec2(0.0, 0.0), new TerrainTile(1.0, SIDE_FRONT)); T = glm::translate(glm::dmat4(1.0), glm::dvec3(0.0, 0.0, -1.0)); R = glm::rotate(glm::dmat4(1.0), glm::radians(0.0), glm::dvec3(1.0, 0.0, 0.0)); sides[1] = new TerrainNode(1.0, radius, R * T, glm::dvec2(0.0, 0.0), new TerrainTile(1.0, SIDE_BACK)); // So on and so forth for the rest of the sides As you can see, for the front side grid, i rotate it 180 degrees to make it face the camera and push it towards the eye;
      the back side is handled almost the same way only that i don't need to rotate it but simply push it away from the eye.
      The same technique is applied for the rest of the faces (obviously, with the proper rotations / translations).
      The matrix that result from the multiplication of R and T (in that particular order) is send to my vertex shader as `r_Grid'.
      // spherify vec3 V = normalize((r_Grid * vec4(r_Vertex, 1.0)).xyz); gl_Position = r_ModelViewProjection * vec4(V, 1.0); The `r_ModelViewProjection' matrix is generated on the CPU in this manner.
      // No the most efficient way, but it works. glm::dmat4 Camera::getMatrix() { // Create the view matrix // Roll, Yaw and Pitch are all quaternions. glm::dmat4 View = glm::toMat4(Roll) * glm::toMat4(Pitch) * glm::toMat4(Yaw); // The model matrix is generated by translating in the oposite direction of the camera. glm::dmat4 Model = glm::translate(glm::dmat4(1.0), -Position); // Projection = glm::perspective(fovY, aspect, zNear, zFar); // zNear = 0.1, zFar = 1.0995116e12 return Projection * View * Model; } I managed to get rid of z-fighting by using a technique called Logarithmic Depth Buffer described in this article; it works amazingly well, no z-fighting at all, at least not visible.
      Each frame i'm rendering each node by sending the generated matrices this way.
      // set the r_ModelViewProjection uniform // Sneak in the mRadiusMatrix which is a matrix that contains the radius of my planet. Shader::setUniform(0, Camera::getInstance()->getMatrix() * mRadiusMatrix); // set the r_Grid matrix uniform i created earlier. Shader::setUniform(1, r_Grid); grid->render(); My planet's radius is around 6400000.0 units, absurdly large, but that's what i really want to achieve;
      Everything works well, the node's split and merge as you'd expect, however whenever i get close to the surface
      of the planet the rounding errors start to kick in giving me that lovely stairs effect.
      I've read that if i could render each grid relative to the camera i could get better precision on the surface, effectively
      getting rid of those rounding errors.
       
      My question is how can i achieve this relative to camera rendering in my scenario here?
      I know that i have to do most of the work on the CPU with double, and that's exactly what i'm doing.
      I only use double on the CPU side where i also do most of the matrix multiplications.
      As you can see from my vertex shader i only do the usual r_ModelViewProjection * (some vertex coords).
       
      Thank you for your suggestions!
       
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!