0% found this document useful (0 votes)
77 views3 pages

Code Rendering With Black Frame Scale

Uploaded by

sarazermane1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views3 pages

Code Rendering With Black Frame Scale

Uploaded by

sarazermane1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

def camera(self):

# scale = self.camera_frame_picture_ratio * self.camera_scale


Cy = self.camera_height * 0.5 - (self.camera_cy - self.camera_height * 0.5)

perspective = np.array([[self.camera_fx*self.camera_scale,
self.camera_skew*self.camera_scale, -
(self.camera_cx+self.camera_width*self.camera_frame_picture_ratio)*self.camera_scal
e, 0],
[0, self.camera_fy*self.camera_scale, -
(Cy+self.camera_height*self.camera_frame_picture_ratio)*self.camera_scale, 0],
[0, 0, (self.camera_near + self.camera_far),
(self.camera_near * self.camera_far)],
[0, 0, -1, 0]], dtype=np.float32)

ortho = np.array([[2 / (self.windowWidth), 0, 0, -1],


[0, 2 / (self.windowHeight), 0, -1],
[0, 0, -2 / (self.camera_far - self.camera_near), -
(self.camera_far + self.camera_near) / (self.camera_far - self.camera_near)],
[0, 0, 0, 1]], dtype=np.float32)

P = np.matmul(ortho, perspective)

P = P.transpose()

# P = np.zeros(shape=(4, 4), dtype=np.float32)

# width_padded = self.camera_width + 2*(self.camera_width *


self.camera_frame_picture_ratio)
# height_padded = self.camera_height + 2*(self.camera_height *
self.camera_frame_picture_ratio)

# P[0, 0] = 2 * (self.camera_fx) / width_padded


# P[1, 1] = 2 * (self.camera_fy) / height_padded
# P[2, 0] = 1 - 2 *
(self.camera_cx+self.camera_width*self.camera_frame_picture_ratio) / width_padded
# P[2, 1] = 2 *
(self.camera_cy+self.camera_height*self.camera_frame_picture_ratio) / height_padded
- 1
# P[2, 2] = -(self.camera_far + self.camera_near) / (self.camera_far -
self.camera_near)
# P[2, 3] = -1.0
# P[3, 2] = - (2 * self.camera_far * self.camera_near) / (self.camera_far -
self.camera_near)

self.projectionMatrix = P

def drawImage(self):
# glBindBuffer(GL_ARRAY_BUFFER, 0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glDepthMask(GL_FALSE)
glDisable(GL_DEPTH_TEST)

scale = self.camera_frame_picture_ratio * self.camera_scale

# Drawable area in the widget


glViewport(int(self.camera_width * scale), int(self.camera_height * scale),
int(self.windowWidth - (2 * self.camera_width * scale)),
int(self.windowHeight - (2 * self.camera_height * scale)))

# glViewport(0, 0, self.windowWidth, self.windowHeight)

glBindVertexArray(self.imageVAO)
glUseProgram(self.imageShaderProgram)
glUniform1f(self.quadScaleLoc, 1.0)
glUniform1f(self.quadAlphaLoc, self.imageAlpha)

glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, self.textureId)

glDrawArrays(GL_TRIANGLES, 0, 6)

glBindVertexArray(0)
glBindTexture(GL_TEXTURE_2D, 0)
glUseProgram(0)

def drawModels(self):
glEnable(GL_DEPTH_TEST) # Depth buffer ON
glDepthMask(GL_TRUE)
glEnable(GL_CULL_FACE)

glViewport(0, 0, self.windowWidth, self.windowHeight) # Draw in the whole


widget area

glBindBuffer(GL_ARRAY_BUFFER, 0)

# Sweep through the existing models:


for i in range(0,len(self.modelVAOs)):
glBindVertexArray(self.modelVAOs[i])
glUseProgram(self.modelShaderProgram)
glUniformMatrix4fv(self.projMatrixLoc, 1, False,
self.projectionMatrix.flatten())

# Compute normal matrix:


rotation_inv = np.linalg.inv(self.modelviewMatrix[0:3,0:3])
self.normalMatrix = np.transpose(rotation_inv)

glUniformMatrix4fv(self.mvMatrixLoc, 1, False,
self.modelviewMatrix.flatten())
glUniformMatrix3fv(self.normalMatrixLoc, 1, False,
self.normalMatrix.flatten())
glUniform3f(self.lightPosLoc, self.lightPosition[0],
self.lightPosition[1], self.lightPosition[2])
glUniform1f(self.usingLightFalloffLoc, 1.0)
glUniform1f(self.modelTexturemappingLoc, 0.0)
glUniform1f(self.modelTexScaleLoc, 1.0)
glUniform1f(self.modelCLoc, self.cForRendering)
glUniform4f(self.modelAmbientColourLoc, self.modelAmbientColour[0],
self.modelAmbientColour[1], self.modelAmbientColour[2], self.modelAmbientColour[3])

glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, self.textureId)

# glEnable(GL_NORMALIZE) # Normalization vectors ON


glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glDrawArrays(GL_TRIANGLES, 0,
GLsizei(round(len(self.modelVertexData[i]) / 17)))

glBindTexture(GL_TEXTURE_2D, 0)
glUseProgram(0)

def __init__(self, parent=None):


# Load camera parameters:
self.path_calibration = "../patient_data/20180905/calibration.xml"
self.camera_width, self.camera_height, self.camera_fx, self.camera_fy,
self.camera_cx, self.camera_cy, self.camera_matrix, self.camera_dist_coeffs =
self.load_camera_params_from_xml(self.path_calibration)
self.camera_skew = 0
self.camera_frame_picture_ratio = 0.2
self.camera_scale = 0.5
frames = (2*self.camera_frame_picture_ratio)+1
scale = self.camera_scale*frames
self.windowWidth = int(self.camera_width * scale)
self.windowHeight = int(self.camera_height * scale)
self.camera_near = 0.0001
self.camera_far = 1000
self.modelviewMatrix = np.identity(4, dtype=np.float32)

image_orig = cv2.imread('../patient_data/20180905/image74.png')
self.background_image =
cv2.undistort(image_orig,self.camera_matrix,self.camera_dist_coeffs)
width, height = self.background_image.shape[1],
self.background_image.shape[0]

self.model_paths =
["../patient_data/20180905/models_registered_hepataug/tumour.obj",

"../patient_data/20180905/models_registered_hepataug/vein.obj",

"../patient_data/20180905/models_registered_hepataug/liver.obj"]

self.model_colors = np.array([[1.0, 1.0, 0.0, 1.0],


[0.0, 0.0, 1.0, 1.0],
[1.0, 1.0, 1.0, 0.5]
], dtype=np.float32)

# Lightning parameters:
self.modelAmbientColour = [0.1, 0.1, 0.1, 1.0]
self.lightPosition = [0.0, 0.0, 0.0]
self.cForRendering = 0.03

self.imageShaderProgram = None
self.positionHandle = None
self.textureId = None
self.imageVAO = None
self.models = []
self.modelVAOs = []
self.modelVBOs = []
self.modelVertexData = []

# Set window size:


self.setMinimumSize(self.windowWidth, self.windowHeight)

You might also like