Example/GLSL Separate Program Basics: Difference between revisions

From OpenGL Wiki
Jump to navigation Jump to search
Example from separate shader compilation.
 
Flfirsla (talk | contribs)
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
Creating two separable programs, one with a vertex shader and one with a fragment shader.
Creating two separable programs, one with a vertex shader and one with a fragment shader.


<source lang="cpp">  
<source lang="cpp">
//Create two separable program objects from a  
// Create two separable program objects from a  
//single source string respectively (vertSrc and fragSrc)
// single source string respectively (vertSrc and fragSrc)
GLuint vertProg = glCreateShaderProgramv(GL_VERTEX_SHADER  , 1, &vertSrc);
GLuint vertProg = glCreateShaderProgramv(GL_VERTEX_SHADER  , 1, &vertSrc);
GLuint fragProg = glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, &fragSrc);
GLuint fragProg = glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, &fragSrc);


//CHECK FOR ERRORS HERE!.
// CHECK FOR ERRORS HERE!.


//Generate a program pipeline and bind the programs to their respective stages
// Generate a program pipeline and attach the programs to their respective stages
glGenProgramPipelines(1, &pipeline);
glGenProgramPipelines(1, &pipeline);
glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT  , vertProg);
glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT  , vertProg);
glUseProgramStages(pipeline, GL_FRAGMENT_SHADER_BIT, fragProg);
glUseProgramStages(pipeline, GL_FRAGMENT_SHADER_BIT, fragProg);


//Query and set any uniforms
// Query and set any uniforms
GLint colorLoc = glGetUniformLocation(fragProg, "Color");
GLint colorLoc = glGetUniformLocation(fragProg, "Color");
glProgramUniform4f(fragProg, colorLoc, 1.f, 0.f, 0.f, 1.f);   
glProgramUniform4f(fragProg, colorLoc, 1.f, 0.f, 0.f, 1.f);   
</source>
</source>
<noinclude>[[Category:Example Code]]</noinclude>
<noinclude>[[Category:Example Code]]</noinclude>

Latest revision as of 11:12, 3 January 2018

Creating two separable programs, one with a vertex shader and one with a fragment shader.

// Create two separable program objects from a 
// single source string respectively (vertSrc and fragSrc)
GLuint vertProg = glCreateShaderProgramv(GL_VERTEX_SHADER  , 1, &vertSrc);
GLuint fragProg = glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, &fragSrc);

// CHECK FOR ERRORS HERE!.

// Generate a program pipeline and attach the programs to their respective stages
glGenProgramPipelines(1, &pipeline);
glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT  , vertProg);
glUseProgramStages(pipeline, GL_FRAGMENT_SHADER_BIT, fragProg);

// Query and set any uniforms
GLint colorLoc = glGetUniformLocation(fragProg, "Color");
glProgramUniform4f(fragProg, colorLoc, 1.f, 0.f, 0.f, 1.f);