Skip to content

Commit

Permalink
Fixed wrong GL context being active in various cases
Browse files Browse the repository at this point in the history
Mesh creation (CreateBuffers), mesh destructor, shader/texture creation.
  • Loading branch information
ousnius committed Apr 24, 2020
1 parent bef23b3 commit 011cc3f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/program/OutfitStudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8899,8 +8899,10 @@ void wxGLPanel::AddMeshFromNif(NifFile* nif, const std::string& shapeName) {
m->BuildEdgeList();
m->ColorFill(Vector3());

if (extInitialized)
if (extInitialized) {
gls.SetContext();
m->CreateBuffers();
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/render/GLSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ void GLSurface::HideSegCursor() {
}

void GLSurface::SetSize(uint w, uint h) {
SetContext();
glViewport(0, 0, w, h);
vpW = w;
vpH = h;
Expand Down Expand Up @@ -765,6 +766,11 @@ void GLSurface::RenderToTexture(GLMaterial* renderShader) {

}

void GLSurface::SetContext() {
if (canvas && context)
canvas->SetCurrent(*context);
}

void GLSurface::RenderOneFrame() {
if (!canvas)
return;
Expand Down Expand Up @@ -1247,6 +1253,8 @@ void GLSurface::SetSkinModelMat(mesh *m, const MatTransform &xformGlobalToSkin)
}

mesh* GLSurface::AddVisPoint(const Vector3& p, const std::string& name, const Vector3* color) {
SetContext();

mesh* m = GetOverlay(name);
if (m) {
m->verts[0] = p;
Expand Down Expand Up @@ -1280,6 +1288,8 @@ mesh* GLSurface::AddVisPoint(const Vector3& p, const std::string& name, const Ve
}

int GLSurface::AddVisCircle(const Vector3& center, const Vector3& normal, float radius, const std::string& name) {
SetContext();

Matrix4 rotMat;
int ringMesh = GetOverlayID(name);
if (ringMesh >= 0)
Expand Down Expand Up @@ -1330,6 +1340,8 @@ int GLSurface::AddVisCircle(const Vector3& center, const Vector3& normal, float
}

mesh* GLSurface::AddVis3dRing(const Vector3& center, const Vector3& normal, float holeRadius, float ringRadius, const Vector3& color, const std::string& name) {
SetContext();

const int nRingSegments = 36;
const int nRingSteps = 4;

Expand Down Expand Up @@ -1427,6 +1439,8 @@ mesh* GLSurface::AddVis3dRing(const Vector3& center, const Vector3& normal, floa


mesh* GLSurface::AddVis3dArrow(const Vector3& origin, const Vector3& direction, float stemRadius, float pointRadius, float length, const Vector3& color, const std::string& name) {
SetContext();

const int nRingVerts = 36;
Matrix4 rotMat;

Expand Down Expand Up @@ -1523,6 +1537,8 @@ mesh* GLSurface::AddVis3dArrow(const Vector3& origin, const Vector3& direction,
}

mesh* GLSurface::AddVis3dCube(const Vector3& center, const Vector3& normal, float radius, const Vector3& color, const std::string& name) {
SetContext();

const int nCubeVerts = 8;
const int nCubeTris = 12;
mesh* m = nullptr;
Expand Down Expand Up @@ -1582,6 +1598,8 @@ mesh* GLSurface::AddVis3dCube(const Vector3& center, const Vector3& normal, floa
}

mesh* GLSurface::AddVisPlane(const Vector3& center, const Vector2& size, float uvScale, float uvOffset, const std::string& name, const Vector3* color) {
SetContext();

mesh* m = nullptr;

int meshID = GetOverlayID(name);
Expand Down Expand Up @@ -1631,6 +1649,8 @@ mesh* GLSurface::AddVisPlane(const Vector3& center, const Vector2& size, float u
}

mesh* GLSurface::AddVisSeg(const Vector3& p1, const Vector3& p2, const std::string& name) {
SetContext();

mesh* m = GetOverlay(name);
if (m) {
m->verts[0] = p1;
Expand Down Expand Up @@ -1796,6 +1816,8 @@ RenderMode GLSurface::SetMeshRenderMode(const std::string& name, RenderMode mode
}

GLMaterial* GLSurface::AddMaterial(const std::vector<std::string>& textureFiles, const std::string& vShaderFile, const std::string& fShaderFile, const bool reloadTextures) {
SetContext();

GLMaterial* mat = resLoader.AddMaterial(textureFiles, vShaderFile, fShaderFile, reloadTextures);
if (mat) {
std::string shaderError;
Expand All @@ -1810,6 +1832,7 @@ GLMaterial* GLSurface::AddMaterial(const std::vector<std::string>& textureFiles,

GLMaterial* GLSurface::GetPrimitiveMaterial() {
if (!primitiveMat) {
SetContext();
primitiveMat = new GLMaterial(Config["AppDir"] + "/res/shaders/primitive.vert", Config["AppDir"] + "/res/shaders/primitive.frag");

std::string shaderError;
Expand Down
9 changes: 9 additions & 0 deletions src/render/GLSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class GLSurface {

void DeleteMesh(int meshID) {
if (meshID < meshes.size()) {
SetContext();

activeMeshes.erase(std::remove(activeMeshes.begin(), activeMeshes.end(), meshes[meshID]), activeMeshes.end());
activeMeshesID.erase(std::remove(activeMeshesID.begin(), activeMeshesID.end(), meshID), activeMeshesID.end());

Expand All @@ -93,6 +95,8 @@ class GLSurface {

void DeleteOverlay(int meshID) {
if (meshID < overlays.size()) {
SetContext();

delete overlays[meshID];
overlays.erase(overlays.begin() + meshID);

Expand Down Expand Up @@ -124,6 +128,8 @@ class GLSurface {
}

void DeleteAllMeshes() {
SetContext();

for (auto &m : meshes)
delete m;

Expand All @@ -142,6 +148,8 @@ class GLSurface {
}

void DeleteOverlays() {
SetContext();

for (int i = 0; i < overlays.size(); i++)
delete overlays[i];

Expand Down Expand Up @@ -340,6 +348,7 @@ class GLSurface {
return &resLoader;
}

void SetContext();
void RenderOneFrame();
void RenderToTexture(GLMaterial* renderShader);
void RenderMesh(mesh* m);
Expand Down

0 comments on commit 011cc3f

Please sign in to comment.