def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 in vec2 vert; void main() { gl_Position = vec4(vert, 0.0, 1.0); } ''') frag = GL.NewFragmentShader(''' #version 330 out vec4 color; void main() { color = vec4(0.30, 0.50, 1.00, 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer(struct.pack('6f', 0.0, 0.8, -0.6, -0.8, 0.6, -0.8)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) except GL.Error as error: print(error) exit(1)
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 uniform vec2 pos; uniform float zoom; in vec2 vert; out vec2 textcoord; void main() { gl_Position = vec4(vert, 0.0, 1.0); textcoord = ((vert + pos) * zoom) / 2.0 + vec2(0.5, 0.5); } ''') frag = GL.NewFragmentShader(''' #version 330 in vec2 textcoord; out vec4 color; uniform int iter; void main() { vec2 z = vec2(3.0 * (textcoord.x - 0.5), 2.0 * (textcoord.y - 0.5)); vec2 c = vec2(0.0, 1.0); int i; for(i = 0; i < iter; i++) { float x = (z.x * z.x - z.y * z.y) + c.x; float y = (z.y * z.x + z.x * z.y) + c.y; if ((x * x + y * y) > 4.0) break; z.x = x; z.y = y; } float cm = fract((i == iter ? 0.0 : float(i)) * 10 / iter); color = vec4(fract(cm + 0.0 / 3.0), fract(cm + 1.0 / 3.0), fract(cm + 2.0 / 3.0), 1.0); } ''') prog = GL.NewProgram([vert, frag]) context['pos'] = prog['pos'] context['zoom'] = prog['zoom'] vbo = GL.NewVertexBuffer( struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) GL.SetUniform(prog['iter'], 100) except GL.Error as error: print(error) exit(1)
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 in vec2 vert; out vec2 tex; void main() { gl_Position = vec4(vert, 0.0, 1.0); tex = vert / 2.0 + vec2(0.5, 0.5); } ''') frag = GL.NewFragmentShader(''' #version 330 in vec2 tex; out vec4 color; uniform float scale; uniform vec2 center; uniform int iter; void main() { vec2 z = vec2(5.0 * (tex.x - 0.5), 3.0 * (tex.y - 0.5)); vec2 c = vec2(1.33 * (tex.x - 0.5) * scale - center.x, (tex.y - 0.5) * scale - center.y); int i; for(i = 0; i < iter; i++) { vec2 v = vec2((z.x * z.x - z.y * z.y) + c.x, (z.y * z.x + z.x * z.y) + c.y); if (dot(v, v) > 4.0) break; z = v; } float cm = fract((i == iter ? 0.0 : float(i)) * 10 / iter); color = vec4(fract(cm + 0.0 / 3.0), fract(cm + 1.0 / 3.0), fract(cm + 2.0 / 3.0), 1.0); } ''') prog = GL.NewProgram([vert, frag]) context['center'] = prog['center'] context['scale'] = prog['scale'] vbo = GL.NewVertexBuffer( struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) GL.SetUniform(prog['iter'], 100) GL.SetUniform(prog['scale'], 1.0) GL.SetUniform(prog['center'], 0.3, 0.2) except GL.Error as error: print(error) exit(1)
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 in vec2 vert; out vec2 tex_coord; uniform vec2 scale; uniform float rotation; void main() { mat2 rot = mat2(cos(rotation), sin(rotation), -sin(rotation), cos(rotation)); gl_Position = vec4((rot * vert) * scale, 0.0, 1.0); tex_coord = vert; } ''') frag = GL.NewFragmentShader(''' #version 330 uniform sampler2D texture; in vec2 tex_coord; out vec4 color; void main() { color = vec4(texture2D(texture, tex_coord).rgb, 1.0); } ''') prog = GL.NewProgram([vert, frag]) context['rotation'] = prog['rotation'] vbo = GL.NewVertexBuffer( struct.pack('6f', 1.0, 0.0, -0.5, 0.86, -0.5, -0.86)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) GL.SetUniform(prog['scale'], context['height'] / context['width'] * 0.75, 0.75) tex = GL.NewTexture(256, 256, Image.open('../DataFiles/Noise.jpg').tobytes()) GL.UseTexture(tex) except GL.Error as error: print(error) exit(1)
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 330 in vec2 vert; in vec3 vert_color; out vec3 frag_color; uniform vec2 scale; uniform float rotation; void main() { frag_color = vert_color; mat2 rot = mat2(cos(rotation), sin(rotation), -sin(rotation), cos(rotation)); gl_Position = vec4((rot * vert) * scale, 0.0, 1.0); } ''') frag = GL.NewFragmentShader(''' #version 330 in vec3 frag_color; out vec4 color; void main() { color = vec4(frag_color, 1.0); } ''') prog = GL.NewProgram([vert, frag]) context['rotation'] = prog['rotation'] vbo = GL.NewVertexBuffer( struct.pack('15f', 1.0, 0.0, 1.0, 0.0, 0.0, -0.5, 0.86, 0.0, 1.0, 0.0, -0.5, -0.86, 0.0, 0.0, 1.0)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f3f', ['vert', 'vert_color']) GL.SetUniform(prog['scale'], context['height'] / context['width'] * 0.75, 0.75) except GL.Error as error: print(error) exit(1)
def initializeGL(self): try: GL.Init() GL.Viewport(0, 0, context['width'], context['height']) vert = GL.NewVertexShader(''' #version 430 in vec2 vert; out vec3 frag_vert; uniform float ratio; uniform vec3 position; uniform vec3 target; void main() { vec3 X = normalize(position - target); vec3 Y = normalize(cross(X, vec3(0.0, 0.0, 1.0))); vec3 Z = normalize(cross(X, Y)); mat3 M = mat3(X, Y, Z); gl_Position = vec4(vert, 0.0, 1.0); frag_vert = M * vec3(1.0, vert.x * ratio, vert.y); } ''') frag = GL.NewFragmentShader(''' #version 430 struct Face { vec4 color; vec3 T1, T2_T1, T3_T1, T2_T1_C_T3_T1, N; }; in vec3 frag_vert; out vec4 frag; uniform vec3 position; layout (binding = 1) buffer Input { int faces; Face face[]; }; void main() { float factor = 1.0; vec3 R1 = position; vec3 R1_R2 = normalize(frag_vert); vec3 color = vec3(0.0, 0.0, 0.0); for (int h = 0; h < 5; ++h) { int hit = 0; float dist = -1; for (int i = 0; i < faces; ++i) { vec3 T1 = face[i].T1; float f = dot(R1_R2, face[i].N); if (f <= 0.0) continue; float D = dot(R1_R2, face[i].T2_T1_C_T3_T1); if (D == 0.0) continue; vec3 R1_T1 = R1 - T1; float u = dot(R1_T1, face[i].T2_T1_C_T3_T1) / D; if (u < 0.0 || (dist > 0.0 && dist < u)) continue; vec3 R1_R2_C_R1_T1 = cross(R1_R2, R1_T1); float v = dot(face[i].T3_T1, R1_R2_C_R1_T1) / D; if (v < 0.0 || v > 1.0) continue; float w = -dot(face[i].T2_T1, R1_R2_C_R1_T1) / D; if (w < 0.0 || w > 1.0) continue; dist = u; hit = i; } if (dist > 0) { float g = dot(R1_R2, face[hit].N); color += face[hit].color.rgb * factor * g; R1 = R1 - R1_R2 * dist; R1_R2 = reflect(R1_R2, face[hit].N); factor *= 1.0 - face[hit].color.a; if (factor < 0.01) break; } else { break; } } frag = vec4(color, 1.0); } ''') prog = GL.NewProgram([vert, frag]) vbo = GL.NewVertexBuffer(struct.pack('8f', -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)) context['vao'] = GL.NewVertexArray(prog, vbo, '2f', ['vert']) ssbo = GL.NewStorageBuffer(open('../DataFiles/Raytrace-scene.dat', 'rb').read()) GL.UseStorageBuffer(ssbo, 1) GL.SetUniform(prog['ratio'], context['width'] / context['height']) GL.SetUniform(prog['position'], 0.7, 0.7, 0.0) GL.SetUniform(prog['target'], 0, 0, 0) except GL.Error as error: print(error) exit(1)
vbo = GL.NewVertexBuffer(struct.pack('72f', *verts)) vao = GL.NewVertexArray(prog, vbo, '3f1f', ['vert', 'diffuse']) nodes = [GL.NewFramebuffer(256, 256) for i in range(6)] enables = [ GL.ENABLE_BLEND, GL.ENABLE_CULL_FACE, GL.ENABLE_DEPTH_TEST, GL.ENABLE_BLEND + GL.ENABLE_DEPTH_TEST, GL.ENABLE_CULL_FACE + GL.ENABLE_BLEND, GL.ENABLE_BLEND + GL.ENABLE_DEPTH_TEST + GL.ENABLE_CULL_FACE, ] GL.Viewport(0, 0, 256, 256) for i, (fbo, color, depth) in enumerate(nodes): GL.UseFramebuffer(fbo) GL.Clear(*bg[i]) GL.EnableOnly(enables[i]) GL.RenderTriangles(vao, 18) for i, (fbo, color, depth) in enumerate(nodes): GL.UpdateTexture(color, i * 8 + 8, 8, 8, 8, b'\x00' * 256) GL.UpdateTexture(color, i * 8 + 8, 24, 8, 8, b'\xff' * 256) for i, (fbo, color, depth) in enumerate(nodes): GL.UseFramebuffer(fbo) Image.frombytes('RGB', (256, 256), GL.ReadPixels(0, 0, 256, 256, 3)).save('TestResults/c_%d.png' % i) pixels = b''.join(struct.pack('B', int(c * 255)) for c in struct.unpack('65536f', GL.ReadDepthPixels(0, 0, 256, 256))) Image.frombytes('L', (256, 256), pixels).save('TestResults/d_%d.png' % i)