我的应用程序的基本前提(截至目前)只是呈现用户点击的点。这是我的代码(在代码片段中 - 如果可以帮助,我会尝试添加我的“思路”):OpenGL不呈现用户点击的点
首先,用户点击任何他想要显示的点(在初始化窗口):
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为setPositionVector(这是下面的方法):
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));
}
其中在回报VEC 3,我再改漂浮在这个方法:
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);
}
所以这一切都是按照这个顺序做了(用户点击 - >获取光标位置 - >设置我的位置向量 - >,最后更新我的VBO与th Ë新信息)
在我的主循环,我得到这个:
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());
}
我没有得到任何错误,只是一个空白屏幕。另外,我非常确定我的代码并不是最佳编码方式,尽管我只是想指出如何让这些点出现。任何帮助。
谢谢!
注意:如果您打算投票表达此帖,请说明我做错了什么,所以我的下一个问题更符合所需的风格。
啊我看到了,只保留我的glBuffer和glBufferData,这样我只能更新我的VBO? – Pepe
如果你的意思是'glBindBuffer'和'glBufferData',那么是的:这是更新缓冲对象所需要的一切。 'glBufferSubData'会更好(如果缓冲区的大小不变),因为它会覆盖缓冲区而不是分配新的内存。 – BDL
嗯奇怪的是,在我的渲染循环之外绑定这些方法并没有做任何事情。我还可以怎样去调试呢? – Pepe