Skip to content

Commit 23c98cb

Browse files
committed
Draw selection rectangle via screen shader
1 parent 43b8a8e commit 23c98cb

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

GUI/OpenGL/MiniGL.cpp

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ GLFWwindow* MiniGL::m_glfw_window = nullptr;
7070
bool MiniGL::m_vsync = false;
7171
double MiniGL::m_lastTime;
7272
Shader MiniGL::m_shader;
73+
Shader MiniGL::m_shader_screen;
7374
Matrix4r MiniGL::m_modelview_matrix;
7475
Matrix4r MiniGL::m_projection_matrix;
7576
Vector3r MiniGL::m_ambientIntensity;
@@ -117,11 +118,9 @@ void MiniGL::coordinateSystem()
117118

118119
void MiniGL::drawVector(const Vector3r &a, const Vector3r &b, const float w, float *color)
119120
{
120-
float thickness = 0.001 * w;
121+
float thickness = 0.001f * w;
121122

122-
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
123123
drawCylinder(a, b, color, thickness, 8, false);
124-
glPolygonMode(GL_FRONT_AND_BACK, drawMode);
125124
}
126125

127126
void MiniGL::drawCylinder(const Vector3r &a, const Vector3r &b, const float *color, const float radius, const unsigned int subdivisions, const bool lighting)
@@ -710,7 +709,7 @@ void MiniGL::initLights ()
710709
m_lightPosition.segment<3>(2 * 3) << 0.0, 10.0, 10.0;
711710
}
712711

713-
void MiniGL::initShader(const std::string& shaderPath)
712+
void MiniGL::initShaders(const std::string& shaderPath)
714713
{
715714
Shader& shader = m_shader;
716715
shader.compileShaderFile(GL_VERTEX_SHADER, shaderPath + "/mini.vert");
@@ -730,6 +729,16 @@ void MiniGL::initShader(const std::string& shaderPath)
730729
shader.addUniform("specularReflectance");
731730
shader.addUniform("shininess");
732731
shader.end();
732+
733+
Shader& screenShader = m_shader_screen;
734+
screenShader.compileShaderFile(GL_VERTEX_SHADER, shaderPath + "/mini_screen.vert");
735+
screenShader.compileShaderFile(GL_FRAGMENT_SHADER, shaderPath + "/mini_screen.frag");
736+
screenShader.createAndLinkProgram();
737+
screenShader.begin();
738+
screenShader.addUniform("width");
739+
screenShader.addUniform("height");
740+
screenShader.addUniform("color");
741+
screenShader.end();
733742
}
734743

735744
void MiniGL::enableShader(const Vector3r& ambientReflectance, const Vector3r& diffuseReflectance, const Vector3r& specularReflectance, const Real shininess, const Real pointSize)
@@ -759,6 +768,21 @@ void MiniGL::disableShader()
759768
shader.end();
760769
}
761770

771+
void MiniGL::enableScreenShader(const Vector3r& color)
772+
{
773+
Shader& shader = m_shader_screen;
774+
shader.begin();
775+
glUniform1f(shader.getUniform("width"), static_cast<Real>(m_width));
776+
glUniform1f(shader.getUniform("height"), static_cast<Real>(m_height));
777+
glUniform3fv(shader.getUniform("color"), 1, &color(0));
778+
}
779+
780+
void MiniGL::disableScreenShader()
781+
{
782+
Shader& shader = m_shader_screen;
783+
shader.end();
784+
}
785+
762786
void MiniGL::supplyVectors(GLuint index, GLuint vbo, unsigned int dim, unsigned int n, const Real* data)
763787
{
764788
glBindBuffer(GL_ARRAY_BUFFER, vbo);
@@ -1006,25 +1030,17 @@ void MiniGL::error_callback(int error, const char* description)
10061030

10071031
void MiniGL::drawSelectionRect()
10081032
{
1009-
glDisable(GL_LIGHTING);
1010-
glPushMatrix();
1011-
glLoadIdentity();
1012-
glMatrixMode(GL_PROJECTION);
1013-
glPushMatrix();
1014-
glLoadIdentity();
1015-
glOrtho(0.0f, m_width, m_height, 0.0f, -1.0f, 1.0f);
1016-
glBegin(GL_LINE_LOOP);
1017-
glColor3f(1, 0, 0); //Set the colour to red
1018-
glVertex2f(static_cast<GLfloat>(m_selectionStart[0]), static_cast<GLfloat>(m_selectionStart[1]));
1019-
glVertex2f(static_cast<GLfloat>(m_selectionStart[0]), static_cast<GLfloat>(mouse_pos_y_old));
1020-
glVertex2f(mouse_pos_x_old, mouse_pos_y_old);
1021-
glVertex2f(mouse_pos_x_old, m_selectionStart[1]);
1022-
glEnd();
1023-
glPopMatrix();
1024-
glMatrixMode(GL_MODELVIEW);
1025-
1026-
glPopMatrix();
1027-
glEnable(GL_LIGHTING);
1033+
Real selectionRect[] = {
1034+
static_cast<Real>(m_selectionStart[0]), static_cast<Real>(m_selectionStart[1]),
1035+
static_cast<Real>(m_selectionStart[0]), static_cast<Real>(mouse_pos_y_old),
1036+
static_cast<Real>(mouse_pos_x_old), static_cast<Real>(mouse_pos_y_old),
1037+
static_cast<Real>(mouse_pos_x_old), static_cast<Real>(m_selectionStart[1])
1038+
};
1039+
enableScreenShader(Vector3r(1, 0, 0));
1040+
supplyVectors(0, m_vbo_vertices, 2, 4, &selectionRect[0]);
1041+
glDrawArrays(GL_LINE_LOOP, 0, 4);
1042+
glDisableVertexAttribArray(0);
1043+
disableScreenShader();
10281044
}
10291045

10301046
void MiniGL::mainLoop()

GUI/OpenGL/MiniGL.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ namespace SPH
109109
static bool m_vsync;
110110
static double m_lastTime;
111111
static Shader m_shader;
112+
static Shader m_shader_screen;
112113
static Matrix4r m_modelview_matrix;
113114
static Matrix4r m_projection_matrix;
114115
static Vector3r m_ambientIntensity;
@@ -220,9 +221,11 @@ namespace SPH
220221
static const Matrix4r& getModelviewMatrix() { return m_modelview_matrix; }
221222
static const Matrix4r& getProjectionMatrix() { return m_projection_matrix; }
222223

223-
static void initShader(const std::string& shaderPath);
224+
static void initShaders(const std::string& shaderPath);
224225
static void enableShader(const Vector3r& ambientReflectance, const Vector3r& diffuseReflectance, const Vector3r& specularReflectance, const Real shininess, const Real pointSize=1.0);
225226
static void disableShader();
227+
static void enableScreenShader(const Vector3r& color);
228+
static void disableScreenShader();
226229

227230
static const GLuint getVao() { return m_vao; }
228231
static const GLuint getVboVertices() { return m_vbo_vertices; }

Simulator/GUI/OpenGL/Simulator_OpenGL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Simulator_OpenGL::~Simulator_OpenGL(void)
3333

3434
void Simulator_OpenGL::initShaders(const std::string &shaderPath)
3535
{
36-
MiniGL::initShader(shaderPath);
36+
MiniGL::initShaders(shaderPath);
3737

3838
string vertFile = shaderPath + "/vs_points_vector.glsl";
3939
string fragFile = shaderPath + "/fs_points.glsl";

Tools/PartioViewer/GUI/OpenGL/PartioViewer_OpenGL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void PartioViewer_OpenGL::getImage(int width, int height, unsigned char *image)
5555

5656
void PartioViewer_OpenGL::initShaders(const std::string &shaderPath)
5757
{
58-
MiniGL::initShader(shaderPath);
58+
MiniGL::initShaders(shaderPath);
5959

6060
string vertFile = shaderPath + "/vs_points_vector.glsl";
6161
string fragFile = shaderPath + "/fs_points.glsl";

data/shaders/mini_screen.frag

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 330
2+
3+
uniform vec3 color;
4+
5+
out vec4 outColor;
6+
7+
void main()
8+
{
9+
outColor = vec4(color, 1.0);
10+
}

data/shaders/mini_screen.vert

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 330
2+
3+
uniform float width;
4+
uniform float height;
5+
6+
layout(location = 0) in vec2 position;
7+
8+
void main()
9+
{
10+
gl_Position = vec4(2.0 * (position.x / width) - 1.0, -2.0 * (position.y / height) + 1.0, -1.0, 1.0);
11+
}

0 commit comments

Comments
 (0)