Ejemplo n.º 1
0
/////////////////////////////////////////////////////////////////
// Load a pair of shaders, compile, and link together. Specify the complete
// path and file name of each ASCII shader file. Note, there is no support for
// just loading say a vertex program... you have to do both.
GLhandleARB gltLoadShaderPair(const char *szVertexProg, const char *szFragmentProg)
	{
    // Temporary Shader objects
    GLhandleARB hVertexShader;
    GLhandleARB hFragmentShader; 
    GLhandleARB hReturn = 0;   
    GLint testVal;
	
    // Create shader objects
    hVertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    hFragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
	
    // Load them. If fail clean up and return null
    if(bLoadShaderFile(szVertexProg, hVertexShader) == false)
		{
        glDeleteObjectARB(hVertexShader);
        glDeleteObjectARB(hFragmentShader);
        return NULL;
		}
	
    if(bLoadShaderFile(szFragmentProg, hFragmentShader) == false)
		{
        glDeleteObjectARB(hVertexShader);
        glDeleteObjectARB(hFragmentShader);
        return NULL;
		}
    
    // Compile them
    glCompileShaderARB(hVertexShader);
    glCompileShaderARB(hFragmentShader);
    
    // Check for errors
    glGetObjectParameterivARB(hVertexShader, GL_OBJECT_COMPILE_STATUS_ARB, &testVal);
    if(testVal == GL_FALSE)
		{
        glDeleteObjectARB(hVertexShader);
        glDeleteObjectARB(hFragmentShader);
        return NULL;
		}
    
    glGetObjectParameterivARB(hFragmentShader, GL_OBJECT_COMPILE_STATUS_ARB, &testVal);
    if(testVal == GL_FALSE)
		{
        glDeleteObjectARB(hVertexShader);
        glDeleteObjectARB(hFragmentShader);
        return NULL;
		}
    
    // Link them - assuming it works...
    hReturn = glCreateProgramObjectARB();
    glAttachObjectARB(hReturn, hVertexShader);
    glAttachObjectARB(hReturn, hFragmentShader);
    glLinkProgramARB(hReturn);
	
    // These are no longer needed
    glDeleteObjectARB(hVertexShader);
    glDeleteObjectARB(hFragmentShader);  
    
    return hReturn;  
	}   
GL_Controller::Shader::Shader(const char* vsfn, const char* fsfn)
{
    vertexShader   = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
    fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

    std::string vertexShaderSource   = ReadFile(vsfn);
    std::string fragmentShaderSource = ReadFile(fsfn);

    const char* v = vertexShaderSource.c_str();
    const char* f = fragmentShaderSource.c_str();

    glShaderSourceARB(vertexShader, 1, &v, NULL);
    glShaderSourceARB(fragmentShader, 1, &f, NULL);

    glCompileShaderARB(vertexShader);
    glCompileShaderARB(fragmentShader);

    printInfoLog(vertexShader);
    printInfoLog(fragmentShader);

    shaderProgram = glCreateProgramObjectARB();
    glAttachObjectARB(shaderProgram, vertexShader);
    glAttachObjectARB(shaderProgram, fragmentShader);

    glLinkProgramARB(shaderProgram);
    printInfoLog(shaderProgram);

    glUseProgramObjectARB(shaderProgram);
}
Ejemplo n.º 3
0
static void setup() {
    glewExperimental=GL_TRUE;
    if (glewInit() != GLEW_OK) {
        std::cerr << "glew init fails!" << std::endl;
        exit(EXIT_SUCCESS);
    }
    // set up shader program
    CHECK_GL_ERROR(vertShader = glCreateShader(GL_VERTEX_SHADER));
    CHECK_GL_ERROR(fragShader = glCreateShader(GL_FRAGMENT_SHADER));
    int vlength = strlen(vertShaderSrc) + 1;
    int flength = strlen(fragShaderSrc) + 1;
    CHECK_GL_ERROR(glShaderSourceARB(vertShader, 1, &vertShaderSrc, &vlength));
    CHECK_GL_ERROR(glShaderSourceARB(fragShader, 1, &fragShaderSrc, &flength));
    CHECK_GL_ERROR(glCompileShaderARB(vertShader));
    CHECK_GL_ERROR(glCompileShaderARB(fragShader));

    CheckShaderCompilation(vertShader);
    CheckShaderCompilation(fragShader);

    CHECK_GL_ERROR(shaderProgram = glCreateProgram());
    CHECK_GL_ERROR(glAttachShader(shaderProgram, vertShader));
    CHECK_GL_ERROR(glAttachShader(shaderProgram, fragShader));
    CHECK_GL_ERROR(glLinkProgram(shaderProgram));

    CheckProgramLinkage(shaderProgram);
}
Ejemplo n.º 4
0
void
Shader::setup(const char *vs, const char *fs)
{
	//fprintf(stdout, "vs, fs = %p, %p\n", vs, fs);
	
	//GLuint vid = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	//GLuint fid = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
	GLhandleARB vid = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	GLhandleARB fid = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
	
	glShaderSourceARB(vid, 1, &vs, 0);
	glShaderSourceARB(fid, 1, &fs, 0);
	
	glCompileShaderARB(vid);
	glCompileShaderARB(fid);
	char glslLog[1024];
	GLsizei glslLogSize;
	glGetInfoLogARB(vid, 1024, &glslLogSize, glslLog);
	if (glslLogSize)
		printf("vertex program log: %s\n", glslLog);
	glGetInfoLogARB(fid, 1024, &glslLogSize, glslLog);
	if (glslLogSize)
		printf("fragment program log: %s\n", glslLog);

	pid = glCreateProgramObjectARB();

	glAttachObjectARB(pid, vid);
	glAttachObjectARB(pid, fid);

	// delete shader objects as soon as they have been attached to a program
	glDeleteObjectARB(vid);
	glDeleteObjectARB(fid);
	
	glLinkProgramARB(pid);
}
Ejemplo n.º 5
0
void shadertest::initShader() {
	filehandler fragtext(fragment_shader_path);
	filehandler verttext(vertex_shader_path);

	const char * fs = (const char*) fragtext.getFileAsCharString();
	const char * vs = (const char*) verttext.getFileAsCharString();
	shaderprogram = glCreateProgramObjectARB();

	fragShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
	vertShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	glShaderSourceARB(fragShader, 1, &fs, NULL);
	glShaderSourceARB(vertShader, 1, &vs, NULL);
	glCompileShaderARB(vertShader);
	glCompileShaderARB(fragShader);
	glAttachObjectARB(shaderprogram, fragShader);
	glAttachObjectARB(shaderprogram, vertShader);
	glLinkProgramARB(shaderprogram);

	glGenTextures(1, &img);
	glBindTexture(GL_TEXTURE_2D, img);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 800, 600, 0, GL_BGR,
			GL_UNSIGNED_BYTE, 0);
	glBindTexture(GL_TEXTURE_2D, 0);

	glGenFramebuffersEXT(1, &fbo);
}
Ejemplo n.º 6
0
void ofxShader::setup(string fragmentName, string vertexName) {
	bLoaded = false;
	if (GLEE_ARB_shader_objects) {
		vertexShader = (GLhandleARB) glCreateShader(GL_VERTEX_SHADER);
		fragmentShader = (GLhandleARB) glCreateShader(GL_FRAGMENT_SHADER);

		string vs = loadShaderText(vertexName);
		string fs = loadShaderText(fragmentName);
		const char* vsptr = vs.c_str();
		const char* fsptr = fs.c_str();
		int vssize = vs.size();
		int fssize = fs.size();

		glShaderSourceARB(vertexShader, 1, &vsptr, &vssize);
		glShaderSourceARB(fragmentShader, 1, &fsptr, &fssize);

		glCompileShaderARB(vertexShader);

		//please add compile status check in:
		//GLint compileStatus = 0;
		//glGetObjectParameterivARB( vertexShader, GL_COMPILE_STATUS, &compileStatus );
		//printf("%i \n", compileStatus);

		char infobuffer[1000];
		GLsizei infobufferlen = 0;

		glGetInfoLogARB(vertexShader, 999, &infobufferlen, infobuffer);
		if (infobufferlen != 0) {
			infobuffer[infobufferlen] = 0;
			printf("vertexShader reports: %s \n", infobuffer);
			return;
		}

		glCompileShaderARB(fragmentShader);

		//glGetObjectParameterivARB( fragmentShader, GL_COMPILE_STATUS, &compileStatus );
		//printf("%i \n", compileStatus);

		glGetInfoLogARB(fragmentShader, 999, &infobufferlen, infobuffer);
		if (infobufferlen != 0) {
			infobuffer[infobufferlen] = 0;
			printf("fragmentShader reports: %s \n", infobuffer);
			return;
		}

		shader = glCreateProgramObjectARB();
		glAttachObjectARB(shader, vertexShader);
		glAttachObjectARB(shader, fragmentShader);
		glLinkProgramARB(shader);

		bLoaded = true;
	} else {
		cout << "Sorry, it looks like you can't run 'ARB_shader_objects'." << endl;
		cout << "Please check the capabilites of your graphics card." << endl;
		cout << "http://www.ozone3d.net/gpu_caps_viewer/)" << endl;
	}
}
Ejemplo n.º 7
0
bool Shader::SetShaderFile(char* sVSFileName, char* sFSFileName)
{
	if (glIsProgram(m_Program))
	{
		CleanUp();
	}
	if (!ReadVertextShader(sVSFileName) || !ReadFragmentShader(sFSFileName))
	{
		return false;
	}
	GLint vertCompiled, fragCompiled;			//状态值
	GLint linked;
	//创建shader对象
	m_Vert = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	m_Frag = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

	const GLcharARB *vv = m_VertexShader;
	const GLcharARB *ff = m_FragmentShader;
	//添加shader
	glShaderSourceARB(m_Vert,1,&vv,NULL);
	glShaderSourceARB(m_Frag,1,&ff,NULL);

	//编译shader
	glCompileShaderARB(m_Vert);
	printOpenGLError(); //检查OpenGL错误
	//glGetObjectParameterivARB(m_Vert, GL_OBJECT_COMPILE_STATUS_ARB, &vertCompiled);
	glGetShaderiv(m_Vert, GL_COMPILE_STATUS, &vertCompiled);
	printInfoLog(m_Vert);
	glCompileShaderARB(m_Frag);
	printOpenGLError(); //检查OpenGL错误
	//glGetObjectParameterivARB(m_Frag, GL_OBJECT_COMPILE_STATUS_ARB, &fragCompiled);
	glGetShaderiv(m_Frag, GL_COMPILE_STATUS, &fragCompiled);
	printInfoLog(m_Frag); 

	if (!vertCompiled || !fragCompiled)
		return false;

	//创建程序对象
	m_Program = glCreateProgramObjectARB();

	//绑定shader到程序对象
	glAttachObjectARB(m_Program,m_Vert);
	glAttachObjectARB(m_Program,m_Frag);	

	//链接程序
	glLinkProgramARB(m_Program);
	printOpenGLError(); //检查OpenGL错误
	//glGetObjectParameterivARB(m_Program, GL_OBJECT_COMPILE_STATUS_ARB, &linked);
	glGetProgramiv(m_Program, GL_LINK_STATUS, &linked);  
	printInfoLog(m_Program);

	if (!linked)
		return false; 
	UseShader(true);				
	return true;
}
Ejemplo n.º 8
0
int
main()
{
	GLenum shader_prog, shader_vert, shader_frag;
	int i;
	char buf[1024];
	const GLcharARB *vertex_shader_code, *fragment_shader_code;
	GLint loc;

	assert((vertex_shader_code = loadShader("shader.vert")));
	assert((fragment_shader_code = loadShader("shader.frag")));
	assert(!SDL_Init(SDL_INIT_EVERYTHING));
	atexit(SDL_Quit);
	SDL_GL_SET_ATTR(RED_SIZE, 8);
	SDL_GL_SET_ATTR(GREEN_SIZE, 8);
	SDL_GL_SET_ATTR(BLUE_SIZE, 8);
	SDL_GL_SET_ATTR(DEPTH_SIZE, 16);
	SDL_GL_SET_ATTR(DOUBLEBUFFER, 1);
	assert(SDL_SetVideoMode(SCRW, SCRH, SCRBPP, SDL_OPENGL /*| SDL_FULLSCREEN*/));
/*	puts((const char *)glGetString(GL_EXTENSIONS));*/
	DO_GL_PROCS(DEFINE_GL_PROC)
	assert((shader_prog = glCreateProgramObjectARB()));
	assert((shader_vert = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)));
	assert((shader_frag = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB)));
	i = strlen(vertex_shader_code);
	printf("vert len: %d\n", i);
	glShaderSourceARB(shader_vert, 1, &vertex_shader_code, &i);
	i = strlen(fragment_shader_code);
	printf("frag len: %d\n", i);
	glShaderSourceARB(shader_frag, 1, &fragment_shader_code, &i);
	glCompileShaderARB(shader_vert);
	glCompileShaderARB(shader_frag);
	glAttachObjectARB(shader_prog, shader_vert);
	glAttachObjectARB(shader_prog, shader_frag);
	glLinkProgramARB(shader_prog);
	i = 0; glGetInfoLogARB(shader_vert, sizeof(buf) - 1, &i, buf); buf[i] = 0; printf("vert error: \"%s\"\n", buf);
	i = 0; glGetInfoLogARB(shader_frag, sizeof(buf) - 1, &i, buf); buf[i] = 0; printf("frag error: \"%s\"\n", buf);
	i = 0; glGetInfoLogARB(shader_prog, sizeof(buf) - 1, &i, buf); buf[i] = 0; printf("prog error: \"%s\"\n", buf);
	glUseProgramObjectARB(shader_prog);
	loc = glGetUniformLocationARB(shader_prog, "time");
	while (!SDL_GetKeyState(0)[SDLK_ESCAPE]) {
		float time = (float)SDL_GetTicks() / 2000.0f;
		SDL_PumpEvents();
		glUniform1fARB(loc, time);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glBegin(GL_QUADS);
		glVertex3f(-1.0f, 1.0f, 0.0f);
		glVertex3f(1.0f, 1.0f, 0.0f);
		glVertex3f(1.0f, -1.0f, 0.0f);
		glVertex3f(-1.0f, -1.0f, 0.0f);
		glEnd();
		SDL_GL_SwapBuffers();
	}
	return 0;
}
Ejemplo n.º 9
0
bool CreateShader(char *VShader, char *PShader, GLhandleARB *glContext) {

   printf("Loading vertex shader:%s\n", VShader) ;
   printf("Loading fragment shader:%s\n", PShader) ;   
   
   char error[4096];
   int result;
   GLhandleARB glVertexShader;
   GLhandleARB glPixelShader;

   // Create the program object.
   *glContext = glCreateProgramObjectARB();

   // Load the vertex shader.
   char *ShaderCode = loadShader(VShader);
   if(!ShaderCode) return false;
   glVertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
   glShaderSourceARB(glVertexShader, 1, (const char**)&ShaderCode, NULL);
   glCompileShaderARB(glVertexShader);
   printInfoLog(glVertexShader) ;
   glGetObjectParameterivARB(glVertexShader, GL_OBJECT_COMPILE_STATUS_ARB, &result);
   glAttachObjectARB(*glContext, glVertexShader);
   delete[] ShaderCode;
   ShaderCode = NULL;

   // Load the pixel shader.
   ShaderCode = loadShader(PShader);
   if(!ShaderCode) return false;
   glPixelShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
   glShaderSourceARB(glPixelShader, 1, (const char**)&ShaderCode, NULL);
   glCompileShaderARB(glPixelShader);
   printInfoLog(glPixelShader) ;
   glGetObjectParameterivARB(glPixelShader, GL_OBJECT_COMPILE_STATUS_ARB, &result);
   glAttachObjectARB(*glContext, glPixelShader);
   delete[] ShaderCode;
   ShaderCode = NULL;

   // Link our shaders.
   glLinkProgramARB(*glContext);
   glGetObjectParameterivARB(*glContext, GL_OBJECT_LINK_STATUS_ARB, &result);

   if(!result) {
       glGetInfoLogARB(*glContext, sizeof(error), NULL, error);
       fprintf(stderr, "Error linking shaders...") ;
       return false;
   }

   glDeleteObjectARB(glVertexShader);
   glDeleteObjectARB(glPixelShader);
   return true;
}
Ejemplo n.º 10
0
GLuint GLUtil::CreateShader(GLenum eShaderType, const char *source) {
    GLuint shader = glCreateShaderObjectARB(eShaderType);
    glShaderSourceARB(shader, 1, (const char**)&source, NULL);
    
    glCompileShaderARB(shader);
    
    // debugging
    if (true)
    {
        GLchar *strInfoLog = new GLchar[1000];
        GLsizei nChars;
        glGetInfoLogARB(shader, 999, &nChars, strInfoLog);
        strInfoLog[999] = '\0';
        
        if (nChars != 0) {
            const char *strShaderType = NULL;
            switch(eShaderType)
            {
                case GL_VERTEX_SHADER: strShaderType = "Vertex"; break;
                case GL_GEOMETRY_SHADER: strShaderType = "Geometry"; break;
                case GL_FRAGMENT_SHADER: strShaderType = "Fragment"; break;
            }
            
            Console::getInstance()->message(CONSOLE_MSG_SYS, "%s Shader: %s", strShaderType, strInfoLog);
        }
        delete [] strInfoLog;
    }
    
	return shader;
}
Ejemplo n.º 11
0
GLhandleARB compile_shader(const char* string,GLenum type) {
    GLhandleARB handle;
    GLint result;				// Compilation code result
    GLint errorLoglength;
    char* errorLogText;
    GLsizei actualErrorLogLength;
    handle = glCreateShaderObjectARB(type);
    if (!handle) {
        printf("Failed creating shader object!");
        return 0;
    }
    glShaderSourceARB(handle, 1, &string, 0);
    glCompileShaderARB(handle);
    //Compilation checking.
    glGetObjectParameterivARB(handle, GL_OBJECT_COMPILE_STATUS_ARB, &result);
    if (!result)
    {
        printf("Failed to compile shader:");
        glGetObjectParameterivARB(handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &errorLoglength);
        errorLogText = malloc(sizeof(char) * errorLoglength);
        glGetInfoLogARB(handle, errorLoglength, &actualErrorLogLength, errorLogText);
        printf("%s\n",errorLogText);
        free(errorLogText);
    }
    return handle;
}
Ejemplo n.º 12
0
static GLuint loadVertexShader(const char * source) {
	
	GLuint shader = glCreateProgramObjectARB();
	if(!shader) {
		LogWarning << "failed to create program object";
		return 0;
	}
	
	GLuint obj = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	if(!obj) {
		LogWarning << "failed to create shader object";
		glDeleteObjectARB(shader);
		return 0;
	}
	
	glShaderSourceARB(obj, 1, &source, NULL);
	glCompileShaderARB(obj);
	if(!checkShader(obj, "compile", GL_OBJECT_COMPILE_STATUS_ARB)) {
		glDeleteObjectARB(obj);
		glDeleteObjectARB(shader);
		return 0;
	}
	
	glAttachObjectARB(shader, obj);
	glDeleteObjectARB(obj);
	
	glLinkProgramARB(shader);
	if(!checkShader(shader, "link", GL_OBJECT_LINK_STATUS_ARB)) {
		glDeleteObjectARB(shader);
		return 0;
	}
	
	return shader;
}
Ejemplo n.º 13
0
GPUShader *GPU_shader_create_lib(const char *code)
{
	GLint status;
	GLcharARB log[5000];
	GLsizei length = 0;
	GPUShader *shader;

	if (!GLEW_ARB_vertex_shader || !GLEW_ARB_fragment_shader)
		return NULL;

	shader = MEM_callocN(sizeof(GPUShader), "GPUShader");

	shader->lib = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

	if (!shader->lib) {
		fprintf(stderr, "GPUShader, object creation failed.\n");
		GPU_shader_free(shader);
		return NULL;
	}

	glShaderSourceARB(shader->lib, 1, (const char**)&code, NULL);

	glCompileShaderARB(shader->lib);
	glGetObjectParameterivARB(shader->lib, GL_OBJECT_COMPILE_STATUS_ARB, &status);

	if (!status) {
		glGetInfoLogARB(shader->lib, sizeof(log), &length, log);
		shader_print_errors("compile", log, code);

		GPU_shader_free(shader);
		return NULL;
	}

	return shader;
}
Ejemplo n.º 14
0
static void load_test_file (const char *filename)
{
   FILE *f;
   GLint size;
   char *code;
   GLint i;

   f = fopen (filename, "r");
   if (f == NULL)
      return;

   fseek (f, 0, SEEK_END);
   size = ftell (f);
   fseek (f, 0, SEEK_SET);

   code = (char *) (malloc (size));
   if (code == NULL) {
      fclose (f);
      return;
   }
   size = fread (code, 1, size, f);
   fclose (f);

   glShaderSourceARB (vert, 1, (const GLcharARB **) (&code), &size);
   glCompileShaderARB (vert);
   if (!CheckObjectStatus (vert))
      exit (0);

   for (i = 0; i < sizeof (attribs) / sizeof (*attribs); i++)
      if (attribs[i].dispatch == A && attribs[i].bind != -1)
         glBindAttribLocationARB (prog, attribs[i].bind, attribs[i].name);
}
Ejemplo n.º 15
0
 void Shader::_compile() {
   if (shader != 0) return;
     
   shader = glCreateShaderObjectARB(shaderType());
   if (shader == 0) {
     throw std::runtime_error("failed to create shader object");
   }
     
   const char *_prog = prog.c_str();
   GLint length = prog.size();
     
   glShaderSourceARB(shader, 1, &_prog, &length);
   glCompileShaderARB(shader);
     
   GLint compile_status = 0;
   glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &compile_status);
   if (compile_status == 0) {
     std::ostringstream err;
     err << "failed to compile shader:" << std::endl << glsl_log(shader) << std::endl;
       
     glDeleteObjectARB(shader);
     shader = 0;
     throw std::runtime_error(err.str());
   }
 }
Ejemplo n.º 16
0
void GlProgram::
compile(GLhandleARB shader, const std::string& sourceString) const
{
	//determine the length of the source code
	GLint length = static_cast<GLint>(sourceString.size());
	
	//upload the shader source into the shader object
	const GLcharARB* code =
        reinterpret_cast<const GLcharARB*>(sourceString.c_str());
	glShaderSourceARB(shader, 1, &code, &length);
	
	//compile the shader source
	glCompileShaderARB(shader);
	
	//check if the shader compiled successfully
	GLint compileStatus;
	glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB,
                              &compileStatus);
	if(!compileStatus)
    {
		//get some more detailed information
		GLcharARB compileLogBuffer[2048];
		GLsizei compileLogSize;
		glGetInfoLogARB(shader, sizeof(compileLogBuffer), &compileLogSize,
                        compileLogBuffer);
		
		//signal an error
		Misc::throwStdErr("%s", compileLogBuffer);
    }
}
Ejemplo n.º 17
0
bool glsl_geometry :: openMessARB(void)
{
  if(m_shaderARB) {
    glDeleteObjectARB( m_shaderARB );
    gem::utils::glsl::delshader(m_shaderARB);
  }
  m_shaderARB = glCreateShaderObjectARB(m_shaderTarget);

  if (!m_shaderARB)
    {
      error("could not create ARB shader object");
      return false;
    }
  const char * vs = m_shaderString;
  glShaderSourceARB( m_shaderARB, 1, &vs, NULL );
  glCompileShaderARB( m_shaderARB );
  glGetObjectParameterivARB( m_shaderARB, GL_OBJECT_COMPILE_STATUS_ARB, &m_compiled );
  if (!m_compiled) {
    GLint	length;
    GLcharARB* log;
    glGetObjectParameterivARB( m_shaderARB, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length );
    log = (GLcharARB*)malloc( length * sizeof(GLcharARB) );
    glGetInfoLogARB( m_shaderARB, length, NULL, log );
    post("compile Info_log:");
    post("%s", log );
    error("shader not loaded");
    free(log);
    return false;
  }

  return true;
}
Ejemplo n.º 18
0
  void Shader::compileFlat() {
    if (shader != 0) return;
      
    shader = glCreateShaderObjectARB(shaderType());
    if (shader == 0) {
      throw std::runtime_error("failed to create shader object");
    }
      
    std::list<std::string> all_source;
    s_tag++;
    _source(all_source);

    std::ostringstream _prog;
    std::copy(all_source.begin(), all_source.end(), std::ostream_iterator<std::string>(_prog, "\n"));
      
    GLint length = _prog.str().size();
    const char *_p = _prog.str().c_str();
    glShaderSourceARB(shader, 1, &_p, &length);
    glCompileShaderARB(shader);
      
    GLint compile_status = 0;
    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &compile_status);
    if (compile_status == 0) {
      std::ostringstream err;
      err << "failed to compile shader:" << std::endl << "----" << std::endl << glsl_log(shader) << std::endl << "----" << std::endl;

      glDeleteObjectARB(shader);
      shader = 0;
      throw std::runtime_error(err.str());
    }
  }
Ejemplo n.º 19
0
int LoadShader( const std::string &filename, ShaderType type )
{
  std::string sfile;
  std::string stype;
  int s;

  if ( type == FRAGMENT )
  {
    stype = "Fragment";
    s = glCreateShaderObjectARB( GL_FRAGMENT_SHADER_ARB );
  }
  else if ( type == VERTEX )
  {
    stype = "Vertex";
    s = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
  }
  else
    return 0;

  LoadFile( filename.c_str(), sfile );

  const char *sf = sfile.c_str();

  glShaderSourceARB( s, 1, &sf, NULL );

  glCompileShaderARB( s );

  PrintIfError( s, stype );

  return s;
}
Ejemplo n.º 20
0
bool GLSL_Shader::compileString ( const char  *_source,
                             GLenum       _type,
                             const char  *_name )
{
  delete [] name   ;
  delete [] source ;

  type   = _type ;
  name   = strdup ( _name   ) ;
  source = strdup ( _source ) ;

  GLint stat ;

  handle = glCreateShaderObjectARB ( type ) ;

  glShaderSourceARB         ( handle, 1, (const GLcharARB **) & source, NULL);
  glCompileShaderARB        ( handle ) ;
  glGetObjectParameterivARB ( handle, GL_OBJECT_COMPILE_STATUS_ARB, & stat ) ;
  showShaderInfo            ( "Compiling", handle, name ) ;

  if ( ! stat )
  {
    fprintf ( stderr, "Failed to compile shader '%s'.\n", name ) ;
    return false ;
  }

  return true ;
}
Ejemplo n.º 21
0
Archivo: Shader.cpp Proyecto: BSkin/MTG
GLhandleARB Shader::makeShader(const char * fileName, GLenum type)
{
	ifstream file(fileName);
	stringstream ss;
	ss << file.rdbuf();
	string source = ss.str();
	char * mem = (char *)alloca(source.length()+1);
	strcpy(mem, source.c_str());
	//mem[source.length] = '\0';

	GLhandleARB shaderHandle = glCreateShaderObjectARB (type);
	glShaderSourceARB (shaderHandle, 1, (const GLcharARB **)&mem, 0);
	glCompileShaderARB (shaderHandle);
	int status; glGetObjectParameterivARB (shaderHandle, GL_OBJECT_COMPILE_STATUS_ARB, &status);
	if (status != 0) 
		return shaderHandle; //Everything OK...

	//It didn't, so log error information...
	//::log ("\nFailed to compile %s shader \"%s\"...", type == GL_VERTEX_SHADER_ARB ? "VERTEX" : "PIXEL", name.c_str ());
	int length = 0; glGetObjectParameterivARB (shaderHandle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
	const long MAXIMUM_LOG_STRING = 1024; char logString [MAXIMUM_LOG_STRING];
	GLsizei messageLength = min (length, MAXIMUM_LOG_STRING);
	if (messageLength > 0) {
		glGetInfoLogARB (shaderHandle, messageLength, 0, logString);
		MessageBox(NULL, logString, "Shader Error", MB_OK);
	}

	glDeleteObjectARB (shaderHandle); 

	return NULL;
}
Ejemplo n.º 22
0
/* ARB_shader_objects / ARB_vertex_shader / ARB_fragment_shader extensions
   API. */
static void
compile_shader_arb (GLuint *shader, GLenum type, const char *text)
{
    const char* strings[1] = { text };
    GLint gl_status;

    *shader = glCreateShaderObjectARB (type);
    glShaderSourceARB (*shader, 1, strings, 0);
    glCompileShaderARB (*shader);
    glGetObjectParameterivARB (*shader, GL_OBJECT_COMPILE_STATUS_ARB, &gl_status);
    if (gl_status == GL_FALSE) {
        GLint log_size;
        glGetObjectParameterivARB (*shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_size);
        if (0 < log_size) {
            char *log = _cairo_malloc (log_size);
            GLint chars;

            log[log_size - 1] = '\0';
            glGetInfoLogARB (*shader, log_size, &chars, log);
            printf ("OpenGL shader compilation failed.  Shader:\n"
                    "%s\n"
                    "OpenGL compilation log:\n"
                    "%s\n",
                    text, log);

            free (log);
        } else {
            printf ("OpenGL shader compilation failed.\n");
        }

	ASSERT_NOT_REACHED;
    }
}
Ejemplo n.º 23
0
void glCompileShaderFromStrings(GLhandleARB shaderObject,size_t numShaderSources,va_list ap)
	{
	/* Get pointers to all shader source strings: */
	const GLcharARB** strings=new const GLcharARB*[numShaderSources];
	for(size_t i=0;i<numShaderSources;++i)
		strings[i]=va_arg(ap,const char*);
	
	/* Upload all shader source strings into the shader object: */
	glShaderSourceARB(shaderObject,numShaderSources,strings,0);
	delete[] strings;
	
	/* Compile the shader source: */
	glCompileShaderARB(shaderObject);
	
	/* Check if the shader compiled successfully: */
	GLint compileStatus;
	glGetObjectParameterivARB(shaderObject,GL_OBJECT_COMPILE_STATUS_ARB,&compileStatus);
	if(!compileStatus)
		{
		/* Get some more detailed information: */
		GLcharARB compileLogBuffer[2048];
		GLsizei compileLogSize;
		glGetInfoLogARB(shaderObject,sizeof(compileLogBuffer),&compileLogSize,compileLogBuffer);
		
		/* Signal an error: */
		Misc::throwStdErr("glCompileShaderFromStrings: Error \"%s\" while compiling shader",compileLogBuffer);
		}
	}
Ejemplo n.º 24
0
void glCompileShaderFromString(GLhandleARB shaderObject,const char* shaderSource)
	{
	/* Determine the length of the source string: */
	GLint shaderSourceLength=GLint(strlen(shaderSource));
	
	/* Upload the shader source into the shader object: */
	const GLcharARB* ss=reinterpret_cast<const GLcharARB*>(shaderSource);
	glShaderSourceARB(shaderObject,1,&ss,&shaderSourceLength);
	
	/* Compile the shader source: */
	glCompileShaderARB(shaderObject);
	
	/* Check if the shader compiled successfully: */
	GLint compileStatus;
	glGetObjectParameterivARB(shaderObject,GL_OBJECT_COMPILE_STATUS_ARB,&compileStatus);
	if(!compileStatus)
		{
		/* Get some more detailed information: */
		GLcharARB compileLogBuffer[2048];
		GLsizei compileLogSize;
		glGetInfoLogARB(shaderObject,sizeof(compileLogBuffer),&compileLogSize,compileLogBuffer);
		
		/* Signal an error: */
		Misc::throwStdErr("glCompileShaderFromString: Error \"%s\" while compiling shader",compileLogBuffer);
		}
	}
Ejemplo n.º 25
0
bool GPU::Shader::PgObject::CompileSrcString( const char *sourceString,
                                              const GLenum pgType,
                                              std::string *logs )
{
    // Ask for a free shader object name to OpenGL.
    m_PgType = pgType;
    if( Instantiate() )
    {
        // Create the shader object.
	    glShaderSourceARB ( m_Id, 1, (const GLcharARB**) &sourceString, NULL );
	    glCompileShaderARB( m_Id );

        // Check the compilation status.
	    GLint status;
        glGetObjectParameterivARB( m_Id, GL_OBJECT_COMPILE_STATUS_ARB, &status );

        if( status )
            return true;
        else if( logs )
            *logs = GPU::Shader::GetLogs( m_Id );

        Release();
    }

    return false;
}
Ejemplo n.º 26
0
GLenum GLShader::CompileProgram(GLenum target, GLcharARB* sourcecode, GLhandleARB *shader)
{
	GLint	logLength;
	GLint	compiled;

	if (sourcecode != 0)
	{
		*shader = glCreateShaderObjectARB(target);
		FailGLError("Failed to create fragment shader");
		glShaderSourceARB(*shader,1,(const GLcharARB **)&sourcecode,0);
		FailGLError("Failed glShaderSourceARB");
		glCompileShaderARB(*shader);
		FailGLError("Failed glCompileShaderARB");

		glGetObjectParameterivARB(*shader,GL_OBJECT_COMPILE_STATUS_ARB,&compiled);
		glGetObjectParameterivARB(*shader,GL_OBJECT_INFO_LOG_LENGTH_ARB,&logLength);
		if (logLength)
		{
			GLcharARB *log = (GLcharARB *)malloc(logLength+128);
			glGetInfoLogARB(*shader, logLength, &logLength, log);
			//printf("Compile log: \n");
			free (log);
		}
		//if (!compiled)
		//	FailWithMessage("shader could not compile");
		
	}
	return GL_NO_ERROR;
}
Ejemplo n.º 27
0
bool ShaderGl::compile(string &messages){
	
	assertGl();

	messages= "Compiling shader: " + source.getPathInfo() + "\n";

	//load source
	GLint length= source.getCode().size();
	const GLcharARB *csource= source.getCode().c_str();
	glShaderSourceARB(handle, 1, &csource, &length);

	//compile
	glCompileShaderARB(handle);
	
	//log
	GLint logLength= 0;
	glGetObjectParameterivARB(handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &logLength);
	char *buffer= new char[logLength+1];
	glGetInfoLogARB(handle, logLength+1, NULL, buffer);
	messages+= buffer;
	delete [] buffer;

	//status
	GLint status= false;
	glGetObjectParameterivARB(handle, GL_OBJECT_COMPILE_STATUS_ARB, &status);
	assertGl();

	return status!=0;
}
Ejemplo n.º 28
0
GLhandleARB LoadGLSLShaderSource(GLenum ShadeType, GLcharARB *Source, char *ErrorLog, int *ErrorLength)
{
	GLhandleARB Handle = 0;

	GLint Length = sizeof(Source)/sizeof(GLcharARB);

	if (Length == 0 || Source == NULL)
		return NULL;

	Handle = glCreateShaderObjectARB(ShadeType);
	glShaderSourceARB(Handle, 1, (const GLcharARB**)&Source, &Length);
	glCompileShaderARB(Handle);

	if (ErrorLog && ErrorLength)
	{
		GLint TotalLength = 0, nCharsWritten = 0;

		glGetObjectParameterivARB(Handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &TotalLength);
		if(TotalLength > 1)
			glGetInfoLogARB(Handle, *ErrorLength, &nCharsWritten, ErrorLog);

		*ErrorLength = TotalLength - 1;
	}

	return Handle;
}
Ejemplo n.º 29
0
GLhandleARB CompileShader(char *ShaderAssembly,GLenum ShaderObject,GLint * bCompiled)
{
	GLhandleARB GLSLShaderObject=glCreateShaderObjectARB(ShaderObject);
	glShaderSourceARB( GLSLShaderObject, 1, (const GLcharARB **)(&ShaderAssembly), NULL );
	glCompileShaderARB( GLSLShaderObject);
	glGetObjectParameterivARB( GLSLShaderObject, GL_OBJECT_COMPILE_STATUS_ARB, bCompiled );
	return GLSLShaderObject;
}
Ejemplo n.º 30
0
void moShaderGLSL::compileFragShader(moText frag_source)
{
    m_FragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
	const char *source = (const char *)frag_source;
	const char **psource = &source;
    glShaderSourceARB(m_FragmentShader, 1, psource, NULL);
    glCompileShaderARB(m_FragmentShader);
    glAttachObjectARB(m_ProgramObject, m_FragmentShader);
}