Esempio n. 1
0
// create shader
GLuint Shader::create_shader(const std::string &text, unsigned int type)
{
    GLuint shader = glCreateShader(type);

    if (0 == shader) {
        std::cerr << "Error: Shader creation failed" << std::endl;
    }

    // shader name strings and length
    const GLchar *shader_sources[1];
    GLint shader_sources_lengh[1];

    shader_sources[0] = text.c_str();
    shader_sources_lengh[0] = text.length();

    // compile the shader
    glShaderSource(shader, 1, shader_sources, shader_sources_lengh);
    glCompileShader(shader);

    // check for compile errors
    check_shader_error(shader, GL_COMPILE_STATUS, false, "Error: Shader "
            "compilation failed: ");

    return shader;
}
Esempio n. 2
0
Shader::Shader(const std::string &fname)
{
    // create new shader program
    m_program = glCreateProgram();

    // start creating shaders, could refer by enum if class is matching
    // two file extensions can be anything, just a convention
    m_shaders[0] = create_shader(load_shader(fname + ".vs"), GL_VERTEX_SHADER);
    m_shaders[1] = create_shader(load_shader(fname + ".fs"), GL_FRAGMENT_SHADER);

    // add all the loaded shaders to program
    for (unsigned int i = 0; i < NUM_SHADERS; i++) {
        glAttachShader(m_program, m_shaders[i]);
    }

    // point to attribute locations
    // this is the position field in our Vertex, and that's what we bind to
    glBindAttribLocation(m_program, 0, "pos");

    // also bind the position of the texture coordinate as a shader attribute
    glBindAttribLocation(m_program, 1, "tex");

    // link the shader to program and check for errors
    glLinkProgram(m_program);
    check_shader_error(m_program, GL_LINK_STATUS, true,
                       "Error: Program linking failed: ");

    // make sure program is valid after linking
    glValidateProgram(m_program);
    check_shader_error(m_program, GL_LINK_STATUS, true,
                       "Error: Program is invalid: ");

    // figure out what handle refers to our transformation uniform, since we
    // called it transform, that's what we look for here
    m_uniforms[TRANSFORM_U] = glGetUniformLocation(m_program, "transform");
}
Esempio n. 3
0
GLuint		load_shader(GLenum type, char *filename)
{
	GLuint			shader;
	GLint			compile_status;
	char			*src;

	compile_status = GL_TRUE;
	shader = glCreateShader(type);
	if (glIsShader(shader) == 0)
		exit(-1);
	src = load_file(filename);
	if (src == NULL)
		exit(-1);
	glShaderSource(shader, 1, (const char*const*)&src, NULL);
	glCompileShader(shader);
	free(src);
	if (check_shader_error(shader, compile_status, filename) == 1)
	{
		glDeleteShader(shader);
		exit(-1);
	}
	return (shader);
}