Shader class, some cleaning
This commit is contained in:
		
							parent
							
								
									da582944ea
								
							
						
					
					
						commit
						c6537a8f8c
					
				@ -3,8 +3,8 @@
 | 
			
		||||
import argparse
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from d3.conv.ply import PLYExporter
 | 
			
		||||
from d3.conv.loadmodel import convert
 | 
			
		||||
from d3.model.ply import PLYExporter
 | 
			
		||||
from d3.model.tools import convert
 | 
			
		||||
from functools import partial
 | 
			
		||||
 | 
			
		||||
def check_path(path, should_exist):
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
from .conv.model import Vertex
 | 
			
		||||
from .geometry import Vector
 | 
			
		||||
 | 
			
		||||
from OpenGL.GL import *
 | 
			
		||||
from OpenGL.GLU import *
 | 
			
		||||
@ -8,9 +8,9 @@ from OpenGL.GLU import *
 | 
			
		||||
 | 
			
		||||
class Camera:
 | 
			
		||||
    def __init__(self, position = None, target = None, up = None):
 | 
			
		||||
        self.position = Vertex() if position is None else position
 | 
			
		||||
        self.target = Vertex() if target is None else target
 | 
			
		||||
        self.up = Vertex(0.0,1.0,0.0) if up is None else target
 | 
			
		||||
        self.position = Vector() if position is None else position
 | 
			
		||||
        self.target = Vector() if target is None else target
 | 
			
		||||
        self.up = Vector(0.0,1.0,0.0) if up is None else target
 | 
			
		||||
 | 
			
		||||
    def look(self):
 | 
			
		||||
        gluLookAt(
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
from .conv.model import Vertex
 | 
			
		||||
from .geometry import Vector
 | 
			
		||||
 | 
			
		||||
import pygame
 | 
			
		||||
 | 
			
		||||
@ -22,7 +22,7 @@ class Controls:
 | 
			
		||||
class TrackBallControls(Controls):
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super().__init__()
 | 
			
		||||
        self.vertex = Vertex()
 | 
			
		||||
        self.vertex = Vector()
 | 
			
		||||
        self.theta = 0
 | 
			
		||||
 | 
			
		||||
    def apply(self):
 | 
			
		||||
@ -36,7 +36,7 @@ class TrackBallControls(Controls):
 | 
			
		||||
        coeff = 0.001
 | 
			
		||||
        move = pygame.mouse.get_rel()
 | 
			
		||||
 | 
			
		||||
        dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0)
 | 
			
		||||
        dV = Vector(move[1] * time * coeff, move[0] * time * coeff, 0)
 | 
			
		||||
        dTheta = dV.norm2()
 | 
			
		||||
 | 
			
		||||
        if abs(dTheta) < 0.00001:
 | 
			
		||||
@ -49,9 +49,9 @@ class TrackBallControls(Controls):
 | 
			
		||||
        cosDT2 = math.cos(dTheta / 2)
 | 
			
		||||
        sinDT2 = math.sin(dTheta / 2)
 | 
			
		||||
 | 
			
		||||
        A = cosT2 * sinDT2 * dV + cosDT2 * sinT2 * self.vertex + sinDT2 * sinT2 * Vertex.cross_product(dV, self.vertex)
 | 
			
		||||
        A = cosT2 * sinDT2 * dV + cosDT2 * sinT2 * self.vertex + sinDT2 * sinT2 * Vector.cross_product(dV, self.vertex)
 | 
			
		||||
 | 
			
		||||
        self.theta = 2 * math.acos(cosT2 * cosDT2 - sinT2 * sinDT2 * Vertex.dot(dV, self.vertex))
 | 
			
		||||
        self.theta = 2 * math.acos(cosT2 * cosDT2 - sinT2 * sinDT2 * Vector.dot(dV, self.vertex))
 | 
			
		||||
 | 
			
		||||
        self.vertex = A
 | 
			
		||||
        self.vertex.normalize()
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
from .model import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
 | 
			
		||||
from .basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
 | 
			
		||||
from functools import reduce
 | 
			
		||||
 | 
			
		||||
def is_obj(filename):
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
from .model import ModelParser, Exporter, Vertex, Face, FaceVertex
 | 
			
		||||
from .basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex
 | 
			
		||||
 | 
			
		||||
def is_ply(filename):
 | 
			
		||||
    return filename[-4:] == '.ply'
 | 
			
		||||
@ -2,6 +2,7 @@
 | 
			
		||||
 | 
			
		||||
from .obj import is_obj, OBJParser, OBJExporter
 | 
			
		||||
from .ply import is_ply, PLYParser, PLYExporter
 | 
			
		||||
from .basemodel import ModelParser, Exporter
 | 
			
		||||
 | 
			
		||||
def load_model(path):
 | 
			
		||||
    parser = None
 | 
			
		||||
							
								
								
									
										41
									
								
								d3/shader.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								d3/shader.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,41 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
dir_path = os.path.dirname(os.path.realpath(__file__))
 | 
			
		||||
 | 
			
		||||
import OpenGL.GL as gl
 | 
			
		||||
import OpenGL.GL.shaders as sh
 | 
			
		||||
 | 
			
		||||
class DefaultShader:
 | 
			
		||||
 | 
			
		||||
    def __init__(self, vertex_path = None, fragment_path = None):
 | 
			
		||||
 | 
			
		||||
        if vertex_path is None:
 | 
			
		||||
            vertex_path = dir_path + '/../assets/shaders/shader.vert'
 | 
			
		||||
        if fragment_path is None:
 | 
			
		||||
            fragment_path = dir_path + '/../assets/shaders/shader.frag'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        with open(vertex_path) as f:
 | 
			
		||||
            self.vertex_src = f.read()
 | 
			
		||||
 | 
			
		||||
        with open(fragment_path) as f:
 | 
			
		||||
            self.fragment_src = f.read()
 | 
			
		||||
 | 
			
		||||
        self.compile_shaders()
 | 
			
		||||
        self.compile_program()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def compile_shaders(self):
 | 
			
		||||
        self.vertex_shader = sh.compileShader(self.vertex_src, gl.GL_VERTEX_SHADER)
 | 
			
		||||
        self.fragment_shader = sh.compileShader(self.fragment_src, gl.GL_FRAGMENT_SHADER)
 | 
			
		||||
 | 
			
		||||
    def compile_program(self):
 | 
			
		||||
        self.program = sh.compileProgram(self.vertex_shader, self.fragment_shader)
 | 
			
		||||
 | 
			
		||||
    def bind(self):
 | 
			
		||||
        gl.glUseProgram(self.program)
 | 
			
		||||
 | 
			
		||||
    def unbind(self):
 | 
			
		||||
        gl.glUseProgram(0)
 | 
			
		||||
        pass
 | 
			
		||||
							
								
								
									
										26
									
								
								viewer.py
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								viewer.py
									
									
									
									
									
								
							@ -12,22 +12,18 @@ from OpenGL.GL import *
 | 
			
		||||
from OpenGL.GL.shaders import *
 | 
			
		||||
from OpenGL.GLU import *
 | 
			
		||||
 | 
			
		||||
from d3.conv.loadmodel import load_model
 | 
			
		||||
from d3.conv.model import Vertex
 | 
			
		||||
from d3.model.tools import load_model
 | 
			
		||||
from d3.geometry import Vector
 | 
			
		||||
from d3.controls import TrackBallControls
 | 
			
		||||
from d3.camera import Camera
 | 
			
		||||
from d3.shader import DefaultShader
 | 
			
		||||
 | 
			
		||||
WINDOW_WIDTH = 1024
 | 
			
		||||
WINDOW_HEIGHT = 768
 | 
			
		||||
 | 
			
		||||
x = -0.5
 | 
			
		||||
y = 0.5
 | 
			
		||||
width = 1
 | 
			
		||||
height = 1
 | 
			
		||||
 | 
			
		||||
def main(args):
 | 
			
		||||
 | 
			
		||||
    camera = Camera(Vertex(0,0,5), Vertex(0,0,0))
 | 
			
		||||
    camera = Camera(Vector(0,0,5), Vector(0,0,0))
 | 
			
		||||
    controls = TrackBallControls()
 | 
			
		||||
 | 
			
		||||
    pygame.init()
 | 
			
		||||
@ -50,15 +46,7 @@ def main(args):
 | 
			
		||||
    model = load_model(args.input)
 | 
			
		||||
    model.generate_vbos()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    VERTEX_SHADER = None
 | 
			
		||||
    FRAGMENT_SHADER = None
 | 
			
		||||
    with open('assets/shaders/shader.vert') as f:
 | 
			
		||||
        VERTEX_SHADER = compileShader(f.read(), GL_VERTEX_SHADER)
 | 
			
		||||
    with open('assets/shaders/shader.frag') as f:
 | 
			
		||||
        FRAGMENT_SHADER = compileShader(f.read(), GL_FRAGMENT_SHADER)
 | 
			
		||||
 | 
			
		||||
    SHADER = compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
 | 
			
		||||
    shader = DefaultShader()
 | 
			
		||||
 | 
			
		||||
    while running:
 | 
			
		||||
        for event in pygame.event.get():
 | 
			
		||||
@ -85,9 +73,9 @@ def main(args):
 | 
			
		||||
 | 
			
		||||
        glPushMatrix()
 | 
			
		||||
        controls.apply()
 | 
			
		||||
        glUseProgram(SHADER)
 | 
			
		||||
        shader.bind()
 | 
			
		||||
        model.gl_draw()
 | 
			
		||||
        glUseProgram(0)
 | 
			
		||||
        shader.unbind()
 | 
			
		||||
        glPopMatrix()
 | 
			
		||||
 | 
			
		||||
        glFlush()
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user