void OpenPSTD::GUI::DGResults::PaintGL(QObject *context, std::unique_ptr<QOpenGLFunctions, void (*)(void *)> const &f) { if(valuesInitialized) { program->bind(); program->enableAttributeArray("a_position"); program->enableAttributeArray("pressure"); f->glBindBuffer(GL_ARRAY_BUFFER, this->positionsBuffer); GLError("DGResults f->glBindBuffer(this->positionsBuffer)"); f->glVertexAttribPointer((GLuint) program->attributeLocation("a_position"), 2, GL_FLOAT, GL_FALSE, 0, 0); GLError("DGResults f->glVertexAttribPointer"); f->glBindBuffer(GL_ARRAY_BUFFER, this->valuesBuffer); GLError("DGResults f->glBindBuffer(this->valuesBuffer)"); f->glVertexAttribPointer((GLuint) program->attributeLocation("pressure"), 1, GL_FLOAT, GL_FALSE, 0, 0); GLError("DGResults f->glVertexAttribPointer"); f->glDrawArrays(GL_TRIANGLES, 0, triangles * 3); GLError("DGResults f->glDrawArrays"); program->disableAttributeArray("pressure"); program->disableAttributeArray("a_position"); } }
void OpenPSTD::GUI::DGTriangleLayer::PaintGL(QObject *context, std::unique_ptr<QOpenGLFunctions, void (*)(void *)> const &f) { program->bind(); program->enableAttributeArray("a_position"); f->glBindBuffer(GL_ARRAY_BUFFER, this->positionsBuffer); GLError("DGTriangleLayer f->glBindBuffer"); f->glVertexAttribPointer((GLuint) program->attributeLocation("a_position"), 2, GL_FLOAT, GL_FALSE, 0, 0); GLError("DGTriangleLayer f->glVertexAttribPointer"); f->glDrawArrays(GL_LINES, 0, lines * 2); GLError("DGTriangleLayer f->glDrawArrays"); program->disableAttributeArray("a_position"); }
void validateNoGLError() { const GLenum error = glGetError(); if (error != GL_NO_ERROR) { throw GLError(error); } }
void OpenPSTD::GUI::DGTriangleLayer::InitializeGL(QObject *context, std::unique_ptr<QOpenGLFunctions, void (*)(void *)> const &f) { f->glGenBuffers(1, &this->positionsBuffer); lines = 0; std::unique_ptr<std::string> vertexFile = std::unique_ptr<std::string>( new std::string(":/GPU/PassPosition.vert")); std::unique_ptr<std::string> fragmentFile = std::unique_ptr<std::string>( new std::string(":/GPU/FixedColor.frag")); std::unique_ptr<std::string> geometricFile = std::unique_ptr<std::string>( new std::string(":/GPU/Line.geom")); program = std::unique_ptr<QOpenGLShaderProgram>(new QOpenGLShaderProgram(nullptr)); program->addShaderFromSourceFile(QOpenGLShader::Vertex, QString::fromStdString(*vertexFile)); program->addShaderFromSourceFile(QOpenGLShader::Fragment, QString::fromStdString(*fragmentFile)); program->addShaderFromSourceFile(QOpenGLShader::Geometry, QString::fromStdString(*geometricFile)); program->link(); program->bind(); QColor color(255, 255, 255, 255); program->setUniformValue("u_color", color); program->setUniformValue("thickness", 0.01f); GLError("DGTriangleLayer program->setUniformValue"); }
Shader::Shader(const std::string& source, GLenum type) : shader_(glCreateShader(type)) { if (!shader_) throw GLError(glGetError(), "Could not create shader object"); const GLchar* shaderSource[] = { source.c_str() }; glShaderSource(shader_, 1, shaderSource, nullptr); glCompileShader(shader_); int compileStatus; glGetShaderiv(shader_, GL_COMPILE_STATUS, &compileStatus); if (compileStatus == GL_FALSE) { GLenum error = glGetError(); fprintf(stderr, "Shader %u source:\n%s\ninfo log:\n%s\n", shader_, source.c_str(), infoLog().c_str()); throw GLError(error, "Shader compilation failed"); } }
Ship::Ship(int windowWidth, int windowHeight) { this->transMat = Matrix4(); this->posX = 0; this->posY = 0; this->WINDOW_WIDTH = windowWidth; this->WINDOW_HEIGHT = windowHeight; // Shaders GLuint vertexShader = ShaderHelper::makeGLShader("ship.vert", GL_VERTEX_SHADER); GLuint fragmentShader = ShaderHelper::makeGLShader("ship.frag", GL_FRAGMENT_SHADER); Ship::shaderProgram = glCreateProgram(); glAttachShader(Ship::shaderProgram, vertexShader); glAttachShader(Ship::shaderProgram, fragmentShader); glLinkProgram(Ship::shaderProgram); GLint success; glGetProgramiv(Ship::shaderProgram, GL_LINK_STATUS, &success); if (!success) { throw GLError(GLErrorType::PROGRAM, Ship::shaderProgram, "GL Ship Shader Program failed to link."); } Ship::SHDR_MatLocation = glGetUniformLocation(shaderProgram, "transMat"); Ship::SHDR_DestroyedLocation = glGetUniformLocation(shaderProgram, "crashed"); glDeleteShader(vertexShader); glDeleteShader(fragmentShader); // Graphics Data glGenVertexArrays(1, &Ship::VAO); glBindVertexArray(Ship::VAO); GLuint shapeVBO; glGenBuffers(1, &shapeVBO); glBindBuffer(GL_ARRAY_BUFFER, shapeVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(shapeVertices), shapeVertices, GL_STATIC_DRAW); GLuint EBO; glGenBuffers(1, &EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (GLvoid*)0); glEnableVertexAttribArray(0); // Unbind buffers and VAO glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
void OpenPSTD::GUI::DGResults::InitializeGL(QObject *context, std::unique_ptr<QOpenGLFunctions, void (*)(void *)> const &f) { f->glGenBuffers(1, &this->positionsBuffer); f->glGenBuffers(1, &this->valuesBuffer); triangles = 0; std::unique_ptr<std::string> vertexFile = std::unique_ptr<std::string>( new std::string(":/GPU/DGResult.vert")); std::unique_ptr<std::string> fragmentFile = std::unique_ptr<std::string>( new std::string(":/GPU/DGResult.frag")); program = std::unique_ptr<QOpenGLShaderProgram>(new QOpenGLShaderProgram(nullptr)); program->addShaderFromSourceFile(QOpenGLShader::Vertex, QString::fromStdString(*vertexFile)); program->addShaderFromSourceFile(QOpenGLShader::Fragment, QString::fromStdString(*fragmentFile)); program->link(); GLError("DGResultsprogram->link"); program->bind(); GLError("DGResults program->bind"); program->setUniformValue("v_log_min", -60.0f); GLError("DGResults program->setUniformValue"); }
void SetupText() { unsigned char buf[131072]; unsigned char* data = GetData(); for (int i = 0; i < 32768; ++i) { buf[i * 4] = data[i * 2]; buf[(i * 4) + 1] = data[i * 2]; buf[(i * 4) + 2] = data[i * 2]; buf[(i * 4) + 3] = data[(i * 2) + 1]; } glGenTextures(1, &fontTex); glBindTexture(GL_TEXTURE_2D, fontTex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glGetError(); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2048, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); GLenum err = glGetError(); if (err != GL_NO_ERROR) { printf("Error loading texture: "); GLError(err); printf("\n"); } else printf("Font loaded.\n"); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); if (!textDraw) { textDraw = new Shader("Text Draw"); textDraw->SetSource(textDrawVertexSource, textDrawFragmentSource); if (!textDraw->Compile()) printf("%s", textDraw->GetErrorLog()); } memset(uniformArray, 0, sizeof(int) * MAXINTS); textInitialised = true; }
/// <summary>Creates an open gl shader by loading in GLSL source code from an external /// file</summary> /// <param name="fileLocation">The path to a file to load the GLSL source code from</param> /// <param name="type">The open gl constant (often found in the form GL_(shader type)_SHADER) representing the type of shader to create</param> /// <returns>The GLuint handle for the compiled shader</returns> /// <exception cref="InitError">Throws InitError if fileLocation is invalid</exception> /// <exception cref="GLError">Throws GLError if the source code fails to compile </exception> GLuint ShaderHelper::makeGLShader(string fileLocation, GLint type) { ifstream file; file.exceptions(ifstream::badbit); string shaderSource; try { file.open(fileLocation); stringstream fileInput; fileInput << file.rdbuf(); file.close(); shaderSource = fileInput.str(); } catch (ifstream::failure) { throw InitError("Failed to open/read shader source code at path \"" + fileLocation + "\""); } const GLchar* cStyleSource = shaderSource.c_str(); GLuint shader; shader = glCreateShader(type); glShaderSource(shader, 1, &cStyleSource, NULL); glCompileShader(shader); GLint success; glGetShaderiv(shader, GL_COMPILE_STATUS, &success); if (!success) { throw GLError(GLErrorType::SHADER, shader, ("GL Shader at \"" + fileLocation + "\" failed to compile.")); } return shader; }
void IconLayer::PaintGL(QObject *context, std::unique_ptr<QOpenGLFunctions, void (*)(void *)> const &f) { program->bind(); program->enableAttributeArray("a_position"); program->enableAttributeArray("a_color"); f->glBindBuffer(GL_ARRAY_BUFFER, this->LineBuffers); f->glVertexAttribPointer((GLuint) program->attributeLocation("a_position"), 2, GL_FLOAT, GL_FALSE, 0, 0); GLError("a_position"); f->glBindBuffer(GL_ARRAY_BUFFER, this->ColorBuffer); f->glVertexAttribPointer((GLuint) program->attributeLocation("a_color"), 4, GL_FLOAT, GL_FALSE, 0, 0); GLError("a_color"); f->glLineWidth(1.0f); f->glDrawArrays(GL_POINTS, 0, lines); GLError("f->glDrawArrays"); program->disableAttributeArray("a_position"); program->disableAttributeArray("a_color"); programIntern->bind(); programIntern->enableAttributeArray("a_position"); programIntern->enableAttributeArray("a_color"); f->glBindBuffer(GL_ARRAY_BUFFER, this->LineBuffers); f->glVertexAttribPointer((GLuint) programIntern->attributeLocation("a_position"), 2, GL_FLOAT, GL_FALSE, 0, 0); GLError("a_position"); f->glBindBuffer(GL_ARRAY_BUFFER, this->ColorBuffer); f->glVertexAttribPointer((GLuint) programIntern->attributeLocation("a_color"), 4, GL_FLOAT, GL_FALSE, 0, 0); GLError("a_color"); f->glDrawArrays(GL_POINTS, 0, lines); GLError("f->glDrawArrays"); programIntern->disableAttributeArray("a_position"); programIntern->disableAttributeArray("a_color"); }
//------------------------------------------------------------------------------ ShaderAsset::ShaderAsset(Hub& hub, const std::string& filename, GLenum shaderType) : BaseAsset(filename), mHasValidShader(false), mHub(hub) { std::string targetFilename = findFileInDirs(filename, hub.getShaderDirs(), false); const std::vector<std::string> dirs(hub.getShaderDirs()); std::ifstream file(targetFilename, std::ios_base::in); if (file.is_open() == false) { Log::message() << "Failed to open shader " << filename << std::endl; throw NotFound("Failed to find shader."); } // Size std::string appropriately before reading file. std::string fileContents; file.seekg(0, std::ios::end); fileContents.resize(static_cast<unsigned int>(file.tellg())); file.seekg(0, std::ios::beg); // Extra parenthesis are essential to avoid the most vexing parse. fileContents.assign( (std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); // Now compile the shader file. GLuint shader; GLint compiled; // Create the shader object. shader = glCreateShader(shaderType); GL_CHECK(); if (0 == shader) { Log::message() << "Failed to create shader of type: " << shaderType << "\n"; throw GLError("Unable to construct shader."); } #ifdef SPIRE_OPENGL_ES_2 const size_t numShaderSources = 2; const char* cFileContents[numShaderSources] = {"#define OPENGL_ES\n#define OPENGL_ES_2\n", fileContents.c_str()}; GL(glShaderSource(shader, numShaderSources, cFileContents, NULL)); #else const char* cFileContents = fileContents.c_str(); GL(glShaderSource(shader, 1, &cFileContents, NULL)); #endif GL(glCompileShader(shader)); // Check the compile status GL(glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled)); // Check compilation status. if (!compiled) { GLint infoLen = 0; GL(glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen)); if (infoLen > 1) { char* infoLog = new char[infoLen]; GL(glGetShaderInfoLog(shader, infoLen, NULL, infoLog)); Log::error() << "Error compiling '" << filename << "':" << std::endl << infoLog << std::endl; delete[] infoLog; } GL(glDeleteShader(shader)); throw GLError("Failed to compile shader."); } mHasValidShader = true; glID = shader; }
Asteroid::Asteroid(GLfloat posX, GLfloat posY, GLfloat radius) { this->posX = posX; this->posY = posY; this->radius = radius; this->thetaZ = 0.0; this->transformation = Matrix4(); this->transformation *= Matrix4::ZRotation(this->thetaZ); this->transformation *= Matrix4::Translation(posX, posY, 0.0f); this->transformation *= Matrix4::Scale(radius); // Generates shaders if they are not already generated if (Asteroid::shaderProgram == 0) { // Asteroid shape vertices std::array<Vec3, 91> vertices = std::array<Vec3, 91>(); for (int i = 0; i < 90; i++) { vertices[i].x = (cos(i * 360 / 90 * PI / 180)); vertices[i].y = (sin(i * 360 / 90 * PI / 180)); } vertices[90].x = 0.0f; vertices[90].y = 0.0f; // Order to create triangles from the vertices // A triangle is created between vertices[indices[n1]], v[i[n2]], v[i[n3]] std::array<int, 270> indices = std::array<int, 270>(); for (int i = 0; i < 90; i++) { indices[3 * i] = 90; indices[(3 * i) + 1] = i; indices[(3 * i) + 2] = i + 1; } indices[269] = 0; // Shaders GLuint vertexShader = ShaderHelper::makeGLShader("asteroid.vert", GL_VERTEX_SHADER); GLuint fragmentShader = ShaderHelper::makeGLShader("asteroid.frag", GL_FRAGMENT_SHADER); // Shader program GLint success; Asteroid::shaderProgram = glCreateProgram(); glAttachShader(Asteroid::shaderProgram, vertexShader); glAttachShader(Asteroid::shaderProgram, fragmentShader); glLinkProgram(Asteroid::shaderProgram); glGetProgramiv(Asteroid::shaderProgram, GL_LINK_STATUS, &success); if (!success) { throw GLError(GLErrorType::PROGRAM, Asteroid::shaderProgram, "GL Asteroid Shader Program failed to link."); } Asteroid::SHDR_MatLocation = glGetUniformLocation(Asteroid::shaderProgram, "transMat"); glDeleteShader(vertexShader); glDeleteShader(fragmentShader); // Binds the Asteroid VAO to store the data layout and VertexAttribPointer data parsing details for usage later glGenVertexArrays(1, &Asteroid::VAO); glBindVertexArray(Asteroid::VAO); GLuint shapeVBO; glGenBuffers(1, &shapeVBO); glBindBuffer(GL_ARRAY_BUFFER, shapeVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); // loading in indices for vertices draw order GLuint EBO; glGenBuffers(1, &EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices, GL_STATIC_DRAW); // GL data interpretation glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, Vec3::size(), (GLvoid*)0); glEnableVertexAttribArray(0); // Unbinds VAO, VBO, and EBO glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } }