求助,学shader时写了个立方体,但是转起来怎么怪怪的

先上图:

cocos2dx版本是3.13.1,仿照网上教程写了个立方体,但是转起来的的时候总是缺一两个面,shader用的是cocos2dx自带的GLProgram::SHADER_NAME_POSITION_TEXTURE。
求各位老司机解惑,谢谢。

主要代码如下:

void CubeTexture::visit(cocos2d::Renderer *renderer, const Mat4 &transform, uint32_t parentFlags)
{
	Layer::visit(renderer, transform, parentFlags);
	_command.init(_globalZOrder);
	_command.func = CC_CALLBACK_0(CubeTexture::onDraw, this);
	Director::getInstance()->getRenderer()->addCommand(&_command);
}

void CubeTexture::onDraw()
{
	Director::getInstance()->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
	Director::getInstance()->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
	Director::getInstance()->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
	Director::getInstance()->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);

	Mat4 projectionMatrix;
	auto size = Director::getInstance()->getWinSize();
	Mat4::createPerspective(60, size.width / size.height, 1.0, 50, &projectionMatrix);
	Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, projectionMatrix);

	Mat4 modelViewMatrix;
	Mat4::createLookAt(Vec3(0, 0, 1), Vec3(0, 0, 0), Vec3(0, 1, 0), &modelViewMatrix);
	modelViewMatrix.translate(0, 0, -5);

	static float rotation = 0;
	modelViewMatrix.rotate(Vec3(1, 1, 1), CC_DEGREES_TO_RADIANS(rotation));
	rotation++;
	if (rotation > 360) {
		rotation = 0;
	}
	Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, modelViewMatrix);

	auto glProgram = getGLProgram();//GLProgram::SHADER_NAME_POSITION_TEXTURE
	glProgram->use();
	glProgram->setUniformsForBuiltins();

	typedef struct {
		float Position[3];
		float TexCoord[2];
	} Vertex;

	Vertex data[] = {
		// Front
		{ { 1, -1, 0 }, { 1, 0 } },
		{ { 1, 1, 0 }, { 1, 1 } },
		{ { -1, 1, 0 }, { 0, 1 } },
		{ { -1, -1, 0 }, { 0, 0 } },
		// Back
		{ { 1, 1, -2 }, { 1, 0 } },
		{ { -1, -1, -2 }, { 0, 1 } },
		{ { 1, -1, -2 }, { 1, 1 } },
		{ { -1, 1, -2 }, { 0, 0 } },
		// Left
		{ { -1, -1, 0 }, { 1, 0 } },
		{ { -1, 1, 0 }, { 1, 1 } },
		{ { -1, 1, -2 }, { 0, 1 } },
		{ { -1, -1, -2 }, { 0, 0 } },
		// Right
		{ { 1, -1, -2 }, { 1, 0 } },
		{ { 1, 1, -2 }, { 1, 1 } },
		{ { 1, 1, 0 }, { 0, 1 } },
		{ { 1, -1, 0 }, { 0, 0 } },
		// Top
		{ { 1, 1, 0 }, { 1, 0 } },
		{ { 1, 1, -2 }, { 1, 1 } },
		{ { -1, 1, -2 }, { 0, 1 } },
		{ { -1, 1, 0 }, { 0, 0 } },
		// Bottom
		{ { 1, -1, -2 }, { 1, 0 } },
		{ { 1, -1, 0 }, { 1, 1 } },
		{ { -1, -1, 0 }, { 0, 1 } },
		{ { -1, -1, -2 }, { 0, 0 } }
	};

	//set data
	GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

	GLubyte indices[] = {
		// Front
		0, 1, 2,
		2, 3, 0,
		// Back
		4, 5, 6,
		4, 5, 7,
		// Left
		8, 9, 10,
		10, 11, 8,
		// Right
		12, 13, 14,
		14, 15, 12,
		// Top
		16, 17, 18,
		18, 19, 16,
		// Bottom
		20, 21, 22,
		22, 23, 20
	};

	//set indices
	GLuint indexVBO;
	glGenBuffers(1, &indexVBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	GLuint positionLocation = glGetAttribLocation(glProgram->getProgram(), "a_position");
	glEnableVertexAttribArray(positionLocation);
	glVertexAttribPointer(positionLocation,
		3,
		GL_FLOAT,
		GL_FALSE,
		sizeof(Vertex),
		(GLvoid*)offsetof(Vertex, Position));

	GLuint TexCoordLocation = glGetAttribLocation(glProgram->getProgram(), "a_texCoord");
	glEnableVertexAttribArray(TexCoordLocation);
	glVertexAttribPointer(TexCoordLocation,
		2,
		GL_FLOAT,
		GL_FALSE,
		sizeof(Vertex),
		(GLvoid*)offsetof(Vertex, TexCoord));

	GLuint textureId;
	textureId = Director::getInstance()->getTextureCache()->addImage("1.png")->getName();
	GL::bindTexture2D(textureId);

	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);

	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, (GLvoid*)0);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	glBindVertexArray(0);
	CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 36);

	CHECK_GL_ERROR_DEBUG();

	Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
	Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}

好像是index错了,现在后面的index(4,5,6)是顺时针转的,改成逆时针的4,6,5
其他面我就没检查了。

感觉在画GL_TRIANGLES的时候,index顺序没影响吧,每次只是画了个独立的三角形而已,不像GL_TRIANGLE_STRIP这种对顺序有要求的。

逆时针是正面,顺时针是反面。你这个立方体肯定要对反面进行裁切的吧,这样有的三角形就看不到了。
不知道看起来怪是不是这个导致的,但是三角形的旋转方向 这个肯定是要注意的。