Skip to content
This repository has been archived by the owner on Nov 2, 2022. It is now read-only.

Commit

Permalink
Clean up ignored files
Browse files Browse the repository at this point in the history
  • Loading branch information
amithm3 committed Dec 23, 2020
1 parent 8bcaf55 commit cf7f4f0
Show file tree
Hide file tree
Showing 14 changed files with 586 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv/
.idea/
__pycache__/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Amith225

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 3D-ENGINE
---
## **CS PROJECT**
### ***This is a python library which can be used for 3D modelling and rendering***
---
### How To Install
* #### _Make sure pip is installed in your system, else install pip before proceeding_
* #### _Download the zip file from [Download v1.0-beta](https://github.com/Amith225/3D-ENGINE/archive/v1.0-beta.zip)_
* #### _After Installing open cmd inside the 3D-ENGINE folder and run ```pip install -r requirements.txt```_
* #### _If using virtiual environment run the command through it_
* #### _Finnaly to see the demo run main.py in scr. Use the renderer.py as a library_
Empty file added __data__/Examples/dumy
Empty file.
Binary file added __data__/Saves/decagon
Binary file not shown.
Empty file added __data__/Saves/dumyl
Empty file.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy==1.19.4
Binary file removed scr/__pycache__/_data_.cpython-37.pyc
Binary file not shown.
Binary file removed scr/__pycache__/gui.cpython-37.pyc
Binary file not shown.
Binary file removed scr/__pycache__/renderer.cpython-37.pyc
Binary file not shown.
105 changes: 105 additions & 0 deletions scr/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import pickle

import scr.generator as gn
import scr.gui as gui

rd = gn.rd


class Main(gui.GUI):
def __init__(self):
gui.GUI.__init__(self, title="3D-ENGINE-Demo")
self.model_button.configure(command=lambda: self.model_it())
self.load_var.trace('wua', lambda *_: self.load_model(self.load_var.get()))
self.rotate = self.rotate_var.get()
self.rotate_var.trace('wua', lambda *_: exec("self.rotate=self.rotate_var.get()", {'self': self}))

self.space = None
self.object = None
self.camera = None
self.light = None

self.loaded = False
self.srz_info = None

def save_model(self, fname):
with open(f'{gui.os.path.dirname(gui.os.getcwd())}/__data__/Saves/{fname}', 'wb') as save_file:
pickle.dump(self.srz_info, save_file)

def load_model(self, fname):
with open(f'{gui.os.path.dirname(gui.os.getcwd())}/__data__/{fname}', 'rb') as save_file:
data = pickle.load(save_file)

self.side.delete(0, 'end'), self.radius.delete(0, 'end'), self.separation.delete(0, 'end')
self.side.insert(0, data[0])
self.radius.insert(0, str(data[1])[1:-1])
self.separation.insert(0, str(data[2])[1:-1])

self.loaded = True
self.model_it()

def exec(self, expr):
exec(expr, {'self': self})

def key_bind(self):
self.canvas.bind("<Up>", lambda event: self.camera.oriental_translation(0, 0.1, 0))
self.canvas.bind("<Down>", lambda event: self.camera.oriental_translation(0, -0.1, 0))
self.canvas.bind("<Right>", lambda event: self.camera.oriental_translation(0.1, 0, 0))
self.canvas.bind("<Left>", lambda event: self.camera.oriental_translation(-0.1, 0, 0))
self.canvas.bind("<space>", lambda event: self.camera.oriental_translation(0, 0, 0.1))
self.canvas.bind("<BackSpace>", lambda event: self.camera.oriental_translation(0, 0, -0.1))
self.canvas.bind('w', lambda event: self.camera.oriental_rotation(1, 0, 0))
self.canvas.bind('s', lambda event: self.camera.oriental_rotation(-1, 0, 0))
self.canvas.bind('d', lambda event: self.camera.oriental_rotation(0, 1, 0))
self.canvas.bind('a', lambda event: self.camera.oriental_rotation(0, -1, 0))
self.canvas.bind('z', lambda event: self.camera.oriental_rotation(0, 0, 1))
self.canvas.bind('<Shift-Z>', lambda event: self.camera.oriental_rotation(0, 0, -1))

self.canvas.bind("l", lambda event: [exec("light.lum += 1") for light in self.space.lights])
self.canvas.bind("<Shift-L>", lambda event: [exec("light.lum -= 1") for light in self.space.lights])
self.canvas.bind("c", lambda event: self.exec("self.camera.clarity += 0.1"))
self.canvas.bind("<Shift-C>", lambda event: self.exec("self.camera.clarity -= 0.1"))
self.canvas.bind("t", lambda event: self.exec("self.camera.shutter += 0.1"))
self.canvas.bind("<Shift-T>", lambda event: self.exec("self.camera.shutter -= 0.1"))

self.canvas.bind("h", lambda event: self.exec("self.hl='white'"))
self.canvas.bind("<Shift-H>", lambda event: self.exec("self.hl=''"))

def model_it(self):
self.space = rd.Space((self.canvas.winfo_reqwidth(), self.canvas.winfo_height()))
self.srz_info = eval(self.side.get()), eval(self.radius.get()), eval(self.separation.get())
if not self.loaded:
self.load_button.configure(text='Load')
else:
self.loaded = False
self.object = gn.Spawn.parallelopiped(*self.srz_info)
fov = self.canvas.winfo_width(), self.canvas.winfo_height()
fov = 120 * fov[0] / sum(fov), 180 * fov[1] / sum(fov)
self.camera = rd.Camera(fov=fov, shutter=1, clarity=1)
self.light = rd.Light(360, 33)
self.space.add_object(self.object, location=(0, 0, 10.))
self.space.add_camera(self.camera, location=(0, 0, 0.), orient=(0, 0, 1.))
self.space.add_light(self.light)

self.fov_bar_x.set(self.camera.fov[0])
self.fov_bar_y.set(self.camera.fov[1])
self.fov_bar_x.configure(command=lambda event: self.camera.change_fov(self.fov_bar_x.get(),
self.fov_bar_y.get()))
self.fov_bar_y.configure(command=lambda event: self.camera.change_fov(self.fov_bar_x.get(),
self.fov_bar_y.get()))
self.look_through.configure(command=lambda: self.camera.change_thresh(self.look_through_var.get()))
self.save_button.configure(command=lambda: self.save_model(self.save_entry.get()) or self.canvas.focus_set())

self.key_bind()
self.canvas.focus_set()

self.draw_triangles(*self.camera.capture())

while 1:
if self.rotate:
self.object.oriental_rotation(0.1, 0.2, 0.5)
self.draw_triangles(*self.camera.capture())


if __name__ == '__main__':
Main().mainloop()
57 changes: 57 additions & 0 deletions scr/generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import scr.renderer as rd

np = rd.np


class Spawn:
@staticmethod
def polygon(s, theta=0, z=0, face='both', rtype='object', append_i=0, r=1):
points = [(0, 0, z),
*[(r * np.cos(np.radians(i * 360 / s + theta)), r * np.sin(np.radians(i * 360 / s + theta)), z) for i
in range(s + 1)]]
if face == 'both':
faces = [(append_i, i, i + 1) for i in range(1 + append_i, len(points) - 1 + append_i)] + \
[(i, i + 1, append_i) for i in range(1 + append_i, len(points) - 1 + append_i)]
elif face == 'front':
faces = [(append_i, i, i + 1) for i in range(1 + append_i, len(points) - 1 + append_i)]
elif face == 'back':
faces = [(i + 1, i, append_i) for i in range(1 + append_i, len(points) - 1 + append_i)]
elif face == 'none':
faces = []
else:
raise Exception('invalid face value')

if rtype == 'object':
return rd.Object(points, faces)
else:
return points, faces

@staticmethod
def parallelopiped(s, r=(1, 1), z=None, theta=0, rtype='object'):
if z is None:
z = [1 for i in r]
points, faces = [], []
j_append = 0
z_append = sum(z)
for i in range(len(r)):
z_append -= z[i]
if i == 0:
pointsi, facesi = Spawn.polygon(s, theta, z_append + z[i], 'back', '', len(points), r[i])
elif i == len(r) - 1:
pointsi, facesi = Spawn.polygon(s, theta, z_append + z[i], 'front', '', len(points), r[i])
else:
pointsi, facesi = Spawn.polygon(s, theta, z_append + z[i], 'none', '', len(points), r[i])

if i == 0:
pass
else:
facesj = [(len(points) + j, j + j_append, len(points) + j + 1) for j in range(1, len(pointsi) - 1)] + \
[(j + j_append, j + j_append + 1, len(points) + j + 1) for j in range(1, len(pointsi) - 1)]
faces.extend(facesj)
j_append = len(points)
points.extend(pointsi), faces.extend(facesi)

if rtype == 'object':
return rd.Object(points, faces)
else:
return points, faces
121 changes: 121 additions & 0 deletions scr/gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import os
import tkinter as tk


class GUI(tk.Tk):
def __init__(self, size=(750, 500), title='No Title', **configurations):
self.configurations = configurations
tk.Tk.__init__(self, **self.configurations)
self.resizable(0, 0)
self.size = size
self.title(title)
x, y = (self.winfo_screenwidth() - self.size[0]) // 2, (self.winfo_screenheight() - self.size[1]) // 4
w, h = self.size[0], self.size[1]
self.geometry(f"{w}x{h}+{x}+{y}")
self.update_idletasks()

self.canvas = tk.Canvas(self, bg='black')
self.canvas.pack(fill='both', expand=True)

self._set_input_frame()

self.bot_frame = tk.Frame(self)
self.model_button = tk.Button(self.bot_frame, text='Model It')
self.model_button.grid(row=0, column=0)
self.save_frame = tk.Frame(self.bot_frame)
self.save_button = tk.Button(self.save_frame, text='Save It')
self.save_button.pack(side='left')
self.save_entry = tk.Entry(self.save_frame)
self.save_entry.pack(side='right', fill='both', expand=True)
self.save_frame.grid(row=0, column=1)
self.load_var = tk.StringVar()
self.load_var.set('Load')
parent = os.path.dirname(os.getcwd()) + '/__data__'
opts = os.listdir(parent)
items = dict([(opt, os.listdir(parent + '/' + opt)) for opt in opts])
self.load_button = tk.Menubutton(self.bot_frame, textvariable=self.load_var, indicatoron=True, relief='raised',
borderwidth=2)
self.topMenu = tk.Menu(self.load_button, tearoff=False)
self.load_button.configure(menu=self.topMenu)
for key in sorted(items.keys()):
menu = tk.Menu(self.topMenu)
self.topMenu.add_cascade(label=key, menu=menu)
for value in items[key]:
menu.add_radiobutton(label=value, variable=self.load_var, value=key+'/'+value)
self.load_button.grid(row=0, column=2)
self.bot_frame.pack()

self.hl = ''

self.canvas.update_idletasks()
self.add_x, self.add_y = self.canvas.winfo_width() / 2, self.canvas.winfo_height() / 2

def _set_input_frame(self):
self.input_frame = tk.Frame(self)
self.right_frame = tk.Frame(self.input_frame)
self.left_frame = tk.Frame(self.input_frame)

self.fov_bar_x = tk.Scale(self.right_frame, from_=0, to=360, length=180, orient='horizontal')
self.fov_bar_x.grid(row=0, column=1)
self.fov_bar_x_text = tk.Label(self.right_frame, text='Fov X:')
self.fov_bar_x_text.grid(row=0, column=0, sticky='n')
self.fov_bar_y = tk.Scale(self.right_frame, from_=0, to=360, length=180, orient='horizontal')
self.fov_bar_y.grid(row=1, column=1)
self.fov_bar_y_text = tk.Label(self.right_frame, text='Fov Y:')
self.fov_bar_y_text.grid(row=1, column=0, sticky='n')

self.side = tk.Entry(self.left_frame)
self.side.grid(row=0, column=1)
self.side_text = tk.Label(self.left_frame, text='Side:')
self.side_text.grid(row=0, column=0, sticky='e')
self.radius = tk.Entry(self.left_frame)
self.radius.grid(row=1, column=1)
self.radius_text = tk.Label(self.left_frame, text='Radius:')
self.radius_text.grid(row=1, column=0, sticky='e')
self.separation = tk.Entry(self.left_frame)
self.separation.grid(row=2, column=1)
self.separation_text = tk.Label(self.left_frame, text='Separation:')
self.separation_text.grid(row=2, column=0, sticky='e')
self.look_through_var = tk.IntVar()
self.look_through_var.set(0)
self.look_through = tk.Checkbutton(self.left_frame, text='Look Through', variable=self.look_through_var)
self.look_through.grid(row=3, column=1)
self.rotate_var = tk.IntVar()
self.rotate_var.set(0)
self.rotate_button = tk.Checkbutton(self.left_frame, text='Rotate', variable=self.rotate_var)
self.rotate_button.grid(row=3, column=0)

self.right_frame.pack(side='right')
self.left_frame.pack(side='left')
self.input_frame.pack()

def draw_triangles(self, points_cluster, face_cluster, draw_orient=None, color=None):
if color is None:
color = self.winfo_rgb('white')
color = color[0] / 256, color[1] / 256, color[2] / 256
self.canvas.delete('all')
for face in face_cluster:
face, shade = face[0], face[1]
p1, p2, p3 = points_cluster[face[0]], points_cluster[face[1]], points_cluster[face[2]]
col = '%02x%02x%02x' % (int(shade * color[0]), int(shade * color[1]), int(shade * color[2]))
col = '#' + col
self.canvas.create_polygon(p1[0][0] + self.add_x, p1[1][0] + self.add_y,
p2[0][0] + self.add_x, p2[1][0] + self.add_y,
p3[0][0] + self.add_x, p3[1][0] + self.add_y,
outline=self.hl, fill=col)

if draw_orient:
self.draw_orient(draw_orient)

self.canvas.update()

def draw_orient(self, orient_cluster):
for orient in orient_cluster:
f, u, r, o = orient

self.canvas.create_line(o[0][0] + self.add_x, o[1][0] + self.add_y, o[0][0] + f[0][0] + self.add_x,
o[1][0] + f[1][0] + self.add_y, fill='white')
self.canvas.create_line(o[0][0] + self.add_x, o[1][0] + self.add_y, o[0][0] + u[0][0] + self.add_x,
o[1][0] + u[1][0] + self.add_y, fill='red')
self.canvas.create_line(o[0][0] + self.add_x, o[1][0] + self.add_y, o[0][0] + r[0][0] + self.add_x,
o[1][0] + r[1][0] + self.add_y, fill='green')
Loading

0 comments on commit cf7f4f0

Please sign in to comment.