Esempio n. 1
0
/*
 * シェーダプログラムのロード
 */
int load_shader(char *vtxShdName, char *frgShdName, GLuint *lpProg)
{
	GLuint vtxShader;
	GLuint frgShader;
	GLuint prog;
	GLint linked;
    
	/* シェーダオブジェクトの作成 */
	vtxShader = glCreateShader(GL_VERTEX_SHADER);
	frgShader = glCreateShader(GL_FRAGMENT_SHADER);
    
	/* バーテックスシェーダのロードとコンパイル */
	if (compile_shader(vtxShader, vtxShdName) < 0)
	{
		return -1;
	}
    
	/* フラグメントシェーダのロードとコンパイル */
	if (compile_shader(frgShader, frgShdName) < 0)
	{
		return -1;
	}
    
	/* プログラムオブジェクトの作成 */
	prog = glCreateProgram();
    
	/* シェーダオブジェクトのシェーダプログラムへの登録 */
	glAttachShader(prog, vtxShader);
	glAttachShader(prog, frgShader);
    
	/* シェーダオブジェクトの削除 */
	glDeleteShader(vtxShader);
	glDeleteShader(frgShader);
    
	/* シェーダプログラムのリンク */
	glLinkProgram(prog);
	glGetProgramiv(prog, GL_LINK_STATUS, &linked);
	print_program_log(prog);
	if (linked == GL_FALSE)
	{
        std::cout << "Link error of " 
        << vtxShdName <<  " & " << frgShdName << " !!" << std::endl;
		return -1;
	} else {
        std::cout << "Link successful: " << vtxShdName <<  " & " << frgShdName << std::endl;
    }
    
	*lpProg = prog;
    
	return 0;
}
Esempio n. 2
0
 bool ShaderProgram::bind()
 {
     if (_program_id != Config::current_program_id()) {
         glUseProgram(_program_id);
         
         GLenum error = glGetError();
         if( error != GL_NO_ERROR )
         {
             printf( "Error binding shader! %s\n", gluErrorString( error ) );
             print_program_log( _program_id );
             return false;
         }
         Config::current_program_id(_program_id);
     }
     
     
     return true;
 }
Esempio n. 3
0
GLuint init_shader_str(const char *ray_v_src, const char *ray_f_src, const char *ray_g_src, lua_State *L)
{
	int i;

	const char *ray_v_src_alias = ray_v_src;
	const char *ray_g_src_alias = ray_g_src;
	const char *ray_f_src_alias = ray_f_src;

	GLuint ray_v = glCreateShader(GL_VERTEX_SHADER);
	GLuint ray_g = (ray_g_src == NULL ? 0 : glCreateShader(GL_GEOMETRY_SHADER));
	GLuint ray_f = glCreateShader(GL_FRAGMENT_SHADER);

	glShaderSource(ray_v, 1, &ray_v_src_alias, NULL);
	if(ray_g_src != NULL) glShaderSource(ray_g, 1, &ray_g_src_alias, NULL);
	glShaderSource(ray_f, 1, &ray_f_src_alias, NULL);

	glCompileShader(ray_v);
	printf("===   VERTEX SHADER ===\n");
	print_shader_log(ray_v);

	if(ray_g_src != NULL)
	{
		glCompileShader(ray_g);
		printf("=== GEOMETRY SHADER ===\n");
		print_shader_log(ray_g);
	}

	glCompileShader(ray_f);
	printf("=== FRAGMENT SHADER ===\n");
	print_shader_log(ray_f);

	GLuint out_shader = glCreateProgram();
	printf("Attaching shaders\n");
	glAttachShader(out_shader, ray_v);
	if(ray_g_src != NULL) glAttachShader(out_shader, ray_g);
	glAttachShader(out_shader, ray_f);

	// TODO: outsource this to a function
	glGetError();
	printf("Binding inputs\n");
	lua_len(L, 2); int len_input = lua_tointeger(L, -1); lua_pop(L, 1);
	for(i = 0; i < len_input; i++)
	{
		lua_geti(L, 2, i+1);
		glBindAttribLocation(out_shader, i, lua_tostring(L, -1));
		lua_pop(L, 1);
	}

	if(!context_is_compat)
	{
		printf("Binding outputs\n");
		lua_len(L, 3); int len_output = lua_tointeger(L, -1); lua_pop(L, 1);
		for(i = 0; i < len_output; i++)
		{
			lua_geti(L, 3, i+1);
			glBindFragDataLocation(out_shader, i, lua_tostring(L, -1));
			lua_pop(L, 1);
		}
	}

	printf("%i\n", glGetError());

	printf("Linking! This is the part where your computer dies\n");
	glLinkProgram(out_shader);

	printf("Getting results\n");
	printf("=== OVERALL PROGRAM ===\n");
	print_program_log(out_shader);

	GLint link_status;
	glGetProgramiv(out_shader, GL_LINK_STATUS, &link_status);
	printf("Link status: %i\n", link_status);

	if(link_status == GL_TRUE)
	{
		return out_shader;

	} else {
		glDeleteProgram(out_shader);
		glDeleteShader(ray_v);
		if(ray_g_src != NULL) glDeleteShader(ray_g);
		glDeleteShader(ray_f);

		return 0;
	}
}
bool Primitive2DProgram::load_program()
{

    //Generate program
    _program_id = glCreateProgram();

    std::string vertex_string = (std::string)("#version 120\n") +
                                "uniform mat4 projection_matrix;\n" +
                                "uniform mat4 model_view_matrix;\n" +
                                "attribute vec2 vertex_position;\n" +
                                "attribute vec4 PolygonColor;" +
                                "varying vec4 primitive_color;" +
                                "void main() {\n" +
                                "primitive_color = PolygonColor;" +
                                "gl_Position = projection_matrix * model_view_matrix * vec4( vertex_position.x, vertex_position.y, 0.0, 1.0 );\n" +
                                "}\n";

    //Load vertex shader
    GLuint vertexShader = load_shader_from_string(vertex_string, GL_VERTEX_SHADER);

    //Check for errors
    if( vertexShader == 0 )
    {
        glDeleteProgram( _program_id );
        _program_id = 0;
        return false;
    }

    //Attach vertex shader to program
    glAttachShader( _program_id, vertexShader );


    std::string fragment_shader_string = (std::string)("#version 120\n") +
                                         "varying vec4 primitive_color;" +
                                         "void main() {\n" +
                                         "gl_FragColor = primitive_color;\n" +
                                         "}\n";

    //Create fragment shader
    GLuint fragmentShader = load_shader_from_string(fragment_shader_string, GL_FRAGMENT_SHADER);

    //Check for errors
    if( fragmentShader == 0 )
    {
        glDeleteProgram( _program_id );
        _program_id = 0;
        return false;
    }

    //Attach fragment shader to program
    glAttachShader( _program_id, fragmentShader );

    //Link program
    glLinkProgram( _program_id );

    //Check for errors
    GLint programSuccess = GL_TRUE;
    glGetProgramiv( _program_id, GL_LINK_STATUS, &programSuccess );
    if( programSuccess != GL_TRUE )
    {
        printf( "Error linking program %d!\n", _program_id );
        print_program_log( _program_id );
        glDeleteProgram( _program_id );
        _program_id = 0;
        return false;
    }

    set_attribute_location(_vertex_position_2d_location, "vertex_position");
    set_attribute_location(_color_location, "PolygonColor");

    set_uniform_location(_projection_matrix_location, "projection_matrix");
    set_uniform_location(_model_view_matrix_location, "model_view_matrix");

    return true;
}
    bool Texture2DProgram::load_program()
    {
        //Generate program
        _program_id = glCreateProgram();

        std::string vertex_shader_string = (std::string)(
                
            "uniform mat4 projection_matrix;\n") +
            "uniform mat4 model_view_matrix;\n" +
                
            "#if __VERSION__ >= 130\n" +
                "in vec2 vertex_position;\n" +
                "in vec2 texture_coordinate;\n" +
                "out vec2 texCoord;\n" +
            "#else\n" +
                "attribute vec2 vertex_position;\n" +
                "attribute vec2 texture_coordinate;\n" +
                "varying vec2 texCoord;\n" +
            "#endif\n" +
        
                "void main() {\n" +
                "texCoord = texture_coordinate;\n" +
                "gl_Position = projection_matrix * model_view_matrix * vec4( vertex_position.x, vertex_position.y, 0.0, 1.0 );\n" +
                "}\n";
        
        //Load vertex shader
        GLuint vertex_shader = load_shader_from_string( vertex_shader_string, GL_VERTEX_SHADER );
        
        //Check for errors
        if( vertex_shader == 0 )
        {
            glDeleteProgram( _program_id );
            _program_id = 0;
            return false;
        }
        
        //Attach vertex shader to program
        glAttachShader( _program_id, vertex_shader );
        
        std::string fragment_shader_string = (std::string)(
                                                      
            "uniform vec4 texture_color;\n")+
            "uniform sampler2D texture_unit;\n" +
            "#if __VERSION__ >= 130\n" +
                "in vec2 texCoord;\n" +
                "out vec4 fragColor;\n" +
            "#else\n"
                "varying vec2 texCoord;\n" +
            "#endif\n"
                
            "void main() { \n" +
                "gl_FragColor = texture2D(texture_unit,texCoord) * texture_color;\n" +
            "}\n";
        
        std::string test_shader = (std::string)(
                                                           
        "uniform vec4 texture_color;\n")+
        "uniform sampler2D texture_unit;\n" +
        
        "const float RADIUS = .75;\n" +
        "const vec2 resolution = vec2(800,600);\n" +
        "const float SOFTNESS = 0.45;\n" +
        "const vec3 SEPIA = vec3(1.2, 1.0, 0.8);\n" +
        
        "#if __VERSION__ >= 130\n" +
        "in vec2 texCoord;\n" +
        "out vec4 fragColor;\n" +
        "#else\n"
        "varying vec2 texCoord;\n" +
        "#endif\n"
        
        "void main() { \n" +
            "vec4 texColor = texture2D(texture_unit,texCoord);\n" +
            "vec2 position = (gl_FragCoord.xy / resolution.xy) - vec2(0.5);\n" +
            "float len = length(position);\n" +
            
            "float vignette = smoothstep(RADIUS, RADIUS-SOFTNESS, len);\n"
            "texColor.rgb = mix(texColor.rgb, texColor.rgb * vignette, 0.5);\n" +
            "float gray = dot(texColor.rgb, vec3(0.299, 0.587, 0.114));\n" +
            //"vec3 sepiaColor = vec3(gray) * SEPIA;\n" +
            //"texColor.rgb = mix(texColor.rgb, sepiaColor, 0.85);\n" +
        
            "gl_FragColor = texColor * texture_color;\n" +
        "}\n";
        //Create fragment shader
        GLuint fragment_shader = load_shader_from_string( test_shader, GL_FRAGMENT_SHADER );
        
        //Check for errors
        if( fragment_shader == 0 )
        {
            glDeleteProgram( _program_id );
            _program_id = 0;
            return false;
        }
        
        //Attach fragment shader to program
        glAttachShader( _program_id, fragment_shader );
        
        //Link program
        glLinkProgram( _program_id );
        
        //Check for errors
        GLint programSuccess = GL_TRUE;
        glGetProgramiv( _program_id, GL_LINK_STATUS, &programSuccess );
        
        if( programSuccess != GL_TRUE )
        {
            printf( "Error linking program %d!\n", _program_id );
            print_program_log( _program_id );
            glDeleteProgram( _program_id );
            _program_id = 0;
            return false;
        }
        
        // set attribute locations
        set_attribute_location(_vertex_position_2d_location, "vertex_position");
        set_attribute_location(_texture_coordinate_location, "texture_coordinate");
        
        // set uniform locations
        set_uniform_location(_texture_color_location, "texture_color");
        set_uniform_location(_texture_unit_location, "texture_unit");
        set_uniform_location(_projection_matrix_location, "projection_matrix");
        set_uniform_location(_model_view_matrix_location, "model_view_matrix");
        
        return true;
    }
Esempio n. 6
0
void load_shader(Shader *s, char *vert_filename, char *frag_filename)
{
    assert(s != NULL);
    assert(vert_filename != NULL);
    assert(frag_filename != NULL);

    strncpy(s->vert_filename, (vert_filename), MAX_FILENAME_LEN);
    strncpy(s->frag_filename, (frag_filename), MAX_FILENAME_LEN);

    s->vert_program_id = glCreateShader(GL_VERTEX_SHADER);
    if (s->vert_program_id == 0) {
        set_error_msg(&s->status, "Could not create vert shader.");
        return;
    }

    s->frag_program_id = glCreateShader(GL_FRAGMENT_SHADER);
    if (s->frag_program_id == 0) {
        set_error_msg(&s->status, "Could not create frag shader.");
        return;
    }

    char *vs = NULL;
    int vs_size;
    vs = text_file_read(s->vert_filename, &vs_size);
    if (vs == NULL) {
        set_error_msg(&s->status, "Could not read vert shader '%s'.",
            s->vert_filename);
        return;
    }

    char *fs = NULL;
    int fs_size;
    fs = text_file_read(s->frag_filename, &fs_size);
    if (fs == NULL) {
        set_error_msg(&s->status, "Could not read frag shader '%s'.",
            s->frag_filename);
        return;
    }

    GLenum glerror;
    GLint result;
    const char * vv = vs;
    const char * ff = fs;

    glShaderSource(s->vert_program_id, 1, &vv, NULL);
    if ((glerror = glGetError()) != GL_NO_ERROR) {
        set_error_msg(&s->status, "Could set vert shader source. (%d)",
            glerror);
        return;
    }

    glShaderSource(s->frag_program_id, 1, &ff, NULL);
    if ((glerror = glGetError()) != GL_NO_ERROR) {
        set_error_msg(&s->status, "Could set frag shader source. (%d)",
            glerror);
        return;
    }

    glCompileShader(s->vert_program_id);
    glGetShaderiv(s->vert_program_id, GL_COMPILE_STATUS, &result);
    if (result != GL_TRUE) {
        set_error_msg(&s->status, "Could not compile vert shader.");
        return;
    }

    glCompileShader(s->frag_program_id);
    glGetShaderiv(s->frag_program_id, GL_COMPILE_STATUS, &result);
    if (result != GL_TRUE) {
        set_error_msg(&s->status, "Could not compile frag shader.");
        return;
    }

    free(vs); free(fs);

    print_shader_log(s->vert_program_id);
    print_shader_log(s->frag_program_id);

    s->program_id = glCreateProgram();
    if (s->program_id == 0) {
        set_error_msg(&s->status, "Could not create program.");
        return;
    }

    glAttachShader(s->program_id, s->vert_program_id);
    if ((glerror = glGetError()) != GL_NO_ERROR) {
        set_error_msg(&s->status, "Could not attach vert shader. (%d)",
            glerror);
        return;
    }

    glAttachShader(s->program_id, s->frag_program_id);
    if ((glerror = glGetError()) != GL_NO_ERROR) {
        set_error_msg(&s->status, "Could not attach frag shader. (%d)",
            glerror);
        return;
    }

    glBindFragDataLocation(s->program_id, 0, "FracColor");
    if ((glerror = glGetError()) != GL_NO_ERROR) {
        set_error_msg(&s->status, "Could not bind frag data location. (%d)",
            glerror);
        return;
    }

    glLinkProgram(s->program_id);
    glGetProgramiv(s->program_id, GL_LINK_STATUS, &result);
    if ((glerror = glGetError()) != GL_NO_ERROR || result == GL_FALSE) {
        set_error_msg(&s->status, "Could not bind frag data location. (%d,%d)",
            glerror, result);
        return;
    }

    print_program_log(s->program_id);

    s->vertex_loc = glGetAttribLocation(s->program_id, "MCvertex");
    s->normal_loc = glGetAttribLocation(s->program_id, "MCnormal");
    s->tangent_loc = glGetAttribLocation(s->program_id, "MCtangent");
    s->tex_coords_loc = glGetAttribLocation(s->program_id, "TexCoord0");

    s->proj_matrix_loc = glGetUniformLocation(s->program_id, "MVPMatrix");
    s->view_matrix_loc = glGetUniformLocation(s->program_id, "MVMatrix");
    s->rot_matrix_loc = glGetUniformLocation(s->program_id, "RotMatrix");
}
Esempio n. 7
0
int init_sdlgl(void)
{
	int status = -1;
	GLint success;

	SDL_INIT(SDL_INIT_VIDEO);
	SDL_GL_SETATTRIBUTE(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SETATTRIBUTE(SDL_GL_CONTEXT_MINOR_VERSION, 1);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

	window = SDL_CREATEWINDOW(title, 50, 50, 800, 600, SDL_WINDOW_OPENGL);
	gl_context = SDL_GL_CREATECONTEXT(window);
	glewExperimental = GL_TRUE;
	GLEWINIT();
	SDL_GL_SETSWAPINTERVAL(1);

	program = glCreateProgram();

	GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
	const GLchar *vertex_shader_src[] = {
		"#version 140\n"
		"in vec2 LVertexPos2D;\n"
		"void main() {\n"
		"	gl_Position = vec4(LVertexPos2D.x, LVertexPos2D.y, 0, 1);\n"
		"}\n"
	};
	glShaderSource(vertex_shader, 1, vertex_shader_src, NULL);
	glCompileShader(vertex_shader);
	success = GL_FALSE;
	glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
	if (success != GL_TRUE) {
		print_shader_log(vertex_shader);
		CHECK1(0, "glCompileShader failed: vertex_shader");
	}
	glAttachShader(program, vertex_shader);

	GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
	const GLchar *fragment_shader_src[] = {
		"#version 140\n"
		"out vec4 LFragment;\n"
		"void main() {\n"
		"	LFragment = vec4(1.0, 0.8, 0.6, 1.0);\n"
		"}"
	};
	glShaderSource(fragment_shader, 1, fragment_shader_src, NULL);
	glCompileShader(fragment_shader);
	success = GL_FALSE;
	glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
	if (success != GL_TRUE) {
		print_shader_log(fragment_shader);
		CHECK1(0, "glCompileShader failed: fragment_shader");
	}
	glAttachShader(program, fragment_shader);

	glLinkProgram(program);
	glGetProgramiv(program, GL_LINK_STATUS, &success);
	if (success != GL_TRUE) {
		print_program_log(program);
		CHECK1(0, "glLinkProgram failed");
	}

	vertex_pos_2d_loc = glGetAttribLocation(program, "LVertexPos2D");
	CHECK1(vertex_pos_2d_loc != -1, "glGetAttribLocation failed: LVertexPos2D");

	glClearColor(0.5f, 0.f, 0.f, 1.f);

	GLfloat vertex_data[] = {
		0, -.5,
		.5, 0,
		0, .5,
		-.5, 0
	};

	GLuint index_data[] = { 0, 1, 2, 3 };

	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, 2 * 4 * sizeof(GLfloat), vertex_data, GL_STATIC_DRAW);

	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), index_data, GL_STATIC_DRAW);

	// TODO On OS X make sure you bind 0 to the draw framebuffer before swapping the window, otherwise nothing will happen.  ???

	status = 0;
error:
	return status;
}