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;
static bool leftMousePressed = false; static bool rightMousePressed = false; static bool middleMousePressed = false; GLuint positionLocation = 0; GLuint velocitiesLocation = 1;
GLuint pointVAO = 0; GLuint pointVBO_positions = 0; GLuint pointVBO_velocities = 0; GLuint pointIBO = 0; 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;
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; } glGenVertexArrays(1, &pointVAO); glGenBuffers(1, &pointVBO_positions); glGenBuffers(1, &pointVBO_velocities); glGenBuffers(1, &pointIBO); glBindVertexArray(pointVAO);
glBindBuffer(GL_ARRAY_BUFFER, pointVBO_positions); glBufferData(GL_ARRAY_BUFFER, 4 * N * sizeof(GLfloat), bodies.get(), GL_DYNAMIC_DRAW); glEnableVertexAttribArray(positionLocation); glVertexAttribPointer((GLuint)positionLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
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);
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); if (!glfwInit()) { std::cout << "Error: Could not initialize GLFW!" << " Perhaps OpenGL 3.3 isn't available?" << std::endl; return ; } 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 = glfwCreateWindow(width, height, windowName.c_str(), NULL, NULL); if (!window) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); 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();
updateCamera(); initShaders(program); glEnable(GL_DEPTH_TEST);
double fps = 0; double timebase = 0; int frame = 0; while (!glfwWindowShouldClose(window) && !BreakLoop) { glfwPollEvents(); frame++; double time = glfwGetTime(); if (time - timebase > 1.0) { fps = frame / (time - timebase); timebase = time; frame = 0; } 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); } 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) { 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]); } }
|