def display(self): GL.glEnable(GL.GL_CULL_FACE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glClearColor(100, 100, 100, 100) #Render G-buffer self.frameBuffer_.bind() GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glUseProgram(self.shaderMesh_) GL.glBindFragDataLocation(self.shaderMesh_, 0, "outputPos") GL.glBindFragDataLocation(self.shaderMesh_, 1, "outputNormal") GL.glBindFragDataLocation(self.shaderMesh_, 2, "outputMat") GL.glBindFragDataLocation(self.shaderMesh_, 3, "outputLight") GL.glUniformMatrix4fv( self.viewMatrixUnif_, 1, GL.GL_TRUE, np.ascontiguousarray(self.viewMat_, dtype=np.float32)) GL.glUniformMatrix4fv( self.projMatrixUnif_, 1, GL.GL_TRUE, np.ascontiguousarray(self.projMat_, dtype=np.float32)) GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.envMap_.frameBufferShadows_.get_shadow_map()) GL.glUniform1i(self.textSMUnif_, 0) GL.glUniform3f(self.camPosRenderUnif_, self.camera_.obs_[0], self.camera_.obs_[1], self.camera_.obs_[2]) GL.glUniform1i(self.numVLPUnif_, self.envMap_.numVPL_) GL.glUniform1i(self.numVLPAxisUnif_, self.envMap_.numPtsAxis_) GL.glBindBufferBase(GL.GL_SHADER_STORAGE_BUFFER, 0, self.envMap_.vlpPosSSBO_) GL.glBindBufferBase(GL.GL_SHADER_STORAGE_BUFFER, 1, self.envMap_.vlpIntSSBO_) GL.glBindBufferBase(GL.GL_SHADER_STORAGE_BUFFER, 2, self.envMap_.vlpViewMatSSBO_) GL.glBindBufferBase(GL.GL_SHADER_STORAGE_BUFFER, 3, self.envMap_.vlpProjMatSSBO_) if self.useFloor_: GL.glUniform3f(self.objColorUnif_, self.planeColor_[0], self.planeColor_[1], self.planeColor_[2]) self.floor_.render_mesh() GL.glUniform3f(self.objColorUnif_, self.objColor_[0], self.objColor_[1], self.objColor_[2]) self.mesh_.render_mesh() GL.glUseProgram(0) GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0) #Compute NN screenproc.computeAOTexture(self.gridSize_) #Render result. GL.glDisable(GL.GL_CULL_FACE) GL.glDisable(GL.GL_DEPTH_TEST) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) attachments = [GL.GL_COLOR_ATTACHMENT0] GL.glDrawBuffers(1, attachments) GL.glUseProgram(self.shaderRender_) GL.glBindFragDataLocation(self.shaderRender_, 0, "outputColor") GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.outTexture_) GL.glUniform1i(self.textUnif_, 0) GL.glActiveTexture(GL.GL_TEXTURE1) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(0)) GL.glUniform1i(self.textPosUnif_, 1) GL.glActiveTexture(GL.GL_TEXTURE2) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(1)) GL.glUniform1i(self.textNormalUnif_, 2) GL.glActiveTexture(GL.GL_TEXTURE3) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(2)) GL.glUniform1i(self.textMattUnif_, 3) GL.glActiveTexture(GL.GL_TEXTURE4) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(3)) GL.glUniform1i(self.textLightUnif_, 4) GL.glActiveTexture(GL.GL_TEXTURE0) if self.lighting_: GL.glUniform1f(self.directLightRenderUnif_, 0.0) else: GL.glUniform1f(self.directLightRenderUnif_, 1.0) if self.gi_: if self.sss_: GL.glUniform2f(self.giRenderUnif_, 1.0, 1.0) else: GL.glUniform2f(self.giRenderUnif_, 1.0, 0.0) else: GL.glUniform2f(self.giRenderUnif_, 0.0, 0.0) self.quad_.render_mesh() GL.glUseProgram(0) GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
def program(*shaders): p = GL.glCreateProgram() for s in shaders: GL.glAttachShader(p, s) GL.glBindFragDataLocation(p, 0, 'color') GL.glLinkProgram(p) return check_shader_or_program_status(p)
def setupGL(self): vertexShaderProgram = """#version 150 uniform mat4 M; in vec2 position; in float scalarVal; out float ScalarVal; void main() { ScalarVal = scalarVal; gl_Position = M * vec4(position.x, position.y, 0.0, 1.0); }""" vertexShader = GL.glCreateShader(GL.GL_VERTEX_SHADER) GL.glShaderSource(vertexShader, vertexShaderProgram) GL.glCompileShader(vertexShader) fragmentShaderProgram = """#version 150 uniform float scalarNormalizer; in float ScalarVal; out vec4 outColor; void main() { outColor = vec4(ScalarVal / scalarNormalizer, 0.0, 1.0 - ScalarVal / scalarNormalizer, 1.0); }""" fragmentShader = GL.glCreateShader(GL.GL_FRAGMENT_SHADER) GL.glShaderSource(fragmentShader, fragmentShaderProgram) GL.glCompileShader(fragmentShader) # shader program self.shaderProgram = GL.glCreateProgram() GL.glAttachShader(self.shaderProgram, vertexShader) GL.glAttachShader(self.shaderProgram, fragmentShader) GL.glBindFragDataLocation(self.shaderProgram, 0, b"outColor") # link the program GL.glLinkProgram(self.shaderProgram) # validate the program GL.glValidateProgram(self.shaderProgram) # activate the program GL.glUseProgram(self.shaderProgram) vao = GL.glGenVertexArrays(1) GL.glBindVertexArray(vao) # set up translation matrix m = np.array([[2.0 / self.width, 0.0, 0.0, -1.0], [0.0, -2.0 / self.height, 0.0, 1.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]], dtype='float32') M = GL.glGetUniformLocation(self.shaderProgram, "M") GL.glUniformMatrix4fv(M, 1, True, m)
def frag_location( self, name, buffers = 0 ): """Sets the fragment output name used within the program. Buffers is the number of buffers receiving the fragment output. By default, this is 0. Frag data locations MUST be bound BEFORE linking or the location will not take effect until the shader is linked again! http://www.opengl.org/sdk/docs/man3/xhtml/glBindFragDataLocation.xml """ GL.glBindFragDataLocation( self.handle, buffers, name )
def frag_location(self, name, buffers=0): """Sets the fragment output name used within the program. Buffers is the number of buffers receiving the fragment output. By default, this is 0. Frag data locations MUST be bound BEFORE linking or the location will not take effect until the shader is linked again! http://www.opengl.org/sdk/docs/man3/xhtml/glBindFragDataLocation.xml """ GL.glBindFragDataLocation(self.handle, buffers, name)
def _link_shader(self): for shader in self._shaders: if shader.attached: gl.glDetachShader(self._id, shader.id) gl.glAttachShader(self._id, shader.id) shader.attached = True gl.glBindFragDataLocation(self._id, self._frag_data_loc, self._frag_data_name) gl.glLinkProgram(self._id) status = gl.glGetProgramiv(self._id, gl.GL_LINK_STATUS, None) if status == gl.GL_FALSE: log = gl.glGetProgramInfoLog(self._id) print(f'An error occurred during linking of: {self._name}') print('-' * 20) print(f'{log.decode("utf-8")}') print('-' * 20) return False return True
def compileProgram(shaders: Sequence[int], frag_bindings: Dict[str, int]) -> EnhShaderProgram: program_i: int = GL.glCreateProgram() for shader in shaders: GL.glAttachShader(program_i, shader) for name, value in frag_bindings.items(): GL.glBindFragDataLocation(program_i, value, name) program = EnhShaderProgram(program_i) GL.glLinkProgram(program.program) # Do not perform shader validity checking at compile time. # On some platforms (OSX), the FBO doesn't exist at initializeGL time, or is not bound #program.check_validate() #program.check_linked() program._update_bindings() for shader in shaders: GL.glDeleteShader(shader) return program
def generate_images(self, ptMin, ptMax, vertexs, vertsTrianIds, faces, viewDir, only_texIds = False): # Create the camera. aabbSize = math.sqrt(np.sum((ptMax-ptMin) ** 2)) auxViewLength = math.sqrt(np.sum((viewDir) ** 2)) distance = aabbSize/math.sin((45.0*math.pi)/180.0) self.camera_ = Camera( [0.0, 0.0, 0.0], [(viewDir[0]*distance)/auxViewLength, (viewDir[1]*distance)/auxViewLength, (viewDir[2]*distance)/auxViewLength], [0.0, -1.0, 0.0], float(self.width_)/float(self.height_), 45.0, 0.1, aabbSize*5.0) self.viewMat_ = self.camera_.get_view_natrix() self.projMat_ = self.camera_.get_projection_matrix() self.worldViewProjMat_ = self.projMat_ * self.viewMat_ # Load the mesh. self.mesh_ = MeshRenderer(vertexs, vertsTrianIds, faces, [self.posLoc_, self.normalLoc_, self.trianIdLoc_]) self.frameBuffer_.bind() GL.glClearColor(0,0,0,0) #Render Mesh GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glUseProgram(self.shaderMesh_) GL.glBindFragDataLocation(self.shaderMesh_, 0, "outId") GL.glBindFragDataLocation(self.shaderMesh_, 1, "outNormal") GL.glBindFragDataLocation(self.shaderMesh_, 2, "outColor") GL.glUniformMatrix4fv(self.worldViewProjMatrixUnif_, 1, GL.GL_TRUE, np.ascontiguousarray(self.worldViewProjMat_, dtype=np.float32)) GL.glUniformMatrix4fv(self.worldViewMatrixUnif_, 1, GL.GL_TRUE, np.ascontiguousarray(self.viewMat_, dtype=np.float32)) self.mesh_.render_mesh() GL.glUseProgram(0) GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0) GL.glActiveTexture(GL.GL_TEXTURE0) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(0)) texIds = GL.glGetTexImage(GL.GL_TEXTURE_2D, 0, GL.GL_RED_INTEGER, GL.GL_INT) if not only_texIds: GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(1)) texNormals = GL.glGetTexImage(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, GL.GL_FLOAT) GL.glBindTexture(GL.GL_TEXTURE_2D, self.frameBuffer_.get_texture(2)) texColors = GL.glGetTexImage(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, GL.GL_FLOAT) GL.glBindTexture(GL.GL_TEXTURE_2D, 0) else: texNormals = None texColors = None self.mesh_.delete_buffers() return texIds, texNormals, texColors
GL.glCompileShader(vs) log = GL.glGetShaderInfoLog(vs) if log: print(log) fs = GL.glCreateShader(GL.GL_FRAGMENT_SHADER) GL.glShaderSource(fs, fs_source) GL.glCompileShader(fs) log = GL.glGetShaderInfoLog(fs) if log: print(log) sp = GL.glCreateProgram() GL.glAttachShader(sp, vs) GL.glAttachShader(sp, fs) GL.glBindFragDataLocation(sp, 0, 'out_color') GL.glLinkProgram(sp) GL.glUseProgram(sp) def set_uniform_f(name, value, count=1): location = GL.glGetUniformLocation(sp, name) GL.glUniform1fv(location, count, value) def set_uniform_i(name, value, count=1): location = GL.glGetUniformLocation(sp, name) GL.glUniform1iv(location, count, value) def set_uniform_matrix(name, value, count=1):
def _set_frag_location(self, name, number): GL.glBindFragDataLocation(self._handle, number, name)
def glBindFragDataLocation(id, colorNumber, name): print "glBindFragDataLocation", id, colorNumber, name gl.glBindFragDataLocation(id, colorNumber, name)
def run(): if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0: print(sdl2.SDL_GetError()) return window = sdl2.SDL_CreateWindow( b"Example 1", sdl2.SDL_WINDOWPOS_UNDEFINED, sdl2.SDL_WINDOWPOS_UNDEFINED, 640, 480, sdl2.SDL_WINDOW_OPENGL) context = sdl2.SDL_GL_CreateContext(window) # get Vertex Array Object name vao = GL.glGenVertexArrays(1) # set this new VAO to the active one GL.glBindVertexArray(vao) # vertex data for one triangle triangle_vertices = [0.0, 0.5, 0.5, -0.5, -0.5, -0.5] # convert to ctypes c_float array triangle_array = ((ctypes.c_float * len(triangle_vertices)) (*triangle_vertices)) # get a VBO name from the graphics card vbo = GL.glGenBuffers(1) # bind our vbo name to the GL_ARRAY_BUFFER target GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo) # move the vertex data to a new data store associated with our vbo GL.glBufferData(GL.GL_ARRAY_BUFFER, ctypes.sizeof(triangle_array), triangle_array, GL.GL_STATIC_DRAW) # vertex shader vertexShaderProgram = """#version 100 in vec2 position; void main() { gl_Position = vec4(position, 0.0, 1.0); }""" vertexShader = GL.glCreateShader(GL.GL_VERTEX_SHADER) GL.glShaderSource(vertexShader, vertexShaderProgram) GL.glCompileShader(vertexShader) # fragment shader fragmentShaderProgram = """#version 100 out vec4 outColor; void main() { outColor = vec4(1.0, 1.0, 1.0, 1.0); }""" fragmentShader = GL.glCreateShader(GL.GL_FRAGMENT_SHADER) GL.glShaderSource(fragmentShader, fragmentShaderProgram) GL.glCompileShader(fragmentShader) # shader program shaderProgram = GL.glCreateProgram() GL.glAttachShader(shaderProgram, vertexShader) GL.glAttachShader(shaderProgram, fragmentShader) # color output buffer assignment GL.glBindFragDataLocation(shaderProgram, 0, b"outColor") # link the program GL.glLinkProgram(shaderProgram) # validate the program GL.glValidateProgram(shaderProgram) # activate the program GL.glUseProgram(shaderProgram) # specify the layout of our vertex data posAttrib = GL.glGetAttribLocation(shaderProgram, b"position") GL.glEnableVertexAttribArray(posAttrib) GL.glVertexAttribPointer(posAttrib, 2, GL.GL_FLOAT, False, 0, ctypes.c_voidp(0)) # do the actual drawing GL.glClearColor(1.0, 0.5, 0.0, 1.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT) GL.glDrawArrays(GL.GL_TRIANGLES, 0, int(len(triangle_vertices) / 2)) # show the back buffer sdl2.SDL_GL_SwapWindow(window) # wait for somebody to close the window event = sdl2.SDL_Event() while sdl2.SDL_WaitEvent(ctypes.byref(event)): if event.type == sdl2.SDL_QUIT: break # cleanup GL.glDisableVertexAttribArray(posAttrib) GL.glDeleteProgram(shaderProgram) GL.glDeleteShader(fragmentShader) GL.glDeleteShader(vertexShader) GL.glDeleteBuffers(1, [vbo]) GL.glDeleteVertexArrays(1, [vao]) sdl2.SDL_GL_DeleteContext(context) sdl2.SDL_Quit()
def update_shadow_maps(self, mesh): self.frameBufferShadows_.bind() GL.glDisable(GL.GL_CULL_FACE) GL.glEnable(GL.GL_DEPTH_TEST) GL.glUseProgram(self.shaderRenderShadows_) GL.glBindFragDataLocation(self.shaderRenderShadows_, 0, "outputPos") for vlpIt, curPt in enumerate(self.vlpPos_): xCoord = vlpIt // self.numPtsAxis_ yCoord = vlpIt % self.numPtsAxis_ GL.glViewport(xCoord * self.vlpFBSize_, yCoord * self.vlpFBSize_, self.vlpFBSize_, self.vlpFBSize_) GL.glUniformMatrix4fv( self.viewMatrixUnifShadows_, 1, GL.GL_TRUE, np.ascontiguousarray(self.vlpViewMats_[vlpIt], dtype=np.float32)) GL.glUniformMatrix4fv( self.projMatrixUnifShadows_, 1, GL.GL_TRUE, np.ascontiguousarray(self.vlpProjMats_[vlpIt], dtype=np.float32)) mesh.render_mesh() GL.glUseProgram(0)
def glBindFragDataLocation(id, colorNumber, name): print 'glBindFragDataLocation', id, colorNumber, name gl.glBindFragDataLocation(id, colorNumber, name)