OpenGL adds unwanted vertex at position (0, 0) when drawing triangle strips

I am using cocos2d-x v4. I can not tell what I have done wrong but I get that weird triangle shape when trying to use TRIANGLE_STRIP.

#include "GameLayer.h"

bool GameLayer::init()
{
	if (!Scene::init())
	{
		return false;
	}

	visibleSize = Director::getInstance()->getWinSize(); // w = 512, h = 512
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	this->onDrawStuffs();

	return true;
}

void GameLayer::onDrawStuffs()
{
	const char* vertexShaderSource = "#version 330 core\n"
		"layout (location = 0) in vec2 aPos;\n"
		"layout (location = 1) in vec3 aColor;\n"
		"out vec3 ourColor;\n"
		"uniform mat4 u_MVPMatrix;\n"
		"void main()\n"
		"{\n"
		"   gl_Position = u_MVPMatrix * vec4(aPos, 0.0, 1.0);\n"
		"   ourColor = aColor;\n"
		"}\0";

	const char* fragmentShaderSource = "#version 330 core\n"
		"out vec4 FragColor;\n"
		"in vec3 ourColor;\n"
		"void main()\n"
		"{\n"
		"	FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
		"}\0";

	auto program = cocos2d::backend::Device::getInstance()->newProgram(vertexShaderSource, fragmentShaderSource);

	
	auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
	_programState = new (std::nothrow) backend::ProgramState(program);
	pipelineDescriptor.programState = _programState;

	auto& vertexLayout = pipelineDescriptor.programState->getVertexLayout();
	const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
	auto iter = attributeInfo.find("aPos");
	if (iter != attributeInfo.end())
	{
		vertexLayout->setAttribute("aPos",
			iter->second.location,
			backend::VertexFormat::FLOAT2,
			0,
			false);
	}


	vertexLayout->setLayout(2 * sizeof(float));

	_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");


	float vertices[] = {
		10, 50,
		50, 150,
		250, 250
	};
	
	m_vertexDataCount = sizeof(vertices) / 2 * sizeof(float);
	_customCommand.createVertexBuffer(sizeof(vertices), m_vertexDataCount, CustomCommand::BufferUsage::DYNAMIC);

	_customCommand.updateVertexBuffer(vertices, sizeof(vertices));

	
	const auto& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
	_customCommand.getPipelineDescriptor().programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
	
	_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
	_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
}

void GameLayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
{
	_customCommand.init(0);
	_customCommand.setVertexDrawInfo(0, m_vertexDataCount);
	renderer->addCommand(&_customCommand);
}

Sure. Let me ask engineering.

May be the issue that your varriable
m_vertexDataCount = sizeof(vertices) / 2 * sizeof(float);
is 24 / 2 * 4 == 48, but should be 3 since you try to draw

float vertices[] = {
		10, 50,
		50, 150,
		250, 250
	};

and
_customCommand.createVertexBuffer(sizeof(vertices), m_vertexDataCount, CustomCommand::BufferUsage::DYNAMIC);

should accept the following parameters if I properly understand: sizeof(one_vertex), vertex count, buffer type, and you should pass
8, 3, CustomCommand::BufferUsage::DYNAMIC

2 Likes

I guess my m_vertexDataCount calculation was wrong thanks. Now it is accurate. Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.