Exemplo n.º 1
0
    def create(self):
        if not (self.status & Resource.LOADED):
            return

        vs = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(vs, self.vs_str)
        glCompileShader(vs)
        if glGetShaderiv(vs, GL_COMPILE_STATUS) != GL_TRUE:
            raise RuntimeError(glGetShaderInfoLog(vs))

        fs = glCreateShader(GL_FRAGMENT_SHADER)
        glShaderSource(fs, self.fs_str)
        glCompileShader(fs)
        if glGetShaderiv(fs, GL_COMPILE_STATUS) != GL_TRUE:
            raise RuntimeError(glGetShaderInfoLog(fs))

        program = glCreateProgram()
        glAttachShader(program, vs)
        glAttachShader(program, fs)

        glLinkProgram(program)
        if glGetProgramiv(program, GL_LINK_STATUS) != GL_TRUE:
            raise RuntimeError(glGetProgramInfoLog(program))

        glValidateProgram(program)
        validation = glGetProgramiv(program, GL_VALIDATE_STATUS)
        if validation == GL_FALSE:
            raise RuntimeError(glGetProgramInfoLog(program))

        # todo: check for errrors before setting status
        self.program = program
        self.status |= Resource.CREATED
Exemplo n.º 2
0
def make_shader(shadertype, source):
    shader = glCreateShader(shadertype)
    glShaderSource(shader, source)
    glCompileShader(shader)
    retval = ctypes.c_uint(GL_UNSIGNED_INT)
    glGetShaderiv(shader, GL_COMPILE_STATUS, retval)
    if not retval:
        print >> sys.stderr, "Failed to compile shader."
        print glGetShaderInfoLog(shader)
        glDeleteShader(shader)
        raise Exception("Failed to compile shader.")
    return shader
Exemplo n.º 3
0
    def add_shader(self, source, shader_type):
        """ Helper function for compiling a GLSL shader

        Parameters
        ----------
        source : str
            String containing shader source code

        shader_type : valid OpenGL shader type
            Type of shader to compile

        Returns
        -------
        value : int
            Identifier for shader if compilation is successful

        """
        try:
            shader_id = glCreateShader(shader_type)
            glShaderSource(shader_id, source)
            glCompileShader(shader_id)
            if glGetShaderiv(shader_id, GL_COMPILE_STATUS) != GL_TRUE:
                info = glGetShaderInfoLog(shader_id)
                raise RuntimeError('Shader compilation failed: %s' % (info))
            return shader_id
        except:
            glDeleteShader(shader_id)
            raise
Exemplo n.º 4
0
    def add_shader(self, source, shader_type):
        """ Helper function for compiling a GLSL shader

        Parameters
        ----------
        source : str
            String containing shader source code

        shader_type : valid OpenGL shader type
            Type of shader to compile

        Returns
        -------
        value : int
            Identifier for shader if compilation is successful

        """
        try:
            shader_id = glCreateShader(shader_type)
            glShaderSource(shader_id, source)
            glCompileShader(shader_id)
            if glGetShaderiv(shader_id, GL_COMPILE_STATUS) != GL_TRUE:
                info = glGetShaderInfoLog(shader_id)
                raise RuntimeError('Shader compilation failed: %s' % (info))
            return shader_id
        except:
            glDeleteShader(shader_id)
            raise
Exemplo n.º 5
0
def initialize_shader(shader_type, shader_source):

    shader = glCreateShader(shader_type)
    glShaderSource(shader, shader_source)
    glCompileShader(shader)
    result = glGetShaderiv(shader, GL_COMPILE_STATUS)
    if result == 0:
        raise RuntimeError(glGetShaderInfoLog(shader))
    return shader
Exemplo n.º 6
0
    def createShader(self, source, shaderType):

        shader = glCreateShader(shaderType)

        glShaderSource(shader, source, None)
        glCompileShader(shader)

        self.status[shaderType] = glGetShaderiv(shader, GL_COMPILE_STATUS)
        self.infoLog[shaderType] = glGetShaderInfoLog(shader)

        return shader
 def getShader(self, shader_source, shader_type):
     try:
         shader_id = glCreateShader(shader_type)
         glShaderSource(shader_id, shader_source)
         glCompileShader(shader_id)
         if glGetShaderiv(shader_id, GL_COMPILE_STATUS) != GL_TRUE:
             info = glGetShaderInfoLog(shader_id)
             raise RuntimeError('Shader compilation failed:\n %s' % info)
         return shader_id
     except:
         glDeleteShader(shader_id)
         raise
Exemplo n.º 8
0
 def add_shader(self, source, shader_type):
     try:
         #print "TEST"
         #print source
         shader_id = glCreateShader(shader_type)
         glShaderSource(shader_id, source)
         glCompileShader(shader_id)
         if glGetShaderiv(shader_id, GL_COMPILE_STATUS) != GL_TRUE:
            info = glGetShaderInfoLog(shader_id)
            raise RuntimeError('Shader compilation failed: %s' % (info))
         return shader_id
     except:
         glDeleteShader(shader_id)
         raise
Exemplo n.º 9
0
    def CompileShader(self, type, source):
        id = glCreateShader(type)
        glShaderSource(id, source)
        glCompileShader(id)

        result = glGetShaderiv(id, GL_COMPILE_STATUS)
        if (result==GL_FALSE):
            message = glGetShaderInfoLog(id).decode('utf-8')
            shadertype = "Vertex" if type==GL_VERTEX_SHADER else "Fragment"
            print(f"Failed to compile {shadertype} shader!")
            print(message)
            glDeleteShader(id)
            return 0
        return id
Exemplo n.º 10
0
	def compilar(self, shader, nombre):
		"""Compila el shader e informa errores.
		shader = shader a compilar
		nombre = nombre del shader en el informe de errores"""
		# Compilamos el shader
		glCompileShader(shader)
		# Obtenemos su estado
		resultado = glGetShaderiv(shader,GL_COMPILE_STATUS)
		# Si el estado es != 1, hubo errores durante la compilación
		if resultado != 1:
			print "Error al compilar {n}, registro:\n".format(n=nombre) + \
					glGetShaderInfoLog(shader)
		# Devolvemos el estado
		return resultado
Exemplo n.º 11
0
 def _createShader(self, shadertype, source):
     try:
         shader = None
         shader = glCreateShader(shadertype)
         glShaderSource(shader, source)
         glCompileShader(shader)
         if glGetShaderiv(shader, GL_COMPILE_STATUS) != GL_TRUE:
             info = glGetShaderInfoLog(shader)
             raise Exception, "Unable to compile shader. Infolog:\n%s" % (info,)
         return shader
     except Exception:
         # Cleanup on exception
         if shader != None:
             glDeleteShader(shader)
         raise
Exemplo n.º 12
0
    def loadAndCompileShaderObject(self, shader_type, path):

        if not os.path.exists(path):
            raise RuntimeError('Shader source not found at: %s' % path)

        source_file = open(path)
        shader_source = source_file.read()
        source_file.close()
        shader_object = glCreateShader(shader_type)
        glShaderSource(shader_object, shader_source)
        glCompileShader(shader_object)
        if glGetShaderiv(shader_object, GL_COMPILE_STATUS) != GL_TRUE:
            info = glGetShaderInfoLog(shader_object)
            raise RuntimeError('Shader compilation failed:\n %s' % info)
        return shader_object
	def attachShader(self,type,source):
		shader = glCreateShader(type)
		glShaderSource(shader,source)
		glCompileShader(shader)
		shader_log = glGetShaderInfoLog(shader)
		if shader_log:
			if type == GL_FRAGMENT_SHADER:
				str_type = "GL_FRAGMENT_SHADER"
			elif type == GL_VERTEX_SHADER:
				str_type = "GL_VERTEX_SHADER"
			elif type == GL_GEOMETRY_SHADER:
				str_type = "GL_GEOMETRY_SHADER"
			else:
				str_type = "unkown shader type %s" % str(type)
			raise RuntimeError("%s\n%s" % (str_type, shader_log))
		glAttachShader(self.program_id, shader)
		self._shaders.append(shader)
Exemplo n.º 14
0
def load_shader(shader_type, source):
    shader = glCreateShader(shader_type)

    if shader == 0:
        raise ShaderException()

    glShaderSource(shader, source)

    glCompileShader(shader)

    if glGetShaderiv(shader, GL_COMPILE_STATUS, None) == GL_FALSE:
        info_log = glGetShaderInfoLog(shader)
        glDeleteShader(shader)

        raise ShaderException(info_log)

    return shader
Exemplo n.º 15
0
    def _compile(self):
        logging.info('glShaderSource(%d, ...)', self._hnd)
        glShaderSource(self._hnd, self._source)
        logging.info('glCompileShader(%d)', self._hnd)
        glCompileShader(self._hnd)

        if not glGetShaderiv(self._hnd, GL_COMPILE_STATUS):
            logging.error('glCompileShader failed')
            compile_log = glGetShaderInfoLog(self._hnd).decode('utf-8')

            glDeleteShader(self._hnd)

            if self._path == '':
                print_source(self._source)

            pretty_print_log(compile_log, self._path)

            raise RuntimeError('shader failed to compile', compile_log)
Exemplo n.º 16
0
    def _compile(self):
        logging.info('glShaderSource(%d, ...)', self._hnd)
        glShaderSource(self._hnd, self._source)
        logging.info('glCompileShader(%d)', self._hnd)
        glCompileShader(self._hnd)

        if not glGetShaderiv(self._hnd, GL_COMPILE_STATUS):
            logging.error('glCompileShader failed')
            compile_log = glGetShaderInfoLog(self._hnd).decode('utf-8')

            glDeleteShader(self._hnd)

            if self._path == '':
                print_source(self._source)

            pretty_print_log(compile_log, self._path)

            raise RuntimeError('shader failed to compile', compile_log)
Exemplo n.º 17
0
 def compileProgram(path, type):
     #Leo el codigo fuente desde el archivo
     sourceFile = open(path, "r")
     source = sourceFile.read()
     #Creo un shader vacio en memoria de video, del tipo indicado
     #En la variable shader queda almacenado un indice que nos va a permitir identificar este shader de ahora en mas
     shader = glCreateShader(type)
     #Le adjunto el codigo fuente leido desde el archivo
     glShaderSource(shader, source)
     #Intento compilarlo
     glCompileShader(shader)
     #Con la funcion glGelShaderiv puedo obtener el estado del compilador de shaders
     #En este caso le pido el stado de la ultima compilacion ejecutada
     if glGetShaderiv(shader, GL_COMPILE_STATUS) != GL_TRUE:
         #Si la compilacion falla, muestro el error y retorno 0 (shader nulo)
         print(path + ': ' + glGetShaderInfoLog(shader))
         #Me aseguro de liberar los recursos que reserve en memoria de vide, ya que no los voy a usar
         glDeleteShader(shader)
         return 0
     else:
         return shader