Reputation: 51
while I am running the drawPlane
with:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
everything is fine. The image is rendered and also my triangles.
However, while changing to
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
the texture is not updated anymore only my triangles...
How can I fix it?
void drawCursorHit() {
float cx = (hit_coord.x / (float)wWnd) * 2 - 1;
float cy = ((hit_coord.y / (float)hWnd) * 2 - 1)*-1.f;
float lx = 0.02f;
float ly = lx * 16. / 9.;
glLineWidth(1.5);
glColor3f(0.25f, 0.55f, 0.85f);
glBegin(GL_LINES);
glVertex3f(cx - lx, cy, 0.0);
glVertex3f(cx + lx, cy, 0.0);
glVertex3f(cx, cy - ly, 0.0);
glVertex3f(cx, cy + ly, 0.0);
glEnd();
}
void drawPlane() {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.25f, 0.55f, 0.85f);
glBegin(GL_TRIANGLES);
for (int i = 0; i < continuous_plane.triangles.size(); i++)
{
sl::float3 vertices_1 = continuous_plane.vertices.at(continuous_plane.triangles.at(i).x);
sl::float3 vertices_2 = continuous_plane.vertices.at(continuous_plane.triangles.at(i).y);
sl::float3 vertices_3 = continuous_plane.vertices.at(continuous_plane.triangles.at(i).z);
int pixel_x_1 = static_cast<int>(camera_cx + (vertices_1.x * camera_fx) / vertices_1.z);
int pixel_y_1 = static_cast<int>(camera_cy + (vertices_1.y * camera_fy) / vertices_1.z);
int pixel_x_2 = static_cast<int>(camera_cx + (vertices_2.x * camera_fx) / vertices_2.z);
int pixel_y_2 = static_cast<int>(camera_cy + (vertices_2.y * camera_fy) / vertices_2.z);
int pixel_x_3 = static_cast<int>(camera_cx + (vertices_3.x * camera_fx) / vertices_3.z);
int pixel_y_3 = static_cast<int>(camera_cy + (vertices_3.y * camera_fy) / vertices_3.z);
float x_gl_1 = (pixel_x_1 / (float)wWnd) * 2 - 1;
float y_gl_1 = ((pixel_y_1 / (float)hWnd) * 2 - 1)*-1.f;
float x_gl_2 = (pixel_x_2 / (float)wWnd) * 2 - 1;
float y_gl_2 = ((pixel_y_2 / (float)hWnd) * 2 - 1)*-1.f;
float x_gl_3 = (pixel_x_3 / (float)wWnd) * 2 - 1;
float y_gl_3 = ((pixel_y_3 / (float)hWnd) * 2 - 1)*-1.f;
glVertex3f(x_gl_1, y_gl_1, 0.0);
glVertex3f(x_gl_2, y_gl_2, 0.0);
glVertex3f(x_gl_3, y_gl_3, 0.0);
}
glEnd();
}
/**
OpenGL draw function
Render Image and wireframe mesh into a texture using the FrameBuffer
**/
void drawGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glViewport(0, 0, wWnd, hWnd);
// Render image and wireframe mesh into a texture using frame buffer
// Bind the frame buffer and specify the viewport (full screen)
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// Render the ZED view (Left) in the framebuffer
glUseProgram(shader_image->getProgramId());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, imageTex);
glUniform1i(texID, 0);
//invert y axis and color for this image (since its reverted from cuda array)
glUniform1i(glGetUniformLocation(shader_image->getProgramId(), "revert"), 1);
glUniform1i(glGetUniformLocation(shader_image->getProgramId(), "rgbflip"), 1);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, quad_vb);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glUseProgram(0);
// Unbind the framebuffer since the texture is now updated
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Render the texture to the screen
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glUseProgram(shader_image->getProgramId());
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glUniform1i(texID, 0);
glUniform1i(glGetUniformLocation(shader_image->getProgramId(), "revert"), 0);
glUniform1i(glGetUniformLocation(shader_image->getProgramId(), "rgbflip"), 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, quad_vb);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glUseProgram(0);
glDisable(GL_TEXTURE_2D);
if (user_hit_the_screen > 0)
{
drawPlane();
drawCursorHit();
}
// Swap buffers
glutSwapBuffers();
}
Upvotes: 1
Views: 255
Reputation: 210948
You have to set glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
before the 1st glDrawArrays
in the function drawGL
:
void drawGL() {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
.....
glDrawArrays(GL_TRIANGLES, 0, 6);
....
if (user_hit_the_screen > 0)
{
drawPlane();
drawCursorHit();
}
glutSwapBuffers();
}
To understand the issue, you have to know that OpenGL is a state machine. Every state which is set, is kept, until it is changed again.
Further, the main loop function drawGL
is executed continuously.
Note, when glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
is set in drawPlane
, then the polygon rasterization mode is changes to GL_LINE
.
All the states which are active when the function is left are still active at the begin, when the function is executed the next time. This means that, in the next frame, the rasterization mode is GL_LINE
at the beginning.
That is what you didn't expect and is not what you want. You have to ensure that all states are proper set at the begin of the function.
Upvotes: 1