| | @@ -155,22 +155,27 @@ |
| 155 | 155 | |
| 156 | 156 | // 頂点データはLuaから受け取る |
| 157 | 157 | lua::LuaEngine luaEngine(info.LUA.code, &shaderProgram, &info); |
| 158 | | - auto &mesh = luaEngine.GetMesh(); |
| 158 | + info.pendingCompile = true; |
| 159 | + auto &objects = luaEngine.GetObjects(); |
| 159 | 160 | |
| 160 | 161 | VertexArrays VAO(1); |
| 161 | 162 | Buffers VBO(Buffer, 1); |
| 162 | 163 | Buffers EBO(ArrayBuffer, 1); |
| 163 | 164 | |
| 164 | | - VAO.BindVertexArray(); |
| 165 | | - VBO.BindBuffer(); |
| 166 | | - VBO.BufferData(mesh.vertices.data(), mesh.vertices.size() * sizeof(f32)); |
| 167 | | - EBO.BindBuffer(); |
| 168 | | - EBO.BufferData(mesh.indices.data(), mesh.indices.size() * sizeof(GLuint)); |
| 165 | + for (auto &obj : objects) { |
| 166 | + VAO.BindVertexArray(); |
| 169 | 167 | |
| 170 | | - // 頂点属性 |
| 171 | | - for (const auto &a : mesh.attr) { |
| 172 | | - glVertexAttribPointer(a.location, a.size, GL_FLOAT, GL_FALSE, a.stride, (void *)(uintptr_t)a.offset); |
| 173 | | - glEnableVertexAttribArray(a.location); |
| 168 | + VBO.BindBuffer(); |
| 169 | + VBO.BufferData(obj.mesh.vertices.data(), obj.mesh.vertices.size() * sizeof(f32)); |
| 170 | + |
| 171 | + EBO.BindBuffer(); |
| 172 | + EBO.BufferData(obj.mesh.indices.data(), obj.mesh.indices.size() * sizeof(GLuint)); |
| 173 | + |
| 174 | + // 頂点属性 |
| 175 | + for (const auto &a : obj.mesh.attr) { |
| 176 | + glVertexAttribPointer(a.location, a.size, GL_FLOAT, GL_FALSE, a.stride, (void *)(uintptr_t)a.offset); |
| 177 | + glEnableVertexAttribArray(a.location); |
| 178 | + } |
| 174 | 179 | } |
| 175 | 180 | |
| 176 | 181 | bool isHold = false; |
| | @@ -181,6 +186,8 @@ |
| 181 | 186 | while (!window.ShouldClose() && info.isRunning) { |
| 182 | 187 | #pragma region Lambda |
| 183 | 188 | info.compile = [&]() { |
| 189 | + if (!info.pendingCompile) return; |
| 190 | + info.pendingCompile = false; |
| 184 | 191 | if (luaEngine.GetInitializationState()) luaEngine.SetInitializationState(false); |
| 185 | 192 | info.VERT.code = vertEditor.Get().GetText(); |
| 186 | 193 | info.FRAG.code = fragEditor.Get().GetText(); |
| | @@ -213,38 +220,40 @@ |
| 213 | 220 | luaEngine.SetProgram(&shaderProgram); |
| 214 | 221 | luaEngine.Reload(info.LUA.code); |
| 215 | 222 | { |
| 216 | | - lua::LuaMesh newMesh = luaEngine.GetMesh(); |
| 217 | | - if (newMesh.vertices.empty() || newMesh.indices.empty()) { |
| 218 | | - string msg; |
| 219 | | - if (info.i18n->GetCurrentLanguage().code == "ja_JP") msg = "メッシュデータが空です。"; |
| 220 | | - else "Mesh data is empty."; |
| 221 | | - throw std::runtime_error(msg); |
| 222 | | - } |
| 223 | + auto &newObj = luaEngine.GetObjects(); |
| 224 | + for (auto &obj : newObj) { |
| 225 | + if (obj.mesh.vertices.empty() || obj.mesh.indices.empty()) { |
| 226 | + string msg; |
| 227 | + if (info.i18n->GetCurrentLanguage().code == "ja_JP") msg = "メッシュデータが空です。"; |
| 228 | + else msg = "Mesh data is empty."; |
| 229 | + throw std::runtime_error(msg); |
| 230 | + } |
| 223 | 231 | |
| 224 | | - VAO.BindVertexArray(); |
| 232 | + VAO.BindVertexArray(); |
| 225 | 233 | |
| 226 | | - VBO.BindBuffer(); |
| 227 | | - VBO.BufferData( |
| 228 | | - newMesh.vertices.data(), |
| 229 | | - newMesh.vertices.size() * sizeof(f32) |
| 230 | | - ); |
| 234 | + VBO.BindBuffer(); |
| 235 | + VBO.BufferData( |
| 236 | + obj.mesh.vertices.data(), |
| 237 | + obj.mesh.vertices.size() * sizeof(f32) |
| 238 | + ); |
| 231 | 239 | |
| 232 | | - EBO.BindBuffer(); |
| 233 | | - EBO.BufferData( |
| 234 | | - newMesh.indices.data(), |
| 235 | | - newMesh.indices.size() * sizeof(u32) |
| 236 | | - ); |
| 237 | | - |
| 238 | | - for (const auto &a : newMesh.attr) { |
| 239 | | - glVertexAttribPointer( |
| 240 | | - a.location, |
| 241 | | - a.size, |
| 242 | | - GL_FLOAT, |
| 243 | | - GL_FALSE, |
| 244 | | - a.stride, |
| 245 | | - (any)(uintptr_t)a.offset |
| 240 | + EBO.BindBuffer(); |
| 241 | + EBO.BufferData( |
| 242 | + obj.mesh.indices.data(), |
| 243 | + obj.mesh.indices.size() * sizeof(u32) |
| 246 | 244 | ); |
| 247 | | - glEnableVertexAttribArray(a.location); |
| 245 | + |
| 246 | + for (const auto &a : obj.mesh.attr) { |
| 247 | + glVertexAttribPointer( |
| 248 | + a.location, |
| 249 | + a.size, |
| 250 | + GL_FLOAT, |
| 251 | + GL_FALSE, |
| 252 | + a.stride, |
| 253 | + (any)(uintptr_t)a.offset |
| 254 | + ); |
| 255 | + glEnableVertexAttribArray(a.location); |
| 256 | + } |
| 248 | 257 | } |
| 249 | 258 | } |
| 250 | 259 | |
| | @@ -346,7 +355,7 @@ |
| 346 | 355 | nTit = info.shaderName + "(" + info.i18n->GetWord("editorlua") + ")"; |
| 347 | 356 | luaEditor.SetTitle(nTit + "###LuaEditor"); |
| 348 | 357 | |
| 349 | | - info.compile(); |
| 358 | + info.pendingCompile = true; |
| 350 | 359 | |
| 351 | 360 | gui::LogEntry entry; |
| 352 | 361 | entry.type = gui::LogType::Info; |
| | @@ -378,7 +387,7 @@ |
| 378 | 387 | nTit = info.shaderName + "(" + info.i18n->GetWord("editorlua") + ")"; |
| 379 | 388 | luaEditor.SetTitle(nTit + "###LuaEditor"); |
| 380 | 389 | |
| 381 | | - info.compile(); |
| 390 | + info.pendingCompile = true; |
| 382 | 391 | |
| 383 | 392 | gui::LogEntry entry; |
| 384 | 393 | entry.type = gui::LogType::Info; |
| | @@ -419,7 +428,7 @@ |
| 419 | 428 | |
| 420 | 429 | if (!isHold) { |
| 421 | 430 | if (isCompileKey) { |
| 422 | | - info.compile(); |
| 431 | + info.pendingCompile = true; |
| 423 | 432 | } |
| 424 | 433 | |
| 425 | 434 | if (isNewKey) { |
| | @@ -481,6 +490,7 @@ |
| 481 | 490 | } |
| 482 | 491 | |
| 483 | 492 | info.create(); |
| 493 | + info.compile(); |
| 484 | 494 | |
| 485 | 495 | #ifndef PRODUCTION_BUILD |
| 486 | 496 | if (showDemo) ImGui::ShowDemoWindow(&showDemo); |
| | @@ -509,9 +519,43 @@ |
| 509 | 519 | luaEngine.Init(); |
| 510 | 520 | luaEngine.Update(); |
| 511 | 521 | |
| 512 | | - // クアッドを描画 |
| 513 | | - VAO.BindVertexArray(); |
| 514 | | - glDrawElements(GL_TRIANGLES, mesh.GetIndexCount(), GL_UNSIGNED_INT, nullptr); |
| 522 | + auto &objects = luaEngine.GetObjects(); |
| 523 | + for (auto &obj : objects) { |
| 524 | + if (obj.mesh.vertices.empty()) continue; |
| 525 | + if (obj.mesh.indices.empty()) continue; |
| 526 | + |
| 527 | + // モデルマトリックスの作成 |
| 528 | + Matrix4 T = mat4Translate( |
| 529 | + obj.position.pos.x, |
| 530 | + obj.position.pos.y, |
| 531 | + obj.position.pos.z |
| 532 | + ); |
| 533 | + |
| 534 | + Matrix4 R = mat4Rotate( |
| 535 | + obj.rotation.pos.x, |
| 536 | + obj.rotation.pos.y, |
| 537 | + obj.rotation.pos.z |
| 538 | + ); |
| 539 | + |
| 540 | + Matrix4 S = mat4Scale( |
| 541 | + obj.scale.pos.x, |
| 542 | + obj.scale.pos.y, |
| 543 | + obj.scale.pos.z |
| 544 | + ); |
| 545 | + |
| 546 | + Matrix4 model = mat4Mul(T, mat4Mul(R, S)); |
| 547 | + |
| 548 | + glUniformMatrix4fv(shaderProgram.GetUniformLocation("uModel"), 1, GL_FALSE, model.m); |
| 549 | + |
| 550 | + // 描画 |
| 551 | + VAO.BindVertexArray(); |
| 552 | + VBO.BindBuffer(); |
| 553 | + VBO.BufferData(obj.mesh.vertices.data(), obj.mesh.vertices.size() * sizeof(f32)); |
| 554 | + EBO.BindBuffer(); |
| 555 | + EBO.BufferData(obj.mesh.indices.data(), obj.mesh.indices.size() * sizeof(GLuint)); |
| 556 | + |
| 557 | + glDrawElements(GL_TRIANGLES, obj.mesh.GetIndexCount(), GL_UNSIGNED_INT, nullptr); |
| 558 | + } |
| 515 | 559 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 516 | 560 | |
| 517 | 561 | // バッファをスワップし、イベントを処理 |