Skip to content

Commit

Permalink
Revert back OPENGL to 3.0. Bugfixes.
Browse files Browse the repository at this point in the history
Revert back OPENGL to 3.0. Bugfixes. Add undo/redo for cull leaf faces.
  • Loading branch information
UnrealKaraulov committed Dec 28, 2023
1 parent 8e5a492 commit c12c5c6
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 50 deletions.
12 changes: 9 additions & 3 deletions src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ BspRenderer::BspRenderer(Bsp* _map, PointEntRenderer* _pointEntRenderer)
this->renderEnts = NULL;
this->renderClipnodes = NULL;

lightEnableFlags[0] = true;
lightEnableFlags[1] = lightEnableFlags[2] = lightEnableFlags[3] = false;
lightEnableFlags[0] = lightEnableFlags[1] = lightEnableFlags[2] = lightEnableFlags[3] = true;

intersectVec = vec3();

Expand Down Expand Up @@ -562,6 +561,7 @@ void BspRenderer::loadLightmaps()
}
}
}

}

}
Expand Down Expand Up @@ -1054,13 +1054,13 @@ int BspRenderer::refreshModel(int modelIdx, bool refreshClipnodes, bool noTriang
memcpy(renderGroups[i].wireframeVerts, &renderGroupWireframeVerts[i][0], renderGroups[i].wireframeVertCount * sizeof(cVert));

auto tmpBuf = renderGroups[i].buffer = new VertexBuffer(g_app->bspShader, 0, GL_TRIANGLES);
tmpBuf->addAttribute(POS_3F, "vPosition");
tmpBuf->addAttribute(TEX_2F, "vTex");
tmpBuf->addAttribute(3, GL_FLOAT, 0, "vLightmapTex0");
tmpBuf->addAttribute(3, GL_FLOAT, 0, "vLightmapTex1");
tmpBuf->addAttribute(3, GL_FLOAT, 0, "vLightmapTex2");
tmpBuf->addAttribute(3, GL_FLOAT, 0, "vLightmapTex3");
tmpBuf->addAttribute(4, GL_FLOAT, 0, "vColor");
tmpBuf->addAttribute(POS_3F, "vPosition");
tmpBuf->setData(renderGroups[i].verts, renderGroups[i].vertCount);

renderGroups[i].wireframeBuffer = new VertexBuffer(g_app->colorShader, COLOR_4B | POS_3F, renderGroups[i].wireframeVerts, renderGroups[i].wireframeVertCount, GL_LINES);
Expand Down Expand Up @@ -2349,14 +2349,20 @@ void BspRenderer::drawModel(RenderEnt* ent, bool transparent, bool highlight, bo
}
else if (lightmapsUploaded && lightmapsGenerated && (g_render_flags & RENDER_LIGHTMAPS))
{

if (rgroup.lightmapAtlas[s] && lightEnableFlags[s])
{
rgroup.lightmapAtlas[s]->bind(s + 1);
if (s > 0 && !map->lightdata)
{
blackTex->bind(s + 1);
}
}
else
{
blackTex->bind(s + 1);
}

}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/editor/BspRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class BspRenderer
Bsp* map;
PointEntRenderer* pointEntRenderer;
vec3 mapOffset;
bool lightEnableFlags[4] = { true,false,false,false };
bool lightEnableFlags[4] = { true,true,true,true };
std::vector<Wad*> wads;
bool texturesLoaded = false;
bool needReloadDebugTextures = false;
Expand Down
176 changes: 138 additions & 38 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2695,67 +2695,146 @@ void Gui::drawMenuBar()
ImGui::EndMenu();
}

if (ImGui::MenuItem("Delete cull faces"))
if (ImGui::BeginMenu("Delete cull faces", map))
{
int leafIdx = 0;

BSPLEAF32& leaf = map->leaves[leafIdx];
int thisLeafCount = map->leafCount;
if (ImGui::MenuItem("Delete from SOLID (0 leaf)"))
{
BSPLEAF32& leaf = map->leaves[leafIdx];
int thisLeafCount = map->leafCount;

int oldVisRowSize = ((thisLeafCount + 63) & ~63) >> 3;
int oldVisRowSize = ((thisLeafCount + 63) & ~63) >> 3;

unsigned char* visData = new unsigned char[oldVisRowSize];
memset(visData, 0xFF, oldVisRowSize);
//DecompressLeafVis(map->visdata + leaf.nVisOffset, map->leafCount - leaf.nVisOffset, visData, map->leafCount);
DecompressVis(map->visdata + leaf.nVisOffset, visData, oldVisRowSize, map->leafCount, map->visDataLength - leaf.nVisOffset);
unsigned char* visData = new unsigned char[oldVisRowSize];
memset(visData, 0xFF, oldVisRowSize);
//DecompressLeafVis(map->visdata + leaf.nVisOffset, map->leafCount - leaf.nVisOffset, visData, map->leafCount);
DecompressVis(map->visdata + leaf.nVisOffset, visData, oldVisRowSize, map->leafCount, map->visDataLength - leaf.nVisOffset);

bool FoundAnyFace = true;
bool FoundAnyFace = true;

int cullfaces = 0;
int cullfaces = 0;

for (int l = 0; l < map->leafCount - 1 && !FoundAnyFace; l++)
{
if (l == leafIdx || CHECKVISBIT(visData, l))
for (int l = 0; l < map->leafCount - 1; l++)
{
auto faceList = map->getLeafFaces(l + 1);
for (const auto& idx : faceList)
if (l == leafIdx || CHECKVISBIT(visData, l))
{
cullfaces++;
auto faceList = map->getLeafFaces(l + 1);
for (const auto& idx : faceList)
{
cullfaces++;
}
}
}
}

g_progress.update("Remove cull faces.[LEAF 0 CLEAN]", cullfaces);
g_progress.update("Remove cull faces.[LEAF 0 CLEAN]", cullfaces);



while (FoundAnyFace)
{
FoundAnyFace = false;
for (int l = 0; l < map->leafCount - 1 && !FoundAnyFace; l++)
while (FoundAnyFace)
{
if (l == leafIdx || CHECKVISBIT(visData, l))
FoundAnyFace = false;
for (int l = 0; l < map->leafCount - 1 && !FoundAnyFace; l++)
{
auto faceList = map->getLeafFaces(l + 1);
for (const auto& idx : faceList)
if (l == leafIdx || CHECKVISBIT(visData, l))
{
map->remove_face(idx);
g_progress.tick();
FoundAnyFace = true;
break;
auto faceList = map->getLeafFaces(l + 1);
for (const auto& idx : faceList)
{
map->remove_face(idx);
g_progress.tick();
FoundAnyFace = true;
break;
}
}
}
}
delete[] visData;

map->getBspRender()->loadLightmaps();
map->getBspRender()->calcFaceMaths();
map->getBspRender()->preRenderFaces();
map->getBspRender()->preRenderEnts();

map->update_lump_pointers();
map->update_ent_lump();


map->getBspRender()->pushModelUndoState("REMOVE FACES FROM SOLID(0 leaf)", EDIT_MODEL_LUMPS);

}
delete[] visData;
std::vector<int> nodeBranch;
vec3 localCamera = cameraOrigin - rend->mapOffset;
int childIdx = -1;
leafIdx = -1;
int headNode = map->models[0].iHeadnodes[0];
map->pointContents(headNode, localCamera, 0, nodeBranch, leafIdx, childIdx);

map->getBspRender()->loadLightmaps();
map->getBspRender()->calcFaceMaths();
map->getBspRender()->preRenderFaces();
map->getBspRender()->preRenderEnts();
if (leafIdx > 0)
{
if (ImGui::MenuItem(fmt::format("Delete from {} leaf",leafIdx).c_str()))
{
BSPLEAF32& leaf = map->leaves[leafIdx];
int thisLeafCount = map->leafCount;

map->update_lump_pointers();
map->update_ent_lump();
int oldVisRowSize = ((thisLeafCount + 63) & ~63) >> 3;

unsigned char* visData = new unsigned char[oldVisRowSize];
memset(visData, 0xFF, oldVisRowSize);
//DecompressLeafVis(map->visdata + leaf.nVisOffset, map->leafCount - leaf.nVisOffset, visData, map->leafCount);
DecompressVis(map->visdata + leaf.nVisOffset, visData, oldVisRowSize, map->leafCount, map->visDataLength - leaf.nVisOffset);

bool FoundAnyFace = true;

int cullfaces = 0;

for (int l = 0; l < map->leafCount - 1; l++)
{
if (l == leafIdx || CHECKVISBIT(visData, l))
{
auto faceList = map->getLeafFaces(l + 1);
for (const auto& idx : faceList)
{
cullfaces++;
}
}
}

g_progress.update("Remove cull faces.[LEAF 0 CLEAN]", cullfaces);

while (FoundAnyFace)
{
FoundAnyFace = false;
for (int l = 0; l < map->leafCount - 1 && !FoundAnyFace; l++)
{
if (l == leafIdx || CHECKVISBIT(visData, l))
{
auto faceList = map->getLeafFaces(l + 1);
for (const auto& idx : faceList)
{
map->remove_face(idx);
g_progress.tick();
FoundAnyFace = true;
break;
}
}
}
}
delete[] visData;

map->getBspRender()->loadLightmaps();
map->getBspRender()->calcFaceMaths();
map->getBspRender()->preRenderFaces();
map->getBspRender()->preRenderEnts();

map->update_lump_pointers();
map->update_ent_lump();


map->getBspRender()->pushModelUndoState(fmt::format("REMOVE FACES FROM {} leaf",leafIdx), EDIT_MODEL_LUMPS);
}
}
ImGui::EndMenu();
}

ImGui::Separator();
Expand Down Expand Up @@ -8236,6 +8315,15 @@ void Gui::drawLightMapTool()
"Light 3"
};

static int light_offsets[] =
{
0,
0,
0,
0,
0
};

ImGui::SetNextWindowSize(ImVec2(windowWidth, windowHeight), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSizeConstraints(ImVec2(windowWidth, windowHeight), ImVec2(windowWidth, -1.0f));

Expand Down Expand Up @@ -8270,7 +8358,11 @@ void Gui::drawLightMapTool()
int lightmapSz = size[0] * size[1] * sizeof(COLOR3);
currentlightMap[i] = new Texture(size[0], size[1], new unsigned char[lightmapSz], "LIGHTMAP");
int offset = face.nLightmapOffset + i * lightmapSz;
memcpy(currentlightMap[i]->data, map->lightdata + offset, lightmapSz);
light_offsets[i] = offset;
if (!map->lightdata || offset + lightmapSz > map->lightDataLength )
memset(currentlightMap[i]->data, 255, lightmapSz);
else
memcpy(currentlightMap[i]->data, map->lightdata + offset, lightmapSz);
currentlightMap[i]->upload(Texture::TEXTURE_TYPE::TYPE_LIGHTMAP);
lightmaps++;
//print_log(get_localized_string(LANG_0418),i,offset);
Expand All @@ -8292,6 +8384,10 @@ void Gui::drawLightMapTool()
ImGui::Dummy(ImVec2(120, 5.0f));
ImGui::SameLine();
ImGui::TextDisabled(light_names[2]);
ImGui::Separator();
ImGui::Dummy(ImVec2(50, 5.0f));
ImGui::SameLine();
ImGui::TextDisabled(fmt::format("Light offest:{}", light_offsets[i]).c_str());
}

if (i == 2)
Expand All @@ -8304,6 +8400,10 @@ void Gui::drawLightMapTool()
ImGui::Dummy(ImVec2(150, 5.0f));
ImGui::SameLine();
ImGui::TextDisabled(light_names[4]);
ImGui::Separator();
ImGui::Dummy(ImVec2(50, 5.0f));
ImGui::SameLine();
ImGui::TextDisabled(fmt::format("Light offest:{}", light_offsets[i]).c_str());
}

if (i == 1 || i > 2)
Expand Down Expand Up @@ -8812,7 +8912,7 @@ void Gui::drawFaceEditorWidget()
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::TextUnformatted("Face will be removed from leaves and now only invisibled!");
ImGui::TextUnformatted("Face will be removed from leaves and make this face only invisibled!");
ImGui::EndTooltip();
}
ImGui::PopStyleColor();
Expand Down
11 changes: 7 additions & 4 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ Renderer::Renderer()
loadSettings();

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

window = glfwCreateWindow(g_settings.windowWidth, g_settings.windowHeight, "bspguy", NULL, NULL);

Expand Down Expand Up @@ -809,6 +806,12 @@ void Renderer::loadSettings()
g_verbose = g_settings.verboseLogs;
zFar = g_settings.zfar;
fov = g_settings.fov;

if (zFar < 1000.0f)
zFar = 1000.0f;
if (fov < 40.0f)
fov = 40.0f;

g_render_flags = g_settings.render_flags;
gui->fontSize = g_settings.fontSize;
rotationSpeed = g_settings.rotSpeed;
Expand Down
6 changes: 3 additions & 3 deletions src/gl/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

struct tVert
{
vec3 pos;
// texture coordinates
float u, v;
vec3 pos;

tVert() = default;
tVert(float x, float y, float z, float u, float v) : u(u), v(v), pos(x, y, z)
Expand All @@ -24,22 +24,22 @@ struct tVert

struct modelVert
{
vec3 pos;
// texture coordinates
float u, v;
vec3 pos;
};


struct lightmapVert
{
vec3 pos;
// texture coordinates
float u, v;
// lightmap texture coordinates
// last value scales the lightmap brightness
float luv[MAX_LIGHTMAPS][3];
// color
float r, g, b, a;
vec3 pos;
};

struct cVert
Expand Down
2 changes: 1 addition & 1 deletion src/gl/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace Shaders
"uniform vec4 colorMult;\n"

// vertex variables
"attribute vec4 vColor;\n"
"attribute vec3 vPosition;\n"
"attribute vec4 vColor;\n"

// fragment variables
"varying vec4 fColor;\n"
Expand Down

0 comments on commit c12c5c6

Please sign in to comment.