我的應用程序的基本前提(截至目前)只是呈現用戶點擊的點。這是我的代碼(在代碼片段中 - 如果可以幫助,我會嘗試添加我的「思路」):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