geometry shader for cuboid

This commit is contained in:
zomseffen 2020-07-25 15:03:05 +02:00
parent 2096b24e74
commit e9d573172d
5 changed files with 244 additions and 72 deletions

View file

@ -6,20 +6,19 @@ import numpy as np
class Cube(Object):
GeometryShaderId = -1
def __init__(self):
Object.__init__(self)
super(Cube, self).__init__()
if (Cube.GeometryShaderId == -1):
self.initializeShader()
def initializeShader(self) -> bool:
@classmethod
def initializeShader(cls) -> bool:
with open('./Objects/Cube/cube_geometry.glsl', 'r') as f:
geometry_shader_string = f.read()
Cube.GeometryShaderId = glCreateShader(GL_GEOMETRY_SHADER)
cls.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

View file

@ -3,20 +3,21 @@ from OpenGL.GLU import *
from OpenGL.GL import *
from Objects.Objects import *
class Cuboid(Object):
GeometryShaderId = -1
class Cuboid(SizedObject):
def __init__(self):
Object.__init__(self)
if(Cuboid.GeometryShaderId == -1):
super(Cuboid, self).__init__()
if Cuboid.GeometryShaderId == -1:
self.initializeShader()
def initializeShader(self)->bool:
@classmethod
def initializeShader(cls)->bool:
with open('./Objects/Cuboid/cuboid_geometry.glsl', 'r') as f:
geometry_shader_string = f.read()
Cuboid.GeometryShaderId = glCreateShader(GL_GEOMETRY_SHADER)
cls.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

View file

@ -4,198 +4,349 @@ 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 = 1) in vec3 size[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];
void main(){
//hinten
gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),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();
//links
gl_Position = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(-size[0].x,-size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,-size[0].y,-size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,size[0].z),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 = projModelViewMatrix *(gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),0));
gl_Position = (gl_in[0].gl_Position + vec4(rotMatrix * vec3(size[0].x,size[0].y,-size[0].z),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();

View file

@ -8,11 +8,12 @@ class Object:
def draw(self) -> bool:
return False
def initializeShader(self) -> bool:
@classmethod
def initializeShader(cls) -> bool:
return True
def __init__(self):
self.pos = np.zeros((3))
self.pos = np.zeros(3)
self.color = np.zeros((3,))
self.programmId = -1
@ -25,3 +26,15 @@ class Object:
self.color[1] = G
self.color[2] = B
return self
class SizedObject(Object):
def __init__(self):
super(SizedObject, self).__init__()
self.size = np.array((0.5, 0.5, 0.5))
def setSize(self, X, Y, Z):
self.size[0] = X
self.size[1] = Y
self.size[2] = Z
return self

22
main.py
View file

@ -124,7 +124,7 @@ def main():
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, Cuboid.GeometryShaderId)
glAttachShader(program_id, fragment_shader_id)
# glAttachShader(program_id, l.FragmentShaderId)
@ -132,7 +132,7 @@ def main():
global struct, cstruct
struct = Structure()
struct.addShape(program_id, Cube().translate(translate(0, 0, 1)).setColor(1, 1, 0))
struct.addShape(program_id, Cuboid().translate(translate(0, 0, 1)).setColor(1, 1, 0))
# struct.addShape(program_id, Cube().translate(translate(1, 1, 0)).setColor(0, 0, 1))
# struct.addShape(program_id, Cube().translate(translate(1, 0, 0)).setColor(0, 1, 0))
# struct.addShape(program_id, Cube().translate(translate(1, -1, 0)).setColor(1, 0, 0))
@ -144,7 +144,8 @@ def main():
for x_pos in range(-10, 10, 1):
for y_pos in range(-10, 10):
struct.addShape(program_id, Cube().translate(translate(x_pos, y_pos, 0)).setColor(
z = random.random() * 2 - 1
struct.addShape(program_id, Cuboid().translate(translate(x_pos, y_pos, z)).setColor(
random.randint(0, 100) / 100.0, random.randint(0, 100) / 100.0, random.randint(0, 100) / 100.0))
@ -231,6 +232,7 @@ def main():
1, 0, 0,
1, 1, 0], dtype=np.float32), program2_id)
struct.render(projMatrix, rotate(rx, 0, 0))
my_timer(0)
glutMainLoop()
return
@ -361,11 +363,11 @@ def display():
global projMatrix
projMatrix = perspectiveMatrix(45, float(width) / float(height), 0.01, 100.0)
newMat = translate(-cx, -cy, -2) * lookAt(cx, cy, 2, 0, 0, 0, 0, 1, 0) * perspectiveMatrix(opening,
newMat = translate(-cx, -cy, -5) * lookAt(cx, cy, 5, 0, 0, 0, 0, 1, 0) * perspectiveMatrix(opening,
float(l.map_size) /
float(l.map_size),
0.01, 100.0)
l.pos = [-cx, -cy, -2]
l.pos = [-cx, -cy, -5]
l.ModelviewProjectionMatrix = newMat
l.lightColor = [1, 1, 1]
@ -441,7 +443,7 @@ def display():
global rx
# rx += 0.025
global ry
glutPostRedisplay()
# glutPostRedisplay()
return
@ -529,7 +531,7 @@ def keyboardHandler(key: int, x: int, y: int):
if key == b'r':
print(cx, cy, opening)
glutPostRedisplay()
# glutPostRedisplay()
# print(key,x,y)
@ -539,6 +541,12 @@ def funcKeydHandler(key: int, x: int, y: int):
# print(key)
def my_timer(x):
glutPostRedisplay()
glutTimerFunc(int(1000/60), my_timer, 0)
if __name__ == '__main__':
rx = 0
ry = 0