Advertisement

Confusing results from glDrawElements

Started by June 16, 2020 01:21 PM
4 comments, last by wintertime 4 years, 5 months ago

I'm relearning OpenGL in its modern form using https://learnopengl.com

Inspired by the coordinate systems lesson https://learnopengl.com/Getting-started/Coordinate-Systems I started modifying their sample code to try and create a glorified cuboctahedron. Here's my attempt. Everything not shown here is, to the best of my knowledge, the way they provided it. My code is intended to describe six faces of a cube, either connected or exploded depending on the relative values of a and b. However, it draws nonsense, connecting things in a way that makes no sense. Here is what I see using only four sets of indices:

Seems to me, the second pair of triplets should refer to the second face of the cube, and should produce two triangles that are at the very least coplanar! How did they come to be folded in the middle?? The first pair of triangles produce a distorted trapezoid instead of outlining the corners of a square, which equally surprises me.

Someone please enlighten me, why I got this instead!

In my main program:

// these govern the shape of the expanded cube solid
// if equal, it becomes a cube
	float a = 0.4;
	float b = 0.4;

float cuboctavertices[] = {
		a, a, b,	//0
		a, -a, b,
		-a, -a, b,
		-a, a, b,

		b, a, a,	//4
		b, a, -a,
		b, -a, -a,
		b, -a, a,
	
		a, a, -b,	//8
		a, -a, -b,
		-a, -a, -b,
		-a, a, -b,

		-b, a, a,	//12
		-b, a, -a,
		-b, -a, -a,
		-b, -a, a,

		a, b, a,	//16
		a, b, -a,
		-a, b, -a,
		-a, b, a,

		a, -b, a,
		a, -b, -a,
		-a, -b, -a,
		-a, b, a
	};

unsigned int cuboctaindices[] = {
		0, 1, 2, // first triangle
		1, 2, 3,  // second triangle
		4, 5, 6,
		5, 6, 7,
	};

	unsigned int VBO, VAO, EBO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);
	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(cuboctavertices), cuboctavertices, GL_STATIC_DRAW);
	//glBufferData(GL_ARRAY_BUFFER, sizeof(cubices), cubices, GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cuboctaindices), cuboctaindices, GL_STATIC_DRAW);

In my render loop:

    // render container
    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);

Wouldn't you need texture coordinates somewhere?

Also, the final 4 coordinates can't be right for a cube, every other 4 coordinates has one column that doesn't change, since a cube face is parallel to an axis, but the 6th 4 coordinates don't have that.

Advertisement

@Alberth I know that a lack of texture coordinates makes the texture form streaky lines and such, but I was going to worry about that later. The basic SHAPE wouldn't be affected, would it?

You're right, the last line should be -a, -b, a. But my indices weren't even referencing that.

Maybe draw fewer elements and/or swap entries, so you can check each triangle/rectangle to be correct individually? At least 12 triangles seems too difficult to understand, less triangles should in theory be simpler :p

I don't understand how you specified texture coordinates but that could be handled in the shader with positive/negative?

I'd guess, you screwed up the winding order. You could try disabling culling.

This topic is closed to new replies.

Advertisement