From 70656d692877d7d85f32228dba4169921ac729aa Mon Sep 17 00:00:00 2001 From: Unreal Karaulov Date: Wed, 6 Dec 2023 08:34:01 +0300 Subject: [PATCH] Remove useless code, add shader for models. Remove useless code, add shader for models. --- src/editor/BspRenderer.cpp | 180 ++++++++++++++----------------------- src/editor/BspRenderer.h | 7 +- src/editor/Gui.cpp | 2 +- src/editor/Renderer.cpp | 52 +++++++---- src/editor/Renderer.h | 4 + src/gl/VertexBuffer.cpp | 9 +- src/gl/primitives.h | 8 ++ src/gl/shaders.cpp | 29 ++++++ src/gl/shaders.h | 15 ++-- src/mdl/mdl_studio.cpp | 17 +--- src/mdl/mdl_studio.h | 4 +- 11 files changed, 166 insertions(+), 161 deletions(-) diff --git a/src/editor/BspRenderer.cpp b/src/editor/BspRenderer.cpp index 4b302225..8b0429a3 100644 --- a/src/editor/BspRenderer.cpp +++ b/src/editor/BspRenderer.cpp @@ -19,14 +19,10 @@ #endif -BspRenderer::BspRenderer(Bsp* _map, ShaderProgram* _bspShader, ShaderProgram* _fullBrightBspShader, - ShaderProgram* _colorShader, PointEntRenderer* _pointEntRenderer) +BspRenderer::BspRenderer(Bsp* _map, PointEntRenderer* _pointEntRenderer) { this->map = _map; this->map->setBspRender(this); - this->bspShader = _bspShader; - this->fullBrightBspShader = _fullBrightBspShader; - this->colorShader = _colorShader; this->pointEntRenderer = _pointEntRenderer; this->lightmaps = NULL; this->glTexturesSwap = NULL; @@ -233,39 +229,12 @@ BspRenderer::BspRenderer(Bsp* _map, ShaderProgram* _bspShader, ShaderProgram* _f calcFaceMaths(); preRenderFaces(); preRenderEnts(); - if (bspShader) - { - bspShader->bind(); - - unsigned int sTexId = glGetUniformLocation(bspShader->ID, "sTex"); - glUniform1i(sTexId, 0); - for (int s = 0; s < MAXLIGHTMAPS; s++) - { - unsigned int sLightmapTexIds = glGetUniformLocation(bspShader->ID, ("sLightmapTex" + std::to_string(s)).c_str()); - // assign lightmap texture units (skips the normal texture unit) - glUniform1i(sLightmapTexIds, s + 1); - } - } - if (fullBrightBspShader) - { - fullBrightBspShader->bind(); + //numRenderClipnodes = map->modelCount; + lightmapFuture = std::async(std::launch::async, &BspRenderer::loadLightmaps, this); + texturesFuture = std::async(std::launch::async, &BspRenderer::loadTextures, this); + clipnodesFuture = std::async(std::launch::async, &BspRenderer::loadClipnodes, this); - unsigned int sTexId2 = glGetUniformLocation(fullBrightBspShader->ID, "sTex"); - glUniform1i(sTexId2, 0); - } - if (colorShader) - { - colorShaderMultId = glGetUniformLocation(colorShader->ID, "colorMult"); - //numRenderClipnodes = map->modelCount; - lightmapFuture = std::async(std::launch::async, &BspRenderer::loadLightmaps, this); - texturesFuture = std::async(std::launch::async, &BspRenderer::loadTextures, this); - clipnodesFuture = std::async(std::launch::async, &BspRenderer::loadClipnodes, this); - } - else - { - loadTextures(); - } // cache ent targets so first selection doesn't lag for (int i = 0; i < map->ents.size(); i++) { @@ -510,15 +479,13 @@ RenderClipnodes* BspRenderer::addClipnodeModel(int modelIdx) void BspRenderer::updateModelShaders() { - ShaderProgram* activeShader = (g_render_flags & RENDER_LIGHTMAPS) ? bspShader : fullBrightBspShader; - for (int i = 0; i < numRenderModels; i++) { RenderModel& model = renderModels[i]; for (int k = 0; k < model.groupCount; k++) { - model.renderGroups[k].buffer->setShader(activeShader, true); - model.renderGroups[k].wireframeBuffer->setShader(activeShader, true); + model.renderGroups[k].buffer->setShader(g_app->activeShader, true); + model.renderGroups[k].wireframeBuffer->setShader(g_app->activeShader, true); } } } @@ -872,8 +839,6 @@ int BspRenderer::refreshModel(int modelIdx, bool refreshClipnodes, bool noTriang std::vector> renderGroupVerts; std::vector> renderGroupWireframeVerts; - ShaderProgram* activeShader = (g_render_flags & RENDER_LIGHTMAPS) ? bspShader : fullBrightBspShader; - for (int i = 0; i < model.nFaces; i++) { int faceIdx = model.iFirstFace + i; @@ -1123,7 +1088,7 @@ int BspRenderer::refreshModel(int modelIdx, bool refreshClipnodes, bool noTriang renderGroups[i].wireframeVertCount = (int)renderGroupWireframeVerts[i].size(); memcpy(renderGroups[i].wireframeVerts, &renderGroupWireframeVerts[i][0], renderGroups[i].wireframeVertCount * sizeof(lightmapVert)); - auto tmpBuf = renderGroups[i].buffer = new VertexBuffer(activeShader, 0, GL_TRIANGLES); + auto tmpBuf = renderGroups[i].buffer = new VertexBuffer(g_app->activeShader, 0, GL_TRIANGLES); tmpBuf->addAttribute(TEX_2F, "vTex"); tmpBuf->addAttribute(3, GL_FLOAT, 0, "vLightmapTex0"); tmpBuf->addAttribute(3, GL_FLOAT, 0, "vLightmapTex1"); @@ -1133,7 +1098,7 @@ int BspRenderer::refreshModel(int modelIdx, bool refreshClipnodes, bool noTriang tmpBuf->addAttribute(POS_3F, "vPosition"); tmpBuf->setData(renderGroups[i].verts, renderGroups[i].vertCount); - auto tmpWireBuff = renderGroups[i].wireframeBuffer = new VertexBuffer(activeShader, 0, GL_LINES); + auto tmpWireBuff = renderGroups[i].wireframeBuffer = new VertexBuffer(g_app->activeShader, 0, GL_LINES); tmpWireBuff->addAttribute(TEX_2F, "vTex"); tmpWireBuff->addAttribute(3, GL_FLOAT, 0, "vLightmapTex0"); tmpWireBuff->addAttribute(3, GL_FLOAT, 0, "vLightmapTex1"); @@ -1450,10 +1415,10 @@ void BspRenderer::generateClipnodeBufferForHull(int modelIdx, int hullIdx) if (wireframeVerts.size()) std::copy(wireframeVerts.begin(), wireframeVerts.end(), wireOutput); - renderClip.clipnodeBuffer[hullIdx] = new VertexBuffer(colorShader, COLOR_4B | POS_3F, output, (GLsizei)allVerts.size(), GL_TRIANGLES); + renderClip.clipnodeBuffer[hullIdx] = new VertexBuffer(g_app->colorShader, COLOR_4B | POS_3F, output, (GLsizei)allVerts.size(), GL_TRIANGLES); renderClip.clipnodeBuffer[hullIdx]->ownData = true; - renderClip.wireframeClipnodeBuffer[hullIdx] = new VertexBuffer(colorShader, COLOR_4B | POS_3F, wireOutput, (GLsizei)wireframeVerts.size(), GL_LINES); + renderClip.wireframeClipnodeBuffer[hullIdx] = new VertexBuffer(g_app->colorShader, COLOR_4B | POS_3F, wireOutput, (GLsizei)wireframeVerts.size(), GL_LINES); renderClip.wireframeClipnodeBuffer[hullIdx]->ownData = true; @@ -2124,16 +2089,14 @@ unsigned int BspRenderer::getFaceTextureId(int faceIdx) void BspRenderer::render(std::vector highlightEnts, bool modelVertsDraw, int clipnodeHull) { - ShaderProgram* activeShader; vec3 renderOffset; + vec3 renderOffset; mapOffset = map->ents.size() ? map->ents[0]->getOrigin() : vec3(); renderOffset = mapOffset.flip(); - activeShader = (g_render_flags & RENDER_LIGHTMAPS) ? bspShader : fullBrightBspShader; - - activeShader->bind(); - activeShader->modelMat->loadIdentity(); - activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - activeShader->updateMatrixes(); + g_app->activeShader->bind(); + g_app->activeShader->modelMat->loadIdentity(); + g_app->activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + g_app->activeShader->updateMatrixes(); for (int pass = 0; pass < 2; pass++) { @@ -2152,14 +2115,14 @@ void BspRenderer::render(std::vector highlightEnts, bool modelVertsDraw, in { if (renderEnts[i].hide) continue; - activeShader->pushMatrix(MAT_MODEL); - *activeShader->modelMat = renderEnts[i].modelMatAngles; - activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - activeShader->updateMatrixes(); + g_app->activeShader->pushMatrix(MAT_MODEL); + *g_app->activeShader->modelMat = renderEnts[i].modelMatAngles; + g_app->activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + g_app->activeShader->updateMatrixes(); drawModel(&renderEnts[i], drawTransparentFaces, g_app->pickInfo.IsSelectedEnt(i), false); - activeShader->popMatrix(MAT_MODEL); + g_app->activeShader->popMatrix(MAT_MODEL); } } if (drawTransparentFaces) @@ -2170,13 +2133,13 @@ void BspRenderer::render(std::vector highlightEnts, bool modelVertsDraw, in if ((g_render_flags & RENDER_POINT_ENTS) && pass == 0) { drawPointEntities(highlightEnts); - activeShader->bind(); + g_app->activeShader->bind(); } } if (clipnodesLoaded) { - colorShader->bind(); + g_app->colorShader->bind(); if (g_render_flags & RENDER_WORLD_CLIPNODES && clipnodeHull != -1) { @@ -2196,26 +2159,26 @@ void BspRenderer::render(std::vector highlightEnts, bool modelVertsDraw, in { continue; // skip rendering for models that have faces, if in auto mode } - colorShader->pushMatrix(MAT_MODEL); - *colorShader->modelMat = renderEnts[i].modelMatAngles; - colorShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - colorShader->updateMatrixes(); + g_app->colorShader->pushMatrix(MAT_MODEL); + *g_app->colorShader->modelMat = renderEnts[i].modelMatAngles; + g_app->colorShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + g_app->colorShader->updateMatrixes(); bool hightlighted = g_app->pickInfo.IsSelectedEnt(i); if (hightlighted) { - glUniform4f(colorShaderMultId, 1.0f, 0.25f, 0.25f, 1.0f); + glUniform4f(g_app->colorShaderMultId, 1.0f, 0.25f, 0.25f, 1.0f); } drawModelClipnodes(renderEnts[i].modelIdx, false, clipnodeHull); if (hightlighted) { - glUniform4f(colorShaderMultId, 1.0f, 1.0f, 1.0f, 1.0f); + glUniform4f(g_app->colorShaderMultId, 1.0f, 1.0f, 1.0f, 1.0f); } - colorShader->popMatrix(MAT_MODEL); + g_app->colorShader->popMatrix(MAT_MODEL); } } } @@ -2225,7 +2188,7 @@ void BspRenderer::render(std::vector highlightEnts, bool modelVertsDraw, in glDepthFunc(GL_ALWAYS); if (highlightEnts.size()) { - activeShader->bind(); + g_app->activeShader->bind(); for (int highlightEnt : highlightEnts) { @@ -2233,17 +2196,15 @@ void BspRenderer::render(std::vector highlightEnts, bool modelVertsDraw, in { if (renderEnts[highlightEnt].hide) continue; - activeShader->pushMatrix(MAT_MODEL); - *activeShader->modelMat = renderEnts[highlightEnt].modelMatAngles; - activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - activeShader->updateMatrixes(); + *g_app->activeShader->modelMat = renderEnts[highlightEnt].modelMatAngles; + g_app->activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + g_app->activeShader->updateMatrixes(); if (modelVertsDraw) glDisable(GL_CULL_FACE); drawModel(&renderEnts[highlightEnt], false, true, true); drawModel(&renderEnts[highlightEnt], true, true, true); if (modelVertsDraw) glEnable(GL_CULL_FACE); - activeShader->popMatrix(MAT_MODEL); } } } @@ -2254,12 +2215,10 @@ void BspRenderer::render(std::vector highlightEnts, bool modelVertsDraw, in void BspRenderer::drawModel(RenderEnt* ent, bool transparent, bool highlight, bool edgesOnly) { - ShaderProgram* activeShader; vec3 renderOffset; + vec3 renderOffset; mapOffset = map->ents.size() ? map->ents[0]->getOrigin() : vec3(); renderOffset = mapOffset.flip(); - activeShader = (g_render_flags & RENDER_LIGHTMAPS) ? bspShader : fullBrightBspShader; - int modelIdx = ent ? ent->modelIdx : 0; if (modelIdx < 0 || modelIdx >= numRenderModels) @@ -2315,14 +2274,14 @@ void BspRenderer::drawModel(RenderEnt* ent, bool transparent, bool highlight, bo if (ent && ent->needAngles) { - activeShader->pushMatrix(MAT_MODEL); - *activeShader->modelMat = ent->modelMatOrigin; - activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - activeShader->updateMatrixes(); + g_app->activeShader->pushMatrix(MAT_MODEL); + *g_app->activeShader->modelMat = ent->modelMatOrigin; + g_app->activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + g_app->activeShader->updateMatrixes(); yellowTex->bind(0); greyTex->bind(1); rgroup.wireframeBuffer->drawFull(); - activeShader->popMatrix(MAT_MODEL); + g_app->activeShader->popMatrix(MAT_MODEL); } if (highlight || (g_render_flags & RENDER_WIREFRAME)) @@ -2405,12 +2364,12 @@ void BspRenderer::drawModel(RenderEnt* ent, bool transparent, bool highlight, bo whiteTex->bind(s + 1); } - activeShader->pushMatrix(MAT_MODEL); - *activeShader->modelMat = ent->modelMatOrigin; - activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - activeShader->updateMatrixes(); + g_app->activeShader->pushMatrix(MAT_MODEL); + *g_app->activeShader->modelMat = ent->modelMatOrigin; + g_app->activeShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + g_app->activeShader->updateMatrixes(); rgroup.buffer->drawFull(); - activeShader->popMatrix(MAT_MODEL); + g_app->activeShader->popMatrix(MAT_MODEL); } } } @@ -2484,14 +2443,12 @@ void BspRenderer::drawModelClipnodes(int modelIdx, bool highlight, int hullIdx) void BspRenderer::drawPointEntities(std::vector highlightEnts) { - ShaderProgram* activeShader; vec3 renderOffset; + vec3 renderOffset; mapOffset = map->ents.size() ? map->ents[0]->getOrigin() : vec3(); renderOffset = mapOffset.flip(); - activeShader = (g_render_flags & RENDER_LIGHTMAPS) ? bspShader : fullBrightBspShader; - + // skip worldspawn - colorShader->pushMatrix(MAT_MODEL); - fullBrightBspShader->pushMatrix(MAT_MODEL); + g_app->colorShader->pushMatrix(MAT_MODEL); for (int i = 1, sz = (int)map->ents.size(); i < sz; i++) { @@ -2504,32 +2461,31 @@ void BspRenderer::drawPointEntities(std::vector highlightEnts) { if ((g_render_flags & RENDER_MODELS) && renderEnts[i].mdl && renderEnts[i].mdl->mdl_mesh_groups.size()) { - fullBrightBspShader->bind(); - - *fullBrightBspShader->modelMat = renderEnts[i].modelMatAngles; - fullBrightBspShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + g_app->modelShader->bind(); - fullBrightBspShader->updateMatrixes(); + *g_app->modelShader->modelMat = renderEnts[i].modelMatAngles; + g_app->modelShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + g_app->modelShader->updateMatrixes(); renderEnts[i].mdl->DrawModel(); - colorShader->bind(); + g_app->colorShader->bind(); - *colorShader->modelMat = renderEnts[i].modelMatAngles; - colorShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + *g_app->colorShader->modelMat = renderEnts[i].modelMatAngles; + g_app->colorShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - colorShader->updateMatrixes(); + g_app->colorShader->updateMatrixes(); renderEnts[i].pointEntCube->wireframeBuffer->drawFull(); } else { - colorShader->bind(); + g_app->colorShader->bind(); - *colorShader->modelMat = renderEnts[i].modelMatAngles; - colorShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + *g_app->colorShader->modelMat = renderEnts[i].modelMatAngles; + g_app->colorShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - colorShader->updateMatrixes(); + g_app->colorShader->updateMatrixes(); renderEnts[i].pointEntCube->selectBuffer->drawFull(); renderEnts[i].pointEntCube->wireframeBuffer->drawFull(); @@ -2539,20 +2495,20 @@ void BspRenderer::drawPointEntities(std::vector highlightEnts) { if ((g_render_flags & RENDER_MODELS) && renderEnts[i].mdl && renderEnts[i].mdl->mdl_mesh_groups.size()) { - *fullBrightBspShader->modelMat = renderEnts[i].modelMatAngles; - fullBrightBspShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + *g_app->modelShader->modelMat = renderEnts[i].modelMatAngles; + g_app->modelShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - fullBrightBspShader->updateMatrixes(); + g_app->modelShader->updateMatrixes(); renderEnts[i].mdl->DrawModel(); } else { - colorShader->bind(); + g_app->colorShader->bind(); - *colorShader->modelMat = renderEnts[i].modelMatAngles; - colorShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); + *g_app->colorShader->modelMat = renderEnts[i].modelMatAngles; + g_app->colorShader->modelMat->translate(renderOffset.x, renderOffset.y, renderOffset.z); - colorShader->updateMatrixes(); + g_app->colorShader->updateMatrixes(); renderEnts[i].pointEntCube->buffer->drawFull(); } @@ -2560,7 +2516,7 @@ void BspRenderer::drawPointEntities(std::vector highlightEnts) } } - colorShader->popMatrix(MAT_MODEL); + g_app->colorShader->popMatrix(MAT_MODEL); } bool BspRenderer::pickPoly(vec3 start, const vec3& dir, int hullIdx, PickInfo& tempPickInfo, Bsp** tmpMap) diff --git a/src/editor/BspRenderer.h b/src/editor/BspRenderer.h index 694f16dc..b7dc81eb 100644 --- a/src/editor/BspRenderer.h +++ b/src/editor/BspRenderer.h @@ -182,7 +182,7 @@ class BspRenderer bool texturesLoaded = false; bool needReloadDebugTextures = false; - BspRenderer(Bsp* map, ShaderProgram* bspShader, ShaderProgram* fullBrightBspShader, ShaderProgram* colorShader, PointEntRenderer* pointEntRenderer); + BspRenderer(Bsp* map, PointEntRenderer* pointEntRenderer); ~BspRenderer(); void render(std::vector highlightEnts, bool highlightAlwaysOnTop, int clipnodeHull); @@ -229,11 +229,6 @@ class BspRenderer bool getRenderPointers(int faceIdx, RenderFace** renderFace, RenderGroup** renderGroup); - ShaderProgram* bspShader; - ShaderProgram* fullBrightBspShader; - ShaderProgram* colorShader; - unsigned int colorShaderMultId; - LightmapInfo* lightmaps = NULL; RenderEnt* renderEnts = NULL; RenderModel* renderModels = NULL; diff --git a/src/editor/Gui.cpp b/src/editor/Gui.cpp index 7998f051..c26b1a19 100644 --- a/src/editor/Gui.cpp +++ b/src/editor/Gui.cpp @@ -6230,7 +6230,7 @@ void Gui::drawImportMapWidget() else if (showImportMapWidget_Type == SHOW_IMPORT_MODEL_BSP) { Bsp* bspModel = new Bsp(mapPath); - BspRenderer* mapRenderer = new BspRenderer(bspModel, NULL, NULL, NULL, NULL); + BspRenderer* mapRenderer = new BspRenderer(bspModel, app->pointEntRenderer); Bsp* map = app->getSelectedMap(); std::vector newPlanes; diff --git a/src/editor/Renderer.cpp b/src/editor/Renderer.cpp index 634575f9..1a091779 100644 --- a/src/editor/Renderer.cpp +++ b/src/editor/Renderer.cpp @@ -123,6 +123,8 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) Renderer::Renderer() { + g_app = this; + glfwSetErrorCallback(error_callback); if (!glfwInit()) @@ -182,14 +184,40 @@ Renderer::Renderer() fullBrightBspShader->setMatrixes(&matmodel, &matview, &projection, &modelView, &modelViewProjection); fullBrightBspShader->setMatrixNames(NULL, "modelViewProjection"); + modelShader = new ShaderProgram(Shaders::g_shader_model_vertex, Shaders::g_shader_model_fragment); + modelShader->setMatrixes(&matmodel, &matview, &projection, &modelView, &modelViewProjection); + modelShader->setMatrixNames(NULL, "modelViewProjection"); + colorShader = new ShaderProgram(Shaders::g_shader_cVert_vertex, Shaders::g_shader_cVert_fragment); colorShader->setMatrixes(&matmodel, &matview, &projection, &modelView, &modelViewProjection); colorShader->setMatrixNames(NULL, "modelViewProjection"); colorShader->setVertexAttributeNames("vPosition", "vColor", NULL); + g_app->bspShader->bind(); + unsigned int sTexId = glGetUniformLocation(g_app->bspShader->ID, "sTex"); + glUniform1i(sTexId, 0); + for (int s = 0; s < MAXLIGHTMAPS; s++) + { + unsigned int sLightmapTexIds = glGetUniformLocation(g_app->bspShader->ID, ("sLightmapTex" + std::to_string(s)).c_str()); + // assign lightmap texture units (skips the normal texture unit) + glUniform1i(sLightmapTexIds, s + 1); + } + + g_app->fullBrightBspShader->bind(); + unsigned int sTexId2 = glGetUniformLocation(g_app->fullBrightBspShader->ID, "sTex"); + glUniform1i(sTexId2, 0); + + g_app->modelShader->bind(); + sTexId2 = glGetUniformLocation(g_app->modelShader->ID, "sTex"); + glUniform1i(sTexId2, 0); + colorShader->bind(); - unsigned int colorMultId = glGetUniformLocation(colorShader->ID, "colorMult"); - glUniform4f(colorMultId, 1, 1, 1, 1); + colorShaderMultId = glGetUniformLocation(g_app->colorShader->ID, "colorMult"); + glUniform4f(colorShaderMultId, 1, 1, 1, 1); + + activeShader = bspShader; + + clearSelection(); oldLeftMouse = curLeftMouse = oldRightMouse = curRightMouse = 0; @@ -197,8 +225,6 @@ Renderer::Renderer() gui->init(); - g_app = this; - g_progress.simpleMode = true; pointEntRenderer = new PointEntRenderer(NULL, colorShader); @@ -354,10 +380,7 @@ void Renderer::renderLoop() } } - //if (SelectedMap && SelectedMap->mdl) - //{ - // SelectedMap->mdl->AdvanceFrame(curTime - oldTime); - //} + activeShader = (g_render_flags & RENDER_LIGHTMAPS) ? bspShader : fullBrightBspShader; int modelIdx = -1; int entIdx = pickInfo.GetSelectedEnt(); @@ -451,9 +474,9 @@ void Renderer::renderLoop() if (SelectedMap->is_mdl_model && SelectedMap->mdl) { - bspShader->bind(); - bspShader->modelMat->loadIdentity(); - bspShader->updateMatrixes(); + modelShader->bind(); + modelShader->modelMat->loadIdentity(); + modelShader->updateMatrixes(); SelectedMap->mdl->DrawModel(); continue; } @@ -504,9 +527,6 @@ void Renderer::renderLoop() } - matmodel.loadIdentity(); - colorShader->bind(); - if (SelectedMap) { if (debugClipnodes && modelIdx > 0) @@ -2128,7 +2148,7 @@ void Renderer::reloadBspModels() tmpBsp->parentMap = bsprend->map; if (tmpBsp->bsp_valid) { - BspRenderer* mapRenderer = new BspRenderer(tmpBsp, bspShader, fullBrightBspShader, colorShader, pointEntRenderer); + BspRenderer* mapRenderer = new BspRenderer(tmpBsp, pointEntRenderer); mapRenderers.push_back(mapRenderer); } } @@ -2166,7 +2186,7 @@ void Renderer::addMap(Bsp* map) */ } - BspRenderer* mapRenderer = new BspRenderer(map, bspShader, fullBrightBspShader, colorShader, pointEntRenderer); + BspRenderer* mapRenderer = new BspRenderer(map, pointEntRenderer); mapRenderers.push_back(mapRenderer); diff --git a/src/editor/Renderer.h b/src/editor/Renderer.h index 5fb9a014..d395d656 100644 --- a/src/editor/Renderer.h +++ b/src/editor/Renderer.h @@ -82,9 +82,13 @@ class Renderer vec3 debugVec2; vec3 debugVec3; + + unsigned int colorShaderMultId; + ShaderProgram* modelShader; ShaderProgram* bspShader; ShaderProgram* fullBrightBspShader; ShaderProgram* colorShader; + ShaderProgram* activeShader; double oldTime = 0.0; double curTime = 0.0; diff --git a/src/gl/VertexBuffer.cpp b/src/gl/VertexBuffer.cpp index e6382a40..529bcc10 100644 --- a/src/gl/VertexBuffer.cpp +++ b/src/gl/VertexBuffer.cpp @@ -134,6 +134,7 @@ void VertexBuffer::addAttribute(int type, const char* varName) { void VertexBuffer::setShader(ShaderProgram* program, bool hideErrors) { shaderProgram = program; attributesBound = false; + for (int i = 0; i < attribs.size(); i++) { if (strlen(attribs[i].varName) > 0) { @@ -141,7 +142,7 @@ void VertexBuffer::setShader(ShaderProgram* program, bool hideErrors) { } } - bindAttributes(hideErrors); + bindAttributes(hideErrors && !g_verbose); if (vboId != (GLuint)-1) { deleteBuffer(); upload(); @@ -159,7 +160,7 @@ void VertexBuffer::bindAttributes(bool hideErrors) { attribs[i].handle = glGetAttribLocation(shaderProgram->ID, attribs[i].varName); - if (!hideErrors && attribs[i].handle == -1) + if ((!hideErrors || g_verbose) && attribs[i].handle == -1) logf(get_localized_string(LANG_0975),attribs[i].varName); } @@ -178,7 +179,7 @@ void VertexBuffer::upload(bool hideErrors) if (!shaderProgram) return; shaderProgram->bind(); - bindAttributes(hideErrors); + bindAttributes(hideErrors && !g_verbose); if (vboId == (GLuint)-1) glGenBuffers(1, &vboId); @@ -212,7 +213,7 @@ void VertexBuffer::deleteBuffer() { void VertexBuffer::drawRange(int _primitive, int start, int end, bool hideErrors) { shaderProgram->bind(); - bindAttributes(hideErrors); + bindAttributes(hideErrors && !g_verbose); char* offsetPtr = (char*)data; if (vboId != (GLuint)-1) { diff --git a/src/gl/primitives.h b/src/gl/primitives.h index 5980213c..a43a6467 100644 --- a/src/gl/primitives.h +++ b/src/gl/primitives.h @@ -20,6 +20,14 @@ struct tVert {} }; +struct modelVert +{ + // texture coordinates + float u, v; + vec3 pos; +}; + + struct lightmapVert { // texture coordinates diff --git a/src/gl/shaders.cpp b/src/gl/shaders.cpp index f33c3aeb..32917629 100644 --- a/src/gl/shaders.cpp +++ b/src/gl/shaders.cpp @@ -62,7 +62,32 @@ namespace Shaders " gl_FragColor = texture2D(sTex, fTex);\n" "}\n"; + const char* g_shader_model_vertex = + // object variables + "uniform mat4 modelViewProjection;\n" + + // vertex variables + "attribute vec3 vPosition;\n" + "attribute vec2 vTex;\n" + + // fragment variables + "varying vec2 fTex;\n" + "void main()\n" + "{\n" + " gl_Position = modelViewProjection * vec4(vPosition, 1);\n" + " fTex = vTex;\n" + "}\n"; + + const char* g_shader_model_fragment = + "varying vec2 fTex;\n" + + "uniform sampler2D sTex;\n" + + "void main()\n" + "{\n" + " gl_FragColor = texture2D(sTex, fTex);\n" + "}\n"; const char* g_shader_multitexture_vertex = // object variables @@ -147,6 +172,10 @@ namespace Shaders // vertex variables "attribute vec3 vPosition;\n" "attribute vec2 vTex;\n" + "attribute vec3 vLightmapTex0;\n" + "attribute vec3 vLightmapTex1;\n" + "attribute vec3 vLightmapTex2;\n" + "attribute vec3 vLightmapTex3;\n" "attribute vec4 vColor;\n" // fragment variables diff --git a/src/gl/shaders.h b/src/gl/shaders.h index c03b5791..406f3958 100644 --- a/src/gl/shaders.h +++ b/src/gl/shaders.h @@ -1,14 +1,17 @@ namespace Shaders { - extern const char* g_shader_cVert_vertex; - extern const char* g_shader_cVert_fragment; + extern const char* g_shader_model_vertex; + extern const char* g_shader_model_fragment; - extern const char* g_shader_tVert_vertex; - extern const char* g_shader_tVert_fragment; + extern const char* g_shader_fullbright_vertex; + extern const char* g_shader_fullbright_fragment; extern const char* g_shader_multitexture_vertex; extern const char* g_shader_multitexture_fragment; - extern const char* g_shader_fullbright_vertex; - extern const char* g_shader_fullbright_fragment; + extern const char* g_shader_cVert_vertex; + extern const char* g_shader_cVert_fragment; + + extern const char* g_shader_tVert_vertex; + extern const char* g_shader_tVert_fragment; } \ No newline at end of file diff --git a/src/mdl/mdl_studio.cpp b/src/mdl/mdl_studio.cpp index 501681ad..10df6f25 100644 --- a/src/mdl/mdl_studio.cpp +++ b/src/mdl/mdl_studio.cpp @@ -681,14 +681,9 @@ void StudioModel::RefreshMeshList(int body) for (int j = 0; j < m_pmodel->nummesh; j++) { - auto tmpBuff = mdl_mesh_groups[body][j].buffer = new VertexBuffer(g_app->fullBrightBspShader, 0, GL_TRIANGLES); - tmpBuff->addAttribute(TEX_2F, "vTex"); - tmpBuff->addAttribute(3, GL_FLOAT, 0, "vLightmapTex0"); - tmpBuff->addAttribute(3, GL_FLOAT, 0, "vLightmapTex1"); - tmpBuff->addAttribute(3, GL_FLOAT, 0, "vLightmapTex2"); - tmpBuff->addAttribute(3, GL_FLOAT, 0, "vLightmapTex3"); - tmpBuff->addAttribute(4, GL_FLOAT, 0, "vColor"); - tmpBuff->addAttribute(POS_3F, "vPosition"); + auto tmpBuf = mdl_mesh_groups[body][j].buffer = new VertexBuffer(g_app->modelShader, 0, GL_TRIANGLES); + tmpBuf->addAttribute(TEX_2F, "vTex"); + tmpBuf->addAttribute(POS_3F, "vPosition"); } } @@ -847,12 +842,6 @@ void StudioModel::RefreshMeshList(int body) if (mdl_mesh_groups[body][j].verts.size() < totalElements) { mdl_mesh_groups[body][j].verts.resize(totalElements); - for (auto& vert : mdl_mesh_groups[body][j].verts) - { - vert.r = vert.g = vert.b = vert.a = 1.0; - vert.luv[0][2] = 1.0; - vert.luv[1][2] = vert.luv[2][2] = vert.luv[3][2] = 0.0f; - } mdl_mesh_groups[body][j].buffer->setData(&mdl_mesh_groups[body][j].verts[0], (int)mdl_mesh_groups[body][j].verts.size()); } for (int z = 0; z < (int)mdl_mesh_groups[body][j].verts.size(); z++) diff --git a/src/mdl/mdl_studio.h b/src/mdl/mdl_studio.h index cbabbcc3..9a236c4b 100644 --- a/src/mdl/mdl_studio.h +++ b/src/mdl/mdl_studio.h @@ -355,12 +355,12 @@ struct StudioMesh { VertexBuffer* buffer; Texture* texture; - std::vector verts; + std::vector verts; StudioMesh() { buffer = NULL; texture = NULL; - verts = std::vector(); + verts = std::vector(); } };