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

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

代码:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

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;
}

效果:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值