Skip to content

Commit

Permalink
Remove useless code, add shader for models.
Browse files Browse the repository at this point in the history
Remove useless code, add shader for models.
  • Loading branch information
UnrealKaraulov committed Dec 6, 2023
1 parent d2cfcfb commit 70656d6
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 161 deletions.
180 changes: 68 additions & 112 deletions src/editor/BspRenderer.cpp

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions src/editor/BspRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> highlightEnts, bool highlightAlwaysOnTop, int clipnodeHull);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BSPPLANE> newPlanes;
Expand Down
52 changes: 36 additions & 16 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)

Renderer::Renderer()
{
g_app = this;

glfwSetErrorCallback(error_callback);

if (!glfwInit())
Expand Down Expand Up @@ -182,23 +184,47 @@ 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;


gui->init();

g_app = this;

g_progress.simpleMode = true;

pointEntRenderer = new PointEntRenderer(NULL, colorShader);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -504,9 +527,6 @@ void Renderer::renderLoop()
}


matmodel.loadIdentity();
colorShader->bind();

if (SelectedMap)
{
if (debugClipnodes && modelIdx > 0)
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions src/editor/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/gl/VertexBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ 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) {
attribs[i].handle = -1;
}
}

bindAttributes(hideErrors);
bindAttributes(hideErrors && !g_verbose);
if (vboId != (GLuint)-1) {
deleteBuffer();
upload();
Expand All @@ -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);
}

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/gl/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ struct tVert
{}
};

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


struct lightmapVert
{
// texture coordinates
Expand Down
29 changes: 29 additions & 0 deletions src/gl/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/gl/shaders.h
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 3 additions & 14 deletions src/mdl/mdl_studio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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++)
Expand Down
4 changes: 2 additions & 2 deletions src/mdl/mdl_studio.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,12 @@ struct StudioMesh
{
VertexBuffer* buffer;
Texture* texture;
std::vector<lightmapVert> verts;
std::vector<modelVert> verts;
StudioMesh()
{
buffer = NULL;
texture = NULL;
verts = std::vector<lightmapVert>();
verts = std::vector<modelVert>();
}
};

Expand Down

0 comments on commit 70656d6

Please sign in to comment.