GLuint GlUtil::CreateProgram(const char* vertex_source, const char* fragment_source)
{
	LOGI("%s\n", vertex_source);
	GLuint vertexShader = 0;
	GLuint fragment_shader = 0;

	if (vertex_source)
	{
		vertexShader = LoadShader(GL_VERTEX_SHADER, vertex_source);
		if (!vertexShader) {
			return 0;
		}
	}

	if (fragment_source)
	{
		fragment_shader = LoadShader(GL_FRAGMENT_SHADER, fragment_source);
		if (!fragment_shader) {
			return 0;
		}
	}

	GLuint program = glCreateProgram();
	if (program) {
		glAttachShader(program, vertexShader);
		CheckGlError("glAttachShader");
		if (fragment_shader)
		{
			glAttachShader(program, fragment_shader);
			CheckGlError("glAttachShader");
		}
		LinkProgram(program);
	}
	return program;
}
Beispiel #2
0
static GLuint LoadProgram ( const char *vertShaderSrc, const char *fragShaderSrc )
{
  GLuint vertexShader;
  GLuint fragmentShader;
  GLuint programObject;
  GLint linked;

  // Load the vertex/fragment shaders
  vertexShader = LoadShader ( GL_VERTEX_SHADER, vertShaderSrc );
  if ( vertexShader == 0 )
    return 0;

  fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fragShaderSrc );
  if ( fragmentShader == 0 )
    {
      glDeleteShader( vertexShader );
      return 0;
    }

  // Create the program object
  programObject = glCreateProgram ( );

  if ( programObject == 0 )
    return 0;

  glAttachShader ( programObject, vertexShader );
  glAttachShader ( programObject, fragmentShader );

  // Link the program
  glLinkProgram ( programObject );

  // Check the link status
  glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );

  if ( !linked )
    {
      GLint infoLen = 0;
      glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );

      if ( infoLen > 1 )
	{
	  char* infoLog = malloc (sizeof(char) * infoLen );

	  glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
	  fprintf (stderr, "Error linking program:\n%s\n", infoLog );

	  free ( infoLog );
	}

      glDeleteProgram ( programObject );
      return 0;
    }

  // Free up no longer needed shader resources
  glDeleteShader ( vertexShader );
  glDeleteShader ( fragmentShader );

  return programObject;
}
///
// Initialize the shader and program object
//
int Init()
{
   char vShaderStr[] =
       "attribute vec4 vPosition;   \n"
       "void main()                 \n"
       "{                           \n"
       "   gl_Position = vPosition; \n"
       "}                           \n";
   char fShaderStr[] =
       "precision mediump float;                   \n"
       "void main()                                \n"
       "{                                          \n"
#ifndef __FIXED__
       " gl_FragColor = vec4(0.2, 0.5, 0.1, 1.0); \n"
#else
       " gl_FragColor = vec4(0.4, 0.3, 0.7, 1.0); \n"
#endif
       "}                                          \n";
   GLuint vertexShader;
   GLuint fragmentShader;
   GLuint programObject;
   GLint linked;
  // Load the vertex/fragment shaders
  vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr);
  fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);
  // Create the program object
  programObject = glCreateProgram();
  if(programObject == 0)
     return -1;
  glAttachShader(programObject, vertexShader);
  glAttachShader(programObject, fragmentShader);
  // Bind vPosition to attribute 0
  glBindAttribLocation(programObject, 0, "vPosition");
  // Link the program
  glLinkProgram(programObject);
  // Check the link status
  glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
  if(!linked)
  {
     GLint infoLen = 0;
     glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
     if(infoLen > 1)
     {
        char* infoLog = (char*)malloc(sizeof(char) * infoLen);
        glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
        printf("Error linking program:\n%s\n", infoLog);
        free(infoLog);
     }
     glDeleteProgram(programObject);
     return -1;
  }
  // Store the program object
#ifndef __FIXED__
  glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
#else
  glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
#endif
  return programObject;
}
// load all shaders
void ShaderManager::LoadShaders()
{
    LoadShader(BasicShader, "../common_res/Basic.vsh", "../common_res/Basic.fsh");
    LoadShader(BasicShaderInstanced, "../common_res/BasicInstanced.vsh", "../common_res/Basic.fsh", "../common_res/BasicInstanced.gsh");
    LoadShader(OVRFrustumShader, "../common_res/OVRFrustum.vsh", "../common_res/OVRFrustum.fsh");
    LoadShader(FontShader, "../common_res/Font.vsh", "../common_res/Font.fsh");
    LoadShader(BasicShaderNoTex, "../common_res/BasicNoTex.vsh", "../common_res/BasicNoTex.fsh");
}
void VulkanTexturedQuad::CreatePipelineStateObject()
{
    vertexShader_ = LoadShader(device_, BasicVertexShader, sizeof(BasicVertexShader));
    fragmentShader_ = LoadShader(device_, TexturedFragmentShader, sizeof(TexturedFragmentShader));

    pipeline_ = CreatePipeline(device_, renderPass_, pipelineLayout_,
        vertexShader_, fragmentShader_);
}
Beispiel #6
0
void InitializeProgram(void) {
    std::cout << "OpenGL initialization function\n" << std::endl;
    std::vector<GLuint> shaders;
    shaders.push_back(LoadShader(GL_VERTEX_SHADER, "pass_along.vert"));
    shaders.push_back(LoadShader(GL_FRAGMENT_SHADER, "uniform_color.frag"));
    shader_program = CreateProgram(shaders);
    std::for_each(shaders.begin(), shaders.end(), glDeleteShader);
}
Beispiel #7
0
bool cShader::BuildShader(const std::string& vs_filename, const std::string& ps_filename)
{
	GLuint vs = LoadShader(vs_filename, GL_VERTEX_SHADER);
	GLuint ps = LoadShader(ps_filename, GL_FRAGMENT_SHADER);
	mProg = CreateShader(vs, ps);

	return mProg != -1;
}
Beispiel #8
0
int main( int argc, char* argv[] )
{
    g_InitialCameraPosition = glm::vec3( 0, 0, 10 );
    g_Camera.SetPosition( g_InitialCameraPosition );
    g_Camera.SetRotation( g_InitialCameraRotation );

    InitGL(argc, argv);
    InitGLEW();

    // Load some shaders.
    GLuint vertexShader = LoadShader( GL_VERTEX_SHADER, "../data/shaders/simpleShader.vert" );
    GLuint fragmentShader = LoadShader( GL_FRAGMENT_SHADER, "../data/shaders/simpleShader.frag" );

    std::vector<GLuint> shaders;
    shaders.push_back(vertexShader);
    shaders.push_back(fragmentShader);

    // Create the shader program.
    g_ShaderProgram = CreateShaderProgram( shaders );
    assert( g_ShaderProgram != 0 );

    GLint positionAtribID = glGetAttribLocation( g_ShaderProgram, "in_position" );
    GLint colorAtribID = glGetAttribLocation( g_ShaderProgram, "in_color" );
    g_uniformMVP = glGetUniformLocation( g_ShaderProgram, "MVP" );

    // Create a VAO for the cube.
    glGenVertexArrays( 1, &g_vaoCube );
    glBindVertexArray( g_vaoCube );

    GLuint vertexBuffer, indexBuffer;
    glGenBuffers( 1, &vertexBuffer );
    glGenBuffers( 1, &indexBuffer );

    glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof(g_Vertices), g_Vertices, GL_STATIC_DRAW );

    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBuffer );
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(g_Indices), g_Indices, GL_STATIC_DRAW );

    glVertexAttribPointer( positionAtribID, 3, GL_FLOAT, false, sizeof(VertexXYZColor),
                            reinterpret_cast<const GLvoid*>(offsetof(VertexXYZColor,m_Pos)) );
    glEnableVertexAttribArray( positionAtribID );

    glVertexAttribPointer( colorAtribID, 3, GL_FLOAT, false, sizeof(VertexXYZColor),
                            reinterpret_cast<const GLvoid*>(offsetof(VertexXYZColor,m_Color)));
    glEnableVertexAttribArray( colorAtribID );

    // Make sure we disable and unbind everything to prevent rendering issues later.
    glBindVertexArray( 0 );
    glBindBuffer( GL_ARRAY_BUFFER, 0 );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    glDisableVertexAttribArray( positionAtribID );
    glDisableVertexAttribArray( colorAtribID );

    glutMainLoop();
}
bool APP_SpotRotate::Start()
{
    m_appName = "Spot rotate";

    m_time = 0.0f;

    Gizmos::create();

    GameCam = new Camera();

    // create shaders
    const char* vsSource = nullptr;
    std::string vsResult = LoadShader("./assets/shaders/RotateVertShader.vert");
    vsSource = vsResult.c_str();

    const char* fsSource = nullptr;
    std::string fsResult = LoadShader("./assets/shaders/RotateFragShader.frag");
    fsSource = fsResult.c_str();

    int success = GL_FALSE;
    unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    //glBindAttributeLocation

    glShaderSource(vertexShader, 1, (const char**)&vsSource, 0);
    glCompileShader(vertexShader);
    glShaderSource(fragmentShader, 1, (const char**)&fsSource, 0);
    glCompileShader(fragmentShader);
    m_programID = glCreateProgram();
    glAttachShader(m_programID, vertexShader);
    glAttachShader(m_programID, fragmentShader);
    glLinkProgram(m_programID);

    glGetProgramiv(m_programID, GL_LINK_STATUS, &success);
    if (success == GL_FALSE) {
        int infoLogLength = 0;
        glGetProgramiv(m_programID, GL_INFO_LOG_LENGTH, &infoLogLength);
        char* infoLog = new char[infoLogLength];
        glGetProgramInfoLog(m_programID, infoLogLength, 0, infoLog);
        printf("Error: Failed to link shader program!\n");
        printf("%s\n", infoLog);
        delete[] infoLog;
    }
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);

    // Generate our GL Buffers
    glGenBuffers(1, &m_VBO);
    glGenBuffers(1, &m_IBO);
    glGenVertexArrays(1, &m_VAO); //generate a VertexArrayObject

    isLoaded = true;

    return true; //not being used in this lesson
}
Beispiel #10
0
void initRender() {

    // Create 2 shader programs
    Shader[0] = glCreateShader(GL_VERTEX_SHADER);
    Shader[1] = glCreateShader(GL_FRAGMENT_SHADER);

    LoadShader((char *)VertexShader, Shader[0]);
    LoadShader((char *)FragmentShader, Shader[1]);

    // Create the prorgam and attach the shaders & attributes
    Program   = glCreateProgram();

    glAttachShader(Program, Shader[0]);
    glAttachShader(Program, Shader[1]);

    glBindAttribLocation(Program, 0, "Position");
    glBindAttribLocation(Program, 1, "Normal");
    glBindAttribLocation(Program, 2, "Texture");

    // Link
    glLinkProgram(Program);

    // Validate our work thus far
    int ShaderStatus;
    glGetProgramiv(Program, GL_LINK_STATUS, &ShaderStatus);

    if (ShaderStatus != GL_TRUE) {
        LOGE("Error: Failed to link GLSL program\n");
        int Len = 1024;
        char Error[1024];
        glGetProgramInfoLog(Program, 1024, &Len, Error);
        LOGE("%s",Error);
        exit(-1);
    }

    glValidateProgram(Program);
	checkGlError("glValidateProgram");

    // Enable the program
    glUseProgram                (Program);

    // Retrieve our uniforms
    iProj   = glGetUniformLocation(Program, "Proj");
	iModel  = glGetUniformLocation(Program, "Model");
	iCamera = glGetUniformLocation(Program, "Camera");

    // Basic GL setup
	glClearColor    (0.15, 0.15, 0.15, 1.0);
    glEnable        (GL_CULL_FACE);
    glEnable 		(GL_DEPTH_TEST);
    glCullFace      (GL_BACK);


}
void InitializeProgram()
{
    std::vector<GLuint> shaderList;

    shaderList.push_back(LoadShader(GL_VERTEX_SHADER, "FragPosition.vert"));
    shaderList.push_back(LoadShader(GL_FRAGMENT_SHADER, "FragPosition.frag"));

    theProgram = CreateProgram(shaderList);

    std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
}
Beispiel #12
0
void InitializeProgram()
{
    std::vector<GLuint> shaderList;

    shaderList.push_back(LoadShader(GL_VERTEX_SHADER, "OrthoWithOffset.vert"));
    shaderList.push_back(LoadShader(GL_FRAGMENT_SHADER, "StandardColors.frag"));

    theProgram = CreateProgram(shaderList);
    offsetUniform = glGetUniformLocation(theProgram, "offset");
    
    std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
}
//--------------------------- Constructor
cShader::cShader(char* Filename){
	//printf("------------- Loading Shader. /n");
//Temp Variables
	GLint iPass = 0;
	GLchar cErrorLog[1024] = { 0 };
//create 2 temp chars, adding the .vert and .frag extensions
//vert
	std::string sVert;
	sVert.append(Filename);
	sVert.append(".vert");
	char* vertShad = Filename;
	vertShad = LoadShaderFile(sVert.c_str());
//frag
	std::string sFrag;
	sFrag.append(Filename);
	sFrag.append(".frag");
	char* fragShad = Filename;
	fragShad = LoadShaderFile(sFrag.c_str());
//Create the shader Program
	GLuint ShaderProgram = glCreateProgram();
//check it opened
	if (ShaderProgram == 0){
		printf("Error creating ShaderProgram \n '%i' \n",ShaderProgram);
		iShaderID = -1;
		return;
	}
//apply the shader
	LoadShader(ShaderProgram,GL_VERTEX_SHADER,vertShad);
	LoadShader(ShaderProgram,GL_FRAGMENT_SHADER,fragShad);
//Link the Shader
	glLinkProgram(ShaderProgram);
	glGetProgramiv(ShaderProgram,GL_LINK_STATUS,&iPass);
//Check it is linked
	if (iPass == 0){
		glGetProgramInfoLog(ShaderProgram,sizeof(cErrorLog),NULL,cErrorLog);
		printf("Error while linking ShaderProgram \n '%s' \n",cErrorLog);
		iShaderID = -1;
		return;
	}
//Validate the Shader
	glValidateProgram(ShaderProgram);
	glGetProgramiv(ShaderProgram,GL_LINK_STATUS,&iPass);
//Check it is Valid
	if (iPass == 0){
		glGetProgramInfoLog(ShaderProgram,sizeof(cErrorLog),NULL,cErrorLog);
		printf("Error while validating ShaderProgram \n '%s' \n",cErrorLog);
		iShaderID = -1;
		return;
	}
	iShaderID = ShaderProgram;
	//printf("------------- Shader Loading finished! \n \n");	
}
		GLuint Shader::load()
		{
			GLuint program = glCreateProgram();
			GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
			GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);

			std::string vertSourceString = LoadShader(m_Vertpath);
			std::string fragSourceString = LoadShader(m_Fragpath);

			const char* vertSource = vertSourceString.c_str();
			const char* fragSource = fragSourceString.c_str();

			glShaderSource(vertex, 1, &vertSource, NULL);
			glCompileShader(vertex);

			GLint result;
			glGetShaderiv(vertex, GL_COMPILE_STATUS, &result);

			if (result == GL_FALSE)
			{
				GLint length;
				glGetShaderiv(vertex, GL_INFO_LOG_LENGTH, &length);
				std::vector<char> error(length);
				glGetShaderInfoLog(vertex, length, &length, &error[0]);
				std::cout <<"Vertex Failed : "  << &error[0] << std::endl;
				glDeleteShader(vertex);
				return 0;
			}
			glShaderSource(fragment, 1, &fragSource, NULL);
			glCompileShader(fragment);
			glGetShaderiv(fragment, GL_COMPILE_STATUS, &result);
			if (result == GL_FALSE)
			{
				GLint length;
				glGetShaderiv(fragment, GL_INFO_LOG_LENGTH, &length);
				std::vector<char> error(length);
				glGetShaderInfoLog(fragment, length, &length, &error[0]);
				std::cout << "fragment f****d " << &error[0] << std::endl;
				glDeleteShader(fragment);
				return 0;
			}
			glAttachShader(program, vertex);
			glAttachShader(program, fragment);

			glLinkProgram(program);
			glValidateProgram(program);

			glDeleteShader(vertex);
			glDeleteShader(fragment);

			return program;
		}
void VulkanQuad::CreatePipelineStateObject ()
{
    vertexShader_ = LoadShader (device_, BasicVertexShader, sizeof (BasicVertexShader));
    fragmentShader_ = LoadShader (device_, BasicFragmentShader, sizeof (BasicFragmentShader));

    pipelineLayout_ = CreatePipelineLayout (device_);
    VkExtent2D extent = {
        static_cast<uint32_t> (window_->GetWidth ()),
        static_cast<uint32_t> (window_->GetHeight ())
    };
    pipeline_ = CreatePipeline(device_, renderPass_, pipelineLayout_,
        vertexShader_, fragmentShader_, extent);
}
Beispiel #16
0
bool GLSLShader::LoadShaders( const char* vertex_file, const char* fragment_file )
{
	Free();

	FILE *fp = nullptr;
	fopen_s(&fp, vertex_file, "r");
	if (fp)
	{
		vertex = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
		LoadShader( vertex, fp );

		fclose(fp);
	}
	
		
	fopen_s(&fp, fragment_file, "r");
	if (fp)
	{
		fragment = glCreateShaderObjectARB( GL_FRAGMENT_SHADER_ARB );
		LoadShader( fragment, fp );

		fclose(fp);
	}
	

	if (vertex > 0 && fragment > 0)
	{

	  programObj = glCreateProgramObjectARB();
	  // check for errors
	  //if ( checkopenglerror ) return false;
	  // attach shader to program object
	  glAttachObjectARB( programObj, vertex );
	  // attach shader to program object
	  glAttachObjectARB( programObj, fragment );

	  GLint linked;
	  // link the program object and print out the info log
	  glLinkProgramARB( programObj );
	
	  CHECK_GL_ERROR();
	  //if ( checkopenglerror ) return false;

	  glGetObjectParameterivARB( programObj, GL_OBJECT_LINK_STATUS_ARB, &linked );
	  loadlog( programObj );

	  return (linked != 0);
	}

	return false;
}
Beispiel #17
0
GLuint LoadProgram(const char* pVtxFilename, const char* pFragFilename)
{
	GLuint uVtx = LoadShader(GL_VERTEX_SHADER, pVtxFilename);
	if (!uVtx)
	{
		return 0;
	}
	
	GLuint uFrag = LoadShader(GL_FRAGMENT_SHADER, pFragFilename);
	if (!uFrag)
	{
		glDeleteShader(uVtx);
		return 0;
	}
	
	GLuint uProg = glCreateProgram();
	glAttachShader(uProg, uVtx);
	glAttachShader(uProg, uFrag);
	glLinkProgram(uProg);
	
	glDeleteShader(uVtx);
	glDeleteShader(uFrag);
	
	int nStatus;
	glGetProgramiv(uProg, GL_LINK_STATUS, &nStatus);
	
	if (nStatus != GL_TRUE)
	{
		int nLogLen;
		glGetProgramiv(uProg, GL_INFO_LOG_LENGTH, &nLogLen);
		
		if (nLogLen > 0)
		{
			char* pInfoLog = new char[nLogLen + 1];
			pInfoLog[nLogLen] = 0;
		
			glGetProgramInfoLog(uProg, nLogLen, NULL, pInfoLog);
		
			printf("Linking error with:\nVertex Shader: %s\nFragment Shader: %s\n", pVtxFilename, pFragFilename);
			printf(pInfoLog);
		
			delete[] pInfoLog;
		}
		
		glDeleteProgram(uProg);
		
		return 0;
	}
	
	return uProg;
}
Beispiel #18
0
int Init ()
{
   GLbyte vShaderStr[] =  
      "attribute vec4 vPosition;    \n"
      "void main()                  \n"
      "{                            \n"
      "   gl_Position = vPosition;  \n"
      "}                            \n";
   
   GLbyte fShaderStr[] =  
      "precision mediump float;\n"\
      "void main()                                  \n"
      "{                                            \n"
      "  gl_FragColor = vec4 ( 0.0, 0.0, 1.0, 1.0 );\n"
      "}                                            \n";

   GLuint vertexShader;
   GLuint fragmentShader;
   GLint linked;

   vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );
   fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );

   programObject = glCreateProgram ( );
   if ( programObject == 0 )
      return 0;

   glAttachShader ( programObject, vertexShader );
   glAttachShader ( programObject, fragmentShader );
   glBindAttribLocation ( programObject, 0, "vPosition" );
   glLinkProgram ( programObject );
   glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
   if ( !linked ) 
   {
      GLint infoLen = 0;
      glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
      if ( infoLen > 1 )
      {
         char* infoLog = malloc (sizeof(char) * infoLen );
         glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
         printf ( "Error linking program:\n%s\n", infoLog );            
         free ( infoLog );
      }
      glDeleteProgram ( programObject );
      return GL_FALSE;
   }

   glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
   return GL_TRUE;
}
ShaderManager::ShaderManager()
{
    for(int i  = 0; i < MAX_SHADERS; i++)
    {
        shaders[i] = 0;
        vertShaders[i] = 0;
        fragShaders[i] = 0;
        vaos[i] = 0;
    }

    LoadShader("vert.vsh","frag.fsh",0);
    LoadShader("colorVert.vsh","colorFrag.fsh",1);
    numShaders = 2;
}
Beispiel #20
0
void LoadProgram(shader_prog_t* shaderProg,const char* vertexShaderPath, const char* fragmentShaderPath, uchar props)
{
	GLuint vertexShader; 
	GLuint fragmentShader; 
	
	GLint linked;
	

	
	//Load simple shader
	vertexShader = LoadShader(vertexShaderPath,GL_VERTEX_SHADER,props);
	fragmentShader = LoadShader(fragmentShaderPath,GL_FRAGMENT_SHADER,props);
	
	
	// Create the program object 
	shaderProg->prog = glCreateProgram(); 
	if(shaderProg->prog == 0) 
	{
		printf("Could not create GL program.");
		return ; 
	}
	
	glAttachShader(shaderProg->prog, vertexShader); 
	glAttachShader(shaderProg->prog, fragmentShader);
	
	
	// Link the program 
	glLinkProgram(shaderProg->prog); 
	
	// Check the link status 
	glGetProgramiv(shaderProg->prog, GL_LINK_STATUS, &linked); 
	if(!linked) 
	{ 
		GLint infoLen = 0; 
		glGetProgramiv(shaderProg->prog, GL_INFO_LOG_LENGTH, &infoLen); 
		
		if(infoLen > 1) 
		{ 
			char* infoLog = malloc(sizeof(char) * infoLen); 
			glGetProgramInfoLog(shaderProg->prog, infoLen, NULL, infoLog); 
			printf("Error linking program:\n%s\n", infoLog); 
			
			free(infoLog); 
		} 
		glDeleteProgram(shaderProg->prog); 
		return ; 
	} 
	
}
void CreateShaderProgram()
{
    /* Allocate shader object */
    ShaderProgram = glCreateProgram();

    if (ShaderProgram == 0) 
    {
        fprintf(stderr, "Error creating shader program\n");
        exit(1);
    }

    /* Load shader code from file */
    VertexShaderString = LoadShader("vertexshader.vs");
    FragmentShaderString = LoadShader("fragmentshader.fs");

    /* Separately add vertex and fragment shader to program */
    AddShader(ShaderProgram, VertexShaderString, GL_VERTEX_SHADER);
    AddShader(ShaderProgram, FragmentShaderString, GL_FRAGMENT_SHADER);

    GLint Success = 0;
    GLchar ErrorLog[1024];

    /* Link shader code into executable shader program */
    glLinkProgram(ShaderProgram);

    /* Check results of linking step */
    glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);

    if (Success == 0) 
    {
        glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
        fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
        exit(1);
    }

    /* Check if shader program can be executed */ 
    glValidateProgram(ShaderProgram);
    glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success);

    if (!Success) 
    {
        glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
        fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
        exit(1);
    }

    /* Put linked shader program into drawing pipeline */
    glUseProgram(ShaderProgram);
}
Beispiel #22
0
//
// Initialise les shader & program object
//
bool esgiUtilsSetupShaders()
{	
	char vShaderStr[] =  
		"attribute vec4 a_Position;			\n"
		"uniform float u_PointSize;			\n"
		"uniform mat4 u_ModelViewMatrix;	\n"
		"uniform mat4 u_ProjectionMatrix;	\n"
		"varying vec4 v_VertexColor;		\n"
		"void main()						\n"
		"{									\n"
		"   v_VertexColor = a_Position;		\n"
		"   gl_PointSize = u_PointSize;		\n"
		"   gl_Position = u_ProjectionMatrix * u_ModelViewMatrix * a_Position;	\n"
		"}									\n";

	char fShaderStr[] =  
		"#ifdef GL_ES						\n"
		"varying lowp vec4 v_VertexColor;	\n"
		"#else								\n"
		"varying vec4 v_VertexColor;		\n"
		"#endif								\n"
		"void main()						\n"
		"{									\n"		
		"  gl_FragColor = v_VertexColor;	\n"
		"}									\n";

	GLint linked;

	// charge les vertex/fragment shaders
	basicVertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr);
	basicFragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);

	// Cree le program object
	basicProgramObject = glCreateProgram();

	if (basicProgramObject == 0)
		return 0;

	glAttachShader(basicProgramObject, basicVertexShader);
	glAttachShader(basicProgramObject, basicFragmentShader);

	glBindAttribLocation(basicProgramObject, ESGI_ATTRIB_POSITION, "a_Position");

	// Liage des shaders dans le programme
	glLinkProgram(basicProgramObject);

	return true;
}
Beispiel #23
0
void loadAndAttach(GLint ProgramID, GLint ShaderType, const char *filepath, Types ... args)
{
    GLint ShaderID = LoadShader(filepath, ShaderType);
    glAttachShader(ProgramID, ShaderID);
    glDeleteShader(ShaderID);
    loadAndAttach(ProgramID, args...);
}
Beispiel #24
0
	Shader::Shader(std::string vertexPath, std::string fragmentPath)
	{
		GLuint vertexShader = LoadShader(vertexPath, GL_VERTEX_SHADER);
		GLuint fragmentShader = LoadShader(fragmentPath, GL_FRAGMENT_SHADER);
		
		_program = glCreateProgram();
		glAttachShader(_program,vertexShader);
		glAttachShader(_program,fragmentShader);
		
		glLinkProgram(_program);
		
		glDeleteShader(vertexShader);
		glDeleteShader(fragmentShader);
		
		CheckForLinkErrors();
	}
Beispiel #25
0
bool RustProcess::Load()
{
    if (!LoadShader("RustProcess", RPCallbackOGL, RPCallbackD3D9, true))
        return false;
    
    MetalTex_   = spRenderer->loadTexture(BasePath + "media/Metal.jpg");
    RustTex_    = spRenderer->loadTexture(BasePath + "media/Rust.jpg");
    FactorTex_  = spRenderer->loadTexture(BasePath + "media/RustFactor.jpg");
    
    Object_ = spScene->createMesh(scene::MESH_CUBE);
    Object_->setPosition(dim::vector3df(0, 0, 2));
    
    Object_->addTexture(MetalTex_);
    Object_->addTexture(RustTex_);
    Object_->addTexture(FactorTex_);
    
    if (spRenderer->getRendererType() == video::RENDERER_OPENGL)
    {
        // The texture layer order needs only to be set in GLSL.
        ShdPixel_->setConstant("ColorMapA", 0);
        ShdPixel_->setConstant("ColorMapB", 1);
        ShdPixel_->setConstant("FactorMap", 2);
    }
    
    Object_->setShaderClass(ShdClass_);
    
    Update();
    Show(false);
    
    return true;
}
Beispiel #26
0
static std::string LoadShader(const std::string& fileName)
{
	std::ifstream file;
	file.open(("./res/shaders/" + fileName).c_str());

	std::string output;
	std::string line;

	if(file.is_open())
	{
		while(file.good())
		{
			getline(file, line);
			
			if(line.find("#include") == std::string::npos)
				output.append(line + "\n");
			else
			{
				std::string includeFileName = Util::Split(line, ' ')[1];
				includeFileName = includeFileName.substr(1,includeFileName.length() - 2);

				std::string toAppend = LoadShader(includeFileName);
				output.append(toAppend + "\n");
			}
		}
	}
	else
	{
		std::cerr << "Unable to load shader: " << fileName << std::endl;
	}

	return output;
};
bool LoadAssets()
{
	// 텍스처 로딩
	gpStoneDM = LoadTexture("Fieldstone_DM.tga");
	if ( !gpStoneDM )
	{
		return false;
	}

	gpStoneSM = LoadTexture("Fieldstone_SM.tga");
	if ( !gpStoneSM )
	{
		return false;
	}

	// 쉐이더 로딩
	gpSpecularMappingShader = LoadShader("SpecularMapping.fx");
	if ( !gpSpecularMappingShader )
	{
		return false;
	}

	// 모델 로딩
	gpSphere = LoadModel("sphere.x");
	if ( !gpSphere )
	{
		return false;
	}

	return true;
}
unsigned int ShaderLoader::LoadProgram(const char* a_szVertexShader, const char* a_szPixelShader, const char* a_szGeometryShader /*= nullptr*/, const char* a_szTessellationControlShader /*= nullptr*/, const char* a_szTessellationEvaluationShader /*= nullptr*/) {
	GLuint vs = LoadShader(a_szVertexShader, GL_VERTEX_SHADER);
	GLuint fs = LoadShader(a_szPixelShader, GL_FRAGMENT_SHADER);
	GLuint gs = 0;
	GLuint tcs = 0;
	GLuint tes = 0;
	if (a_szGeometryShader != nullptr) {
		gs = LoadShader(a_szGeometryShader, GL_GEOMETRY_SHADER);
	}
	if (a_szTessellationControlShader && a_szTessellationEvaluationShader) {
		tcs = LoadShader(a_szTessellationControlShader, GL_TESS_CONTROL_SHADER);
		tes = LoadShader(a_szTessellationEvaluationShader, GL_TESS_EVALUATION_SHADER);
	}
	unsigned int uiShaderID = CreateProgram(vs, tcs, tes, gs, fs);
	return uiShaderID;
}
Beispiel #29
0
bool LoadAssets()
{
	// 텍스처 로딩
	gpStoneDM = LoadTexture("Fieldstone_DM.tga");
	if (!gpStoneDM)
		return false;

	gpStoneSM = LoadTexture("Fieldstone_SM.tga");
	if (!gpStoneSM)
		return false;

	gpStoneNM = LoadTexture("Fieldstone_NM.tga");
	if (!gpStoneNM)
		return false;

	// 쉐이더 로딩
	gpNormalMappingShader = LoadShader("NormalMapping.fx");
	if (!gpNormalMappingShader)
		return false;

	// 모델 로딩
	// gpSphere = LoadModel("sphere.x");
	gpSphere = LoadModel("SphereWithTangent.x");
	if (!gpSphere)
		return false;

	return true;
}
Beispiel #30
0
static string LoadShader(const string& _fileName) {
	std::ifstream file;
	file.open(("./shaders/" + _fileName).c_str());

	std::string output;
	std::string line;

	if (file.is_open()) {
		while (file.good()) {
			getline(file, line);

			if (line.find("#include") == std::string::npos) {
				output.append(line + "\n");
			} else {
				std::string includeFileName = Split(line, ' ')[1];
				includeFileName = includeFileName.substr(1, includeFileName.length() - 2);

				std::string toAppend = LoadShader(includeFileName);
				output.append(toAppend + "\n");
			}
		}
	} else {
		std::cerr << "Unable to load shader: " << _fileName << std::endl;
		Debug::LogError("Shader error. Line '" + to_string(__LINE__) + " Error: Unable to load shader file '" + _fileName);
	}

	return output;
};