Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa
Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa
Marvel's Wolverine porta Logan in un'avventura inedita, violenta e fortemente narrativa, costruita attorno alla sua natura di combattente e al difficile rapporto con il proprio passato. Insomniac Games punta su combattimenti spettacolari, progressione e personalizzazione, inserendo l'azione in un mondo segnato dalla persecuzione dei mutanti. Un viaggio intenso, che alterna mattanza, esplorazione e momenti sorprendentemente emotivi.
DJI Romo 2: tante novità lo rendono un robot completo
DJI Romo 2: tante novità lo rendono un robot completo
Romo 2 è la seconda generazione di robot lavapavimenti di DJI, un modello che si caratterizza per la precisione nel sistema di navigazione e per il funzionamento particolarmente silenzioso. Con le modifiche introdotte in questa seconda versione, e un posizionamento di prezzo più allineato alla concorrenza, rappresenta una valida alternativa sul mercato delle soluzioni di pulizia domestica
Sony Bravia 9 II: il True RGB alla prova, dove l'LCD sfida l'OLED
Sony Bravia 9 II: il True RGB alla prova, dove l'LCD sfida l'OLED
Il primo Sony con retroilluminazione True RGB alla prova del banco di misura e dei contenuti: luminanza enorme, colori accurati in HDR e un antiriflesso molto efficace. I limiti sono due sole HDMI 2.1 e il blooming fuori asse
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 13-12-2013, 12:17   #1
-Ivan-
Senior Member
 
L'Avatar di -Ivan-
 
Iscritto dal: Mar 2003
Città: Rimini
Messaggi: 1846
[OpenGL] Esercizio per disegnare un cubo mostra solo la faccia frontale

Sto facendo un esercizio in cui cerco di disegnare un cubo con OpenGL.
Non riesco a capire perchè mi mostri solo la faccia frontale.
Per disegnarlo uso glDrawElements dopo aver creato un buffer di indici ed il buffer con i vertici del cubo.

Codice:
// include...


enum Attrib_IDs { vPosition = 0 };

GLuint VERTEX_SHADER_ID;

GLuint VBOs[1];		// Vertex Buffer Objects
GLuint IBOs[1];		// Index Buffer Objects

GLuint SCREEN_WIDTH = 512, SCREEN_HEIGHT = 512;

glm::mat4 mvp;		//model view projection matrix



#pragma region INITIALIZE DATA AND OPENGL

void ModelViewProjectionMatrix()
{
	// Matrix to translate the cube deep far in the scene
	glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, 0.0, -4.0));

	// The lookat function takes: eye (position of the camera), center (where the camera is pointed to), and up (top of the camera).
	glm::mat4 view = glm::lookAt(glm::vec3(0.0, 2.0, 0.0), glm::vec3(0.0, 0.0, -4.0), glm::vec3(0.0, 1.0, 0.0));

	// The perpective function takes: fovy (the lens angle), aspect (the aspect ratio), and zNear, zFar (clipping planes).
	glm::mat4 projection = glm::perspective(45.0f, 1.0f * SCREEN_WIDTH/SCREEN_HEIGHT, 0.1f, 10.0f);


	// We end up with a model view projection matrix giving us the result for our camera
	mvp = projection * view * model;
}

void CreateVerticesData()
{
	Vector3D cube_vertices[] = {
		// front
		Vector3D(-1.0, -1.0,  1.0),
		Vector3D( 1.0, -1.0,  1.0),
		Vector3D( 1.0,  1.0,  1.0),
		Vector3D(-1.0,  1.0,  1.0),
		// back
		Vector3D(-1.0, -1.0, -1.0),
		Vector3D( 1.0, -1.0, -1.0),
		Vector3D( 1.0,  1.0, -1.0),
		Vector3D(-1.0,  1.0, -1.0)
	};

	// Generate the Vertex Buffer Object storing the data for the 8 vertices of the cube.
	// In total there are 24 floats representing the x, y, z components of each vertex.
	glGenBuffers(1, VBOs);
	glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
}

void CreateIndexData()
{
	// Indices of the vertices of the cube.
	// This is used to avoid duplicates. As the faces of the cube share vertices we can duplicate this vertices ending with a
	// total of 36 (6 faces x 2 triangles per face x 3 vertices each triangle = 36) or we can store them just once and refer to them
	// using these indices and drawing the cube with glDrawElements instead than with glDrawArray that just iterates through an array
	// drawing every vertices it finds in it.
	GLushort cube_elements[] = {
		// front
		0, 1, 2,
		2, 3, 0,
		// top
		3, 2, 6,
		6, 7, 3,
		// back
		7, 6, 5,
		5, 4, 7,
		// bottom
		4, 5, 1,
		1, 0, 4,
		// left
		4, 0, 3,
		3, 7, 4,
		// right
		1, 5, 6,
		6, 2, 1,
	};

	// Generate the Index Buffer Object to index the vertices inside the Vertex Buffer Object.
	// The indexes are related to the 8 vertices making the cube.
	glGenBuffers(1, IBOs);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOs[0]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
}

void Init()
{
	glEnable(GL_DEPTH_TEST);
	ModelViewProjectionMatrix();

	CreateVerticesData();
	CreateIndexData();

	// Load and use the shaders
	VERTEX_SHADER_ID = LoadShader("shader.vert", "shader.frag");
	glUseProgram(VERTEX_SHADER_ID);

	// Set up the value of the uniform value inside the vertex shader
	GLuint mvp_Loc = glGetUniformLocation(VERTEX_SHADER_ID, "mvp");
	glUniformMatrix4fv(mvp_Loc, 1, GL_FALSE, glm::value_ptr(mvp));

	// Defines parameters to pass to the shaders
	glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
	glEnableVertexAttribArray(vPosition);
}

#pragma endregion

void Display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	int size;
	glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOs[0]);
	glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

	// marks the current window as needing to be redisplayed
    glutPostRedisplay();
	glFlush();
}

int _tmain(int argc, _TCHAR* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA);
	glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	glutInitContextVersion(3, 3);
	glutInitContextProfile(GLUT_CORE_PROFILE);
	glutCreateWindow("Cube");

	if( glewInit() )
	{
		std::cerr << "Error...exiting";
		exit(EXIT_FAILURE);
	}

	Init();

	glutDisplayFunc(Display);
	glutMainLoop();

	return 0;
}
-Ivan- è offline   Rispondi citando il messaggio o parte di esso
Old 13-12-2013, 15:24   #2
-Ivan-
Senior Member
 
L'Avatar di -Ivan-
 
Iscritto dal: Mar 2003
Città: Rimini
Messaggi: 1846
Ho trovato.
L'errore era in:
Codice:
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
che invece deve essere:

Codice:
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
Stavo ignorando l'asse Z. Purtroppo avevo copiato quella riga da un altro esercizio in cui disegnavo un triangolo e quindi avevo coordinate su due dimensioni.
-Ivan- è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Marvel's Wolverine, la recensione: Logan torna protagonista in un'avventura brutale e intensa Marvel's Wolverine, la recensione: Logan torna p...
DJI Romo 2: tante novità lo rendono un robot completo DJI Romo 2: tante novità lo rendono un ro...
Sony Bravia 9 II: il True RGB alla prova, dove l'LCD sfida l'OLED Sony Bravia 9 II: il True RGB alla prova, dove l...
Geely EX5, un mese al volante: il SUV elettrico cinese che ci ha sorpreso (quasi) senza riserve Geely EX5, un mese al volante: il SUV elettrico ...
Mova Z70 Ultra Roller Complete: motore potente, rullo di lavaggio e l'IA a guidare Mova Z70 Ultra Roller Complete: motore potente, ...
La Serie A con DAZN e Amazon Prime con l...
Giochi Ubisoft su Steam senza Ubisoft Co...
Miami Beach ha autorizzato la maxi opera...
Apple regala un altro anno di funzioni s...
Alla fine è successo davvero: Vol...
Il meglio di Amazon del weekend in uno s...
Speciale TV in offerta su Amazon: Hisens...
Non c'è pace per Trezor: 347.000 e-mail ...
È un portatile Dell e li vale tut...
Apple iPhone 17 Pro Max 256GB a 1.195€ (...
GPT-6 Astra è davvero AGI o non s...
LG OLED G6S 48'' a 845€ e G6 55'' a 1368...
Mantax Otax: il malware Android che crip...
Musk incassa un altro maxi contratto IA:...
Le vendite di EV sono esplose in tutto i...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 23:01.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v