[C++] Caricamento modello 3d Opengl 3.3

cyrus2
Salve, devo caricare dei modelli 3d in Opengl 3.3 per un progetto di grafica 3D. Il problema è che riesco a caricare i modelli sono il opengl 3.0 ma non in Opengl 3.3 e ottengo una schermata vuota.Di seguito il codice per renderizzare il modello 3d.

void recursive_render(const struct aiScene* sc, const struct aiNode* nd, float scale){


	unsigned int i;
	unsigned int n = 0, t;
	aiMatrix4x4 m = nd->mTransformation;

	aiMatrix4x4 m2;
	aiMatrix4x4::Scaling(aiVector3D(scale, scale, scale), m2);
	m = m * m2;

	m.Transpose();

	float aux[16];
	memcpy(aux, &m, sizeof(float) * 16);

	for (; n < nd->mNumMeshes; ++n)
	{
		const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];

		
		glGenVertexArrays(1, &VAO3);
		glBindVertexArray(VAO3);
	
		glDrawElements(GL_TRIANGLES, mesh->mNumFaces * 3, GL_UNSIGNED_INT, 0);
		

	}

	// draw all children
	for (n = 0; n < nd->mNumChildren; ++n)
		recursive_render(sc, nd->mChildren[n], scale);

	//glPopMatrix();
}

Codice per generare i vertex array objects
void genVAOsAndUniformBuffer(const aiScene* sc, const struct aiNode* nd, float scale) {

	unsigned int i;
	unsigned int n = 0, t;
	for (; n < nd->mNumMeshes; ++n)
	{

		const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
		unsigned int* faceArray;
		faceArray = (unsigned int*)malloc(sizeof(unsigned int) * mesh->mNumFaces * 3);
		unsigned int faceIndex = 0;
		for (unsigned int t = 0; t < mesh->mNumFaces; ++t) {
			const aiFace* face = &mesh->mFaces[t];

			memcpy(&faceArray[faceIndex], face->mIndices, 3 * sizeof(unsigned int));
			faceIndex += 3;
		}
		glGenVertexArrays(1, &VAO3);
		glBindVertexArray(VAO3);

		// buffer for faces
		glGenBuffers(1, &VBO3);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO3);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(mesh->mNumFaces) * 3, faceArray, GL_STATIC_DRAW);

		// buffer for vertex positions
		if (mesh->HasPositions()) {
			glGenBuffers(1, &VBO3);
			glBindBuffer(GL_ARRAY_BUFFER, VBO3);
			glBufferData(GL_ARRAY_BUFFER, sizeof(mesh->mNumVertices) * 3, mesh->mVertices, GL_STATIC_DRAW);
			glEnableVertexAttribArray(0);
			glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, 0);
		}
		if (mesh->HasNormals()) {
			glGenBuffers(1, &VBO3);
			glBindBuffer(GL_ARRAY_BUFFER, VBO3);
			glBufferData(GL_ARRAY_BUFFER, sizeof(mesh->mNumVertices) * 3, mesh->mNormals, GL_STATIC_DRAW);
			glEnableVertexAttribArray(1);
			glVertexAttribPointer(1, 3, GL_FLOAT, 0, 0, 0);
		}
	}
}

Codice per importare il modello 3d da file.
bool Import3DFromFile(const std::string& pFile)
{
	// Check if file exists
	std::ifstream fin(pFile.c_str());
	if (!fin.fail())
	{
		fin.close();
	}
	else
	{
		std::cout << "Couldn't open file: " << pFile.c_str() << std::endl;
		//logInfo(importer.GetErrorString());
		return false;
	}

	scene = importer.ReadFile(pFile, aiProcessPreset_TargetRealtime_Quality);

	// If the import failed, report it
	if (!scene)
	{
		std::cout << "Import of scene 5" << pFile << " succeeded." << std::endl;
		//logInfo(importer.GetErrorString());
		return false;
	}

	// Now we can access the file's contents.
	std::cout << "Import of scene " << pFile << " succeeded." << std::endl;

	// We're done. Everything will be cleaned up by the importer destructor
	return true;
}

I modelli 3d che utilizzo hanno estensione .3ds e .dae. Non riesco a capire dove siano gli errori. Avete qualche suggerimento?

Risposte
apatriarca
Sei sicuro di essere riuscito a creare un context valido per OpenGL 3.3? È difficile trovare errori nel tuo codice semplicemente guardando il codice. Dovresti verificare la presenza di errori dopo le varie chiamate OpenGL. Ti consiglio poi di provare con un programma come RenderDoc.

cyrus2
Il punto è che in Opengl 3.0 riesco a caricarlo, il problema è il 3.3 perchè non sono sicuro che il codice per reinderizzare il modello in visual studio sia corretto. Sto provando RenderDoc ma a cosa dovrebbe servirmi esattamente?

apatriarca
RenderDoc è un debugger per applicazioni OpenGL, Vulkan, D3D, ... Permette di analizzare le risorse allocate sulla GPU, vedere i buffer, analizzare le texture e i framebuffer, dare una occhiata ai comandi OpenGL.. Dovrebbe aiutarti a capire che cosa è andato storto nel tuo programma.

L'errore del tuo programma potrebbe essere causato da un sacco di ragioni e non è facile (né forse possibile) trovare l'errore dal poco codice che ci hai fornito. Per esempio manca la parte del codice in cui hai inizializzato il context OpenGL, la parte del codice in cui definisci gli shader usati per visualizzare il tuo oggetto.. L'errore potrebbe inoltre essere in qualche modo legato al tuo sistema (il driver installato per esempio o la scheda video).

cyrus2
Ho risolto, grazie lo stesso

apatriarca
E qual'era il problema?

cyrus2
Il codice utilizzato per il caricamento del modello 3d era completamente sbagliato, su un sito ho trovato quello corretto e ha funzionato.

Rispondi
Per rispondere a questa discussione devi prima effettuare il login.