void loadProgram(Program &p, const std::string& name) { //Initializes a Program object p.create(); //Gets the path to the vertex shader (ending in ".vsh") string sv = rh.pathToResource(name, "vsh"); // cout << "path of vertex shader is: " << sv << endl; //Compiles the vertex shader and attaches it to our Program object p.attach(rh.contentsOfFile(sv), GL_VERTEX_SHADER); //Binds attribute variables to a particular ID glBindAttribLocation(p.id(), posLoc, "vertexPosition"); glBindAttribLocation(p.id(), colLoc, "vertexColor"); //Gets the path to the fragment shader (ending in ".fsh") string sp = rh.pathToResource(name, "fsh"); // cout << "path of vertex shader is: " << sp << endl; //Compiles the fragment shader and attaches it to our Program object p.attach(rh.contentsOfFile(sp), GL_FRAGMENT_SHADER); //Links the Program object to the GPU so that it can be activated when needed p.link(); }
void loadProgram(Program &p, const std::string& name) { //can't link shaders unless a vao is bound... //bindDefaultVAO(); p.create(); string sv = rh.pathToResource(name, "vsh"); p.attach(rh.contentsOfFile(sv), GL_VERTEX_SHADER); glBindAttribLocation(p.id(), posLoc, "vertexPosition"); glBindAttribLocation(p.id(), texCoordLoc, "vertexTexCoord"); string sg = rh.pathToResource(name, "gsh"); cout << "path of vertex shader is: " << sg << endl; p.attach(rh.contentsOfFile(sg), GL_GEOMETRY_SHADER); string sp = rh.pathToResource(name, "fsh"); p.attach(rh.contentsOfFile(sp), GL_FRAGMENT_SHADER); p.link(); }
void loadProgram(Program &p, const std::string& name) { p.create(); p.attach(p.loadText(name + ".vsh"), GL_VERTEX_SHADER); glBindAttribLocation(p.id(), posLoc, "vertexPosition"); glBindAttribLocation(p.id(), normalLoc, "vertexNormal"); p.attach(p.loadText(name + ".fsh"), GL_FRAGMENT_SHADER); p.link(); }
Main() { mScale = 0.1f; mOffset = 1.0f; mDataOffset = 0; isClicked = false; screen = Vec2<unsigned int>(800, 600); std::setlocale(LC_ALL, "en_US.UTF-8"); glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); GLFWwindow* window = glfwCreateWindow(screen.x, screen.y, "test", nullptr, nullptr); if (window == nullptr) { printf("cant create window"); return; } glfwSetWindowSizeCallback(window, windowSizeCallback); glfwSetKeyCallback(window, keyCallback); glfwSetMouseButtonCallback(window, clickCallback); glfwSetCursorPosCallback(window, mouseCallback); glfwMakeContextCurrent(window); glewExperimental = true; glewInit(); int tmp; glGetIntegerv(GL_MAX_ELEMENTS_VERTICES , &tmp); std::cout << "GL_MAX_ELEMENTS_VERTICES: " << tmp << std::endl; int err = Pa_Initialize(); if (err != paNoError) printf("error"); int num = Pa_GetDeviceCount(); const PaDeviceInfo* devInfo; const PaHostApiInfo* apiInfo; for (int i = 0; i < num; ++i) { devInfo = Pa_GetDeviceInfo(i); apiInfo = Pa_GetHostApiInfo(devInfo->hostApi); printf("%i, %s on %s\n", i, devInfo->name, apiInfo->name); } float sampleRate = 44100.0f; double t = glfwGetTime(); Kern k(sampleRate, 12, 4 * 16.352f, sampleRate / 2); BlockMatrix<std::complex<double>> b(k.K, k.mN0, k.mB, 0.01); mAudioData = new double[b.getWidth()]; mAudioLength = b.getWidth(); for (unsigned int i = 0; i < mAudioLength; ++i) { mAudioData[i] = wave(55, sampleRate, i) + wave(110, sampleRate, i) + wave(220, sampleRate, i) + wave(440, sampleRate, i) + wave(880, sampleRate, i) + wave(1760, sampleRate, i) + wave(3520, sampleRate, i) + wave(7040, sampleRate, i); } printf("kernel time:%f\n", glfwGetTime() - t); float drawArray[k.mB * 2]; std::complex<double> out[k.mB]; CQT::transform(mAudioData, out, b, mAudioLength); t = glfwGetTime(); printf("transform time:%f\n", glfwGetTime() - t); //glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glDebugMessageCallback(debugCallback, nullptr); //glEnable(GL_DEBUG_OUTPUT); printf("%s\n", glGetString(GL_VERSION)); Shader fs("res/shader/fragment.c", true, GL_FRAGMENT_SHADER); Shader vs("res/shader/vertex.c", true, GL_VERTEX_SHADER); Program* p = new Program(); p->attach(fs); p->attach(vs); p->build(); p->use(); Program p2; Shader fs2("res/shader/fragment2.c", true, GL_FRAGMENT_SHADER); Shader vs2("res/shader/vertex2.c", true, GL_VERTEX_SHADER); p2.attach(fs2); p2.attach(vs2); p2.build(); p2.use(); int uniformData = p2.getUniformLocation("data"); unsigned int waterfallSize = 512; tm = new TextureManager(); unsigned int waterfallTexture; unsigned int waterfallId = tm->getFreeTexture(); glGenTextures(1, &waterfallTexture); glActiveTexture(GL_TEXTURE0 + waterfallId); glBindTexture( GL_TEXTURE0, waterfallTexture ); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); unsigned char* textureTmp = new unsigned char[waterfallSize * b.getWidth()]; glTexImage2D( GL_TEXTURE_2D_ARRAY, 0, GL_R8, b.getWidth(), waterfallSize, 0, GL_RED, GL_UNSIGNED_BYTE, textureTmp); delete textureTmp; float max = 0; for (unsigned int i = 0; i < k.mB; ++i) { drawArray[2 * i + 0] = (float)i / k.mB * 2.0f - 1.0f; float tmp = std::abs(out[i]); drawArray[2 * i + 1] = tmp; max = std::max(tmp, max); } font = new Font(512, "res/font/DroidSans.woff", 32, tm); print = new Print(font); //print.set(&font, "res/shader/fontVertex.c", "res/shader/fontFragment.c"); print->setScreenSize(screen); glm::vec2* vert = new glm::vec2[1024]; glm::vec2* debug = new glm::vec2[b.getWidth()]; for (unsigned int i = 0; i < b.getWidth(); ++i) { debug[i].x = (float)i / b.getWidth() * 2.0f - 1.0f; } uint32_t vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); uint32_t vbo[2]; glGenBuffers(1, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glEnableVertexAttribArray(0); glBufferData(GL_ARRAY_BUFFER, k.mB * sizeof(glm::vec2), drawArray, GL_DYNAMIC_DRAW); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glEnable(GL_BLEND); glfwSetWindowUserPointer(window, this); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); double time, timeo; glfwSwapInterval(1); PaStream* stream; PaStreamParameters params; params.device = 21; params.channelCount = 1; params.sampleFormat = paFloat32; params.hostApiSpecificStreamInfo = nullptr; params.suggestedLatency = 0.5; err = Pa_OpenStream(&stream, ¶ms, nullptr, sampleRate, paFramesPerBufferUnspecified, 0, paCallback, this); if (err != paNoError) printf("error %i", err); Pa_StartStream(stream); while(!glfwWindowShouldClose(window)) { timeo = time; time = glfwGetTime(); CQT::transform(mAudioData, out, b, mAudioLength); max = 0.0f; for (unsigned int i = 0; i < k.mB; ++i) { drawArray[2 * i + 0] = (float)i / k.mB * 2.0f - 1.0f; float tmp = std::abs(out[i]); drawArray[2 * i + 1] = tmp; max = std::max(tmp, max); } for (unsigned int i = 0; i < k.mB; ++i) { drawArray[2 * i + 1] = std::log(drawArray[2 * i +1]) * mScale + mOffset; } //printf("%f\n", drawArray[1]); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, k.mB * sizeof(glm::vec2), drawArray, GL_DYNAMIC_DRAW); p->use(); glDrawArrays(GL_LINE_STRIP, 0, k.mB); for (unsigned int i = 0; i < b.getWidth(); ++i) { debug[i].y = mAudioData[i] / 15.0; } glBufferData(GL_ARRAY_BUFFER, b.getWidth() * sizeof(glm::vec2), debug, GL_DYNAMIC_DRAW); glDrawArrays(GL_LINE_STRIP, 0, b.getWidth()); print->printfAt(-300.0f, 100.0f, 16.0f, 16.0f, u8"Fps:%03.3f", 1/(time-timeo)); glfwSwapBuffers(window); glClear(GL_COLOR_BUFFER_BIT); glfwPollEvents(); } Pa_StopStream(stream); Pa_CloseStream(stream); Pa_Terminate(); std::cout << "Hello World. I'm Peach." << std::endl; }
Main() { isClicked = false; screen = Vec2<unsigned int>(800, 600); std::setlocale(LC_ALL, "en_US.UTF-8"); glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); GLFWwindow* window = glfwCreateWindow(screen.x, screen.y, "test", nullptr, nullptr); if (window == nullptr) { printf("cant create window"); return; } glfwSetWindowSizeCallback(window, windowSizeCallback); glfwSetKeyCallback(window, keyCallback); glfwSetMouseButtonCallback(window, clickCallback); glfwSetCursorPosCallback(window, mouseCallback); glfwMakeContextCurrent(window); glewExperimental = true; glewInit(); //glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glDebugMessageCallback(debugCallback, nullptr); glEnable(GL_DEBUG_OUTPUT); printf("%s\n", glGetString(GL_VERSION)); Shader* fs = new Shader("res/shader/fragment.c", true, GL_FRAGMENT_SHADER); Shader* vs = new Shader("res/shader/vertex.c", true, GL_VERTEX_SHADER); Program* p = new Program(); p->attach(fs); p->attach(vs); p->build(); p->use(); tm = new TextureManager(); font = new Font(512, "res/font/DroidSans.woff", 32, tm); print = new Print(font); //print.set(&font, "res/shader/fontVertex.c", "res/shader/fontFragment.c"); print->setScreenSize(screen); glm::vec2* vert = new glm::vec2[1024]; uint32_t vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); uint32_t vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glEnableVertexAttribArray(0); glBufferData(GL_ARRAY_BUFFER, 1024 * sizeof(glm::vec2), vert, GL_DYNAMIC_DRAW); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glEnable(GL_BLEND); glfwSetWindowUserPointer(window, this); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); tr = new TileRenderer(); d = new Drawing(tr); tr->setScreenSize(screen); //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); double time, timeo; glfwSwapInterval(0); while(!glfwWindowShouldClose(window)) { timeo = time; time = glfwGetTime(); glClear(GL_COLOR_BUFFER_BIT); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); d->render(); //tr->renderTile(t); print->printfAt(-0.3f, 0.7f, 16.0f, 16.0f, u8"Fps:%03.3f", 1/(time-timeo)); glfwSwapBuffers(window); glfwWaitEvents(); } std::cout << "Hello World. I'm Peach." << std::endl; }