#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
static void keyCallBack(GLFWwindow* window, int key, int scancode, int action, int
mods);
GLfloat vertices[] =
{// COORDINATES / COLORS
-.5f, -.5f, 0.0f, 0.8f, 0.3f, 0.02f,
0.5f, -.5f, 0.0f, 0.8f, 0.3f, 0.02f,
-.5f, .5f, 0.0f, 0.8f, 0.7f, 1.0f,
};
int main() {
{...}
GLFWwindow* window = glfwCreateWindow(400, 300, "Test-Window", NULL, NULL);
glfwSetKeyCallback(window, keyCallBack);
while (!glfwWindowShouldClose(window)) {
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
{...}
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
static void keyCallBack(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_UP && action == GLFW_PRESS) {
vertices[1] = vertices[1] + .1f;
vertices[7] = vertices[7] + .1f;
vertices[13] = vertices[13] + .1f;
}
if (key == GLFW_KEY_LEFT && action == GLFW_PRESS){
{...}//shift vertices left
}
{...} //shift vertex points right and down
}
键回调函数正在工作,因为当按下相应的箭头键时,数组内的坐标会发生变化。如何将三角形图像更新为窗口上的新坐标。
我按照你们中的一些人的建议使用了 glm 矩阵,这似乎有效。
更新代码:
GLfloat l(0.0f), m(0.0f), n(0.0f);
int main() {
{...}
while (!glfwWindowShouldClose(window)) {
{...}
glm::mat4 transform(1);
transform = glm::translate(transform, glm::vec3(l, m, n));
{...}
}
static void keyCallBack(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_UP && action == GLFW_PRESS) {
m = m + 0.01f;
}
{...} //other keys
}
使用全局变量可能不是最佳实践,但我不知道如何操作转换,因为回调函数不接受预定义输入以外的变量