From 3ca078c7e7611fc6c2a4cc1b2f9752fced395819 Mon Sep 17 00:00:00 2001 From: Steffen Temme Date: Sun, 27 Aug 2017 12:51:26 +0200 Subject: [PATCH] Initial commit --- .gitignore | 4 + Lights/LightingManager.py | 38 +++ Lights/Lights.py | 74 +++++ Lights/Spotlight/Spotlight.py | 0 Lights/depthfragment.glsl | 15 + MatrixStuff/Transformations.py | 65 ++++ Objects/Cube/Cube.py | 22 ++ Objects/Cube/cube_geometry.glsl | 363 ++++++++++++++++++++ Objects/Cuboid/Cuboid.py | 22 ++ Objects/Cuboid/cuboid_geometry.glsl | 204 ++++++++++++ Objects/Objects.py | 14 + Objects/Structure.py | 119 +++++++ fragment.glsl | 61 ++++ main.py | 492 ++++++++++++++++++++++++++++ passthroughvertex.glsl | 12 + texturefragment.glsl | 13 + vertex.glsl | 22 ++ 17 files changed, 1540 insertions(+) create mode 100644 .gitignore create mode 100644 Lights/LightingManager.py create mode 100644 Lights/Lights.py create mode 100644 Lights/Spotlight/Spotlight.py create mode 100644 Lights/depthfragment.glsl create mode 100644 MatrixStuff/Transformations.py create mode 100644 Objects/Cube/Cube.py create mode 100644 Objects/Cube/cube_geometry.glsl create mode 100644 Objects/Cuboid/Cuboid.py create mode 100644 Objects/Cuboid/cuboid_geometry.glsl create mode 100644 Objects/Objects.py create mode 100644 Objects/Structure.py create mode 100644 fragment.glsl create mode 100644 main.py create mode 100644 passthroughvertex.glsl create mode 100644 texturefragment.glsl create mode 100644 vertex.glsl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc02803 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +.idea/ +*.dll +*.exe \ No newline at end of file diff --git a/Lights/LightingManager.py b/Lights/LightingManager.py new file mode 100644 index 0000000..4a3a4aa --- /dev/null +++ b/Lights/LightingManager.py @@ -0,0 +1,38 @@ +from OpenGL.GL import * +import numpy as np +from OpenGL.GL.ARB.vertex_array_object import glDeleteVertexArrays +from OpenGL.GL.framebufferobjects import glBindRenderbuffer +from OpenGL.GLUT import * +import OpenGL.GLUT.freeglut +from OpenGL.GLU import * +from OpenGL.GL import * +from ctypes import sizeof, c_float, c_void_p, c_uint +from MatrixStuff.Transformations import * + +class LightingManager: + def __init__(self): + self.Lights = [] + self.renderSteps = [] + + def addLight(self,l): + self.Lights.append(l) + + def removeLight(self,l): + self.Lights.remove(l) + + class __Renderstep: + def __init__(self,program, vai): + self.program = program + self.vai = vai + def __eq__(self, other): + if type(other) is type(self): + return self.program == other.program and self.vai == self.vai + else: + return False + + def addRenderStep(self,program,vai): + r = self.__Renderstep(program,vai) + self.renderSteps.append(r) + def removeRenderStep(self,program,vai): + r = self.__Renderstep(program, vai) + self.renderSteps.remove(r) \ No newline at end of file diff --git a/Lights/Lights.py b/Lights/Lights.py new file mode 100644 index 0000000..b47c0de --- /dev/null +++ b/Lights/Lights.py @@ -0,0 +1,74 @@ +from OpenGL.GL import * +import numpy as np +from OpenGL.GL.ARB.vertex_array_object import glDeleteVertexArrays +from OpenGL.GL.framebufferobjects import glBindRenderbuffer +from OpenGL.GLUT import * +import OpenGL.GLUT.freeglut +from OpenGL.GLU import * +from OpenGL.GL import * +from ctypes import sizeof, c_float, c_void_p, c_uint +from MatrixStuff.Transformations import * + + +class Light: + programId = {} + depthshaderId = -1 + + def getDepthProgram(self,vertexshader=-1,geometryshader=-1): + if ((vertexshader,geometryshader) not in self.programId.keys() and vertexshader != -1 and geometryshader != -1): + if self.depthshaderId == -1: + with open('./Lights/depthfragment.glsl', 'r') as f: + fragment_shader_string = f.read() + fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER) + glShaderSource(fragment_shader_id, fragment_shader_string) + glCompileShader(fragment_shader_id) + if glGetShaderiv(fragment_shader_id, GL_COMPILE_STATUS) != GL_TRUE: + raise RuntimeError(glGetShaderInfoLog(fragment_shader_id)) + + program_id = glCreateProgram() + glAttachShader(program_id, vertexshader) + glAttachShader(program_id, geometryshader) + glAttachShader(program_id, fragment_shader_id) + glLinkProgram(program_id) + self.programId[(vertexshader, geometryshader)] = program_id + return program_id + else: + if (vertexshader,geometryshader) not in self.programId.keys(): + return -1 + return self.programId[(vertexshader,geometryshader)] + + + + def __init__(self): + self.ModelviewProjectionMatrix = np.identity(4) + self.FramebufferId = -1 + self.DepthBuffer = -1 + def prepareForDepthMapping(self): + new = False + if self.FramebufferId == -1: + self.FramebufferId = glGenFramebuffers(1) + new = True + glClearColor(1.0,1.0,1.0,1.0) + glBindFramebuffer(GL_FRAMEBUFFER,self.FramebufferId) + glCullFace(GL_FRONT) + glViewport(0, 0, 512, 512) + if new: + if self.DepthBuffer == -1: + self.DepthBuffer = glGenTextures(1) + glBindTexture(GL_TEXTURE_2D, self.DepthBuffer) + glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT, 512, 512, 0,GL_DEPTH_COMPONENT, GL_FLOAT, None) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, self.FramebufferId, 0) + + DrawBuffers = [GL_NONE] + glDrawBuffers(DrawBuffers) + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE): + return False + + def finishDepthMapping(self): + glCullFace(GL_BACK) + DrawBuffers = [GL_COLOR_ATTACHMENT0] + glDrawBuffers(DrawBuffers) + glClearColor(0.0, 0.0, 0.0, 1.0) + glBindFramebuffer(GL_FRAMEBUFFER,0) \ No newline at end of file diff --git a/Lights/Spotlight/Spotlight.py b/Lights/Spotlight/Spotlight.py new file mode 100644 index 0000000..e69de29 diff --git a/Lights/depthfragment.glsl b/Lights/depthfragment.glsl new file mode 100644 index 0000000..98690a4 --- /dev/null +++ b/Lights/depthfragment.glsl @@ -0,0 +1,15 @@ +#version 410 + +layout(location = 2) in vec3 pos; + +uniform float near; +uniform float far; + +void main() +{ + float z = gl_FragCoord.z; + //convert to linear values + //formula can be found at www.roxlu.com/2014/036/rendering-the-depth-buffer + float c = (2.0 * near) / (far + near - z * (far - near)); + gl_FragDepth = c; +} diff --git a/MatrixStuff/Transformations.py b/MatrixStuff/Transformations.py new file mode 100644 index 0000000..3527260 --- /dev/null +++ b/MatrixStuff/Transformations.py @@ -0,0 +1,65 @@ +import math as math +import numpy as np + + +def lookAt(eyeX,eyeY,eyeZ,cX,cY,cZ,upX,upY,upZ): + F = np.matrix([cX-eyeX,cY-eyeY,cZ-eyeZ]) + UP = np.matrix([upX,upY,upZ]) + f = F / math.sqrt(np.sum(np.square(F))) + UP = UP / math.sqrt(np.sum(np.square(UP))) + s = np.cross(f,UP) + u = np.cross((s/math.sqrt(np.sum(np.square(s)))),f) + mat = np.matrix([ + [s[0,0],s[0,1],s[0,2],0], + [u[0,0],u[0,1],u[0,2],0], + [-f[0,0],-f[0,1],-f[0,2],0], + [0,0,0,1] + ]) + + return np.transpose(mat) + +def orthogonalMatrix(r,l,t,b,f,n): + mat = np.matrix([ + [2/(r-l),0,0,-(r+l)/(r-l)], + [0,2/(t-b),0,-(t+b)/(t-b)], + [0,0,-2/(f-n),-(f+n)/(f-n)], + [0,0,0,1] + ]) + + return np.transpose(mat) + + +def perspectiveMatrix(fovy,aspect,znear,zfar): + fovy_rads = fovy*math.pi/180 + f = math.cos(fovy_rads/2.0)/math.sin(fovy_rads/2.0) + a = (zfar+znear)/(znear-zfar) + b = (2 * zfar * znear) / (znear - zfar) + + mat = np.matrix([[f/aspect,0,0,0], + [0,f,0,0], + [0,0,a,b], + [0,0,-1,0]],np.float32) + + return np.transpose(mat) + +def translate(x, y, z): + mat = np.matrix([[1, 0, 0, x], + [0, 1, 0, y], + [0, 0, 1, z], + [0, 0, 0, 1]], np.float32) + + return np.transpose(mat) + +def rotate(x,y,z,d4 = False): + if not d4: + mat = np.matrix([[1, 0, 0], [0, math.cos(x), -math.sin(x)], [0, math.sin(x), math.cos(x)]], np.float32) + mat = mat * np.matrix([[math.cos(y), 0, -math.sin(y)], [0, 1, 0], [math.sin(y), 0, math.cos(y)]], np.float32) + mat = mat * np.matrix([[math.cos(z), -math.sin(z), 0], [math.sin(z), math.cos(z), 0], [0, 0, 1]], np.float32) + else: + mat = np.matrix([[1, 0, 0, 0], [0, math.cos(x), -math.sin(x), 0], [0, math.sin(x), math.cos(x), 0], [0, 0, 0, 1]], np.float32) + mat = mat * np.matrix([[math.cos(y), 0, -math.sin(y), 0], [0, 1, 0, 0], [math.sin(y), 0, math.cos(y), 0], [0, 0, 0, 1]], np.float32) + mat = mat * np.matrix([[math.cos(z), -math.sin(z), 0, 0], [math.sin(z), math.cos(z), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32) + return np.transpose(mat) + + + diff --git a/Objects/Cube/Cube.py b/Objects/Cube/Cube.py new file mode 100644 index 0000000..b92d7de --- /dev/null +++ b/Objects/Cube/Cube.py @@ -0,0 +1,22 @@ +from OpenGL.GLUT import * +from OpenGL.GLU import * +from OpenGL.GL import * +from Objects.Objects import * +import numpy as np + +class Cube(Object): + GeometryShaderId = -1 + def __init__(self): + Object.__init__(self) + if(Cube.GeometryShaderId == -1): + self.initializeShader() + def initializeShader(self)->bool: + with open('./Objects/Cube/cube_geometry.glsl', 'r') as f: + geometry_shader_string = f.read() + Cube.GeometryShaderId = glCreateShader(GL_GEOMETRY_SHADER) + glShaderSource(Cube.GeometryShaderId, geometry_shader_string) + glCompileShader(Cube.GeometryShaderId) + if glGetShaderiv(Cube.GeometryShaderId, GL_COMPILE_STATUS) != GL_TRUE: + raise RuntimeError(glGetShaderInfoLog(Cube.GeometryShaderId)) + return False + return True \ No newline at end of file diff --git a/Objects/Cube/cube_geometry.glsl b/Objects/Cube/cube_geometry.glsl new file mode 100644 index 0000000..b1f860e --- /dev/null +++ b/Objects/Cube/cube_geometry.glsl @@ -0,0 +1,363 @@ +#version 410 + +layout(points) in; +layout(triangle_strip,max_vertices=24) out; + +uniform mat4 projModelViewMatrix; +uniform mat3 normalMatrix; +uniform mat3 rotMatrix; + +uniform mat4 lightProjModelViewMatrix[7]; +uniform int numLights; +uniform float width; +uniform float height; +uniform float near; +uniform float far; + +layout(location = 0) in vec3 colorgeo[1]; +layout(location = 0) out vec3 colorout; +layout(location = 1) out vec3 normal; +layout(location = 2) out vec3 pos; +layout(location = 3) out vec4 lightviewpos[3]; + +/* + lightpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightpos[i] = lightpos[i] / lightpos[i].w; + lightpos[i].z = (2.0 * near) / (far + near - lightpos[i].z * (far - near)); + lightpos[i] = vec4(lightpos[i].x/2.0 + 0.5,lightpos[i].y/2.0 + 0.5,lightpos[i].z,lightpos[i].w); +*/ + +void main(){ + //hinten + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,-0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,-1)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,-0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,-1)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,-1)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,-1)); + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //vorne + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,1)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,-0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,1)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,1)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,-0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,1)); + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //oben + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,1,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,1,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,1,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,1,0)); + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //unten + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,-0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,-1,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,-0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,-1,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,-0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,-1,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,-0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,-1,0)); + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //inks + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(-1,0,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,-0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(-1,0,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(-1,0,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-0.5,-0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(-1,0,0)); + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //rechts* + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,-0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(1,0,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,-0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(1,0,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,0.5,0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(1,0,0)); + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(0.5,0.5,-0.5),0)); + for(int i = 0;i < numLights; i++){ + lightviewpos[i] = lightProjModelViewMatrix[i] * gl_Position; + lightviewpos[i] = lightviewpos[i] / lightviewpos[i].w; + lightviewpos[i] = vec4(lightviewpos[i].x/2.0 + 0.5,lightviewpos[i].y/2.0 + 0.5,lightviewpos[i].z/2.0 + 0.5,lightviewpos[i].w); + lightviewpos[i].z = (2.0 * near) / (far + near - lightviewpos[i].z * (far - near)); + } + gl_Position = projModelViewMatrix * gl_Position; + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(1,0,0)); + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + +} + diff --git a/Objects/Cuboid/Cuboid.py b/Objects/Cuboid/Cuboid.py new file mode 100644 index 0000000..ed51823 --- /dev/null +++ b/Objects/Cuboid/Cuboid.py @@ -0,0 +1,22 @@ +from OpenGL.GLUT import * +from OpenGL.GLU import * +from OpenGL.GL import * +from Objects.Objects import * + +class Cuboid(Object): + GeometryShaderId = -1 + + def __init__(self): + Object.__init__(self) + if(Cuboid.GeometryShaderId == -1): + self.initializeShader() + def initializeShader(self)->bool: + with open('./Objects/Cuboid/cuboid_geometry.glsl', 'r') as f: + geometry_shader_string = f.read() + Cuboid.GeometryShaderId = glCreateShader(GL_GEOMETRY_SHADER) + glShaderSource(Cuboid.GeometryShaderId, geometry_shader_string) + glCompileShader(Cuboid.GeometryShaderId) + if glGetShaderiv(Cuboid.GeometryShaderId, GL_COMPILE_STATUS) != GL_TRUE: + raise RuntimeError(glGetShaderInfoLog(Cuboid.GeometryShaderId)) + return False + return True \ No newline at end of file diff --git a/Objects/Cuboid/cuboid_geometry.glsl b/Objects/Cuboid/cuboid_geometry.glsl new file mode 100644 index 0000000..cd8003b --- /dev/null +++ b/Objects/Cuboid/cuboid_geometry.glsl @@ -0,0 +1,204 @@ +#version 410 + +layout(points) in; +layout(triangle_strip,max_vertices=24) out; + +uniform mat4 projModelViewMatrix; +uniform mat3 normalMatrix; +uniform mat3 rotMatrix; + +layout(location = 0) in vec3 colorgeo[1]; +layout(location = 1) in vec3 size[1]; +layout(location = 0) out vec3 colorout; +layout(location = 1) out vec3 normal; +layout(location = 2) out vec3 pos; + +void main(){ + //hinten + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,-1)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,-1)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,-1)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,-1)); + + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //vorne + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,1)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,1)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,1)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,0,1)); + + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //oben + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,1,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,1,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,1,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,1,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //unten + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,-1,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,-1,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,-1,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(0,-1,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //links + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(-1,0,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(-1,0,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(-1,0,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(-1,0,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + + //rechts + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(1,0,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(1,0,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(1,0,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),0)); + colorout = colorgeo[0]; + normal = normalize(rotMatrix * vec3(1,0,0)); + + pos = gl_Position.xyz; + EmitVertex(); + + EndPrimitive(); + +} \ No newline at end of file diff --git a/Objects/Objects.py b/Objects/Objects.py new file mode 100644 index 0000000..cf06f42 --- /dev/null +++ b/Objects/Objects.py @@ -0,0 +1,14 @@ +import numpy as np +import typing +class Object: + GeometryShaderId = -1 + def draw(self)->bool: + return False + def initializeShader(self)->bool: + return True + def __init__(self): + self.pos = np.zeros((3)) + self.color = np.zeros((3)) + self.programmId = -1 + def translate(self,M): + self.pos = self.pos * M \ No newline at end of file diff --git a/Objects/Structure.py b/Objects/Structure.py new file mode 100644 index 0000000..428a70b --- /dev/null +++ b/Objects/Structure.py @@ -0,0 +1,119 @@ +from wsgiref.validate import check_errors + +from OpenGL.GL.ARB.vertex_array_object import glDeleteVertexArrays +from OpenGL.GL.framebufferobjects import glBindFramebuffer +from OpenGL.GLUT import * +import OpenGL.GLUT.freeglut +from OpenGL.GLU import * +from OpenGL.GL import * + +import numpy as np + +def check_error(message): + gl_error = glGetError() + if (gl_error != GL_NO_ERROR): + print("Error: " + message) + if (gluErrorString(gl_error)): + print(gluErrorString(gl_error)) + else: + print(hex(gl_error)) + return True + return False + +class Structure: + def __init__(self): + self.Objects = {} + self.vais = {} + self.Matrix = np.identity(4,np.float32) + self.dirty = False + + def addShape(self,program,shape): + if not program in self.Objects.keys(): + self.Objects[program] = [] + self.Objects[program].append(shape) + self.dirty = True + + def removeShape(self,program,shape): + if program in self.Objects.keys(): + self.Objects[program].remove(shape) + if len(self.Objects[program]) == 0: + self.Objects.pop(program) + self.dirty = True + + def buildvertexArrays(self): + if self.dirty: + self.clearVertexArrays() + glEnableClientState(GL_VERTEX_ARRAY) + glEnableClientState(GL_TEXTURE_COORD_ARRAY) + glEnableClientState(GL_NORMAL_ARRAY) + glEnableClientState(GL_COLOR_ARRAY) + self.vais = {} + + for key,objects in self.Objects.items(): + tvai = GLuint(-1) + tpbi = -1 + tcbi = -1 + tsbi = -1 + glGenVertexArrays(1, tvai) + glBindVertexArray(tvai) + + vid = glGetAttribLocation(key, "in_position") + glEnableVertexAttribArray(vid) + + tpbi = glGenBuffers(1) + glBindBuffer(GL_ARRAY_BUFFER, tpbi) + positions = [] + for o in objects: + positions.append(o.pos[0]) + positions.append(o.pos[1]) + positions.append(o.pos[2]) + glBufferData(GL_ARRAY_BUFFER, np.asarray(positions), GL_STATIC_DRAW) + glVertexAttribPointer(vid, 3, GL_FLOAT, GL_FALSE, 0, None) + check_error("Could not create position buffer") + + colors = [] + for o in objects: + colors.append(o.color[0]) + colors.append(o.color[1]) + colors.append(o.color[2]) + tcbi = glGenBuffers(1) + glBindBuffer(GL_ARRAY_BUFFER, tcbi) + glBufferData(GL_ARRAY_BUFFER, np.asarray(colors), GL_STATIC_DRAW) + vc = glGetAttribLocation(key, "MyInColor") + if vc != -1: + glEnableVertexAttribArray(vc) + glVertexAttribPointer(vc, 3, GL_FLOAT, GL_FALSE, 0, None) + check_error("Could not create color buffer") + + if hasattr(objects[0],'size'): + sizes = [] + for o in objects: + sizes.append(o.size[0]) + sizes.append(o.size[1]) + sizes.append(o.size[2]) + tsbi = glGenBuffers(1) + glBindBuffer(GL_ARRAY_BUFFER, tsbi) + glBufferData(GL_ARRAY_BUFFER, np.asarray(sizes), GL_STATIC_DRAW) + vs = glGetAttribLocation(key, "MyInSize") + if vs != -1: + glEnableVertexAttribArray(vs) + glVertexAttribPointer(vs, 3, GL_FLOAT, GL_FALSE, 0, None) + check_error("Could not create size buffer") + + glBindVertexArray(0) + self.vais[key] = (tvai,tpbi,tcbi,tsbi) + self.dirty = False + + def clearVertexArrays(self): + for key,(a,p,c,s) in self.vais.items(): + if p != -1: + glDisableVertexAttribArray(p) + glDeleteBuffers(1,[p]) + if c != -1: + glDisableVertexAttribArray(c) + glDeleteBuffers(1,[c]) + if s != -1 and s != GLuint(-1): + glDisableVertexAttribArray(s) + glDeleteBuffers(1,[s]) + glDeleteVertexArrays(1, a) + check_error("Could not destroy vertex array") \ No newline at end of file diff --git a/fragment.glsl b/fragment.glsl new file mode 100644 index 0000000..91929f4 --- /dev/null +++ b/fragment.glsl @@ -0,0 +1,61 @@ +#version 410 + +layout(location = 0) in vec3 colorin; +layout(location = 1) in vec3 normal; +layout(location = 2) in vec3 pos; +layout(location = 3) in vec4 lightviewpos[3]; +layout(location = 8) in vec2 UV; + +uniform int numLights; +uniform sampler2D ShadowMaps[3]; +uniform vec3 lightpos[3]; +uniform vec3 lightColor[3]; + +layout(location = 0) out vec4 colorOut; +layout(location = 1) out float depth; + + +const float ambientFactor = 0.25; +const float diffuseFactor = 0.5; +const float specFactor = 1.0; +const float shininess = 16.0; +const float screenGamma = 2.2; +const float pitl = 3.14159265359 / 16.0; + +void main() +{ + vec3 colorLinear = ambientFactor * colorin; + + for(int i = 0; i < numLights; i++){ + vec3 lightDir = -normalize(lightpos[i] - pos.xyz); + float lambertian = max(dot(normalize(normal),lightDir),0.0); + float specular = 0; + vec3 viewDir = normalize(-pos.xyz); + vec3 halfDir = normalize(lightDir + viewDir); + float specAngle = max(dot(halfDir, normalize(normal)), 0.0); + specular = int(lambertian > 0)*pow(specAngle, shininess); + //int visible = int(!(texture(ShadowMaps,lightpos[i].xy/2 + vec2(0.5,0.5)).z < (lightpos[i].z))); + float visible = 0; + for(int j = 0; j < 4; j++){ + vec2 offs = vec2(sin(j*pitl),cos(j*pitl)) / 700; + visible += float(!(texture(ShadowMaps[i],lightviewpos[i].xy + offs).x <= (lightviewpos[i].z - 0.0005*tan(acos(dot(normal,-normalize(lightpos[i] - pos.xyz))))))) * 1.0/16.0; + } + if(visible == 0.25) + visible = 1.0; + else + for(int j = 4; j < 16; j++){ + vec2 offs = vec2(sin(j*pitl),cos(j*pitl)) / 700; + visible += float(!(texture(ShadowMaps[i],lightviewpos[i].xy + offs).x <= (lightviewpos[i].z - 0.0005*tan(acos(dot(normal,-normalize(lightpos[i] - pos.xyz))))))) * 1.0/16.0; + } + colorLinear += (visible * 0.5 + 0.5) *(lambertian * diffuseFactor * colorin * lightColor[i] + specular * specFactor*colorin); + //colorLinear = vec3(visible * specular); + } + + vec3 colorGammaCorrected = colorLinear;//pow(colorLinear, vec3(1.0/screenGamma)); + + + //colorOut = vec4(colorin, 1.0)*max(dot(normal,normalize(vec3(0.0,0.0,10.0)-pos)),0.0); + colorOut = vec4(colorGammaCorrected,1.0) * float(gl_FragCoord.y <= 768 && gl_FragCoord.x <= 1024 && gl_FragCoord.x >= 0 && gl_FragCoord.y >= 0); + + depth = gl_FragCoord.z; +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..d3a8154 --- /dev/null +++ b/main.py @@ -0,0 +1,492 @@ +from wsgiref.validate import check_errors + +from OpenGL.GL.ARB.vertex_array_object import glDeleteVertexArrays +from OpenGL.GL.framebufferobjects import glBindFramebuffer +from OpenGL.GLUT import * +import OpenGL.GLUT.freeglut +from OpenGL.GLU import * +from OpenGL.GL import * +from ctypes import sizeof, c_float, c_void_p, c_uint + +import sys +import math as math +from Objects.Cube.Cube import * +from Objects.Cuboid.Cuboid import * +from Objects.Structure import * +from MatrixStuff.Transformations import * +from Lights.Lights import * +from Lights.LightingManager import * +import numpy as np +import time + +name = b'ball_glut' + +program_id = 0 +program2_id = 0 +program3_id = 0 +start = time.time() +frames = 0 +width = 1024 +height = 768 +opening = 45 +l = Light() +def main(): + lm = LightingManager() + lm.addRenderStep(0,0) + lm.addRenderStep(1,1) + lm.removeRenderStep(0,0) + + glutInit(sys.argv) + w = width + h = height + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) + glutInitWindowSize(w,h) + glutCreateWindow(name) + #glutEnterGameMode() + + print("Vendor:", glGetString(GL_VENDOR)) + print("Renderer:", glGetString(GL_RENDERER)) + print("Version:", glGetString(GL_VERSION)) + print("GLSL:", glGetString(GL_SHADING_LANGUAGE_VERSION)) + + c = Cube() + cuboid = Cuboid() + + + glClearColor(0.,0.,0.,1.) + #glShadeModel(GL_SMOOTH) + #glDisable(GL_CULL_FACE) + glEnable(GL_CULL_FACE) + glCullFace(GL_BACK) + glEnable(GL_TEXTURE_2D) + glEnable(GL_DEPTH_TEST) + + #glEnable(GL_LIGHTING) + '''lightZeroPosition = [10.,4.,10.,1.] + lightZeroColor = [0.8,1.0,0.8,1.0] #green tinged + glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition) + glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor) + glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1) + glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05) + glEnable(GL_LIGHT0)''' + glutDisplayFunc(display) + glutReshapeFunc(resize) + glutKeyboardFunc(keyboardHandler) + glutSpecialFunc(funcKeydHandler) + '''glMatrixMode(GL_PROJECTION) + gluPerspective(40.,1.,1.,40.) + glMatrixMode(GL_MODELVIEW)''' + #gluLookAt(0,0,10, + # 0,0,0, + # 0,1,0) + #glPushMatrix() + glViewport(0, 0, w,h) + glFrustum(-10,10,-10,10,0.01,100) + + with open('passthroughvertex.glsl', 'r') as f: + vertex_shader_string = f.read() + passthrough_vertex_shader_id = glCreateShader(GL_VERTEX_SHADER) + glShaderSource(passthrough_vertex_shader_id, vertex_shader_string) + glCompileShader(passthrough_vertex_shader_id) + if glGetShaderiv(passthrough_vertex_shader_id, GL_COMPILE_STATUS) != GL_TRUE: + raise RuntimeError(glGetShaderInfoLog(passthrough_vertex_shader_id)) + + with open('vertex.glsl', 'r') as f: + vertex_shader_string = f.read() + vertex_shader_id = glCreateShader(GL_VERTEX_SHADER) + glShaderSource(vertex_shader_id, vertex_shader_string) + glCompileShader(vertex_shader_id) + if glGetShaderiv(vertex_shader_id, GL_COMPILE_STATUS) != GL_TRUE: + raise RuntimeError(glGetShaderInfoLog(vertex_shader_id)) + + with open('texturefragment.glsl', 'r') as f: + fragment_shader_string = f.read() + texturefragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER) + glShaderSource(texturefragment_shader_id, fragment_shader_string) + glCompileShader(texturefragment_shader_id) + if glGetShaderiv(texturefragment_shader_id, GL_COMPILE_STATUS) != GL_TRUE: + raise RuntimeError(glGetShaderInfoLog(texturefragment_shader_id)) + + with open('fragment.glsl', 'r') as f: + fragment_shader_string = f.read() + fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER) + glShaderSource(fragment_shader_id, fragment_shader_string) + glCompileShader(fragment_shader_id) + if glGetShaderiv(fragment_shader_id, GL_COMPILE_STATUS) != GL_TRUE: + raise RuntimeError(glGetShaderInfoLog(fragment_shader_id)) + + global program_id, program2_id, program3_id + program_id = glCreateProgram() + glAttachShader(program_id, vertex_shader_id) + glAttachShader(program_id,Cube.GeometryShaderId) + #glAttachShader(program_id, Cuboid.GeometryShaderId) + glAttachShader(program_id, fragment_shader_id) + #glAttachShader(program_id, l.FragmentShaderId) + glLinkProgram(program_id) + + + struct = Structure() + struct.addShape(program_id,c) + struct.buildvertexArrays() + struct.clearVertexArrays() + + program3_id = l.getDepthProgram(vertex_shader_id,Cube.GeometryShaderId) + + program2_id = glCreateProgram() + glAttachShader(program2_id, passthrough_vertex_shader_id) + glAttachShader(program2_id, texturefragment_shader_id) + glLinkProgram(program2_id) + + if glGetProgramiv(program_id, GL_LINK_STATUS) != GL_TRUE: + raise RuntimeError(glGetProgramInfoLog(program_id)) + + global vai, pbi,cbi,vai2,vai3 + vai, pbi, cbi = create_vertex_buffers(np.array([0, 0, 1, + 1,1,0, + 1, 0, 0, + 1, -1, 0, + 0, -1, 0, + -1, -1, 0, + -1, 0, 0, + -1, 1, 0, + 0, 1, 0],dtype=np.float32), + np.array([1, 1, 0, + 0, 0, 1, + 0, 1, 0, + 1, 0, 0, + 0, 1, 0, + 0, 0, 1, + 0, 1, 1, + 1, 1, 1, + 0, 0, 1],dtype=np.float32), program_id, + sizes=np.array( + [0.5, 1.5, 1.5, + 0.5, 1.5, 1.5, + 0.5, 1.5, 1.5],dtype=np.float32)) + v = [] + color = [] + for i in range(-11,12): + for j in range(-11,12): + v.append(i) + v.append(j) + v.append(0) + color.append(1) + color.append(1) + color.append(1) + + + '''vai, pbi, cbi = create_vertex_buffers(np.array(v, dtype=np.float32), + np.array(color, dtype=np.float32), program_id, + sizes=np.array( + [0.5, 1.5, 1.5, + 0.5, 1.5, 1.5, + 0.5, 1.5, 1.5], dtype=np.float32))''' + + vai2, _, _ = create_vertex_buffers(np.array([0.4,0.4,0, + 1,0.4,0, + 0.4,1,0, + 0.4,1,0, + 1,0.4,0, + 1,1,0], dtype=np.float32), + np.array([0,0,0, + 1,0,0, + 0,1,0, + 0,1,0, + 1,0,0, + 1,1,0],dtype=np.float32),program2_id) + vai3, _, _ = create_vertex_buffers(np.array([-1, -1, 0, + 1, -1, 0, + -1, 1, 0, + -1, 1, 0, + 1, -1, 0, + 1, 1, 0], dtype=np.float32), + np.array([0, 0, 0, + 1, 0, 0, + 0, 1, 0, + 0, 1, 0, + 1, 0, 0, + 1, 1, 0], dtype=np.float32), program2_id) + glutMainLoop() + return + +projMatrix = perspectiveMatrix(45.0, 400 / 400, 0.01, 100.0); + +def check_error(message): + gl_error = glGetError() + if (gl_error != GL_NO_ERROR): + print("Error: " + message) + if (gluErrorString(gl_error)): + print(gluErrorString(gl_error)) + else: + print(hex(gl_error)) + return True + return False + +def create_vertex_buffers(positions,colors,program_id,sizes=np.array([])): + glEnableClientState(GL_VERTEX_ARRAY) + glEnableClientState(GL_TEXTURE_COORD_ARRAY) + glEnableClientState(GL_NORMAL_ARRAY) + glEnableClientState(GL_COLOR_ARRAY) + + #global vai, pbi, cbi + tvai = GLuint(0) + tpbi = GLuint(0) + tcbi = GLuint(0) + + #test = glGetString(GL_VERSION) + + glGenVertexArrays(1,tvai) + glBindVertexArray(tvai) + + vid = glGetAttribLocation(program_id,"in_position") + glEnableVertexAttribArray(vid) + + tpbi = glGenBuffers(1) + glBindBuffer(GL_ARRAY_BUFFER,tpbi) + glBufferData(GL_ARRAY_BUFFER,positions,GL_STATIC_DRAW) + glVertexAttribPointer(vid,3,GL_FLOAT,GL_FALSE,0,None) + check_error("Could not create position buffer") + + tcbi = glGenBuffers(1) + glBindBuffer(GL_ARRAY_BUFFER,tcbi) + glBufferData(GL_ARRAY_BUFFER, colors,GL_STATIC_DRAW) + vc = glGetAttribLocation(program_id, "MyInColor") + if vc != -1: + glEnableVertexAttribArray(vc) + glVertexAttribPointer(vc, 3, GL_FLOAT, GL_FALSE, 0, None) + check_error("Could not create color buffer") + + if len(sizes) > 0: + sbi = glGenBuffers(1) + glBindBuffer(GL_ARRAY_BUFFER,sbi) + glBufferData(GL_ARRAY_BUFFER,sizes,GL_STATIC_DRAW) + vs = glGetAttribLocation(program_id, "MyInSize") + if vs != -1: + glEnableVertexAttribArray(vs) + glVertexAttribPointer(vs, 3, GL_FLOAT, GL_FALSE, 0 ,None) + check_error("Could not create size buffer") + + glBindVertexArray(0) + return tvai, tpbi, tcbi + +def clear_buffer(buffer_id): + glDisableVertexAttribArray(buffer_id) + glDeleteBuffers(1,[buffer_id]) + +def clear_vertex_array(va_id): + glDeleteVertexArrays(1,va_id) + check_error("Could not destroy vertex array") + +vai, pbi, cbi = 0,0,0 +vai2,pbi2,cbi2 = 0,0,0 +vai3,pbi3,cbi3 = 0,0,0 +rx = 0 +ry = 0 + +def render(program_id,projMatrix,vai,x,y,z): + ident = np.transpose(np.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]], np.float32)) + + glUseProgram(program_id) + check_error("Renderingprogram is not initialized!") + + projection = glGetUniformLocation(program_id, 'projModelViewMatrix') + normal = glGetUniformLocation(program_id, 'normalMatrix') + rot = glGetUniformLocation(program_id, 'rotMatrix') + + glUniformMatrix4fv(projection, 1, GL_FALSE, np.array(translate(x, y, z) * rotate(0,ry,0,True) * projMatrix)) + glUniformMatrix3fv(normal, 1, GL_FALSE, np.array(ident)) + glUniformMatrix3fv(rot, 1, GL_FALSE, np.array(rotate(rx, 0, 0))) + + + glBindVertexArray(vai) + glDrawArrays(GL_POINTS, 0, 9)#529 + check_error("Rendering problem") + + glBindVertexArray(0) + glUseProgram(0) + +def display(): + '''if time.time() - start >= 1: + print(frames+1) + global start, frames + frames = 0 + start = time.time() + else: + global frames + frames += 1''' + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + + global l + l.prepareForDepthMapping() + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + + #newMat = orthogonalMatrix(10, -10, 10, -10, 10, -10) * lookAt(cx, cy, 5, 0, 0, 0, 0, 1,0) * np.identity(4) + #newMat = translate(0, 0, -10) * newMat + + newMat = translate(-cx,-cy,-5)*lookAt(cx,cy,5,0,0,0,0,1,0) * perspectiveMatrix(opening,float(width)/float(height),0.01,100.0) + oldMat = rotate(0, -0.5, 0, True) * projMatrix + glUseProgram(program3_id) + widthid = glGetUniformLocation(program3_id, 'width') + heightid = glGetUniformLocation(program3_id, 'height') + nearid = glGetUniformLocation(program3_id, 'near') + farid = glGetUniformLocation(program3_id, 'far') + glUniform1f(nearid, 0.01) + glUniform1f(farid, 100) + glUniform1f(widthid, width) + glUniform1f(heightid, height) + render(program3_id, newMat, vai, 0, 0, 0) + glFlush() + l.finishDepthMapping() + resize(width, height) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + + glUseProgram(program_id) + widthid = glGetUniformLocation(program_id, 'width') + heightid = glGetUniformLocation(program_id, 'height') + nearid = glGetUniformLocation(program_id, 'near') + farid = glGetUniformLocation(program_id, 'far') + glUniform1f(nearid, 0.01) + glUniform1f(farid, 100) + glUniform1f(widthid,width) + glUniform1f(heightid, height) + + + lightProjModelViewMatrix = glGetUniformLocation(program_id, 'lightProjModelViewMatrix') + numLights = glGetUniformLocation(program_id, 'numLights') + lightpos = glGetUniformLocation(program_id, 'lightpos') + lightcolorid = glGetUniformLocation(program_id, 'lightColor') + + glUniformMatrix4fv(lightProjModelViewMatrix,1,GL_FALSE,np.array(newMat)) + glUniform1iv(numLights, 1, 1) + glUniform3fv(lightpos, 1, [-cx,-cy,-5]) + glUniform3fv(lightcolorid, 1, [4,1,1]) + + texID = glGetUniformLocation(program_id, 'ShadowMaps') + glActiveTexture(GL_TEXTURE0) + glBindTexture(GL_TEXTURE_2D, l.DepthBuffer) + glUniform1i(texID, 0) + + render(program_id,projMatrix,vai,0,0,-10) + + temp, _, _ = create_vertex_buffers(np.array([cx, cy, 5], dtype=np.float32), + np.array([1, 1, 0], dtype=np.float32), program_id) + render(program_id, projMatrix, temp, 0, 0, -10) + #ry += 0.05 + + + glUseProgram(program2_id) + check_error("Renderingprogram is not initialized!") + + texID = glGetUniformLocation(program2_id, "Tex") + glActiveTexture(GL_TEXTURE0) + glBindTexture(GL_TEXTURE_2D, l.DepthBuffer) + glUniform1i(texID, 0) + + glBindVertexArray(vai2) + glDrawArrays(GL_TRIANGLES, 0, 6) + check_error("Rendering problem") + glBindVertexArray(0) + glUseProgram(0) + glFlush() + + glFlush() + glutSwapBuffers() + global rx + #rx += 0.025 + global ry + glutPostRedisplay() + return + +cx = 0 +cy = 0 +def display2(): + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + global l + l.prepareForDepthMapping() + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + + newMat = orthogonalMatrix(10, -10, 10, -10, 20, 0.01) * lookAt(-cx, -cy, 1, 0, 0, 0, 0, 1, 0) * np.identity(4) + newMat = translate(5,0,-10)*lookAt(-5,0,10,0,0,0,0,1,0) * projMatrix + oldMat = rotate(0,-0.5,0,True) * projMatrix + glUseProgram(program3_id) + widthid = glGetUniformLocation(program3_id, 'width') + heightid = glGetUniformLocation(program3_id, 'height') + nearid = glGetUniformLocation(program3_id, 'near') + farid = glGetUniformLocation(program3_id, 'far') + glUniform1f(nearid, 0.01) + glUniform1f(farid, 100) + glUniform1f(widthid, width) + glUniform1f(heightid, height) + render(program3_id, newMat,vai,0,0,0) + glFlush() + l.finishDepthMapping() + + glUseProgram(program2_id) + check_error("Renderingprogram is not initialized!") + + texID = glGetUniformLocation(program2_id, "Tex") + glActiveTexture(GL_TEXTURE0) + glBindTexture(GL_TEXTURE_2D,l.DepthBuffer) + glUniform1i(texID,0) + + glBindVertexArray(vai3) + glDrawArrays(GL_TRIANGLES, 0, 6) + check_error("Rendering problem") + glBindVertexArray(0) + glUseProgram(0) + glFlush() + glutSwapBuffers() + glutPostRedisplay() + +def resize(w,h): + glViewport(0,0,w,h) + global projMatrix + projMatrix = perspectiveMatrix(45.0,float(w)/float(h),0.01,100.0) + global width, height + width = w + height = h + +def keyboardHandler(key:int,x:int,y:int): + if key == b'\x1b': + exit() + + if key == b'+': + global rx + rx += 0.25 + if key == b'-': + global rx + rx -= 0.25 + + if key == b'w': + global cy + cy += 0.25 + if key == b's': + global cy + cy -= 0.25 + + if key == b'a': + global cx + cx -= 0.25 + if key == b'd': + global cx + cx += 0.25 + + if key == b'q': + global opening + opening -= 0.25 + if key == b'e': + global opening + opening += 0.25 + + if key == b'r': + print(cx,cy, opening) + glutPostRedisplay() + #print(key,x,y) + +def funcKeydHandler(key:int,x:int,y:int): + if key == 11: + glutFullScreenToggle() + #print(key) + +if __name__ == '__main__': main() \ No newline at end of file diff --git a/passthroughvertex.glsl b/passthroughvertex.glsl new file mode 100644 index 0000000..5cba3e5 --- /dev/null +++ b/passthroughvertex.glsl @@ -0,0 +1,12 @@ +#version 410 +layout(location = 0) in vec3 in_position; + +//color will be used for texcoords instead +layout(location = 1) in vec3 MyInColor; + +layout(location = 0) out vec2 UV; + +void main() { + UV = MyInColor.xy; + gl_Position = vec4(in_position,1.0); +} diff --git a/texturefragment.glsl b/texturefragment.glsl new file mode 100644 index 0000000..9548d17 --- /dev/null +++ b/texturefragment.glsl @@ -0,0 +1,13 @@ +#version 410 + +layout(location = 0)in vec2 UV; + +layout(location = 0)out vec4 color; + +uniform sampler2D Tex; + +void main() { + //color = vec4(0,0,0,1); + color = vec4(vec3(texture(Tex,UV).x),1); + +} diff --git a/vertex.glsl b/vertex.glsl new file mode 100644 index 0000000..366213d --- /dev/null +++ b/vertex.glsl @@ -0,0 +1,22 @@ +#version 410 + + +uniform mat4 projModelViewMatrix; +uniform mat3 normalMatrix; +uniform mat3 rotMatrix; + +layout(location = 0) in vec3 in_position; +layout(location = 1) in vec3 MyInColor; +layout(location = 2) in vec3 MyInSize; + +layout(location = 0) out vec3 colorgeo; +layout(location = 1) out vec3 sizegeo; +void main() +{ + colorgeo = MyInColor; + sizegeo = MyInSize; + gl_ClipDistance[0] = 100; + //gl_Position = projModelViewMatrix * vec4(rotMatrix *in_position, 1.0); + //gl_Position = vec4(rotMatrix *(projModelViewMatrix * vec4(in_position, 1.0)).xyz,1.0); + gl_Position = vec4(rotMatrix * in_position, 1.0); +} \ No newline at end of file