Spaces:
Runtime error
Runtime error
File size: 14,344 Bytes
c7d7131 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
import numpy as np
import random
from .framework import *
from .cam_render import CamRender
class PRTRender(CamRender):
def __init__(self, width=1600, height=1200, name='PRT Renderer', uv_mode=False, ms_rate=1, egl=False):
program_files = ['prt.vs', 'prt.fs'] if not uv_mode else ['prt_uv.vs', 'prt_uv.fs']
CamRender.__init__(self, width, height, name, program_files=program_files, color_size=8, ms_rate=ms_rate, egl=egl)
# WARNING: this differs from vertex_buffer and vertex_data in Render
self.vert_buffer = {}
self.vert_data = {}
self.norm_buffer = {}
self.norm_data = {}
self.tan_buffer = {}
self.tan_data = {}
self.btan_buffer = {}
self.btan_data = {}
self.prt1_buffer = {}
self.prt1_data = {}
self.prt2_buffer = {}
self.prt2_data = {}
self.prt3_buffer = {}
self.prt3_data = {}
self.uv_buffer = {}
self.uv_data = {}
self.render_texture_mat = {}
self.vertex_dim = {}
self.n_vertices = {}
self.norm_mat_unif = glGetUniformLocation(self.program, 'NormMat')
self.normalize_matrix = np.eye(4)
self.shcoeff_unif = glGetUniformLocation(self.program, 'SHCoeffs')
self.shcoeffs = np.zeros((9,3))
self.shcoeffs[0,:] = 1.0
#self.shcoeffs[1:,:] = np.random.rand(8,3)
self.hasAlbedoUnif = glGetUniformLocation(self.program, 'hasAlbedoMap')
self.hasNormalUnif = glGetUniformLocation(self.program, 'hasNormalMap')
self.analyticUnif = glGetUniformLocation(self.program, 'analytic')
self.analytic = False
self.rot_mat_unif = glGetUniformLocation(self.program, 'RotMat')
self.rot_matrix = np.eye(3)
def set_texture(self, mat_name, smplr_name, texture):
# texture_image: H x W x 3
width = texture.shape[1]
height = texture.shape[0]
texture = np.flip(texture, 0)
img_data = np.fromstring(texture.tostring(), np.uint8)
if mat_name not in self.render_texture_mat:
self.render_texture_mat[mat_name] = {}
if smplr_name in self.render_texture_mat[mat_name].keys():
glDeleteTextures([self.render_texture_mat[mat_name][smplr_name]])
del self.render_texture_mat[mat_name][smplr_name]
self.render_texture_mat[mat_name][smplr_name] = glGenTextures(1)
glActiveTexture(GL_TEXTURE0)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glBindTexture(GL_TEXTURE_2D, self.render_texture_mat[mat_name][smplr_name])
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
glGenerateMipmap(GL_TEXTURE_2D)
def set_albedo(self, texture_image, mat_name='all'):
self.set_texture(mat_name, 'AlbedoMap', texture_image)
def set_normal_map(self, texture_image, mat_name='all'):
self.set_texture(mat_name, 'NormalMap', texture_image)
def set_mesh(self, vertices, faces, norms, faces_nml, uvs, faces_uvs, prt, faces_prt, tans, bitans, mat_name='all'):
self.vert_data[mat_name] = vertices[faces.reshape([-1])]
self.n_vertices[mat_name] = self.vert_data[mat_name].shape[0]
self.vertex_dim[mat_name] = self.vert_data[mat_name].shape[1]
if mat_name not in self.vert_buffer.keys():
self.vert_buffer[mat_name] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vert_buffer[mat_name])
glBufferData(GL_ARRAY_BUFFER, self.vert_data[mat_name], GL_STATIC_DRAW)
self.uv_data[mat_name] = uvs[faces_uvs.reshape([-1])]
if mat_name not in self.uv_buffer.keys():
self.uv_buffer[mat_name] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.uv_buffer[mat_name])
glBufferData(GL_ARRAY_BUFFER, self.uv_data[mat_name], GL_STATIC_DRAW)
self.norm_data[mat_name] = norms[faces_nml.reshape([-1])]
if mat_name not in self.norm_buffer.keys():
self.norm_buffer[mat_name] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.norm_buffer[mat_name])
glBufferData(GL_ARRAY_BUFFER, self.norm_data[mat_name], GL_STATIC_DRAW)
self.tan_data[mat_name] = tans[faces_nml.reshape([-1])]
if mat_name not in self.tan_buffer.keys():
self.tan_buffer[mat_name] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.tan_buffer[mat_name])
glBufferData(GL_ARRAY_BUFFER, self.tan_data[mat_name], GL_STATIC_DRAW)
self.btan_data[mat_name] = bitans[faces_nml.reshape([-1])]
if mat_name not in self.btan_buffer.keys():
self.btan_buffer[mat_name] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.btan_buffer[mat_name])
glBufferData(GL_ARRAY_BUFFER, self.btan_data[mat_name], GL_STATIC_DRAW)
self.prt1_data[mat_name] = prt[faces_prt.reshape([-1])][:,:3]
self.prt2_data[mat_name] = prt[faces_prt.reshape([-1])][:,3:6]
self.prt3_data[mat_name] = prt[faces_prt.reshape([-1])][:,6:]
if mat_name not in self.prt1_buffer.keys():
self.prt1_buffer[mat_name] = glGenBuffers(1)
if mat_name not in self.prt2_buffer.keys():
self.prt2_buffer[mat_name] = glGenBuffers(1)
if mat_name not in self.prt3_buffer.keys():
self.prt3_buffer[mat_name] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.prt1_buffer[mat_name])
glBufferData(GL_ARRAY_BUFFER, self.prt1_data[mat_name], GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, self.prt2_buffer[mat_name])
glBufferData(GL_ARRAY_BUFFER, self.prt2_data[mat_name], GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, self.prt3_buffer[mat_name])
glBufferData(GL_ARRAY_BUFFER, self.prt3_data[mat_name], GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
def set_mesh_mtl(self, vertices, faces, norms, faces_nml, uvs, faces_uvs, tans, bitans, prt):
for key in faces:
self.vert_data[key] = vertices[faces[key].reshape([-1])]
self.n_vertices[key] = self.vert_data[key].shape[0]
self.vertex_dim[key] = self.vert_data[key].shape[1]
if key not in self.vert_buffer.keys():
self.vert_buffer[key] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vert_buffer[key])
glBufferData(GL_ARRAY_BUFFER, self.vert_data[key], GL_STATIC_DRAW)
self.uv_data[key] = uvs[faces_uvs[key].reshape([-1])]
if key not in self.uv_buffer.keys():
self.uv_buffer[key] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.uv_buffer[key])
glBufferData(GL_ARRAY_BUFFER, self.uv_data[key], GL_STATIC_DRAW)
self.norm_data[key] = norms[faces_nml[key].reshape([-1])]
if key not in self.norm_buffer.keys():
self.norm_buffer[key] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.norm_buffer[key])
glBufferData(GL_ARRAY_BUFFER, self.norm_data[key], GL_STATIC_DRAW)
self.tan_data[key] = tans[faces_nml[key].reshape([-1])]
if key not in self.tan_buffer.keys():
self.tan_buffer[key] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.tan_buffer[key])
glBufferData(GL_ARRAY_BUFFER, self.tan_data[key], GL_STATIC_DRAW)
self.btan_data[key] = bitans[faces_nml[key].reshape([-1])]
if key not in self.btan_buffer.keys():
self.btan_buffer[key] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.btan_buffer[key])
glBufferData(GL_ARRAY_BUFFER, self.btan_data[key], GL_STATIC_DRAW)
self.prt1_data[key] = prt[faces[key].reshape([-1])][:,:3]
self.prt2_data[key] = prt[faces[key].reshape([-1])][:,3:6]
self.prt3_data[key] = prt[faces[key].reshape([-1])][:,6:]
if key not in self.prt1_buffer.keys():
self.prt1_buffer[key] = glGenBuffers(1)
if key not in self.prt2_buffer.keys():
self.prt2_buffer[key] = glGenBuffers(1)
if key not in self.prt3_buffer.keys():
self.prt3_buffer[key] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.prt1_buffer[key])
glBufferData(GL_ARRAY_BUFFER, self.prt1_data[key], GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, self.prt2_buffer[key])
glBufferData(GL_ARRAY_BUFFER, self.prt2_data[key], GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, self.prt3_buffer[key])
glBufferData(GL_ARRAY_BUFFER, self.prt3_data[key], GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
def cleanup(self):
glBindBuffer(GL_ARRAY_BUFFER, 0)
for key in self.vert_data:
glDeleteBuffers(1, [self.vert_buffer[key]])
glDeleteBuffers(1, [self.norm_buffer[key]])
glDeleteBuffers(1, [self.uv_buffer[key]])
glDeleteBuffers(1, [self.tan_buffer[key]])
glDeleteBuffers(1, [self.btan_buffer[key]])
glDeleteBuffers(1, [self.prt1_buffer[key]])
glDeleteBuffers(1, [self.prt2_buffer[key]])
glDeleteBuffers(1, [self.prt3_buffer[key]])
glDeleteBuffers(1, [])
for smplr in self.render_texture_mat[key]:
glDeleteTextures([self.render_texture_mat[key][smplr]])
self.vert_buffer = {}
self.vert_data = {}
self.norm_buffer = {}
self.norm_data = {}
self.tan_buffer = {}
self.tan_data = {}
self.btan_buffer = {}
self.btan_data = {}
self.prt1_buffer = {}
self.prt1_data = {}
self.prt2_buffer = {}
self.prt2_data = {}
self.prt3_buffer = {}
self.prt3_data = {}
self.uv_buffer = {}
self.uv_data = {}
self.render_texture_mat = {}
self.vertex_dim = {}
self.n_vertices = {}
def randomize_sh(self):
self.shcoeffs[0,:] = 0.8
self.shcoeffs[1:,:] = 1.0*np.random.rand(8,3)
def set_sh(self, sh):
self.shcoeffs = sh
def set_norm_mat(self, scale, center):
N = np.eye(4)
N[:3, :3] = scale*np.eye(3)
N[:3, 3] = -scale*center
self.normalize_matrix = N
def draw(self):
self.draw_init()
glDisable(GL_BLEND)
#glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_MULTISAMPLE)
glUseProgram(self.program)
glUniformMatrix4fv(self.norm_mat_unif, 1, GL_FALSE, self.normalize_matrix.transpose())
glUniformMatrix4fv(self.model_mat_unif, 1, GL_FALSE, self.model_view_matrix.transpose())
glUniformMatrix4fv(self.persp_mat_unif, 1, GL_FALSE, self.projection_matrix.transpose())
if 'AlbedoMap' in self.render_texture_mat['all']:
glUniform1ui(self.hasAlbedoUnif, GLuint(1))
else:
glUniform1ui(self.hasAlbedoUnif, GLuint(0))
if 'NormalMap' in self.render_texture_mat['all']:
glUniform1ui(self.hasNormalUnif, GLuint(1))
else:
glUniform1ui(self.hasNormalUnif, GLuint(0))
glUniform1ui(self.analyticUnif, GLuint(1) if self.analytic else GLuint(0))
glUniform3fv(self.shcoeff_unif, 9, self.shcoeffs)
glUniformMatrix3fv(self.rot_mat_unif, 1, GL_FALSE, self.rot_matrix.transpose())
for mat in self.vert_buffer:
# Handle vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, self.vert_buffer[mat])
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, self.vertex_dim[mat], GL_DOUBLE, GL_FALSE, 0, None)
# Handle normal buffer
glBindBuffer(GL_ARRAY_BUFFER, self.norm_buffer[mat])
glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 3, GL_DOUBLE, GL_FALSE, 0, None)
# Handle uv buffer
glBindBuffer(GL_ARRAY_BUFFER, self.uv_buffer[mat])
glEnableVertexAttribArray(2)
glVertexAttribPointer(2, 2, GL_DOUBLE, GL_FALSE, 0, None)
# Handle tan buffer
glBindBuffer(GL_ARRAY_BUFFER, self.tan_buffer[mat])
glEnableVertexAttribArray(3)
glVertexAttribPointer(3, 3, GL_DOUBLE, GL_FALSE, 0, None)
# Handle btan buffer
glBindBuffer(GL_ARRAY_BUFFER, self.btan_buffer[mat])
glEnableVertexAttribArray(4)
glVertexAttribPointer(4, 3, GL_DOUBLE, GL_FALSE, 0, None)
# Handle PTR buffer
glBindBuffer(GL_ARRAY_BUFFER, self.prt1_buffer[mat])
glEnableVertexAttribArray(5)
glVertexAttribPointer(5, 3, GL_DOUBLE, GL_FALSE, 0, None)
glBindBuffer(GL_ARRAY_BUFFER, self.prt2_buffer[mat])
glEnableVertexAttribArray(6)
glVertexAttribPointer(6, 3, GL_DOUBLE, GL_FALSE, 0, None)
glBindBuffer(GL_ARRAY_BUFFER, self.prt3_buffer[mat])
glEnableVertexAttribArray(7)
glVertexAttribPointer(7, 3, GL_DOUBLE, GL_FALSE, 0, None)
for i, smplr in enumerate(self.render_texture_mat[mat]):
glActiveTexture(GL_TEXTURE0 + i)
glBindTexture(GL_TEXTURE_2D, self.render_texture_mat[mat][smplr])
glUniform1i(glGetUniformLocation(self.program, smplr), i)
glDrawArrays(GL_TRIANGLES, 0, self.n_vertices[mat])
glDisableVertexAttribArray(7)
glDisableVertexAttribArray(6)
glDisableVertexAttribArray(5)
glDisableVertexAttribArray(4)
glDisableVertexAttribArray(3)
glDisableVertexAttribArray(2)
glDisableVertexAttribArray(1)
glDisableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glUseProgram(0)
glDisable(GL_BLEND)
glDisable(GL_MULTISAMPLE)
self.draw_end()
|