Skip to content

Instantly share code, notes, and snippets.

@DevHwan
Last active August 9, 2018 15:41
Show Gist options
  • Save DevHwan/e84828ad743e581ae63903564e913181 to your computer and use it in GitHub Desktop.
Save DevHwan/e84828ad743e581ae63903564e913181 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <cmath>
#include <GLFW/glfw3.h>
#include <OpenGL/GL.h>
#include <OpenGL/GLU.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
struct alignas(4) Vertex {
float fX = 0.0f;
float fY = 0.0f;
float fZ = 0.0f;
float fR = 0.0f;
float fG = 0.0f;
float fB = 0.0f;
float fA = 1.0f;
Vertex() = default;
Vertex(const float x, const float y, const float z) : fX(x), fY(y), fZ(z) {}
};
class Model final {
public:
Model() = default;
~Model() { Destroy(); }
void Generate(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices) {
if (!glIsBuffer(fVBO))
glGenBuffers(1, &fVBO);
glBindBuffer(GL_ARRAY_BUFFER, fVBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
if (!glIsBuffer(fIBO))
glGenBuffers(1, &fIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint32_t), indices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
fIndicesCount = static_cast<GLsizei>(indices.size());
}
void Destroy() {
glDeleteBuffers(1, &fVBO);
glDeleteBuffers(1, &fIBO);
fVBO = 0;
fIBO = 0;
}
void Draw() {
glBindBuffer(GL_ARRAY_BUFFER, fVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fIBO);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 7 * sizeof(float), (void*)(0));
glColorPointer(3, GL_FLOAT, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glDrawElements(GL_POINTS, fIndicesCount, GL_UNSIGNED_INT, (void*)(0));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
private:
GLuint fVBO = 0;
GLuint fIBO = 0;
GLsizei fIndicesCount = 0;
};
bool swapflag = false;
std::vector<Vertex> vertices;
std::vector<Vertex> vertices2;
std::vector<uint32_t> indices;
Model model;
void funcKeyEvent(GLFWwindow* _window, int _key, int _scancode, int _action, int _mods)
{
if (_action != GLFW_RELEASE)
return;
if (swapflag)
model.Generate(vertices, indices);
else
model.Generate(vertices2, indices);
swapflag = !swapflag;
std::cout << "model changed\n";
}
int main(int argc, const char * argv[]) {
if (!glfwInit()) {
return EXIT_FAILURE;
}
auto window = glfwCreateWindow(1280, 720, "MainWindow", nullptr, nullptr);
if (nullptr == window) {
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
vertices.reserve(1000000);
vertices2.reserve(1000000);
indices.reserve(1000000);
for (size_t idx = 0; idx < 1000000; ++idx){
const auto _v = static_cast<float>(idx);
vertices.emplace_back(_v * 0.0001f * std::sin(_v), _v * 0.0001f * std::cos(_v), _v * 0.0001f);
vertices2.emplace_back(_v * 0.0001f * std::cos(_v), _v * 0.0001f, _v * 0.0001f * std::sin(_v));
indices.emplace_back(idx);
}
model.Generate(vertices, indices);
glfwSetKeyCallback(window, funcKeyEvent);
glPointSize(3.0f);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
const auto matProj = glm::perspective(60.0f, 1280.0f/720.0f, -10.0f, 10.0f);
const auto matModelView = glm::lookAt(glm::vec3(-10.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(matProj));
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(matModelView));
model.Draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
model.Destroy();
glfwTerminate();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment