glfw显示点云

一个简单的glfw显示点云的例程

一个简单的glfw显示点云的例程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <functional>
#include <memory>
#include <iostream>
#include <string>
#include <vector>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include "src/glslUtility.hpp"


#ifndef PI
#define PI 3.141592654
#endif

using namespace std;
//要实现设置显示点的数量
static int N=100000;
static int N2;
GLFWwindow *window = nullptr;

// For camera controls
static bool leftMousePressed = false;
static bool rightMousePressed = false;
static bool middleMousePressed = false;
GLuint positionLocation = 0; // Match results from glslUtility::createProgram.
GLuint velocitiesLocation = 1; // Also see attribtueLocations below.

GLuint pointVAO = 0;//所有需要画的粒子都绑定在上面
GLuint pointVBO_positions = 0;
GLuint pointVBO_velocities = 0;
GLuint pointIBO = 0;//indexs
GLuint displayImage;
GLuint program[2];
const unsigned int PROG_POINT = 0;//

const float fovy = (float)(PI / 4);
const float zNear = 0.10f;
const float zFar = 10.0f;
//窗口相关
std::string windowName = std::string("GLvisualizationWin");
int width = 1280;
int height = 720;
int pointSize = 2;
bool BreakLoop = false;
double lastX;
double lastY;
float theta = 1.22f;
float phi = -0.70f;
float zoom = 4.0f;
glm::vec3 lookAt = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraPosition;
glm::mat4 projection;

//外部输入数据
int numObjects_fixed = 1000;
int numObjects_rotated = 1000;
int blockSize = 128;

//被init()调用
void errorCallback(int error, const char *description);
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);//键盘响应
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);//鼠标按键响应
void mousePositionCallback(GLFWwindow* window, double xpos, double ypos);//鼠标位置响应
void updateCamera();
void initShaders(GLuint *program);
void endrun();

void initVAO()
{
std::unique_ptr<GLfloat[]> bodies{ new GLfloat[4 * N] };//数据
std::unique_ptr<GLfloat[]> rgbs{ new GLfloat[4 * N] };//数据
std::unique_ptr<GLuint[]> bindices{ new GLuint[N] };//索引
glm::vec4 ul(-1.0, -1.0, 1.0, 1.0);
glm::vec4 lr(1.0, 1.0, 0.0, 0.0);
for (int i = 0; i < N; i++) {
float x = float(rand()) / 9.99f;
x = x - (int)x;
float y = float(rand()) / 9.99f;
y = y - (int)y;
float z = float(rand()) / 9.99f;
z = z - (int)z;

bodies[4 * i + 0] = x;
bodies[4 * i + 1] = y;
bodies[4 * i + 2] = z;
bodies[4 * i + 3] = 1.0f;
rgbs[4 * i + 0] = 1;
rgbs[4 * i + 1] = 0;
rgbs[4 * i + 2] = 0;
rgbs[4 * i + 3] = 1.0f;
bindices[i] = i;
}
//创建VAO 把所有需要画粒子的东西都粘在上面
glGenVertexArrays(1, &pointVAO); // Attach everything needed to draw a particle to this
glGenBuffers(1, &pointVBO_positions);//该函数用来生成缓冲区对象的名称,第一个参数是要生成的缓冲区对象的数量,第二个是要用来存储缓冲对象名称的数组
glGenBuffers(1, &pointVBO_velocities);
glGenBuffers(1, &pointIBO);//生成索引缓存区的名字
glBindVertexArray(pointVAO);//绑定一个顶点数组对象

// Bind the positions array to the pointVAO by way of the pointVBO_positions
glBindBuffer(GL_ARRAY_BUFFER, pointVBO_positions); // bind the buffer pointVBO_positions变成了一个顶点缓冲类型
glBufferData(GL_ARRAY_BUFFER, 4 * N * sizeof(GLfloat), bodies.get(), GL_DYNAMIC_DRAW); // transfer data,创建和初始化一个buffer object的数据存储。
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer((GLuint)positionLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);

// Bind the velocities array to the pointVAO by way of the pointVBO_velocities
glBindBuffer(GL_ARRAY_BUFFER, pointVBO_velocities);
glBufferData(GL_ARRAY_BUFFER, 4 * N * sizeof(GLfloat), rgbs.get(), GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(velocitiesLocation);
glVertexAttribPointer((GLuint)velocitiesLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);

//给索引数据也绑定buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pointIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, N * sizeof(GLuint), bindices.get(), GL_STATIC_DRAW);
glBindVertexArray(0);
}

void testGLFW()
{
//初始化前设置错误回调函数
glfwSetErrorCallback(errorCallback);
//调用glfwInit函数,初始化glfw,在程序终止之前,必须终止glfw,这个函数只能在主线程上被调用
if (!glfwInit()) {
std::cout
<< "Error: Could not initialize GLFW!"
<< " Perhaps OpenGL 3.3 isn't available?"
<< std::endl;
return ;
}
//在创建窗口之前调用glfwWindowHint,设置一些窗口的信息
//这些hints,设置以后将会保持不变,只能由glfwWindowHint、glfwDefaultWindowHints或者glfwTerminate修改。
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//如果创建window失败则终结glfw,指定尺寸
//(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwidnow* share);
window = glfwCreateWindow(width, height, windowName.c_str(), NULL, NULL);
if (!window) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();//glfwTerminate会销毁窗口释放资源,因此在调用该函数后,如果想使用glfw库函数,就必须重新初始化
return ;
}
//
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyCallback);
glfwSetCursorPosCallback(window, mousePositionCallback);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
return ;
}
// 初始化绘图状态
initVAO();
//互操作
//cudaGLSetGLDevice(0);//设置CUDA环境
////用cuda登记缓冲区,该命令告诉OpenGL和CUDA 驱动程序该缓冲区为二者共同使用。
//cudaGLRegisterBufferObject(pointVBO_positions);//pointVBO_positions=0
//cudaGLRegisterBufferObject(pointVBO_velocities);//pointVBO_velocities=0

updateCamera();
initShaders(program);
glEnable(GL_DEPTH_TEST);

double fps = 0;
double timebase = 0;
int frame = 0;
//返回指定窗口是否关闭的flag变量,可以在任何线程中被调用。
while (!glfwWindowShouldClose(window) && !BreakLoop)
{
//这个函数主要用来处理已经在事件队列中的事件,通常处理窗口的回调事件,包括输入,窗口的移动,窗口大小的改变等,
//回调函数可以自己手动设置,比如之前所写的设置窗口大小的回调函数;如果没有该函数,则不会调用回调函数,同时也不会接收用户输入,例如接下来介绍的按键交互就不会被响应;
glfwPollEvents();
frame++;
double time = glfwGetTime();//当前时间
if (time - timebase > 1.0) {
fps = frame / (time - timebase);
timebase = time;
frame = 0;
}
//runcuda();//更新点云
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(program[PROG_POINT]);
glBindVertexArray(pointVAO);
glPointSize((GLfloat)pointSize);
glDrawElements(GL_POINTS, N + N2 + 1, GL_UNSIGNED_INT, 0);
glPointSize(1.0f);
glUseProgram(0);
glBindVertexArray(0);
glfwSwapBuffers(window);//opengl采用双缓冲机制,该函数用于交换前后颜色缓冲区的内容
}
endrun();
return ;

}

void endrun()
{
glfwDestroyWindow(window);
glfwTerminate();

}
void initShaders(GLuint *program)
{
GLint location;
const char *vertexShaderPath = "shaders/boid.vert.glsl";
const char *geometryShaderPath = "shaders/boid.geom.glsl";
const char *fragmentShaderPath = "shaders/boid.frag.glsl";
const char *attributeLocations[] = { "Position", "Velocity" };
program[PROG_POINT] = glslUtility::createProgram(vertexShaderPath, geometryShaderPath, fragmentShaderPath, attributeLocations, GLuint(2));

glUseProgram(program[PROG_POINT]);
if ((location = glGetUniformLocation(program[PROG_POINT], "u_projMatrix")) != -1) {
glUniformMatrix4fv(location, 1, GL_FALSE, &projection[0][0]);
}
if ((location = glGetUniformLocation(program[PROG_POINT], "u_cameraPos")) != -1) {
glUniform3fv(location, 1, &cameraPosition[0]);
}
}
void errorCallback(int error, const char *description)
{
fprintf(stderr, "error %d: %s\n", error, description);
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}


void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
leftMousePressed = (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS);
rightMousePressed = (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS);
middleMousePressed = (button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS);
}

void mousePositionCallback(GLFWwindow* window, double xpos, double ypos)
{
if (leftMousePressed) {
// compute new camera parameters
phi += (xpos - lastX) / width;
theta -= (ypos - lastY) / height;
theta = std::fmax(0.01f, std::fmin(theta, 3.14f));
updateCamera();
}
else if (rightMousePressed) {
zoom += (ypos - lastY) / height;
zoom = std::fmax(0.1f, std::fmin(zoom, 5.0f));
updateCamera();
}
else if (middleMousePressed){
glm::vec3 forward = -glm::normalize(cameraPosition);
forward.y = 0.0f;
forward = glm::normalize(forward);
glm::vec3 right = glm::cross(forward, glm::vec3(0, 1, 0));
right.y = 0.0f;
right = glm::normalize(right);

lookAt -= (float)(xpos - lastX) * right * 0.01f;
lookAt += (float)(ypos - lastY) * forward * 0.01f;
updateCamera();
}

lastX = xpos;
lastY = ypos;
}

void updateCamera()
{
cameraPosition.x = zoom * sin(phi) * sin(theta);
cameraPosition.z = zoom * cos(theta);
cameraPosition.y = zoom * cos(phi) * sin(theta);
cameraPosition += lookAt;
cout << lookAt.x << ", " << lookAt.y << ", " << lookAt.z << "," << endl;
projection = glm::perspective(fovy, float(width) / float(height), zNear, zFar);
glm::mat4 view = glm::lookAt(cameraPosition, lookAt, glm::vec3(0, 0, 1));
projection = projection * view;
GLint location;
glUseProgram(program[PROG_POINT]);
if ((location = glGetUniformLocation(program[PROG_POINT], "u_projMatrix")) != -1) {
glUniformMatrix4fv(location, 1, GL_FALSE, &projection[0][0]);
}
}
# OpenGL

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×