Modern OpenGL---做一些好玩的东西03 动态绘制点云模型

这次增加了一些键盘控制功能,通过 wasd 来控制 上下左右四个方向,通过x键来进行缩小,z键进行放大。大致思路就是通过注册一个键盘相应函数,来控制坐标和缩放因子,放大缩小只需要对坐标乘上缩放因子,上下左右平移只需要对坐标进行加减操作即可。然后通过uniform来将这些全局变量传入到顶点着色器中即可。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

static void ParsePLY(const std::string filename, std::vector<float> &positions, std::vector<unsigned int> &indices)
{
	std::ifstream fin(filename);
	std::string str = "";
	int pointNum;
	while (str != "vertex")
	{
		fin >> str;
	}
	fin >> pointNum;

	while (str != "end_header")
	{
		fin >> str;
	}
	int cnt = 0;
	while (cnt < pointNum)
	{
		for (int i = 0; i < 3; i++)
		{
			float pos;
			fin >> pos;
			positions.emplace_back(pos);
		}
		
		cnt++;
	}

	while (!fin.eof())
	{
		unsigned int idx;
		fin >> idx;   
		for (int i = 0; i < 3; i++)
		{
			fin >> idx;
			indices.emplace_back(idx);
		}
	}
}

float r = 3.0f;
float x = 0.0f, y = 0.0f, z = 0.0f;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
	switch (key)
	{
	case GLFW_KEY_Z:
		r += 0.1f;
		break;
	case GLFW_KEY_X:
		r -= 0.1f;
		break;
	case GLFW_KEY_A:
		x -= 0.01f;
		break;
	case GLFW_KEY_S:
		y -= 0.01f;
		break;
	case GLFW_KEY_D:
		x += 0.01f;
		break;
	case GLFW_KEY_W:
		y += 0.01f;
		break;
	default:
		break;
	}
}

int main()
{
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	//glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

	GLFWwindow* window = glfwCreateWindow(1000, 1000, "draw mesh", nullptr, nullptr);
	glfwMakeContextCurrent(window);
	glfwSetKeyCallback(window, key_callback);
	glewInit();

	std::cout << glGetString(GL_VERSION) << std::endl;

	std::vector<float> vertices;
	std::vector<unsigned int> indices;
	ParsePLY("./data/bunny.ply", vertices, indices);

	unsigned int vao;
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	unsigned int vbo;
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER,vbo);
	glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW);

	unsigned int ibo;
	glGenBuffers(1, &ibo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);

	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);

	const char* vertexShaderSource = "#version 330 core\n"
		"layout(location = 0) in vec4 pos;\n"
		"uniform float r;\n"
		"uniform vec3 bias_pos;\n"
		"void main()\n"
		"{\n"
		"	gl_Position = vec4(pos.x * r + r / 20 + bias_pos.x, pos.y * r - r / 10 + bias_pos.y, pos.z * r + bias_pos.z, pos.w);\n"
		"}\0";
	unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vs, 1, &vertexShaderSource, nullptr);
	glCompileShader(vs);
	int success1;
	glGetShaderiv(vs, GL_COMPILE_STATUS, &success1);
	if (success1 == 0)
	{
		char message[512];
		glGetShaderInfoLog(vs, 512, nullptr, message);
		std::cout << message << std::endl;
	}

	const char* fragmentShaderSource = "#version 330 core\n"
		"out vec4 color;\n"
		"void main()\n"
		"{\n"
		"	color = vec4(0.5f, 0.8f, 0.2f, 1.0f);\n"
		"}\0";
	unsigned int fs;
	fs = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fs, 1, &fragmentShaderSource, nullptr);
	glCompileShader(fs);
	int success;
	glGetShaderiv(fs, GL_COMPILE_STATUS, &success);
	if (success == 0)
	{
		char message[512];
		glGetShaderInfoLog(fs, 512, nullptr, message);
		std::cout << message << std::endl;
	}

	unsigned int program;
	program = glCreateProgram();
	glAttachShader(program, vs);
	glAttachShader(program, fs);
	glLinkProgram(program);
	glDeleteShader(vs);
	glDeleteShader(fs);

	glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);

	int location_z = glGetUniformLocation(program, "r");
	int location_bias = glGetUniformLocation(program, "bias_pos");
	int count = 0;
	while (!glfwWindowShouldClose(window))
	{
		if (count < indices.size()) count += 30;
		glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		glBindVertexArray(vao);
		glUseProgram(program);
		glUniform1f(location_z, r);
		glUniform3f(location_bias, x, y, z);
		glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr);

		glfwSwapBuffers(window);
		glfwPollEvents();

	}
	glDeleteProgram(program);
	glfwTerminate();

	return 0;
}

效果:

你可能感兴趣的:(计算机图形学,c++,opengl)