def __init__(self, width, height): global myobject, tex_dragon myobject.load("dragon.sbm") load_shaders() ktxobj = KTXObject() tex_dragon = ktxobj.ktx_load("pattern1.ktx")
from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, glBindVertexArray except: print(''' ERROR: PyOpenGL not installed properly. ''') sys.exit() import numpy as np from math import cos, sin import glm identityMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] myobject = SBMObject() ktxobject = KTXObject() overlay = OVERLAY_() program = GLuint(0) mv_location = GLint(0) mvp_location = GLint(0) stretch_location = GLint(0) vao = GLuint(0) buffer = GLuint(0) class Scene: def __init__(self, width, height): global program global mv_location global mvp_location
def __init__(self, width, height): global render_prog global render_vao global tex_alien_array global rain_buffer global droplet_x_offset, droplet_rot_speed, droplet_fall_speed self.width = width self.height = height vs = GLuint(0) fs = GLuint(0) vs_source = ''' #version 410 core layout (location = 0) in int alien_index; out VS_OUT { flat int alien; vec2 tc; } vs_out; struct droplet_t { float x_offset; float y_offset; float orientation; float unused; }; layout (std140) uniform droplets { droplet_t droplet[256]; }; void main(void) { const vec2[4] position = vec2[4](vec2(-0.5, -0.5), vec2( 0.5, -0.5), vec2(-0.5, 0.5), vec2( 0.5, 0.5)); vs_out.tc = position[gl_VertexID].xy + vec2(0.5); float co = cos(droplet[alien_index].orientation); float so = sin(droplet[alien_index].orientation); mat2 rot = mat2(vec2(co, so), vec2(-so, co)); vec2 pos = 0.25 * rot * position[gl_VertexID]; gl_Position = vec4(pos.x + droplet[alien_index].x_offset, pos.y + droplet[alien_index].y_offset, 0.5, 1.0); vs_out.alien = alien_index % 64; } ''' fs_source = ''' #version 410 core layout (location = 0) out vec4 color; in VS_OUT { flat int alien; vec2 tc; } fs_in; uniform sampler2DArray tex_aliens; void main(void) { color = texture(tex_aliens, vec3(fs_in.tc, float(fs_in.alien))); } ''' vs = glCreateShader(GL_VERTEX_SHADER) glShaderSource(vs, vs_source) glCompileShader(vs) glGetShaderInfoLog(vs) fs = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(fs, fs_source) glCompileShader(fs) glGetShaderInfoLog(vs) render_prog = glCreateProgram() glAttachShader(render_prog, vs) glAttachShader(render_prog, fs) glLinkProgram(render_prog) glDeleteShader(vs) glDeleteShader(fs) glGetProgramInfoLog(render_prog) glGenVertexArrays(1, render_vao) glBindVertexArray(render_vao) ktxobj = KTXObject() tex_alien_array = ktxobj.ktx_load("aliens.ktx") glBindTexture(GL_TEXTURE_2D_ARRAY, tex_alien_array) glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glGenBuffers(1, rain_buffer) glBindBuffer(GL_UNIFORM_BUFFER, rain_buffer) glBufferData(GL_UNIFORM_BUFFER, 256 * 4 * 4, None, GL_DYNAMIC_DRAW) for i in range(0, 256): droplet_x_offset.append(random_float() * 2.0 - 1.0) droplet_rot_speed.append( (random_float() + 0.5) * (-3.0 if (i & 1) else 3.0)) droplet_fall_speed.append(random_float() + 0.2) glBindVertexArray(render_vao) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
def __init__(self, width, height): self.width = width self.height = height B = (0x00, 0x00, 0x00, 0x00) W = (0xFF, 0xFF, 0xFF, 0xFF) tex_data = [ B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, ] tex_object.append(glGenTextures(1)) #glGenTextures(1, tex_object[0]); glBindTexture(GL_TEXTURE_2D, tex_object[0]) glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, 16, 16) glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE, tex_data) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) tex_object.append(glGenTextures(1)) ktxobj = KTXObject() tex_object[1] = ktxobj.ktx_load("pattern1.ktx") #tex_object[1] = sb7::ktx::file::load("pattern1.ktx"); myobject.load("torus_nrms_tc.sbm") load_shaders() glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL)
def __init__(self, width, height): global grass_buffer global grass_vao global grass_program global tex_grass_color global tex_grass_length global tex_grass_orientation global tex_grass_bend global uniform self.width = width self.height = height grass_vs = GLuint(0) grass_fs = GLuint(0) grass_vs_source = ''' #version 420 core // Incoming per vertex position in vec4 vVertex; // Output varyings out vec4 color; uniform mat4 mvpMatrix; layout (binding = 0) uniform sampler1D grasspalette_texture; layout (binding = 1) uniform sampler2D length_texture; layout (binding = 2) uniform sampler2D orientation_texture; layout (binding = 3) uniform sampler2D grasscolor_texture; layout (binding = 4) uniform sampler2D bend_texture; int random(int seed, int iterations) { int value = seed; int n; for (n = 0; n < iterations; n++) { value = ((value >> 7) ^ (value << 9)) * 15485863; } return value; } vec4 random_vector(int seed) { int r = random(gl_InstanceID, 4); int g = random(r, 2); int b = random(g, 2); int a = random(b, 2); return vec4(float(r & 0x3FF) / 1024.0, float(g & 0x3FF) / 1024.0, float(b & 0x3FF) / 1024.0, float(a & 0x3FF) / 1024.0); } mat4 construct_rotation_matrix(float angle) { float st = sin(angle); float ct = cos(angle); return mat4(vec4(ct, 0.0, st, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(-st, 0.0, ct, 0.0), vec4(0.0, 0.0, 0.0, 1.0)); } void main(void) { vec4 offset = vec4(float(gl_InstanceID >> 10) - 512.0, 0.0f, float(gl_InstanceID & 0x3FF) - 512.0, 0.0f); int number1 = random(gl_InstanceID, 3); int number2 = random(number1, 2); offset += vec4(float(number1 & 0xFF) / 256.0, 0.0f, float(number2 & 0xFF) / 256.0, 0.0f); // float angle = float(random(number2, 2) & 0x3FF) / 1024.0; vec2 texcoord = offset.xz / 1024.0 + vec2(0.5); // float bend_factor = float(random(number2, 7) & 0x3FF) / 1024.0; float bend_factor = texture(bend_texture, texcoord).r * 2.0; float bend_amount = cos(vVertex.y); float angle = texture(orientation_texture, texcoord).r * 2.0 * 3.141592; mat4 rot = construct_rotation_matrix(angle); vec4 position = (rot * (vVertex + vec4(0.0, 0.0, bend_amount * bend_factor, 0.0))) + offset; position *= vec4(1.0, texture(length_texture, texcoord).r * 0.9 + 0.3, 1.0, 1.0); gl_Position = mvpMatrix * position; // (rot * position); // color = vec4(random_vector(gl_InstanceID).xyz * vec3(0.1, 0.5, 0.1) + vec3(0.1, 0.4, 0.1), 1.0); // color = texture(orientation_texture, texcoord); color = texture(grasspalette_texture, texture(grasscolor_texture, texcoord).r) + vec4(random_vector(gl_InstanceID).xyz * vec3(0.1, 0.5, 0.1), 1.0); } ''' grass_fs_source = ''' #version 420 core in vec4 color; out vec4 output_color; void main(void) { output_color = color; } ''' grass_blade = [ -0.3, 0.0, 0.3, 0.0, -0.20, 1.0, 0.1, 1.3, -0.05, 2.3, 0.0, 3.3 ] glGenBuffers(1, grass_buffer) glBindBuffer(GL_ARRAY_BUFFER, grass_buffer) ar = array("f", grass_blade) glBufferData(GL_ARRAY_BUFFER, ar.tostring(), GL_STATIC_DRAW) glGenVertexArrays(1, grass_vao) glBindVertexArray(grass_vao) glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, None) glEnableVertexAttribArray(0) grass_program = glCreateProgram() grass_vs = glCreateShader(GL_VERTEX_SHADER) grass_fs = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(grass_vs, grass_vs_source) glShaderSource(grass_fs, grass_fs_source) glCompileShader(grass_vs) glCompileShader(grass_fs) glAttachShader(grass_program, grass_vs) glAttachShader(grass_program, grass_fs) glLinkProgram(grass_program) glDeleteShader(grass_fs) glDeleteShader(grass_vs) uniform.mvpMatrix = glGetUniformLocation(grass_program, "mvpMatrix") ktx = KTXObject() glActiveTexture(GL_TEXTURE1) tex_grass_length = ktx.ktx_load("grass_length.ktx") glActiveTexture(GL_TEXTURE2) tex_grass_orientation = ktx.ktx_load("grass_orientation.ktx") glActiveTexture(GL_TEXTURE3) tex_grass_color = ktx.ktx_load("grass_color.ktx") glActiveTexture(GL_TEXTURE4) tex_grass_bend = ktx.ktx_load("grass_bend.ktx")
def __init__(self, width, height): global render_prog global render_vao global uniform global tex_wall, tex_ceiling, tex_floor self.width = width self.height = height vs = GLuint(0) fs = GLuint(0) vs_source = ''' #version 420 core out VS_OUT { vec2 tc; } vs_out; uniform mat4 mvp; uniform float offset; void main(void) { const vec2[4] position = vec2[4](vec2(-0.5, -0.5), vec2( 0.5, -0.5), vec2(-0.5, 0.5), vec2( 0.5, 0.5)); vs_out.tc = (position[gl_VertexID].xy + vec2(offset, 0.5)) * vec2(30.0, 1.0); gl_Position = mvp * vec4(position[gl_VertexID], 0.0, 1.0); } ''' fs_source = ''' #version 420 core layout (location = 0) out vec4 color; in VS_OUT { vec2 tc; } fs_in; layout (binding = 0) uniform sampler2D tex; void main(void) { color = texture(tex, fs_in.tc); } ''' vs = glCreateShader(GL_VERTEX_SHADER) glShaderSource(vs, vs_source) glCompileShader(vs) glGetShaderInfoLog(vs) fs = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(fs, fs_source) glCompileShader(fs) glGetShaderInfoLog(vs) render_prog = glCreateProgram() glAttachShader(render_prog, vs) glAttachShader(render_prog, fs) glLinkProgram(render_prog) glDeleteShader(vs) glDeleteShader(fs) glGetProgramInfoLog(render_prog) uniform.mvp = glGetUniformLocation(render_prog, "mvp") uniform.offset = glGetUniformLocation(render_prog, "offset") glGenVertexArrays(1, render_vao) glBindVertexArray(render_vao) ktxobj = KTXObject() tex_wall = ktxobj.ktx_load("brick.ktx") tex_ceiling = ktxobj.ktx_load("ceiling.ktx") tex_floor = ktxobj.ktx_load("floor.ktx") textures = [tex_floor, tex_wall, tex_ceiling] for i in range(0, 3): glBindTexture(GL_TEXTURE_2D, textures[i]) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glBindVertexArray(render_vao)
def __init__(self, width, height): global texture global program global vao self.width = width self.height = height vs = GLuint(0) fs = GLuint(0) vs_source = ''' #version 410 core uniform vec2 offset; out vec2 tex_coord; void main(void) { const vec4 vertices[] = vec4[](vec4(-0.45, -0.45, 0.5, 1.0), vec4( 0.45, -0.45, 0.5, 1.0), vec4(-0.45, 0.45, 0.5, 1.0), vec4( 0.45, 0.45, 0.5, 1.0)); gl_Position = vertices[gl_VertexID] + vec4(offset, 0.0, 0.0); tex_coord = vertices[gl_VertexID].xy * 3.0 + vec2(0.45 * 3); } ''' fs_source = ''' #version 410 core uniform sampler2D s; out vec4 color; in vec2 tex_coord; void main(void) { color = texture(s, tex_coord); } ''' #// Generate a name for the texture glGenTextures(1, texture) #// Load texture from file ktxobj = KTXObject() texture = ktxobj.ktx_load("rightarrows.ktx") #// Now bind it to the context using the GL_TEXTURE_2D binding point glBindTexture(GL_TEXTURE_2D, texture) vs = glCreateShader(GL_VERTEX_SHADER) glShaderSource(vs, vs_source) glCompileShader(vs) fs = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(fs, fs_source) glCompileShader(fs) program = glCreateProgram() glAttachShader(program, vs) glAttachShader(program, fs) glLinkProgram(program) glDeleteShader(vs) glDeleteShader(fs) glGetProgramInfoLog(program) glGenVertexArrays(1, vao) glBindVertexArray(vao)