//-----------------------------------------------------------------------------
//	Shader - Shader From a Buffer
//-----------------------------------------------------------------------------
bool shader::loadBuffer( const char* buffer, int length, int shaderType ) {
    GLint shaderStatus(0);
    GLuint shaderId( 0 );
    
    if ( !programId ) {
        programId = glCreateProgram();
        
        if ( !programId ) {
            std::cerr << "ERROR: Unable to create a shader program." << std::endl;
            return false;
        }
    }
    

    shaderId = glCreateShader( shaderType ); // Fragment shader or Vertex Shader
    glShaderSource( shaderId, 1, (const GLchar**)&buffer, (const GLint*)&length );

    glCompileShader( shaderId );
    glGetShaderiv( shaderId, GL_COMPILE_STATUS, &shaderStatus );

    if ( shaderStatus != GL_TRUE ) {
        printShaderError( 0, shaderId );
        return false;
    }
    
    glAttachShader( programId, shaderId );
    shaderIds.push_back( shaderId );

    printGlError("General shader loading error");
    return true;
}
Exemple #2
0
void TriMesh::drawEdges(QGLShaderProgram& prog,
                        const TransformState& transState) const
{
    unsigned int vertexShaderId = shaderId("meshedge");
    unsigned int vertexArray = getVAO("meshedge");

    transState.translate(offset()).setUniforms(vertexShaderId);

    glBindVertexArray(vertexArray);
    glDrawElements(GL_LINES, (GLsizei)m_edges.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}
Exemple #3
0
void TriMesh::drawFaces(QGLShaderProgram& prog,
                        const TransformState& transState) const
{
    // TODO: The hasTexture uniform shader variable would be unnecessary if we
    // supported more than one mesh face shader...
    GLint hasTextureLoc = glGetUniformLocation(prog.programId(), "hasTexture");
    if (m_texture)
    {
        GLint textureSamplerLoc = glGetUniformLocation(prog.programId(), "texture0");
        if (textureSamplerLoc != -1)
            m_texture->bind(textureSamplerLoc);
    }
    if (hasTextureLoc != -1)
        glUniform1i(hasTextureLoc, m_texture ? 1 : 0);
    unsigned int vertexShaderId = shaderId("meshface");
    unsigned int vertexArray = getVAO("meshface");

    transState.translate(offset()).setUniforms(vertexShaderId);

    glBindVertexArray(vertexArray);
    glDrawElements(GL_TRIANGLES, (GLsizei)m_triangles.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}
Exemple #4
0
void TriMesh::initializeVertexGL(const char * vertArrayName, const std::vector<unsigned int>& elementInds,
                                 const char * positionAttrName, const char * normAttrName,
                                 const char * colorAttrName, const char* texCoordAttrName)
{
    unsigned int vertexShaderId = shaderId(vertArrayName);

    if (!vertexShaderId) {
        return;
    }

    // create VBA VBO for rendering ...
    GLuint vertexArray;
    glGenVertexArrays(1, &vertexArray);
    glBindVertexArray(vertexArray);

    // store this vertex array id
    setVAO(vertArrayName, vertexArray);

    // Buffer for element indices
    GlBuffer elementBuffer;
    elementBuffer.bind(GL_ELEMENT_ARRAY_BUFFER);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementInds.size()*sizeof(unsigned int), &elementInds[0], GL_STATIC_DRAW);

    // Position attribute
    GlBuffer positionBuffer;
    positionBuffer.bind(GL_ARRAY_BUFFER);
    glBufferData(GL_ARRAY_BUFFER, m_verts.size() * sizeof(float), &m_verts[0], GL_STATIC_DRAW);
    GLuint positionAttribute = glGetAttribLocation(vertexShaderId, positionAttrName);
    glVertexAttribPointer(positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(float) * (3), (const GLvoid *) 0);
    glEnableVertexAttribArray(positionAttribute);

    // Normal attribute
    GlBuffer normalBuffer;
    normalBuffer.bind(GL_ARRAY_BUFFER);
    glBufferData(GL_ARRAY_BUFFER, m_normals.size() * sizeof(float), &m_normals[0], GL_STATIC_DRAW);
    GLuint normalAttribute = glGetAttribLocation(vertexShaderId, normAttrName);
    glVertexAttribPointer(normalAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(float) * (3), (const GLvoid *) 0);
    glEnableVertexAttribArray(normalAttribute);

    // Color attribute
    GlBuffer colorBuffer;
    colorBuffer.bind(GL_ARRAY_BUFFER);
    if (!m_colors.empty())
    {
        glBufferData(GL_ARRAY_BUFFER, m_colors.size() * sizeof(float), &m_colors[0], GL_STATIC_DRAW);
    }
    else
    {
        std::vector<float> tmp_colors(m_verts.size(), 1.0f);
        glBufferData(GL_ARRAY_BUFFER, tmp_colors.size() * sizeof(float), &tmp_colors[0], GL_STATIC_DRAW);
    }
    GLuint colorAttribute = glGetAttribLocation(vertexShaderId, colorAttrName);
    glVertexAttribPointer(colorAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(float) * (3), (const GLvoid *) 0);
    glEnableVertexAttribArray(colorAttribute);

    // Texture coordinate
    GLint texcoordsLocation = glGetAttribLocation(vertexShaderId, texCoordAttrName);
    GlBuffer texcoordBuffer;
    if (texcoordsLocation != -1)
    {
        if (m_texcoords.empty())
        {
            glDisableVertexAttribArray(texcoordsLocation);
            glVertexAttrib2f(texcoordsLocation, 0, 0);
        }
        else
        {
            texcoordBuffer.bind(GL_ARRAY_BUFFER);
            glBufferData(GL_ARRAY_BUFFER, m_texcoords.size() * sizeof(float), &m_texcoords[0], GL_STATIC_DRAW);
            glVertexAttribPointer(texcoordsLocation, 2, GL_FLOAT, GL_FALSE, sizeof(float) * (2), (const GLvoid *) 0);
            glEnableVertexAttribArray(texcoordsLocation);
        }
    }
    glBindVertexArray(0);
}