Exemple #1
0
	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)
Exemple #3
0
    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)
Exemple #4
0
    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)
Exemple #6
0
    def __init__(self, **kwargs):
        super(CustomWidget, self).__init__(**kwargs)

        with self.canvas:

            GL.Init()

            self.vert = GL.NewVertexShader('''
				#version 330

				in vec2 vert;

				void main() {
					gl_Position = vec4(vert, 0.0, 1.0);
				}
			''')

            self.frag = GL.NewFragmentShader('''
				#version 330
				
				out vec4 color;

				void main() {
					color = vec4(0.30, 0.50, 1.00, 1.0);
				}
			''')

            self.prog = GL.NewProgram([self.vert, self.frag])

            self.vbo = GL.NewVertexBuffer(
                struct.pack('6f', 0.0, 0.8, -0.6, -0.8, 0.6, -0.8))
            self.vao = GL.NewVertexArray(self.prog, self.vbo, '2f', ['vert'])

            self.draw()

            Callback(self.draw)
Exemple #7
0
import ModernGL as GL
import GLWindow as WND
import struct

WND.Init()
GL.Init()

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;
Exemple #8
0
width, height = 800, 600
start = time.time()

pygame.init()
pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL)

GL.Init()

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;
Exemple #9
0
]

WND.Init()
GL.Init()

grass_vert = GL.NewVertexShader('''
	#version 400

	uniform mat4 mat;

	in vec3 vert;
	in vec3 direction;
	in vec3 color;
	in float thickness;
	in float power;

	out vec3 frag_color;

	void main() {
		vec4 vertex = vec4(vert - (direction * power) / 4.0, 1.0);
		frag_color = color * smoothstep(0.0, 1.0, vertex.z / 22.0);
		frag_color *= 0.75 + 0.25 * smoothstep(0.0, 1.0, thickness);
		frag_color *= 1.0 - smoothstep(0.0, 1.0, thickness);
		gl_Position = mat * vertex + vec4(thickness, 0.0, 0.0, 0.0);
	}
''')

grass_frag = GL.NewFragmentShader('''
	#version 400

	in vec3 frag_color;
Exemple #10
0
import ModernGL as GL
import GLWindow as WND
import struct, random

WND.Init()
GL.Init()

tvert = GL.NewVertexShader('''
	#version 330

	uniform vec2 acc;

	in vec2 in_pos;
	in vec2 in_prev;

	out vec2 out_pos;
	out vec2 out_prev;

	void main() {
		out_pos = in_pos * 2.0 - in_prev + acc;
		out_prev = in_pos;
	}
''')

vert = GL.NewVertexShader('''
	#version 330

	in vec2 vert;

	void main() {
		gl_Position = vec4(vert, 0.0, 1.0);
Exemple #11
0
WND.Init()
GL.Init()

# gl_InstanceID is added to the rotation.
# Now different instances won't overlap all the time.

vert = GL.NewVertexShader('''
	#version 330

	in vec2 vert;
	in vec3 vert_color;

	out vec3 frag_color;

	uniform float rotate;
	uniform vec2 scale;

	void main() {
		float r = rotate * (0.5 + gl_InstanceID * 0.05);
		mat2 rot = mat2(cos(r), sin(r), -sin(r), cos(r));
		gl_Position = vec4((rot * vert) * scale, 0.0, 1.0);
		frag_color = vert_color;
	}
''')

frag = GL.NewFragmentShader('''
	#version 330

	in vec3 frag_color;
	out vec4 color;
Exemple #12
0
import GLWindow as WND
import ModernGL as GL
import numpy, random
import math

WND.Init()
GL.Init()

points = GL.NewProgram([
    GL.NewVertexShader('''
		#version 330 core

		in vec2 vert;
		uniform vec2 scale;

		void main() {
			gl_Position = vec4(vert / scale, 0.0, 1.0);
		}
	'''),
    GL.NewGeometryShader('''
		#version 330 core

		layout(points) in;
		layout(line_strip, max_vertices = 17) out;

		uniform vec2 scale;

		void main() {
			for (int i = 0; i <= 16; ++i) {
				float a = 3.14159265 * 2.0 * ((i * 6) / 16.0);
				vec2 move = vec2(cos(a) * 0.1, sin(a) * 0.1) / scale;
Exemple #13
0
import ModernGL as GL
import GLWindow as WND
import struct, time

WND.Init()
GL.Init()

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() {
Exemple #14
0
vert = GL.NewVertexShader('''
	#version 330

	in vec3 vert;

	uniform float znear;
	uniform float zfar;
	uniform float fovy;
	uniform float ratio;

	uniform vec3 center;
	uniform vec3 eye;
	uniform vec3 up;

	mat4 perspective() {
		float zmul = (-2.0 * znear * zfar) / (zfar - znear);
		float ymul = 1.0 / tan(fovy * 3.14159265 / 360);
		float xmul = ymul / ratio;

		return mat4(
			xmul, 0.0, 0.0, 0.0,
			0.0, ymul, 0.0, 0.0,
			0.0, 0.0, -1.0, -1.0,
			0.0, 0.0, zmul, 0.0
		);
	}

	mat4 lookat() {
		vec3 forward = normalize(center - eye);
		vec3 side = normalize(cross(forward, up));
		vec3 upward = cross(side, forward);
		return mat4(
			side.x, upward.x, -forward.x, 0,
			side.y, upward.y, -forward.y, 0,
			side.z, upward.z, -forward.z, 0,
			-dot(eye, side), -dot(eye, upward), dot(eye, forward), 1
		);
	}

	void main() {
		gl_Position = perspective() * lookat() * vec4(vert, 1.0);
	}
''')
Exemple #15
0
GL.UpdateStorageBuffer(C, 0, struct.pack('64i', *[4 for i in range(64)]))
GL.UpdateStorageBuffer(C, 64, struct.pack('32i', *[9 for i in range(32)]))

print(struct.unpack('64i', GL.ReadStorageBuffer(B)))
print(struct.unpack('64i', GL.ReadStorageBuffer(BX)))
print(struct.unpack('64i', GL.ReadStorageBuffer(BY)))
print(struct.unpack('64i', GL.ReadStorageBuffer(BZ)))
print(struct.unpack('64i', GL.ReadStorageBuffer(C)))

vert = GL.NewVertexShader('''
	#version 330

	in vec3 vert;
	in float diffuse;

	out float d;

	void main() {
		gl_Position = vec4(vert, 1.0);
		d = diffuse;
	}
''')

frag = GL.NewFragmentShader('''
	#version 330
	
	in float d;
	out vec4 color;

	void main() {
		color = vec4(0.30 * d, 0.50 * d, 1.00 * d, 0.5);
Exemple #16
0
WND.Init()
GL.Init()

width, height = WND.GetSize()

circles_vert = GL.NewVertexShader('''
	#version 430

	in vec2 vert;
	uniform vec2 scale;

	struct Circle {
		vec2 pos;
		float radius;
	};

	uniform Circles {
		int circles;
		Circle circle[64];
	};
	
	void main() {
		gl_Position = vec4((circle[gl_InstanceID].pos + vert * circle[gl_InstanceID].radius) * scale, 0.0, 1.0);
	}
''')

circles_frag = GL.NewFragmentShader('''
	#version 430
	
	out vec4 color;
Exemple #17
0
import ModernGL as GL
import GLWindow as WND
import struct, math

WND.Init()
GL.Init()

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;
Exemple #18
0
	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)
import ModernGL as GL
import GLWindow as WND
import struct

WND.Init()
GL.Init()

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));
Exemple #20
0
# Rendering with modern OpenGL:

# First we will need shaders, buffers and vertex arrays

# Shaders and Programs:
# a shader is a computer program that is used to do shading,
# transform vertices to the screen coordinates and assign colors to it.

# The first example will contain a simple Vertex and Fragment shader.
# The vertex shader will pass the vertices as is.

vert = GL.NewVertexShader('''
	#version 330
	in vec2 vert;
	void main() {
		gl_Position = vec4(vert, 0.0, 1.0);
	}
''')

# The fragment shader will assign a blue color for each fragment.

frag = GL.NewFragmentShader('''
	#version 330
	out vec4 color;
	void main() {
		color = vec4(0.30, 0.50, 1.00, 1.0);
	}
''')

# We will need a program object that will contain our shaders attached and linked together.
Exemple #21
0
# Lets add a color attribute per vertex
# We need the color in the fragment shader but we can specify attributes
# for the vertex shader so the value will be forwarded for the fragment shader

# VertexShader:

# the vert_color is an input
# the frag_color is an output

vert = GL.NewVertexShader('''
	#version 330

	in vec2 vert;
	in vec3 vert_color;

	out vec3 frag_color;

	void main() {
		gl_Position = vec4(vert, 0.0, 1.0);
		frag_color = vert_color;
	}
''')

# FragmentShader:

# the frag_color is an input
# the color is the output

frag = GL.NewFragmentShader('''
	#version 330