感谢您阅读。我是OpenGl Core Profile的新手,并坚持以下问题:
描述:
我想绘制一组砖并以速度单独移动它们如果它们碰到边界或彼此它们将转过来。所以我决定做一个开始演示,但不久我发现我无法单独设置砖块的位置:
glm::translate(model, translatePOS[i] * (float)glfwGetTime())
时才能更改所有砖块。但我的意思是非线性地改变位移。与time * speed
不同,但与Displacement_Function(time)
相似。glm::translate(model, translatePOS[i]+ x[i] * xUnit[i] + y[i] * yUnit[i] + z[i] * zUnit[i])
时,我观察到的只是一块砖将在空间中移动。问题:
为什么只有一块砖移动?我怎么能解决这个问题?
只有(float)glfwGetTime()
相关字符可以更新所有职位吗?
使用OpenGL核心配置文件设置和更改多个对象的标准方法是什么?
我真的需要别人的帮助来解决这个问题。如果你能给我一个线索背后出了什么问题,我会非常感激,真诚!
我的代码在此环境中运行并使用这些库和API:
Windows 10企业版
Microsoft Visual Studio Professional 2017
OpenGL(高于3.3)
GLFW
高兴
“shader_m.h”(由shader_m.h file下载)由Sean Barrett提供
整个代码在这里(从learnopengl website的免费教程中修改):
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader_m.h"
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);
// build and compile our shader zprogram
// ------------------------------------
Shader ourShader("DX.vs", "PX.fs");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
// world space positions of our cubes
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// load and create a texture
// -------------------------
unsigned int texture1, texture2;
// texture 1
// ---------
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
unsigned char *data = stbi_load("textures/t1.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// texture 2
// ---------
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
data = stbi_load("textures/s4.png", &width, &height, &nrChannels, 0);
if (data)
{
// note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// tell opengl for each sampler to which texture unit it belongs to (only has to be done once)
// -------------------------------------------------------------------------------------------
ourShader.use();
ourShader.setInt("texture1", 0);
ourShader.setInt("texture2", 1);
// render loop
// -----------
#define BrickNUM 100
glm::vec3 translatePOS[BrickNUM];
for (int i = 0; i < BrickNUM; i++) {
float Lx = -1.0;
float Hx = 1.0;
float Ly = -1.0;
float Hy = 1.0;
float Lz = -20.0;
float Hz = -30.0;
float r1 = Lx + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hx - Lx)));
float r2 = Ly + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hy - Ly)));
float r3 = Lz + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hz - Lz)));
translatePOS[i].x = r1;
translatePOS[i].y = r2;
translatePOS[i].z = r3;
std::cout << "(" << translatePOS[i].x << "," << translatePOS[i].y << "," << translatePOS[i].z << ")" << std::endl;
}
glm::vec3 velocity[BrickNUM];
for (int i = 0; i < BrickNUM; i++) {
float Lx = -0.1;
float Hx = 0.1;
float Ly = -0.1;
float Hy = 0.1;
float Lz = -0.1;
float Hz = 0.1;
float r1 = Lx + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hx - Lx)));
float r2 = Ly + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hy - Ly)));
float r3 = Lz + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hz - Lz)));
velocity[i] = glm::vec3(r1, r2, r3);
}
float velocityFloat[BrickNUM][3];
for (int i = 0; i < BrickNUM; i++) {
float Lx = -0.1;
float Hx = 0.1;
float Ly = -0.1;
float Hy = 0.1;
float Lz = -0.1;
float Hz = 0.1;
float r1 = Lx + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hx - Lx)));
float r2 = Ly + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hy - Ly)));
float r3 = Lz + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hz - Lz)));
velocityFloat[i][0] = 10.0*r1;
velocityFloat[i][1] = 10.0*r2;
velocityFloat[i][2] = 10.0*r3;
}
glm::vec3 rotateDEC[BrickNUM];
for (int i = 0; i < BrickNUM; i++) {
float Lx = 0.0;
float Hx = 1.0;
float Ly = 0.0;
float Hy = 1.0;
float Lz = 0.0;
float Hz = 1.0;
float r1 = Lx + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hx - Lx)));
float r2 = Ly + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hy - Ly)));
float r3 = Lz + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hz - Lz)));
rotateDEC[i] = glm::vec3(r1, r2, r3);
}
glm::vec3 test= glm::vec3(0.0, 0.0, 0.0);
float x[BrickNUM];
float y[BrickNUM];
float z[BrickNUM];
for (int i = 0; i < BrickNUM; i++) {
x[i] = translatePOS[i].x;
y[i] = translatePOS[i].y;
z[i] = translatePOS[i].z;
}
glm::vec3 xUnit[BrickNUM];
glm::vec3 yUnit[BrickNUM];
glm::vec3 zUnit[BrickNUM];
for (int i = 0; i < BrickNUM; i++) {
float Lx = 0.0;
float Hx = 1.0;
float Ly = 0.0;
float Hy = 1.0;
float Lz = 0.0;
float Hz = 1.0;
float r1 = Lx + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hx - Lx)));
float r2 = Ly + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hy - Ly)));
float r3 = Lz + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (Hz - Lz)));
xUnit[i] = glm::vec3(1.0, 0.0, 0.0);
yUnit[i] = glm::vec3(0.0, 1.0, 0.0);
zUnit[i] = glm::vec3(0.0, 0.0, -1.0);
}
float timeLast = (float)glfwGetTime();
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // also clear the depth buffer now!
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
// activate shader
ourShader.use();
// create transformations
glm::mat4 view;
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
// pass transformation matrices to the shader
ourShader.setMat4("projection", projection); // note: currently we set the projection matrix each frame, but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
ourShader.setMat4("view", view);
// render boxes
for (unsigned int i = 1; i < 5; i++)
{
// calculate the model matrix for each object and pass it to shader before drawing
glm::mat4 model;
glBindVertexArray(VAO);
float timeNow = (float)glfwGetTime();
//std::cout << timeNow << std::endl;
if (1) {
x[i] += (timeNow - timeLast) * velocityFloat[i][0];
y[i] += (timeNow - timeLast) * velocityFloat[i][1];
z[i] += (timeNow - timeLast) * velocityFloat[i][2];
std::cout << "(" << x[i] << "," << y[i] << "," << z[i] << ")" << std::endl;
timeLast = timeNow;
if (x[i] > 10.0) {
velocityFloat[i][0] = -velocityFloat[i][0];
}
if (y[i] > 10.0) {
velocityFloat[i][1] = -velocityFloat[i][1];
}
if (z[i] < -90.0) {
velocityFloat[i][2] = -velocityFloat[i][2];
}
if (x[i] < -10.0) {
velocityFloat[i][0] = -velocityFloat[i][0];
}
if (y[i] < -10.0) {
velocityFloat[i][1] = -velocityFloat[i][1];
}
if (z[i] > -1.0) {
velocityFloat[i][2] = -velocityFloat[i][2];
}
}
//glBindVertexArray(VAO);
float xx = x[i];
float yy = y[i];
float zz = z[i];
glm::vec3 tmp3;
tmp3.x = translatePOS[i].x + xx * xUnit[i].x;
tmp3.y = translatePOS[i].y + yy * yUnit[i].y;
tmp3.z = translatePOS[i].z + zz * zUnit[i].z;
glm::vec3 tmp4 = glm::vec3((translatePOS[i].x + xx * xUnit[i].x), (translatePOS[i].y + yy * yUnit[i].y), (translatePOS[i].z + zz * zUnit[i].z));
float testTime = (float)glfwGetTime() + xx;
//model = glm::translate(model, tmp4);
//model = glm::translate(model, testTime * zUnit[i]);
//model = glm::translate(model, translatePOS[i]+ x[i] * xUnit[i] + y[i] * yUnit[i] + z[i] * zUnit[i]);
model = glm::translate(model, translatePOS[i] + x[i] * xUnit[i]);
float angle = 20.0f * i;
model = glm::rotate(model, timeNow* glm::radians(angle) / 10, rotateDEC[i]);
ourShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}