Пример #1
1
int main( int argc, char *argv[] )
{
   glutInit( &argc, argv );
   glutInitWindowPosition( 200, 200 );
   glutInitWindowSize( 800, 500 );
   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
   glutCreateWindow("World of Awesome");
   glutReshapeFunc( ReshapeGL );
   glutDisplayFunc( Draw );
   glutKeyboardFunc( keyboard );
   glutMouseFunc( mouse );
   glutMotionFunc( mouseMove );
   glutPassiveMotionFunc( passiveMove );
   glutTimerFunc(TIMER_DELAY, tock, 0);

   g_width = g_height = 200;

#ifdef _WIN32 
   GLenum err = glewInit();
   if (GLEW_OK != err)
   {
      std::cerr << "Error initializing glew! " << glewGetErrorString(err) << std::endl;
      return 1;
   }
#endif
#ifdef __APPLE__
   glutSetCursor(GLUT_CURSOR_NONE); 
#endif

   backShade = glm::vec3(0.2,0.5,0.9);

   Initialize();
   
   //test the openGL version
   getGLversion();
   //install the shader
   if (!InstallShader(textFileRead((char *)"shaders/vert.glsl"), textFileRead((char *)"shaders/frag.glsl")))   {
      printf("Error installing shader!\n");
      return 0;
   }

   InitGeom();


   g_shadeType = PHONG;

   g_pitch = 0;
   g_yaw = M_PI / 2;
   float tx = cos(g_pitch)*cos(g_yaw);
   float ty = sin(g_pitch);
   float tz = cos(g_pitch)*cos(M_PI/2 - g_yaw);
   eye = glm::vec3(0, 2.5, 0);
   target = eye + glm::vec3(tx, ty, tz);
   sunDir = normalize(vec3(-0.2, -1.0, 0.0));


   sunShade = glm::vec3(1.0, 1.0, 0.9);

   glutMainLoop();
   return 0;
}
Пример #2
0
void 
VSShaderLib::loadShader(VSShaderLib::ShaderType st, std::string fileName) {

	// init should always be called first
	assert(pInited);

	char *s = NULL;

	s = textFileRead(fileName);

	if (s != NULL) {
		const char * ss = s;

		pShader[st] = glCreateShader(spGLShaderTypes[st]);
		glShaderSource(pShader[st], 1, &ss,NULL);
		glAttachShader(pProgram, pShader[st]);
		glCompileShader(pShader[st]);

		free(s);
	}
}
Пример #3
0
int main( int argc, char *argv[] )
{
   	glutInit( &argc, argv );
   	glutInitWindowPosition( 20, 20 );
   	glutInitWindowSize( 400, 400 );
   	glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
   	glutCreateWindow("My first triangle");
   	glutReshapeFunc( ReshapeGL );
   	glutDisplayFunc( Draw );
   	Initialize();
	
	//test the openGL version
	getGLversion();
	//install the shader
	if (!InstallShader(textFileRead((char *)"GLSL_Lab1.glsl"))) {
		printf("Error installing shader!\n");
		return 0;
	}
		
	InitGeom();
  	glutMainLoop();
   	return 0;
}
Пример #4
0
void Shader::attach(int type, char* filename) //连接不同种类Shader
{
	auto mem = textFileRead(filename);
	GLuint handle = glCreateShader(type);
	glShaderSource(handle, 1, (const GLchar**)(&mem), 0);

	glCompileShader(handle);

	GLint compileSuccess=0;
	GLchar compilerSpew[256];

	glGetShaderiv(handle, GL_COMPILE_STATUS, &compileSuccess);

	if (!compileSuccess) 
	{
		glGetShaderInfoLog(handle, sizeof(compilerSpew), 0, compilerSpew);
		printf("Shader%s\n%s\ncompileSuccess=%d\n",filename, compilerSpew, compileSuccess);

		while(1);;
	}
	glAttachShader(m_Program, handle); 

}
Пример #5
0
Shader::Shader(const char *vert_fname)
{
    install(textFileRead((char *)vert_fname), NULL);
    initialize();
}
GLuint initShaders(char** shaders, GLenum* types, int numShaders)
{
	GLuint program = glCreateProgram();
	//loop through the shaders passed in and initialize them
	for(int i = 0; i < numShaders; i++)
	{
		//Create the shader
		GLuint shader;

		//get a shader handler
		shader = glCreateShader(types[i]);
		//read the shader from the source file
		const char* shaderSource = textFileRead(shaders[i]);
		//pass source to GL
		glShaderSource(shader, 1, &shaderSource, NULL);
		//delete the memory from the source text
		delete[] shaderSource;
		//Compile shader
		glCompileShader(shader);

		//Check compilation errors
		GLint  compiled;
		glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
		if ( !compiled ) 
		{
			std::cerr << shaders[i] << " failed to compile:" << std::endl;
			GLint  logSize;
			glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
			char* logMsg = new char[logSize];
			glGetShaderInfoLog( shader, logSize, NULL, logMsg );
			std::cerr << logMsg << std::endl;
			delete [] logMsg;

			exit( EXIT_FAILURE );
		}

		glAttachShader(program, shader);
	}
	//Tell the fragment shader which buffer to write to
	glBindFragDataLocation(program, 0, "outColor");

	glLinkProgram(program);

	//Check linking errors
	GLint  linked;
    glGetProgramiv( program, GL_LINK_STATUS, &linked );
    if ( !linked ) 
	{
		std::cerr << "Shader program failed to link" << std::endl;
		GLint  logSize;
		glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
		char* logMsg = new char[logSize];
		glGetProgramInfoLog( program, logSize, NULL, logMsg );
		std::cerr << logMsg << std::endl;
		delete [] logMsg;

		exit( EXIT_FAILURE );
    }
	
	return program;
}
Пример #7
0
GLuint setup_shader_program(char * sourcefilename)
{
  GLuint retval;
  GLuint frag_shader, shader_program;
  char *frag_source;
  GLchar ** frag_sourcep;
  
  frag_shader = glCreateShader(GL_FRAGMENT_SHADER); 

  if (0 == frag_shader)
    {
      fprintf(stderr, "Error creating shader\n");
      check_error("Error creating fragment shader");
      retval = 0; /* error  */
    }
  else
    {
     /* read the source from sourcefilename into a string  */
      /* pointed to by frag_source.                          */
  
      frag_source = textFileRead(sourcefilename);
  
      if (NULL == frag_source)
	{
	  fprintf(stderr, "Error:no file to read the fragment shader from\n");
	  fprintf(stderr, "  can't find file '%s'\n", sourcefilename);
	  retval = 0; /* error  */
	}
      
      frag_sourcep = &frag_source;

      glShaderSource(frag_shader, 1, (const GLchar **)frag_sourcep,NULL);

      check_error("after glShaderSource");

      /* compile the source we loaded into the fragment shader */
      glCompileShader(frag_shader);

      check_error("after glCompileShader");

      shader_program = glCreateProgram();
      
      if (0 == shader_program)
	{
	  fprintf(stderr,
		  "Error creating shader program with glCreateProgram\n");
	  check_error("Error creating shader program");
	  retval =  0; /* error  */
	}
      else
	{
	  glAttachShader(shader_program, frag_shader);

	  check_error("after glAttachShader");

	  glLinkProgram(shader_program);
	  check_error("after glAttachShader");

	  /* print out any link-stage warnings or errors  */
	  print_shader_info_log(shader_program);


	  /* make this program part of the opengl state  */
  
	  glUseProgram(shader_program);
	  check_error("after glUseProgram");

	  retval = shader_program;
	}
    } 
  return(retval);
}
Пример #8
0
void setShaders() {
	char *vs = NULL,*fs = NULL;

	v = glCreateShader(GL_VERTEX_SHADER);
	f = glCreateShader(GL_FRAGMENT_SHADER);

	vs = textFileRead("shader.vert");
	fs = textFileRead("shader.frag");

	const char * ff = fs;
	const char * vv = vs;

	glShaderSource(v, 1, &vv,NULL);
	glShaderSource(f, 1, &ff,NULL);

	free(vs);free(fs);

	glCompileShader(v);
	glCompileShader(f);

	GLint vCompiled;
	glGetShaderiv(v, GL_COMPILE_STATUS, &vCompiled);
	if (!vCompiled) { 
		GLint length; 
		GLchar* log; 
 
		glGetShaderiv(v, GL_INFO_LOG_LENGTH, &length ); 
 
		log = (GLchar*) malloc(length); 
		glGetShaderInfoLog(v, length, &length, log); 
		fprintf(stderr, "[v] compile log = '%s'\n", log); 
		free(log); 
	} 

	GLint fCompiled;
	glGetShaderiv(f, GL_COMPILE_STATUS, &fCompiled);
	if (!fCompiled) { 
		GLint length; 
		GLchar* log; 
 
		glGetShaderiv(f, GL_INFO_LOG_LENGTH, &length ); 
 
		log = (GLchar*) malloc(length); 
		glGetShaderInfoLog(f, length, &length, log); 
		fprintf(stderr, "[f] compile log = '%s'\n", log); 
		free(log); 
	} 

	p = glCreateProgram();
	glAttachShader(p,f);
	glAttachShader(p,v);

	glLinkProgram(p);
	GLint linked = 0;
	glGetProgramiv(p, GL_LINK_STATUS, &linked );
	if (linked) {
		glUseProgram(p);
	} else {
		GLint length;
		GLchar* log;
		glGetProgramiv(p, GL_INFO_LOG_LENGTH, &length );
		log = (GLchar*) malloc(length);
		glGetProgramInfoLog(p, length, &length, log);
		fprintf(stderr, "link log = '%s'\n", log);
		free(log);
	}

	iLocPosition = glGetAttribLocation(p, "av4position");
	iLocTexCoord = glGetAttribLocation(p, "av2texCoord");

	iLocMVP = glGetUniformLocation(p, "mvp");

	iLocModelMatrix  = glGetUniformLocation(p, "um4modelMatrix");

	iLocTexMap = glGetUniformLocation(p, "texMapping");
	
	glUseProgram(p);
}
Пример #9
0
void LoadShader( GLuint &prog, std::string vertstr, std::string fragstr )
{
	GLuint v,f;
	std::string vs, fs;
	const int MaxInfoLogLength = 2048;
	GLchar infoLog[MaxInfoLogLength];
	GLsizei length=0;
	GLint param = GL_TRUE;

	const std::string shaderpath("./shaders/");

	/*const std::string libpath( shaderpath + "noise_lib.glsl");
	const std::string lib = textFileRead( libpath.c_str() );
	vs = lib;
	fs = lib;*/

	const std::string vertname( shaderpath + vertstr + ".vert");
	const std::string fragname( shaderpath + fragstr + ".frag");

	vs += textFileRead( vertname.c_str() );
	fs += textFileRead( fragname.c_str() );

	if( vs.size()>0 && fs.size()>0 )
	{
		v = glCreateShader(GL_VERTEX_SHADER);
		checkGLError();
		f = glCreateShader(GL_FRAGMENT_SHADER);
		checkGLError();

		const char * vv = vs.c_str();
		const char * ff = fs.c_str();;

		glShaderSource(v, 1, &vv,NULL);
		checkGLError();
		{
			glCompileShader(v);
			checkGLError();
			glGetShaderiv(v,GL_COMPILE_STATUS,&param);
			if( param==GL_FALSE ) {
				glGetShaderInfoLog(v,MaxInfoLogLength,&length,infoLog);
				printf("GLSL Error: %s", infoLog);
			}
			checkGLError();
		}

		glShaderSource(f, 1, &ff,NULL);
		checkGLError();
		{
			glCompileShader(f);
			checkGLError();
			glGetShaderiv(f,GL_COMPILE_STATUS,&param);
			if( param==GL_FALSE ) {
				glGetShaderInfoLog(f,MaxInfoLogLength,&length,infoLog);
				printf("GLSL Error: %s", infoLog);
			}
			checkGLError();
		}

		prog = glCreateProgram();
		checkGLError();
		glAttachShader(prog,v);
		checkGLError();
		glAttachShader(prog,f);
		checkGLError();

		glBindFragDataLocation(prog,0,"out_colour");

		glLinkProgram(prog);
		glGetProgramiv(prog,GL_LINK_STATUS,&param);
		if( param==GL_FALSE ) {
			glGetProgramInfoLog(prog,MaxInfoLogLength,&length,infoLog);
			printf("GLSL Link Error: %s", infoLog);
		}
		checkGLError();
	}
}
Пример #10
0
int main()
{
    int numValues = 6400000;
    double* values = new double [numValues];
    double value;
    MersenneTwister rng;
    for ( int i = 0; i < numValues; ++i )
    {
        values[i] = rng.rand_double_closed(-10000000, 10000000);
    }

    std::cout << "Testing big endian read/write" << std::endl;
    // write big
    std::ofstream bigFile( "/tmp/binaryBig", std::ios::binary );
    DwD bigWriter( bigFile, DwD::Binary, DwD::BigEndian );
    for ( int i = 0; i < numValues; ++i )
    {
        bigWriter.write( values + i );
    }
    bigFile.close();

    // read big and validate
    std::ifstream bigFileRead( "/tmp/binaryBig", std::ios::binary );
    DrD bigReader( bigFileRead, DrD::Binary, DrD::BigEndian );
    int count = 0;
    while ( bigReader.read( value ) )
    {
        assert( fabs( value - values[count] ) < 1e-8 );
        ++count;
    }
    assert( count == numValues );
    bigFileRead.close();
    std::cout << "  passed" << std::endl;
    
    std::cout << "Testing little endian read/write" << std::endl;
    // write little
    std::ofstream littleFile( "/tmp/binaryLittle", std::ios::binary );
    DwD littleWriter( littleFile, DwD::Binary, DwD::LittleEndian );
    for ( int i = 0; i < numValues; ++i )
    {
        littleWriter.write( values[i] );
    }
    littleFile.close();

    // read little and validate
    std::ifstream littleFileRead( "/tmp/binaryLittle", std::ios::binary );
    DrD littleReader( littleFileRead, DrD::Binary, DrD::LittleEndian );
    count = 0;
    while ( littleReader.read( value ) )
    {
        assert( fabs( value - values[count] ) < 1e-8 );
        ++count;
    }
    assert( count == numValues );
    littleFileRead.close();
    std::cout << "  passed" << std::endl;

    std::cout << "Testing text read/write" << std::endl;
    // write text
    std::ofstream textFile( "/tmp/text" );
    textFile.precision(16);
    textFile.setf( std::ios::scientific );
    DwD textWriter( textFile, DwD::Ascii );
    for ( int i = 0; i < 1000; ++i )
    {
        textWriter.write( values[i] );
        textWriter.linebreak();
    }
    textFile.close();

    // read text and validate
    std::ifstream textFileRead( "/tmp/text" );
    DrD textReader( textFileRead, DrD::Ascii );
    count = 0;
    double foo;
    while( textReader.read( value ) )
    {
        if ( fabs( value - values[ count ] ) >= 1e-8 )
        {
            std::cout << "value:" << value << " values[" 
                << count << "]:" << values[count] << std::endl;
        }
        assert( fabs( value - values[ count ] ) < 1e-8 );
        ++count;
    }
    assert( count == 1000 );
    textFileRead.close();
    std::cout << "  passed" << std::endl;
    delete [] values;
}
Пример #11
0
GLuint setupShaders() {
 
    char *vs = NULL,*fs = NULL,*fs2 = NULL;
 
    GLuint p,v,f;
 
    v = glCreateShader(GL_VERTEX_SHADER);
    f = glCreateShader(GL_FRAGMENT_SHADER);
 
    vs = textFileRead((char *)vertexFileName);
    fs = textFileRead((char *)fragmentFileName);
 
    const char * vv = vs;
    const char * ff = fs;
 
    glShaderSource(v, 1, &vv,NULL);
    glShaderSource(f, 1, &ff,NULL);
 
    free(vs);free(fs);
 
    GLint shaderCompiled;

    printf("compiling vertex shader...\n");
    glCompileShader(v);
    glGetShaderiv(v, GL_COMPILE_STATUS, &shaderCompiled);
    if(shaderCompiled == GL_FALSE)
    {
    	char * info = getShaderLog(shaderCompiled);
	if(info != NULL)printf("%s\n", info);
	else printf("NULL error info.\n");
    }
    else
    {
	printf("Vertex shader compiled!\n");

    }

    printf("compiling fragment shader...\n");
    glCompileShader(f);
    glGetShaderiv(f, GL_COMPILE_STATUS, &shaderCompiled);
    if(shaderCompiled == GL_FALSE)
    {
    	char * info = getShaderLog(shaderCompiled);
	if(info != NULL)printf("%s\n", info);
	else printf("NULL error info.\n");
    }
    else
    {
	printf("Fragment shader compiled!\n");

    }
 
    printShaderInfoLog(v);
    printShaderInfoLog(f);
 
    p = glCreateProgram();
    glAttachShader(p,v);
    glAttachShader(p,f);
 
   // glBindFragDataLocation(p, 0, "outputF");
    glLinkProgram(p);
    printProgramInfoLog(p);
 
    vertexLoc = glGetAttribLocation(p,"position");
    colorLoc = glGetAttribLocation(p, "color"); 

    printf("vertexLoc: %d\n", vertexLoc);
    printf("colorLoc: %d\n", colorLoc);
 
    projMatrixLoc = glGetUniformLocation(p, "projMatrix");
    viewMatrixLoc = glGetUniformLocation(p, "viewMatrix");
 
    return(p);
}
Пример #12
0
void setShaders()
{
	GLuint v, f, p;
	char *vs = NULL;
	char *fs = NULL;

	v = glCreateShader(GL_VERTEX_SHADER);
	f = glCreateShader(GL_FRAGMENT_SHADER);

	vs = textFileRead("shader.vert");
	fs = textFileRead("shader.frag");

	glShaderSource(v, 1, (const GLchar**)&vs, NULL);
	glShaderSource(f, 1, (const GLchar**)&fs, NULL);

	free(vs);
	free(fs);

	// compile vertex shader
	glCompileShader(v);
	GLint vShaderCompiled;
	showShaderCompileStatus(v, &vShaderCompiled);
	if(!vShaderCompiled) system("pause"), exit(123);

	// compile fragment shader
	glCompileShader(f);
	GLint fShaderCompiled;
	showShaderCompileStatus(f, &fShaderCompiled);
	if(!fShaderCompiled) system("pause"), exit(456);

	p = glCreateProgram();

	// bind shader
	glAttachShader(p, f);
	glAttachShader(p, v);

	// link program
	glLinkProgram(p);
	GLint linked = 0;
	glGetProgramiv(p, GL_LINK_STATUS, &linked);
	if (linked) {
		glUseProgram(p);
	}
	else {
		GLint length;
		GLchar* log;
		glGetProgramiv(p, GL_INFO_LOG_LENGTH, &length);
		log = (GLchar*)malloc(length);
		glGetProgramInfoLog(p, length, &length, log);
		fprintf(stderr, "link log = '%s'\n", log);
		free(log);
	}


	iLocPosition = glGetAttribLocation (p, "av4position");
	iLocNormal	 = glGetAttribLocation (p, "av3normal");
	iLocTexCoord = glGetAttribLocation (p, "av2texCoord");
	iLocMVP		 = glGetUniformLocation(p, "mvp");
	iLocVMN      = glGetUniformLocation(p, "vmn");
	iLocMN       = glGetUniformLocation(p, "mn");
	iLocV		 = glGetUniformLocation(p, "v");

	iLocMDiffuse   = glGetUniformLocation(p, "Material.diffuse");
	iLocMAmbient   = glGetUniformLocation(p, "Material.ambient");
	iLocMSpecular  = glGetUniformLocation(p, "Material.specular");
	iLocMShininess = glGetUniformLocation(p, "Material.shininess");

	iLocLDAmbient      = glGetUniformLocation(p, "LightSource[0].ambient");
	iLocLDDiffuse      = glGetUniformLocation(p, "LightSource[0].diffuse");
	iLocLDSpecular	   = glGetUniformLocation(p, "LightSource[0].specular");
	iLocLDPosition	   = glGetUniformLocation(p, "LightSource[0].position");
	iLocLDSpotcutoff   = glGetUniformLocation(p, "LightSource[0].spotCutoff");
	iLocLDCAttenuation = glGetUniformLocation(p, "LightSource[0].constantAttenuation");

	iLocLPAmbient	   = glGetUniformLocation(p, "LightSource[1].ambient");
	iLocLPDiffuse	   = glGetUniformLocation(p, "LightSource[1].diffuse");
	iLocLPSpecular	   = glGetUniformLocation(p, "LightSource[1].specular");
	iLocLPPosition	   = glGetUniformLocation(p, "LightSource[1].position");
	iLocLPSpotcutoff   = glGetUniformLocation(p, "LightSource[1].spotCutoff");
	iLocLPCAttenuation = glGetUniformLocation(p, "LightSource[1].constantAttenuation");
	iLocLPLAttenuation = glGetUniformLocation(p, "LightSource[1].linearAttenuation");
	iLocLPQAttenuation = glGetUniformLocation(p, "LightSource[1].quadraticAttenuation");

	iLocLSAmbient      = glGetUniformLocation(p, "LightSource[2].ambient");
	iLocLSDiffuse      = glGetUniformLocation(p, "LightSource[2].diffuse");
	iLocLSSpecular     = glGetUniformLocation(p, "LightSource[2].specular");
	iLocLSPosition     = glGetUniformLocation(p, "LightSource[2].position");
	iLocLSSpotcutoff   = glGetUniformLocation(p, "LightSource[2].spotCutoff");
	iLocLSCAttenuation = glGetUniformLocation(p, "LightSource[2].constantAttenuation");
	iLocSSpotDirection = glGetUniformLocation(p, "LightSource[2].spotDirection");
	iLocLSLAttenuation = glGetUniformLocation(p, "LightSource[2].linearAttenuation");
	iLocLSQAttenuation = glGetUniformLocation(p, "LightSource[2].quadraticAttenuation");
	iLocSSpotExponent  = glGetUniformLocation(p, "LightSource[2].spotExponent");

	iLocLType = glGetUniformLocation(p, "lightType");
	eyePos	  = glGetUniformLocation(p, "eyeP");
	iLocTexMapping = glGetUniformLocation(p, "texMapping");

	glUseProgram(p);

	glUniform4fv(iLocLDAmbient,  1,  mm.lightSource[0].ambient);
	glUniform4fv(iLocLDDiffuse,  1,  mm.lightSource[0].diffuse);
	glUniform4fv(iLocLDSpecular, 1,  mm.lightSource[0].specular);
	glUniform4fv(iLocLDPosition, 1,  mm.lightSource[0].position);
	glUniform1f (iLocLDSpotcutoff,   mm.lightSource[0].spotCutoff);
	glUniform1f (iLocLDCAttenuation, mm.lightSource[0].constantAttenuation);

	glUniform4fv(iLocLPAmbient,  1,  mm.lightSource[1].ambient);
	glUniform4fv(iLocLPDiffuse,  1,  mm.lightSource[1].diffuse);
	glUniform4fv(iLocLPSpecular, 1,  mm.lightSource[1].specular);
	glUniform4fv(iLocLPPosition, 1,  mm.lightSource[1].position);
	glUniform1f (iLocLPSpotcutoff,   mm.lightSource[1].spotCutoff);
	glUniform1f (iLocLPCAttenuation, mm.lightSource[1].constantAttenuation);
	glUniform1f (iLocLPLAttenuation, mm.lightSource[1].linearAttenuation);
	glUniform1f (iLocLPQAttenuation, mm.lightSource[1].quadraticAttenuation);

	glUniform4fv(iLocLSAmbient,  1,  mm.lightSource[2].ambient);
	glUniform4fv(iLocLSDiffuse,  1,  mm.lightSource[2].diffuse);
	glUniform4fv(iLocLSSpecular, 1,  mm.lightSource[2].specular);
	glUniform4fv(iLocLSPosition, 1,  mm.lightSource[2].position);
	glUniform1f (iLocLSSpotcutoff,   mm.lightSource[2].spotCutoff);
	glUniform1f (iLocLSCAttenuation, mm.lightSource[2].constantAttenuation);
	glUniform1f (iLocLSLAttenuation, mm.lightSource[2].linearAttenuation);
	glUniform1f (iLocLSQAttenuation, mm.lightSource[2].quadraticAttenuation);

	glUniform4fv(iLocSSpotDirection, 1, mm.lightSource[2].spotDirection);
	glUniform1f (iLocSSpotExponent,     mm.lightSource[2].spotExponent);
}
Пример #13
0
void LoadShader(ShaderInfo* _shader, ShaderFiles sFiles)
{
	char *vs = NULL,*fs = NULL, *gs = NULL;

	GLuint vert, geom, frag;
	vert= glCreateShader(GL_VERTEX_SHADER);
	if(sFiles.geomFile != NULL)
	{
		geom	= glCreateShader(GL_GEOMETRY_SHADER);
	}
	frag	= glCreateShader(GL_FRAGMENT_SHADER);

	// Read in the shader files
	vs = textFileRead(sFiles.vertFile);
	if(sFiles.geomFile != NULL)
	{
		gs = textFileRead(sFiles.geomFile);
	}
	fs = textFileRead(sFiles.fragFile);

	const char * vv = vs;
	const char * gg = gs;
	const char * ff = fs;

	// Grab source files for vert and frag shaders
	glShaderSource(vert, 1, &vv,NULL);
	if(sFiles.geomFile != NULL)
	{
		glShaderSource(geom, 1, &gg,NULL);
	}
	glShaderSource(frag, 1, &ff,NULL);

	free((char*)vv);
	free((char*)gg);
	free((char*)ff);

	// Compile Shaders--------------------------------------------------------
	// Compile vertex shader, then test if it was successful
	glCompileShader(vert);
	GLQueryCompileStatus(vert, GL_VERTEX_SHADER);

	if(sFiles.geomFile != NULL)
	{
		// Compile fragment shader, then test if it was successful
		glCompileShader(geom);
		GLQueryCompileStatus(geom, GL_GEOMETRY_SHADER);
	}

	// Compile fragment shader, then test if it was successful
	glCompileShader(frag);
	GLQueryCompileStatus(frag, GL_FRAGMENT_SHADER);

	// Create shader programs-------------------------------------------------------
	// Create shader program to become an anchor point of shaders to link to.
	// Multiple shaders of each type can be attached here, but only one file per type (vert and frag)
	// can have a main() function
	GLuint prog = glCreateProgram();
	glAttachShader(prog,frag);
	if(sFiles.geomFile != NULL)
	{
		glAttachShader(prog,geom);
	}
	glAttachShader(prog,vert);

	// Link Programs-----------------------------------------------------------------
	// Link program into openGL, then test to see if there were any errors
	glLinkProgram(prog);
	GLQueryLinkStatus(prog);

	// Validate shaders---------------------------------------------------------------
	// Validate that all shaders play well together, see if there are errors
	glValidateProgram(prog);
	GLQueryValidation(prog);

	// Assign values
	_shader->VertexShaderId = vert;
	if(sFiles.geomFile != NULL)
	{
		_shader->GeometryShaderId = geom;
	}
	_shader->FragmentShaderId = frag;
	_shader->ProgramId = prog;
}
Пример #14
0
void setShaders() {

  const char * vv;
  const char * ff;
                char *vs,*fs;
                GLint status;
                GLhandleARB vertex_program,fragment_program;
                GLint sourceSize, shader_texture_source;

                vertex_program = glCreateShader(GL_VERTEX_SHADER);
                fragment_program = glCreateShader(GL_FRAGMENT_SHADER);

                vs = textFileRead(SHADER_DIR "demosaic.vrt");
                if (vs==NULL) {
                  fprintf(stderr,"ERROR: failed to read vertex shader %s\n",
			  SHADER_DIR "demosaic.vrt");
                  use_shaders = 0;
                  return;
                }
                fs = textFileRead(SHADER_DIR "demosaic.frg");
                if (fs==NULL) {
                  fprintf(stderr,"ERROR: failed to read fragment shader %s\n",
			  SHADER_DIR "demosaic.frg");
                  use_shaders = 0;
                  return;
                }

                vv = vs;
                ff = fs;

                glShaderSource(vertex_program, 1, &vv,NULL);
                glShaderSource(fragment_program, 1, &ff,NULL);

                free(vs);free(fs);

                glCompileShader(vertex_program);
                glGetShaderiv(vertex_program,GL_COMPILE_STATUS,&status);
                if (status!=GL_TRUE) {
                  fprintf(stderr,
                          "ERROR: GLSL vertex shader compile error, disabling shaders\n");
                  use_shaders = 0;
                  return;
                }

                glCompileShader(fragment_program);
                glGetShaderiv(fragment_program,GL_COMPILE_STATUS,&status);
                if (status!=GL_TRUE) {
                  fprintf(stderr,
                          "ERROR: GLSL fragment shader compile error, disabling shaders\n");
                  use_shaders = 0;
                  return;
                }

                printShaderInfoLog(vertex_program);
                printShaderInfoLog(fragment_program);

                glsl_program = glCreateProgram();
                glAttachShader(glsl_program,vertex_program);
                glAttachShader(glsl_program,fragment_program);
                glLinkProgram(glsl_program);

                printProgramInfoLog(glsl_program);

                glGetProgramiv(glsl_program,GL_LINK_STATUS,&status);
                if (status!=GL_TRUE) {
                  fprintf(stderr,"ERROR: GLSL link error, disabling shaders\n");
                  use_shaders = 0;
                  return;
                }

                glUseProgram(glsl_program);
                printf("GLSL shaders in use\n");


                sourceSize = glGetUniformLocation(glsl_program,"sourceSize");
                shader_texture_source = glGetUniformLocation(glsl_program,"source");

                glUniform4f(sourceSize,
                            PBO_stride,height,
                            1.0/PBO_stride,1.0/height);
                glUniform1i(shader_texture_source, 0);
}
void initGL()
{
	/* Initialize GLEW; this gives us access to OpenGL Extensions.
	 */
	glewInit();  

	/* Print information about OpenGL and ensure that we've got at a context 
	 * that supports least OpenGL 3.0. Then setup the OpenGL Debug message
	 * mechanism.
	 */
	startupGLDiagnostics();
	setupGLDebugMessages();

	/* Workaround for AMD. It might no longer be necessary, but I dunno if we
	 * are ever going to remove it. (Consider it a piece of living history.)
	 */
	if( !glBindFragDataLocation )
	{
		glBindFragDataLocation = glBindFragDataLocationEXT;
	}

	/* As a general rule, you shouldn't need to change anything before this 
	 * comment in initGL().
	 */

	// Define the positions for each of the three vertices of the triangle
	const float positions[] = {
		//	 X      Y     Z
		0.0f,   0.5f, 1.0f,		// v0
		-0.5f,  -0.5f, 1.0f,	// v1
		0.5f,  -0.5f, 1.0f		// v2
	};

	// Define the colors for each of the three vertices of the triangle
	const float colors[] = {
		//  R     G		B
		1.0f, 1.0f, 1.0f,		// White
		1.0f, 1.0f, 1.0f,		// White
		1.0f, 1.0f, 1.0f		// White
	};

	// Create a handle for the position vertex buffer object
	// See OpenGL Spec §2.9 Buffer Objects 
	// - http://www.cse.chalmers.se/edu/course/TDA361/glspec30.20080923.pdf#page=54
	GLuint positionBuffer; 
	glGenBuffers( 1, &positionBuffer );
	// Set the newly created buffer as the current one
	glBindBuffer( GL_ARRAY_BUFFER, positionBuffer );
	// Send the vertex position data to the current buffer
	glBufferData( GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW );

	// Create a handle for the vertex color buffer
	GLuint colorBuffer; 
	glGenBuffers( 1, &colorBuffer );
	// Set the newly created buffer as the current one
	glBindBuffer( GL_ARRAY_BUFFER, colorBuffer );	
	// Send the vertex color data to the current buffer
	glBufferData( GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW );

	//******* Connect triangle data with the vertex array object *******
	//
	// Connect the vertex buffer objects to the vertex array object
	// See OpenGL Spec §2.10 
	// - http://www.cse.chalmers.se/edu/course/TDA361/glspec30.20080923.pdf#page=64
	glGenVertexArrays(1, &vertexArrayObject);

	// Bind the vertex array object
	// The following calls will affect this vertex array object.
	glBindVertexArray(vertexArrayObject);
	// Makes positionBuffer the current array buffer for subsequent calls.
	glBindBuffer( GL_ARRAY_BUFFER, positionBuffer );
	// Attaches positionBuffer to vertexArrayObject, in the 0th attribute location
	glVertexAttribPointer(0, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/ );	

	// Makes colorBuffer the current array buffer for subsequent calls.
	glBindBuffer( GL_ARRAY_BUFFER, colorBuffer );
	// Attaches colorBuffer to vertexArrayObject, in the 1st attribute location
	glVertexAttribPointer(1, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/ );

	glEnableVertexAttribArray(0); // Enable the vertex position attribute
	glEnableVertexAttribArray(1); // Enable the vertex color attribute 


	///////////////////////////////////////////////////////////////////////////
	// Create shaders
	///////////////////////////////////////////////////////////////////////////	

	// See OpenGL spec §2.20 http://www.cse.chalmers.se/edu/course/TDA361/glspec30.20080923.pdf#page=104&zoom=75
	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

	// Invoke helper functions (in glutil.h/cpp) to load text files for vertex and fragment shaders.
	const char *vs = textFileRead("simple.vert");
	const char *fs = textFileRead("simple.frag");

	glShaderSource(vertexShader, 1, &vs, NULL);
	glShaderSource(fragmentShader, 1, &fs, NULL);

	// we are now done with the source and can free the file data, textFileRead uses new [] to.
  // allocate the memory so we must free it using delete [].
	delete [] vs;
	delete [] fs;

	// Compile the shader, translates into internal representation and checks for errors.
	glCompileShader(vertexShader);
	int compileOK;
	// check for compiler errors in vertex shader.
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compileOK);
	if(!compileOK) {
		std::string err = GetShaderInfoLog(vertexShader);
		fatal_error( err );
		return;
	}

	// Compile the shader, translates into internal representation and checks for errors.
	glCompileShader(fragmentShader);
	// check for compiler errors in fragment shader.
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compileOK);
	if(!compileOK) {
		std::string err = GetShaderInfoLog(fragmentShader);
		fatal_error( err );
		return;
	}

	// Create a program object and attach the two shaders we have compiled, the program object contains
	// both vertex and fragment shaders as well as information about uniforms and attributes common to both.
	shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, fragmentShader);
	glAttachShader(shaderProgram, vertexShader);

	// Now that the fragment and vertex shader has been attached, we no longer need these two separate objects and should delete them.
	// The attachment to the shader program will keep them alive, as long as we keep the shaderProgram.
	glDeleteShader( vertexShader );
	glDeleteShader( fragmentShader );

	// We have previously (in the glVertexAttribPointer calls) decided that our 
	// vertex position data will be the 0th attribute. Bind the attribute with 
	// name "position" to the 0th stream
	glBindAttribLocation(shaderProgram, 0, "position"); 
	// And bind the attribute called "color" in the shader to the 1st attribute
	// stream. 
	glBindAttribLocation(shaderProgram, 1, "color");

	// This tells OpenGL which draw buffer the fragment shader out variable 'fragmentColor' will end up in.
	// Since we only use one output and draw buffer this is actually redundant, as the default will be correct.
	glBindFragDataLocation(shaderProgram, 0, "fragmentColor");

	// Link the different shaders that are bound to this program, this creates a final shader that 
	// we can use to render geometry with.
	glLinkProgram(shaderProgram);

	// Check for linker errors, many errors, such as mismatched in and out variables between 
	// vertex/fragment shaders,  do not appear before linking.
	{
		GLint linkOk = 0;
		glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkOk);
		if(!linkOk) 
		{
			std::string err = GetShaderInfoLog(shaderProgram);
			fatal_error( err );
			return;
		}
	}
}
Пример #16
0
Shader::Shader(const char *vert_fname, const char *frag_fname)
{
    install(textFileRead((char *)vert_fname), 
            textFileRead((char *)frag_fname));
    initialize();
}
Пример #17
0
///////////////////////////////////////////////////////////////////////
//load, compile and set the shaders
void setShaders()
{
	char *vs,*fs,*gs;

	v = glCreateShader(GL_VERTEX_SHADER);
	f = glCreateShader(GL_FRAGMENT_SHADER);
	g = glCreateShader(GL_GEOMETRY_SHADER);

	vs = textFileRead("./shader04a.vert");
	fs = textFileRead("./shader04a.frag");
	gs = textFileRead("./shader04a.geom");

	const char * ff = fs;
	const char * vv = vs;
	const char * gg = gs;

	glShaderSource(v, 1, &vv,NULL);
	glShaderSource(f, 1, &ff,NULL);
	glShaderSource(g, 1, &gg,NULL);

	free(vs);free(fs);free(gs);

	glCompileShader(v);
	glCompileShader(f);
	glCompileShader(g);

	GLint blen = 0; 
	GLsizei slen = 0;

	glGetShaderiv(v, GL_INFO_LOG_LENGTH , &blen);       
	if (blen > 1)
	{
		GLchar* compiler_log = (GLchar*)malloc(blen);
		glGetShaderInfoLog(v, blen, &slen, compiler_log);
		std::cout << "compiler_log vertex shader:\n" << compiler_log << std::endl;
		free (compiler_log);
	}
	blen = 0; 
	slen = 0;
	glGetShaderiv(f, GL_INFO_LOG_LENGTH , &blen);       
	if (blen > 1)
	{
		GLchar* compiler_log = (GLchar*)malloc(blen);
		glGetShaderInfoLog(f, blen, &slen, compiler_log);
		std::cout << "compiler_log fragment shader:\n" << compiler_log << std::endl;
		free (compiler_log);
	}
	blen = 0; 
	slen = 0;
	glGetShaderiv(g, GL_INFO_LOG_LENGTH , &blen);       
	if (blen > 1)
	{
		GLchar* compiler_log = (GLchar*)malloc(blen);
		glGetShaderInfoLog(g, blen, &slen, compiler_log);
		std::cout << "compiler_log geometry shader:\n" << compiler_log << std::endl;
		free (compiler_log);
	}

	p = glCreateProgram();

	glAttachShader(p,f);
	glAttachShader(p,v);
	glAttachShader(p,g);

	glLinkProgram(p);
	// comment out this line to not use the shader

	glUseProgram(p);

	GLint loc = glGetUniformLocation(p, "ambientColor");
	if (loc != -1)
	{
		GL_CHECK(glUniform4f(loc, 0.5,0.5,0.5,1.0));
	}

	loc = glGetUniformLocation(p, "diffuseColor");
	if (loc != -1)
	{
		GL_CHECK(glUniform4f(loc, 0.5,0.5,0.5,1.0));
	}

	loc = glGetUniformLocation(p, "specularColor");
	if (loc != -1)
	{
		GL_CHECK(glUniform4f(loc, 0.5,0.5,0.5,1.0));
	}

	loc = glGetUniformLocation(p, "specularExponent");
	if (loc != -1)
	{
		GL_CHECK(glUniform1f(loc, 25.0));
	}

}
Пример #18
0
Renderer::Renderer () {
	//Everybody does this
	glClearColor(0, 0, 0, 1);
	glEnable(GL_DEPTH_TEST);
	glClearDepth(1.0);
	glDepthFunc(GL_LEQUAL);

	//here is stuff for setting up our shaders
	const char* fragFile = "diffuseFrag.frag";
	const char* vertFile = "diffuseVert.vert";
	vertexShader = glCreateShader(GL_VERTEX_SHADER);
	fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	shaderProgram = glCreateProgram();
	
	//load up the source, compile and link the shader program
	const char* vertSource = textFileRead(vertFile);
	const char* fragSource = textFileRead(fragFile);
	glShaderSource(vertexShader, 1, &vertSource, 0);
	glShaderSource(fragmentShader, 1, &fragSource, 0);
	glCompileShader(vertexShader);
	glCompileShader(fragmentShader);

	//For your convenience, i decided to throw in some compiler/linker output helper functions
	//from CIS 565
	GLint compiled;
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compiled);
	if (!compiled)
	{
		printShaderInfoLog(vertexShader);
	} 
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compiled);
	if (!compiled)
	{
		printShaderInfoLog(fragmentShader);
	} 

	//set the attribute locations for our shaders
	glBindAttribLocation(shaderProgram, positionLocation, "vs_position");
	glBindAttribLocation(shaderProgram, normalLocation, "vs_normal");
	glBindAttribLocation(shaderProgram, colorLocation, "vs_color");

	//finish shader setup
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	glLinkProgram(shaderProgram);

	//check for linking success
	GLint linked;
	glGetProgramiv(shaderProgram,GL_LINK_STATUS, &linked);
	if (!linked) 
	{
		printLinkInfoLog(shaderProgram);
	}

	//Get the uniform locations for our shaders, unfortunately they can not be set by us, we have
	//to ask OpenGL for them
	u_modelMatrixLocation = glGetUniformLocation(shaderProgram, "u_modelMatrix");
	u_projMatrixLocation = glGetUniformLocation(shaderProgram, "u_projMatrix");

	//Always remember that it doesn't do much good if you don't have OpenGL actually use the shaders
	glUseProgram(shaderProgram);
}
Пример #19
0
DisplayClass::DisplayClass(void)
{
	prism = new Prism(0, 1.0f, 1.0f, 1.0f, glm::vec4(-0.5f, -0.5f, 0.5f, 1.0f));
	sphere = new Sphere();
	cylinder = new Cylinder();
	lightPos = new glm::vec3(10.0f, 6.0f, 0.0f);
	lightCol = new glm::vec3(1.0f, 1.0f, 1.0f);
	ambientCol = new glm::vec3(1.0f, 1.0f, 1.0f);
	this->camera = new Camera();
	rotation = 0.0f;

	//Call GLEW only _after_ you get the window
	//I had to tell the author of your textbook that ;-)  -Cory
	glewInit();

	//Create the VBOs and IBO we'll be using to render images in OpenGL
	glGenBuffers(1, &vbo);
	glGenBuffers(1, &cbo);
	glGenBuffers(1, &nbo);
	glGenBuffers(1, &ibo);

	// assign locations
	positionLocation = 0;
	colorLocation = 1;
	normalLocation = 2;

	//Everybody does this
	glClearColor(0.6, 0.6, 0.6, 1);
	glEnable(GL_DEPTH_TEST);
	glClearDepth(1.0);
	glDepthFunc(GL_LEQUAL);

	//here is stuff for setting up our shaders
	const char* fragFile = "diffuseFrag.frag";
	const char* vertFile = "diffuseVert.vert";
	vertexShader = glCreateShader(GL_VERTEX_SHADER);
	fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	shaderProgram = glCreateProgram();

	//load up the source, compile and link the shader program
	const char* vertSource = textFileRead(vertFile);
	const char* fragSource = textFileRead(fragFile);
	glShaderSource(vertexShader, 1, &vertSource, 0);
	glShaderSource(fragmentShader, 1, &fragSource, 0);
	glCompileShader(vertexShader);
	glCompileShader(fragmentShader);

	//For your convenience, i decided to throw in some compiler/linker output helper functions
	//from CIS 565
	GLint compiled;
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compiled);
	if (!compiled)
	{
		printShaderInfoLog(vertexShader);
	}
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compiled);
	if (!compiled)
	{
		printShaderInfoLog(fragmentShader);
	} 

	//set the attribute locations for our shaders
	glBindAttribLocation(shaderProgram, positionLocation, "vs_position");
	glBindAttribLocation(shaderProgram, normalLocation, "vs_normal");
	glBindAttribLocation(shaderProgram, colorLocation, "vs_color");

	//finish shader setup
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	glLinkProgram(shaderProgram);

	//check for linking success
	GLint linked;
	glGetProgramiv(shaderProgram,GL_LINK_STATUS, &linked);
	if (!linked) 
	{
		printLinkInfoLog(shaderProgram);
	}

	//Get the uniform locations for our shaders, unfortunately they can not be set by us, we have
	//to ask OpenGL for them
	u_modelMatrixLocation = glGetUniformLocation(shaderProgram, "u_modelMatrix");
	u_projMatrixLocation = glGetUniformLocation(shaderProgram, "u_projMatrix");
	u_lightPosLocation = glGetUniformLocation(shaderProgram, "u_lightPos");
	u_lightColLocation = glGetUniformLocation(shaderProgram, "u_lightCol");
	u_cameraPosLocation = glGetUniformLocation(shaderProgram, "u_cameraPos");
	u_ambientColLocation = glGetUniformLocation(shaderProgram, "u_ambientCol");

	//Always remember that it doesn't do much good if you don't have OpenGL actually use the shaders
	glUseProgram(shaderProgram);

	camera->resizeWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
Пример #20
0
	//inizializza
	void ShaderDX::loadShader(bool  geometry, 
							  const Utility::Path& vs,
							  const Utility::Path& ps, 
							  const Utility::Path& gs,
		                      const std::vector<String>& defines){

		HRESULT hr = NULL;
		DWORD vShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
		//vShaderFlags |= D3D10_SHADER_DEBUG;
		//vShaderFlags |= D3D10_SHADER_FORCE_VS_SOFTWARE_NO_OPT;
#endif

		String vShaderFile = textFileRead(vs);
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		ID3D10Blob* vErrors = NULL;
		hr = D3D10CompileShader(vShaderFile, vShaderFile.size(), String(vs.getFilename()), NULL, NULL, "main", "vs_4_0", vShaderFlags, &vShaderBinary, &vErrors);
		dxShaderError(vErrors);
		DX_ASSERT_MSG(hr);
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		DX_ASSERT_MSG(render->d3dDevice->CreateVertexShader((DWORD*)vShaderBinary->GetBufferPointer(), vShaderBinary->GetBufferSize(), &vShader));
		getContants(render->d3dDevice, "vs", vVariablesRef, vShaderBinary, &vConstantBuffer10, vSizeConstantBuffer);
		getResources(render->d3dDevice, "vs", vResourcesRef, vSamplerRef, vShaderBinary);
		if (vSizeConstantBuffer)
			vBufferCpu.resize(vSizeConstantBuffer);
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		DWORD pShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
		//pShaderFlags |= D3D10_SHADER_DEBUG;
		//pShaderFlags |= D3D10_SHADER_FORCE_PS_SOFTWARE_NO_OPT;
#endif
		String pShaderFile = textFileRead(ps);
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		ID3D10Blob* pErrors = NULL;
		hr = D3D10CompileShader(pShaderFile, pShaderFile.size(), String(ps.getFilename()), NULL, NULL, "main", "ps_4_0", pShaderFlags, &pShaderBinary, &pErrors);
		dxShaderError(pErrors);
		DX_ASSERT_MSG(hr);
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		DX_ASSERT_MSG(render->d3dDevice->CreatePixelShader((DWORD*)pShaderBinary->GetBufferPointer(), pShaderBinary->GetBufferSize(), &pShader));
		getContants(render->d3dDevice, "ps", pVariablesRef, pShaderBinary, &pConstantBuffer10, pSizeConstantBuffer);
		getResources(render->d3dDevice, "ps", pResourcesRef, pSamplerRef, pShaderBinary);
		if (pSizeConstantBuffer)
			pBufferCpu.resize(pSizeConstantBuffer);
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		if (geometry){
			DWORD gShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
			#if defined( DEBUG ) || defined( _DEBUG )
			//gShaderFlags |= D3D10_SHADER_DEBUG;
			//gShaderFlags |= D3D10_SHADER_FORCE_PS_SOFTWARE_NO_OPT;
			#endif
			String gShaderFile = textFileRead(gs);
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			ID3D10Blob* gErrors = NULL;
			hr = D3D10CompileShader(gShaderFile, gShaderFile.size(), String(gs.getFilename()), NULL, NULL, "main", "gs_4_0", gShaderFlags, &gShaderBinary, &gErrors);
			dxShaderError(gErrors);
			DX_ASSERT_MSG(hr);
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			DX_ASSERT_MSG(render->d3dDevice->CreateGeometryShader((DWORD*)gShaderBinary->GetBufferPointer(), gShaderBinary->GetBufferSize(), &gShader));
			getContants(render->d3dDevice, "gs", gVariablesRef, gShaderBinary, &gConstantBuffer10, gSizeConstantBuffer);
			getResources(render->d3dDevice, "gs", gResourcesRef, gSamplerRef, gShaderBinary);
			if (gSizeConstantBuffer)
				gBufferCpu.resize(gSizeConstantBuffer);
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		}
	}
Пример #21
0
void Shader::loadShader(const std::string &vertexShader, const std::string &fragmentShader) {
    const char *vs = textFileRead(vertexShader.c_str());
    const char *fs = textFileRead(fragmentShader.c_str());
    compileAndLink(vs,fs);
}
Пример #22
0
///////////////////////////////////////////////////////////////////////
//load, compile and set the shaders
void setShaders()
{
	char *vs,*fs,*gs;

	v = glCreateShader(GL_VERTEX_SHADER);
	f = glCreateShader(GL_FRAGMENT_SHADER);
	g = glCreateShader(GL_GEOMETRY_SHADER);

	vs = textFileRead("./shader01.vert");
	fs = textFileRead("./shader01.frag");
	gs = textFileRead("./shader01.geom");

	const char * ff = fs;
	const char * vv = vs;
	const char * gg = gs;

	GL_CHECK(glShaderSource(v, 1, &vv,NULL));
	GL_CHECK(glShaderSource(f, 1, &ff,NULL));
	GL_CHECK(glShaderSource(g, 1, &gg,NULL));

	free(vs);free(fs);free(gs);

	GL_CHECK(glCompileShader(v));
	GL_CHECK(glCompileShader(f));
	GL_CHECK(glCompileShader(g));

	GLint blen = 0;
	GLsizei slen = 0;

	glGetShaderiv(v, GL_INFO_LOG_LENGTH , &blen);
	if (blen > 1)
	{
		GLchar* compiler_log = (GLchar*)malloc(blen);
		glGetShaderInfoLog(v, blen, &slen, compiler_log);
		std::cout << "compiler_log vertex shader:\n" << compiler_log << std::endl;
		free (compiler_log);
	}
	blen = 0;
	slen = 0;
	glGetShaderiv(f, GL_INFO_LOG_LENGTH , &blen);
	if (blen > 1)
	{
		GLchar* compiler_log = (GLchar*)malloc(blen);
		glGetShaderInfoLog(f, blen, &slen, compiler_log);
		std::cout << "compiler_log fragment shader:\n" << compiler_log << std::endl;
		free (compiler_log);
	}
	blen = 0;
	slen = 0;
	glGetShaderiv(g, GL_INFO_LOG_LENGTH , &blen);
	if (blen > 1)
	{
		GLchar* compiler_log = (GLchar*)malloc(blen);
		glGetShaderInfoLog(g, blen, &slen, compiler_log);
		std::cout << "compiler_log geometry shader:\n" << compiler_log << std::endl;
		free (compiler_log);
	}

	p = glCreateProgram();

	GL_CHECK(glAttachShader(p,f));
	GL_CHECK(glAttachShader(p,v));
	GL_CHECK(glAttachShader(p,g));

	GL_CHECK(glLinkProgram(p));
	//comment out this line to not use the shader
	GL_CHECK(glUseProgram(p));
}