c_cpp 着色器 - Vertexbuffer无法正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 着色器 - Vertexbuffer无法正常工作相关的知识,希望对你有一定的参考价值。

bool Renderer::initialize()
{

	initializePrimitives();
	_texture2DShader.loadFromString(GL_VERTEX_SHADER,
		"#version 150\n"
		"in vec2 position;\n"
		"in vec3 color;\n"
		"in vec2 texcoord;\n"
		"out vec2 TextureCoordinate;\n"
		"out vec3 Color;\n"
		"uniform mat4 model;\n"
		"uniform mat4 view;\n"
		"uniform mat4 projection;\n"
		"void main(){\n"
		"TextureCoordinate = texcoord;\n"
		"Color = color;\n"
		"\n"
		"\n"
		"\n"
		"gl_Position = projection * view * model * vec4(position, 0.0, 1.0);}\n"
		"\n");

	_texture2DShader.loadFromString(GL_FRAGMENT_SHADER,
		"#version 150\n"
		"in vec3 Color;\n"
		"in vec2 TextureCoordinate;\n"
		"out vec4 outColor;\n"
		"uniform sampler2D tex;\n"
		"void main(){\n"
		"outColor = texture(tex, TextureCoordinate);\n}"
		"\n"
		"\n"
		"\n"
		"\n");

	_texture2DShader.createAndLink();

	_texture2DShader.findAttributeLocation("position");
	_texture2DShader.findAttributeLocation("color");
	_texture2DShader.findAttributeLocation("texcoord");

	_texture2DShader.setAttribute("position", 2, GL_FLOAT, GL_FALSE, sizeof(float) * 7, nullptr);
	_texture2DShader.setAttribute("color", 3, GL_FLOAT, GL_FALSE, sizeof(float) * 7, reinterpret_cast<void*>(sizeof(float) * 2));
	_texture2DShader.setAttribute("texcoord", 2, GL_FLOAT, GL_FALSE, sizeof(float) * 7, reinterpret_cast<void*>(sizeof(float) * 5));

	_texture2DShader.findUniformLocation("model");
	_texture2DShader.findUniformLocation("view");
	_texture2DShader.findUniformLocation("projection");
	

	setupOpenGL();

	return true;
}

void Renderer::initializePrimitives()
{
	float planeVertices[] = {
		//  Position      Color             Texcoords
		0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Top-left
		1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // Top-right
		1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // Bottom-right
		0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f  // Bottom-left
	};

	GLuint planceIndices[] = {
		0, 1, 2,
		2,3,0
	};

	_planeVertexArray = new Rainbow::Engine::Core::Graphics::VertexArray();
	_planeVertexArray->bind();

	_planeVertexBuffer = new Rainbow::Engine::Core::Graphics::DeviceBuffer(GL_ARRAY_BUFFER);
	_planeVertexBuffer->bind();
	_planeVertexBuffer->setData(sizeof(planeVertices), planeVertices);

	_planeIndexBuffer = new Rainbow::Engine::Core::Graphics::DeviceBuffer(GL_ELEMENT_ARRAY_BUFFER);
	_planeIndexBuffer->bind();
	_planeIndexBuffer->setData(sizeof(planceIndices), planceIndices);
	
	_planeVertexArray->unbind();
	_planeIndexBuffer->unbind();
	_planeVertexBuffer->unbind();
}

以上是关于c_cpp 着色器 - Vertexbuffer无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

从顶点着色器中修改着色器存储缓冲区对象

c_cpp 一个加载着色器文件,编译和链接到着色器程序的函数。

c_cpp 加载GLSL着色器

带有片段着色器的OpenGL 3.3不同颜色

c_cpp openFrameworks内联着色器模板(仅限vert和frag)

OpenGL ES画板