gpt4 book ai didi

c++ - OpenGL:纹理渲染卡住

转载 作者:行者123 更新时间:2023-11-30 04:58:52 24 4
gpt4 key购买 nike

当我运行 drawPlane 时:

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

一切都很好。图像已渲染,还有我的三角形。

但是,当更改为

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

纹理不再更新,只有我的三角形......

我该如何解决?


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();

}

最佳答案

您必须在 drawGL 函数中的第一个 glDrawArrays 之前设置 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);:

void drawGL() {

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

.....
glDrawArrays(GL_TRIANGLES, 0, 6);
....

if (user_hit_the_screen > 0)
{
drawPlane();
drawCursorHit();
}
glutSwapBuffers();
}

要理解这个问题,您必须知道 OpenGL 是一个状态机。设置的每个状态都会保留,直到再次更改。
进一步,主循环函数drawGL不断执行。

注意,当glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)drawPlane中设置,则多边形光栅化模式改为GL_LINE
函数离开时处于事件状态的所有状态在下次执行函数时在开始时仍然处于事件状态。这意味着,在下一帧中,开始时光栅化模式是GL_LINE
那是你没想到的,也不是你想要的。您必须确保在函数开始时正确设置所有状态。

关于c++ - OpenGL:纹理渲染卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51507212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com