私のアプリケーションの基本的な前提は、ポイントをユーザーがクリックするようにレンダリングすることです。これは私が持っているコードです(スニペットで - 私が "思考の列"を追加しようとすると助けになります):レンダリングポイントがユーザーによってクリックされない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);
}
}
}
}
は、だから私は過渡的に移動し、次の方法である(setPositionVectorに私のカーソルのxとyを取る):
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を取り込み、最初にそれを自分のワールド座標に変更します(これは以下の方法):
私は、この方法でfloatに変更vec3リターンで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));
}
:
その後void vectorsToFloat(glm::vec3 vector) {
vectorOfVertices.push_back(vector.x);
vectorOfVertices.push_back(vector.y);
vectorOfVertices.push_back(vector.z);
}
を、私が仮定されて私のupdateVBO()メソッドを持っている、だけでなく、 >カーソルPOSを取得 - - >設定私の位置ベクトル - >そして最後に更新それは(
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毎回ユーザーがクリックを更新し、言います私のVBOとth私は、ちょうど空白の画面を任意のエラーを取得していないよ
void draw()
{
glGenBuffers(1, &pointsVBO);
glGenVertexArrays(1, &pointsVAO);
glBindVertexArray(pointsVAO);
glDrawArrays(GL_POINTS, 0, vectorOfVertices.size());
}
:
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;
}
マイ描画機能:E私のメインループでは、新たな情報)
私はこれを得ました。また、コードを最適化していないことを確かめていますが、これらの点をどのように表示するかについての指針がほしいと思います。誰でも助けてくれるだろう。
ありがとうございます!
注:この投稿に投票する場合は、私の次の質問がよりスタイルに合ったものになるように間違ったことを示してください。
ああ、私は参照してください、私だけの私のVBBOを更新することができますので、私のglBufferとglBufferDataだけを維持するだろうか? – Pepe
あなたが 'glBindBuffer'と' glBufferData'を意味するならば、これはyesです:これはバッファオブジェクトを更新するのに必要なすべてです。 'glBufferSubData'は新しいメモリを割り当てる代わりにバッファをオーバーライドするので、バッファのサイズが変更されない場合はさらに良いでしょう。 – BDL
奇妙なことに、私のレンダリングループの外側でそれらのメソッドをバインドすることは何もしていません。これをデバッグするにはどうすればいいですか? – Pepe