int main(void){ WindowGL window("render2texture", 640, 480); DriverGPU * driver = DriverGPU::get(); Texture tulipsTex("./Tulips.jpg"); Shader vShader(eShaderType::eVertexShader, "../../src/shaders/flat.vertex"); Shader sobelShader(eShaderType::eFragmentShader, "../../src/shaders/sobel.fragment"); ShaderProgram program; program.attachShader(vShader); program.attachShader(sobelShader); program.link(); #if defined(_WIN32) window.peekMessage(); double t0 = STime::get()->getTime(); drawImage(tulipsTex, program); double t1 = STime::get()->getTime(); #endif std::cout << "Edge detection spent " << t1 - t0 << std::endl; window.swapBuffers(); // delete window; Class has no destructor, undefined behaviour delete driver; system("PAUSE"); return 0; }
void SceneLoader::appendProgram(const QDomElement & programNode) { string name, shaderUrl; vector<string> flags; ShaderProgram * program = new ShaderProgram(); if (programNode.hasAttribute("name")) name = programNode.attribute("name").toStdString(); QDomElement programInfo = programNode.firstChildElement(); while (!programInfo.isNull()) { if (programInfo.tagName() == "Shader") { shaderUrl = programInfo.attribute("url").toStdString(); if (programInfo.hasAttribute("type")) { QString shaderType = programInfo.attribute("type"); if (shaderType == "VERTEX_SHADER") program->attachShader(shaderUrl, GL_VERTEX_SHADER, false); else if (shaderType == "GEOMETRY_SHADER") program->attachShader(shaderUrl, GL_GEOMETRY_SHADER, false); else if (shaderType == "FRAGMENT_SHADER") program->attachShader(shaderUrl, GL_FRAGMENT_SHADER, false); else if (shaderType == "CONTROL_SHADER") program->attachShader(shaderUrl, GL_TESS_CONTROL_SHADER, false); else if (shaderType == "EVALUATION_SHADER") program->attachShader(shaderUrl, GL_TESS_EVALUATION_SHADER, false); } else { if (programInfo.hasAttribute("flags")) { flags = splitValues<string> (programInfo.attribute( "flags")); program->attachVertFrag(shaderUrl, flags); } else { program->attachVertFrag(shaderUrl, false); } } } else if (programInfo.tagName() == "Template") { shaderUrl = programInfo.attribute("url").toStdString(); program->attachVertFrag(shaderUrl, true); } else if (programInfo.tagName() == "Uniform") { program->uniforms.push_back(Uniform<float> (programInfo.attribute( "name").toStdString(), splitValues<float> ( programInfo.attribute("value")))); } else if (programInfo.tagName() == "Uniformi") { program->uniformsi.push_back(Uniform<int> (programInfo.attribute( "name").toStdString(), splitValues<int> ( programInfo.attribute("value")))); } programInfo = programInfo.nextSiblingElement(); } QList<string> attributes; attributes.push_back("uv"); attributes.push_back("normal"); attributes.push_back("tangent"); program->init(attributes); program->name = name; SceneData::Instance().shaderPrograms.insert(name, program); }
int main(void){ WindowGL window("Texture loader", 640, 480); DriverGPU * driver = DriverGPU::get(); Shader vShader(eShaderType::eVertexShader, "../../src/shaders/flat.vertex"); Shader fShader(eShaderType::eFragmentShader, "../../src/shaders/flat.fragment"); ShaderProgram program; program.attachShader(vShader); program.attachShader(fShader); program.link(); bool condition = true; do{ #if defined(_WIN32) window.peekMessage(); drawImage(program); #endif window.swapBuffers(); } while (condition); window.hide(); #ifdef _WIN32 system("PAUSE"); #endif // delete window; Class has no destructor, undefined behaviour delete driver; return 0; }
void openGLThread(bool _draw, GLfloat _red) { //WindowGL * window = WindowGL::createWindow(640, 480); Context context; DriverGPU *driver = DriverGPU::get(); Shader vShader(eShaderType::eVertexShader, "../../src/shaders/vTriangleShader.vertex"); Shader fShader(eShaderType::eFragmentShader, "../../src/shaders/fTriangleShader.fragment"); ShaderProgram sProgram; sProgram.attachShader(vShader); sProgram.attachShader(fShader); sProgram.bindAttribute(0, "vPosition"); sProgram.link(); while(running) { //window->peekMessage(); //window->swapBuffers(); //if (_draw) // drawOnBuffer(640, 480, sProgram, _red); } delete driver; }
ShaderManager::ShaderProgram* ShaderManager::makeShaderProgram(const char* programName, const char* vertexShaderFile, const char* fragmentShaderFile){ //Create the shaders with filename as name Shader* vsShader = makeShader(vertexShaderFile, Vertex, vertexShaderFile); Shader* fsShader = makeShader(fragmentShaderFile, Fragment, fragmentShaderFile); if( vsShader == 0 || fsShader == 0 ) return 0; ShaderProgram* newProgram = new ShaderProgram(); shaderPrograms.insert(ShaderProgramContainer::value_type(programName, newProgram)); newProgram->attachShader(vsShader); newProgram->attachShader(fsShader); return newProgram; }
int main(int argc, char** argv){ int width = 800; int height = 600; bool fullscreen = false; // Device must be the very first thing created! Device device(width, height, fullscreen); //Default parameters MeshType meshType = TETRAHEDRON; // If meshType is MESH_FILE, load file from mesh stored in meshFile char* meshFile = NULL; // If true, run in visual mode. In this, all calculations are done on the GPU. // Otherwise, you get a pretty boring CSV outputted. Activate with -v bool visualization = false; // Number of times the shape is bisected (any number for tetrahedra, preset numbers for // icosahedra, and it means nothing for other meshes). Set with -n=## int idealTriangleCount = 400; // Times Update is called if running in non visual mode. Set with -i=## int iterations = 10; // If true, use the GPU for calculations. Else, use the CPU. Set with -cpu bool gpu = true; //Type of stuff to output, set with -o OutputType output[8]; int outputLength = 0; // scale of the visualization, set with -s=## int scale = 5; // if timing is true, activate with -t bool timing = false; // Parse Command Line Arguments: for(int i=1;i<argc;i++){ if(argMatch(argv[i], "-v")){ visualization = true; }else if(argMatch(argv[i], "--help")){ help(); return 0; }else if(argMatch(argv[i], "-n=")){ idealTriangleCount = atoi(argv[i]+3); }else if(argMatch(argv[i], "-i=")){ iterations = atoi(argv[i]+3); }else if(argMatch(argv[i], "-iterations=")){ iterations = atoi(argv[i]+12); }else if(argMatch(argv[i], "-m")){ i++; if(argMatch(argv[i], "tetra")){ meshType = TETRAHEDRON; }else if(argMatch(argv[i], "icosa")){ meshType = ICOSAHEDRON; }else if(argMatch(argv[i], "jeep")){ meshType = MESH_FILE; meshFile = "models/jeep.mtl"; }else if(argMatch(argv[i], "dragon")){ meshType = MESH_FILE; meshFile = "models/dragon.ply"; }else{ meshType = MESH_FILE; meshFile = argv[i]; } }else if(argMatch(argv[i], "-cpu")){ gpu = false; } else if (argMatch(argv[i], "-s=")){ scale = atoi(argv[i] + 3); } else if (argMatch(argv[i], "-t")){ timing = true; } else if (argMatch(argv[i], "-o")){ do{ i++; if(argMatch(argv[i], "SurfaceArea")){ output[outputLength++] = TOTAL_SURFACE_AREA; }else if(argMatch(argv[i], "Volume")){ output[outputLength++] = TOTAL_VOLUME; }else if(argMatch(argv[i], "MeanNetForce")){ output[outputLength++] = MEAN_NET_FORCE; }else if(argMatch(argv[i], "Curvature")){ output[outputLength++] = MEAN_CURVATURE; }else if(argMatch(argv[i], "AreaForces")){ output[outputLength++] = AREA_FORCES; }else if(argMatch(argv[i], "VolumeForces")){ output[outputLength++] = VOLUME_FORCES; }else if(argMatch(argv[i], "NetForces")){ output[outputLength++] = NET_FORCES; }else if(argMatch(argv[i], "Points")){ output[outputLength++] = POINTS; } } while(lastChar(argv[i]) == ','); } } Mesh* mesh; switch(meshType){ case TETRAHEDRON: mesh = new TetrahedronMesh(ceil(sqrt(idealTriangleCount/4.0))); break; case ICOSAHEDRON: if(idealTriangleCount <= 100){ meshFile = "models/icosa1.obj"; }else if(idealTriangleCount <= 1000){ meshFile = "models/icosa2.obj"; }else if(idealTriangleCount <= 10000){ meshFile = "models/icosa3.obj"; }else if(idealTriangleCount <= 100000){ meshFile = "models/icosa4.obj"; }else{ meshFile = "models/icosa5.obj"; } mesh = new ExternalMesh(meshFile); break; default: mesh = new ExternalMesh(meshFile); break; } // visualization only applies to GPU calculation, so: if(visualization){ SceneManager manager(&device); CameraNode camera(&device, width, height); camera.getTransform().setTranslation(20.0f, 20.0f, 20.0f); manager.addNode(&camera); ModelNode mn; //mn.getTransform().setScale(0.025f, 0.025f, 0.025f); mn.getTransform().setScale(scale, scale, scale); mn.getTransform().setTranslation(0.0f, 0.0f, 0.0f); mn.setMesh(mesh); manager.addNode(&mn); ShaderProgram geometryProgram; Shader geometryVertexShader("shaders/geometry_pass.vert", VERTEX); Shader geometryFragShader("shaders/geometry_pass.frag", FRAGMENT); geometryProgram.attachShader(&geometryVertexShader); geometryProgram.attachShader(&geometryFragShader); geometryProgram.link(); manager.setGeometryProgram(&geometryProgram); GPUEvolver evolver(mesh, 10); while (device.run()) { evolver.update(); evolver.synchronizeToMesh(); manager.drawAll(); device.endScene(); } } else { if (gpu) { GPUEvolver evolver(mesh, 10); evolver.setOutputFormat(output, outputLength); std::clock_t start; start = std::clock(); for(int i=0; i < iterations; i++){ evolver.update(); evolver.outputData(); } if (timing){ std::cout << "Time: " << (std::clock() - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl; } } else { CPUEvolver evolver(mesh, 10); evolver.setOutputFormat(output, outputLength); std::clock_t start; start = std::clock(); for(int i=0; i < iterations; i++){ evolver.update(); evolver.outputData(); } if (timing){ std::cout << "Time: " << (std::clock() - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl; } } } return 0; }
void SceneLoader::appendProgram(const QDomElement & programNode) { string name, shaderUrl; vector<string> flags; QList<string> attributes = QList<string>() << "uv" << "normal" << "tangent" << "bitangent"; if (programNode.hasAttribute("name")) name = programNode.attribute("name").toStdString(); ShaderProgram * program = new ShaderProgram(name); QDomElement programInfo = programNode.firstChildElement(); while (!programInfo.isNull()) { if (programInfo.tagName() == "Shader") { shaderUrl = programInfo.attribute("url").toStdString(); if (programInfo.hasAttribute("flags")) { flags = splitValues<string> (programInfo.attribute("flags")); TemplateEngine::Instance().addFlags(flags); } if (programInfo.hasAttribute("type")) { QString shaderType = programInfo.attribute("type"); if (shaderType == "VERTEX_SHADER") program->attachShader(shaderUrl, GL_VERTEX_SHADER); else if (shaderType == "GEOMETRY_SHADER") program->attachShader(shaderUrl, GL_GEOMETRY_SHADER); else if (shaderType == "FRAGMENT_SHADER") program->attachShader(shaderUrl, GL_FRAGMENT_SHADER); else if (shaderType == "CONTROL_SHADER") program->attachShader(shaderUrl, GL_TESS_CONTROL_SHADER); else if (shaderType == "EVALUATION_SHADER") program->attachShader(shaderUrl, GL_TESS_EVALUATION_SHADER); } else { program->attachVertFrag(shaderUrl); } } else if (programInfo.tagName() == "Uniform") { program->uniforms.push_back(Uniform<float> (programInfo.attribute( "name").toStdString(), splitValues<float> ( programInfo.attribute("value")))); } else if (programInfo.tagName() == "Uniformi") { program->uniformsi.push_back(Uniform<int> (programInfo.attribute( "name").toStdString(), splitValues<int> ( programInfo.attribute("value")))); } else if (programInfo.tagName() == "Layer") { if (programInfo.hasAttribute("texture")) { Texture * texture = Scene::Instance().textures.value( programInfo.attribute("texture").toStdString()); if (Scene::Instance().textures.count(programInfo.attribute("texture").toStdString()) == 0) LogError << "Texture " << programInfo.attribute("texture").toStdString() << " not found."; // TODO(bmonkey): uniform name reset LogDebug << programInfo.attribute("sampler").toStdString(); texture->name = programInfo.attribute("sampler").toStdString(); program->addTexture(texture); } } programInfo = programInfo.nextSiblingElement(); } program->init(attributes); program->samplerUniforms(); Scene::Instance().shaders.insert(name, program); }
int main(int argc, char** argv) { --argc; ++argv; const int WIDTH = 800; const int HEIGHT = 600; /* * Create window */ sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Window", sf::Style::Default, sf::ContextSettings(32)); /* * Initialize GLEW */ GLenum status = glewInit(); if(status != GLEW_OK) { std::cerr << "[F] GLEW NOT INITIALIZED: "; std::cerr << glewGetErrorString(status) << std::endl; window.close(); return -1; } /* * Create GUI */ tgui::Gui gui(window); tgui::Gui gui2(window); gui.setGlobalFont("fonts/DejaVuSans.ttf"); loadWidgets(gui, gui2, window); char* args[argc + 1]; /* * load geometry */ Model cube; getArgs(argc, argv, ".obj", args); if (!args[0]) { std::cerr << "[F] MUST SUPPLY 1+ OBJ FILES IN COMMAND LINE ARGS <filename.obj>" << std::endl; exit(-1); } //if loadGeometry(cube, args[0]); getArgs(argc, argv, ".scale", args); if (args[0]) { cube.scale = glm::vec3(strtof(args[0], NULL)); } //if else { cube.scale = glm::vec3(1.0f); } //else /* * load shader */ Shader vertexShader(GL_VERTEX_SHADER); getArgs(argc, argv, ".vs", args); vertexShader.loadFromFile(args[0]? args[0] : ".vs"); vertexShader.compile(); Shader fragmentShader(GL_FRAGMENT_SHADER); getArgs(argc, argv, ".fs", args); fragmentShader.loadFromFile(args[0]? args[0] : ".fs"); fragmentShader.compile(); /* * create program */ ShaderProgram program; program.attachShader(vertexShader); program.attachShader(fragmentShader); program.linkProgram(); program.attachShader(vertexShader); program.attachShader(fragmentShader); program.linkProgram(); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); glClearDepth(1.f); Camera camera; sf::Event event; tgui::Callback callback; Planet p(&cube); /* * main loop */ while (window.isOpen()) { while (window.pollEvent(event)) { switch (event.type) { case sf::Event::Closed: window.close(); break; case sf::Event::Resized: glViewport(0, 0, event.size.width, event.size.height); camera.projection = glm::perspective(45.0f, float(event.size.width)/float(event.size.height), 0.01f, 100.0f); break; case sf::Event::KeyPressed: switch(event.key.code) { case sf::Keyboard::Escape: window.close(); exit(0); break; default: break; } //switch break; default: break; } //switch gui.handleEvent(event); } //if while (gui.pollCallback(callback)) { gui.handleEvent(event); } //if window.clear(); guiDraw(window, gui2); glClear(GL_DEPTH_BUFFER_BIT); /* * render OpenGL here */ for (Planet* planet : planets) { render(*planet, camera, program); } //for guiDraw(window, gui); window.display(); } //while // Clean up after ourselves if (window.isOpen()) { window.close(); } //if return EXIT_SUCCESS; } //main
void engine_portal_init(void) { glClearColor(0.1f, 0.2f, 0.3f, 1.0f); const GLfloat vertices[6][2] = { { -0.50, -0.50 }, { 0.50, -0.50 }, { 0.50, 0.50 }, { -0.50, 0.50 } }; const GLfloat colors[] = { 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, }; const GLushort vertex_indies[] = { 0, 1, 2, 0, 2, 3 }; glGenBuffers(1, &indiesBufferObject); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indiesBufferObject); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indies), vertex_indies, GL_STATIC_DRAW); glGenVertexArrays(1, &vertexArrayObject); glBindVertexArray(vertexArrayObject); glGenBuffers(1, &vertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), nullptr, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors); ShaderElement * shaderVert = ShaderElement::createWithShaderCode(GL_VERTEX_SHADER, File::readAllText("../Shader/triangles.vert")); if (!shaderVert->compile()) { cout << shaderVert->getError() << endl; cout << "Vertex Shader Compile Error" << endl; } ShaderElement * shaderFrag = ShaderElement::createWithShaderCode(GL_FRAGMENT_SHADER, File::readAllText("../Shader/triangles.frag")); if (!shaderFrag->compile()) { cout << "Fragment Shader Compile Error" << endl; } ShaderProgram * shaderProgram = ShaderProgram::create(); shaderProgram->attachShader(*shaderVert); shaderProgram->attachShader(*shaderFrag); if (!shaderProgram->linkProgram()) { cout << "ShaderProgram Linking Error" << endl; } GLint uboSize; GLuint uboIndex = glGetUniformBlockIndex(shaderProgram->programId(), "Uniforms"); GLvoid * uboData; GLuint ubo; if (uboIndex == GL_INVALID_INDEX) { cout << "Uniforms Is Not Uniform Block Name" << endl; } glGetActiveUniformBlockiv(shaderProgram->programId(), uboIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uboSize); uboData = (GLvoid *)malloc(uboSize); if (uboData == nullptr) { cout << "uboData Malloc Error" << endl; } enum { vTranslation, vRotation, vScale, numUniforms }; const char* uniformsNames[numUniforms] = { "vTranslation", "vRotation", "vScale" }; GLuint indices[numUniforms]; GLint sizes[numUniforms]; GLint offsets[numUniforms]; GLint types[numUniforms]; glGetUniformIndices(shaderProgram->programId(), numUniforms, uniformsNames, indices); glGetActiveUniformsiv(shaderProgram->programId(), numUniforms, indices, GL_UNIFORM_SIZE, sizes); glGetActiveUniformsiv(shaderProgram->programId(), numUniforms, indices, GL_UNIFORM_OFFSET, offsets); glGetActiveUniformsiv(shaderProgram->programId(), numUniforms, indices, GL_UNIFORM_TYPE, types); //data GLfloat translation[3] = { 0.3f, 0.3f, 0.0f}; GLfloat rotation[3] = { 0.0f, 0.0f, 90.0f}; GLfloat scale[3] = { 0.5f, 0.8f, 0.0f}; memcpy((void *)((GLint)uboData + offsets[vTranslation]), translation, sizes[vTranslation] * ShaderProgram::TypeSize(types[vTranslation])); memcpy((void *)((GLint)uboData + offsets[vRotation]), rotation, sizes[vRotation] * ShaderProgram::TypeSize(types[vRotation])); memcpy((void *)((GLint)uboData + offsets[vScale]), scale, sizes[vScale] * ShaderProgram::TypeSize(types[vScale])); glGenBuffers(1, &ubo); glBindBuffer(GL_UNIFORM_BUFFER, ubo); glBufferData(GL_UNIFORM_BUFFER, uboSize, uboData, GL_STATIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, uboIndex, ubo); glUseProgram(shaderProgram->programId()); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (const void *)sizeof(vertices)); glEnableVertexAttribArray(1); }