Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Peugeot Polygon Concept: ecco il futuro delle utilitarie
Peugeot Polygon Concept: ecco il futuro delle utilitarie
Polygon è la concept car di Peugeot che mostra il futuro delle soluzioni del segmento B: tra design compatti e innovativi affiancati da dimensioni compatte uno scherzo dalla manovrabilità incredibile per le manovre a bassa velocità
Reno16 Pro: il compatto di OPPO punta su fotocamera da 200MP e il nuovo Bubble! La recensione
Reno16 Pro: il compatto di OPPO punta su fotocamera da 200MP e il nuovo Bubble! La recensione
OPPO ha portato in Italia, dal 1° luglio 2026, Reno16 Pro: display AMOLED da 6,32 pollici a 144Hz, tripla fotocamera con sensore principale da 200 megapixel, chip Dimensity 8550 Super e batteria da 6000mAh, al prezzo di lancio di 899 euro. Lo abbiamo provato per due settimane insieme al nuovo accessorio Bubble, per capire se la formula compatta della serie regge ancora di fronte a un listino da 1099 euro
 Hisense 55U7SE: tuttofare e accessibile, il MiniLED per film, sport e gioco
Hisense 55U7SE: tuttofare e accessibile, il MiniLED per film, sport e gioco
MiniLED di fascia media con local dimming a 192 zone, 144 Hz nativi e audio firmato Devialet. La prova strumentale riscontra colori affidabili e gaming reattivo, per un prodotto molto accessibile e convincente. Ma la soundbar aggiuntiva è quasi d'obbligo
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


Peugeot Polygon Concept: ecco il futuro delle utilitarie Peugeot Polygon Concept: ecco il futuro delle ut...
Reno16 Pro: il compatto di OPPO punta su fotocamera da 200MP e il nuovo Bubble! La recensione Reno16 Pro: il compatto di OPPO punta su fotocam...
 Hisense 55U7SE: tuttofare e accessibile, il MiniLED per film, sport e gioco Hisense 55U7SE: tuttofare e accessibile, il Min...
Kindle Scribe Colorsoft: riduce le cornici e diventa a colori, ma il prezzo è alto Kindle Scribe Colorsoft: riduce le cornici e div...
L'IA cambia tutte le regole della sicurezza tra vulnerabilità e sorveglianza. Intervista al CEO di Proofpoint L'IA cambia tutte le regole della sicurezza tra ...
PamStealer, il malware per Mac che prima...
NAVEE EXO S Pro, il robot esoscheletro p...
Samsung Galaxy A57 5G a 399€ con 256 GB:...
Volevano collegare delle aragoste vive a...
La crisi dei PC è peggiore del pr...
Alibaba pronta a vietare Claude Code ai ...
Sovranità sui dati: Cloud Firewal...
FiberCop porterà la fibra Gigabit...
Data center in Lombardia: 20 progetti sc...
Tutti i modi in cui la scommessa di Orac...
Kioxia e SanDisk sbandierano i numeri de...
iPhone 18 Pro potrebbe usare modem Qualc...
Basta 'AI slop': Godot vieta ufficialmen...
Un annuncio sponsorizzato su X diffonde ...
Data center in Italia: l’IA spinge la de...
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: 17:03.


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