OpenGL未渲染用户点击的点

OpenGL not rendering points clicked by users

本文关键字:用户 OpenGL      更新时间:2023-10-16

我应用程序的基本前提(截至目前)只是为了渲染用户点击的点。这是我拥有的代码(在摘要中 - 如果可以帮助您,我将尝试添加"思想列车"):

首先,用户单击他希望显示点的任何位置(在窗口初始化之后):

  void mouseButtonCallback(GLFWwindow* _window, int button, int action, int mods)
{
     switch (button)
   {
    case GLFW_MOUSE_BUTTON_LEFT:
    if (action == GLFW_RELEASE)
    {
        if (transitionalFlag == false) {

            glfwGetCursorPos(window, &x, &y);
            setPositionVector(x, y);
            flag = true;
        }
        else { //please ignore the else for the moment seeing as I can't get the first one to work
            glfwGetCursorPos(window, &x, &y);
            setPositionVector(x, y);
            pointsClickedByUserTranslational.push_back(positionsClickedByUser);
        }
    }
  }
}

因此,每当我必须采用第一行时,我都会去过渡,然后将光标的x和y带入setPosition向量(这是以下方法):

void setPositionVector(double xpos, double ypos) {
    if (transitionalFlag == false) {
         positionsClickedByUser = windowToWorldCoords(glm::vec2(x, y));
         vectorsToFloat(positionsClickedByUser);
         updatePointsVBO();
}
   else {
       positionsClickedByUser = glm::vec3(xpos, ypos, 0);
       vectorsToFloat(positionsClickedByUser);
       updatePointsVBO();
  }
}

此方法采用X和Y,首先将其更改为我的世界坐标(这是以下方法):

glm::vec3 windowToWorldCoords(const glm::vec2 p)
{
// Get window dimensions
   int w, h;
   glfwGetWindowSize(window, &w, &h);
// Transform to camera coordinates; we assume the z-coordinate to be 0
   const GLfloat cameraX = 2 * p.x / w - 1;
   const GLfloat cameraY = -(2 * p.y / h - 1);
// Transform to world coordinates by inverting the transformation matrix
   return glm::vec3(glm::inverse(transformationMatrix) * glm::vec4(cameraX,         cameraY, 0.0f, 1.0f));
}    

在返回一个vec3中,我然后以这种方法更改为float:

 void vectorsToFloat(glm::vec3 vector) {
      vectorOfVertices.push_back(vector.x);
      vectorOfVertices.push_back(vector.y);
       vectorOfVertices.push_back(vector.z);
}

之后,我有我的updatevbo()方法,该方法可以说,每当用户在屏幕上单击

时,都会更新我的VBO。
void  updatePointsVBO()
{
    glBindVertexArray(pointsVAO);
    glBindBuffer(GL_ARRAY_BUFFER, pointsVBO);
    glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
    if (vectorOfVertices.size() > 0)
    {
       glBufferData(GL_ARRAY_BUFFER, sizeof(vectorOfVertices[0]) *      vectorOfVertices.size(), &vectorOfVertices[0], GL_STATIC_DRAW);
   }
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
  glEnableVertexAttribArray(0);
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindVertexArray(0);
}

因此,所有这些都是按照该顺序完成的(用户单击 ->获取光标pos->设置我的位置向量 ->最后使用新信息更新我的VBO)

在我的主循环中,我得到了:

int main() {

   initializeOpenGL();
   shader_program = shadersInitialization("vertex.shader", "fragment.shader");
   //linke your matrices to your shader
   modelMatrixLocation = glGetUniformLocation(shader_program, "modelMatrix");
   projectionMatrixLocation = glGetUniformLocation(shader_program, "projectionMatrix");
   viewMatrixLocation = glGetUniformLocation(shader_program, "viewMatrix");


   //Function allows to set camera to look at object
   modelMatrix = glm::lookAt(cameraPos, cameraTarget, upVector);

  while (!glfwWindowShouldClose(window)) {
      glfwPollEvents();

       glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

       glUseProgram(shader_program);
       //projMatrix = glm::perspective(45.0f, (float)WIDTH/(float)HEIGHT, 0.1f, 1000.0f );

       glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
       glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix));
       glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
      draw();

      glfwSwapBuffers(window);
   }

   glDeleteVertexArrays(1, &pointsVAO);
   glDeleteBuffers(1, &pointsVBO);
   glfwTerminate();
   return 0;
}

我的抽奖功能:

 void draw()
{
  glGenBuffers(1, &pointsVBO);
  glGenVertexArrays(1, &pointsVAO);
  glBindVertexArray(pointsVAO);
  glDrawArrays(GL_POINTS, 0, vectorOfVertices.size());
 }

我没有遇到任何错误,只是一个空白屏幕。另外,我敢肯定,我的代码现在不是现在的编码方式,尽管我只是喜欢关于如何使这些点出现的一些指示。任何会有所帮助。

谢谢!

注意:请,如果您要拒绝这篇文章,请指出我做错了什么,所以我的下一个问题更适合所需的样式。

在Draw方法的每个调用中都生成了一个新的缓冲区。由于您将数据上传到这样的缓冲区,然后将其扔掉并生成新的缓冲区,因此您总是用空的缓冲区绘制。您应该在渲染循环外移动对象生成。

此外,目前只有最后一个pointsVBOpointsVAO被删除。您应该阅读一些有关哪个函数属于初始化代码的信息,并且只能调用一次(例如glGenVertexArraysglVertexAttribPointerglVertexAttribPointer),并且应仅调用一次,并且必须称哪些函数为每个帧。