所以我正在嘗試制作一個我想更新我的玩家和敵人的游戲。敵人和玩家是正方形。我有一個正方形類,其中包含 VAO、VBO、EBO 和加載正方形的頂點。
void Square::loadSquare()
{
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind vertex array object
glBindVertexArray(VAO);
// bind vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
// bind element buffer objects
// EBO is stored in the VAO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// registered VBO as the vertex attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// unbind the VAO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
和繪制方法:
void Square::drawSquare()
{
// Bind the VAO so OpenGL knows to use it
glBindVertexArray(VAO);
// Draw the triangle using the GL_TRIANGLES primitive
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
Player 和 Enemy 類繼承自 square 類,它們有自己的更新方法,我在 Carte 類中呼叫更新方法。
void Carte::update(){
drawMap();
enemy.update(GrapMap,0);
player.update(GrapMap);
}
在繪制地圖中,我只是簡單地繪制我的敵人和玩家。
void Carte::drawMap(){
enemy.drawSquare();
player.drawSquare();
for(auto & wall : walls){
wall.drawSquare();
}
}
墻壁也是正方形,但在我的情況下,我在我想要的地方繪制它們并且我對它們沒有問題。在敵人和玩家的每次更新結束后,我呼叫了他們的頂點
glBindBuffer(GL_ARRAY_BUFFER, VAO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
當我只更新播放器時,它可以完美運行。但是對于敵人,我看不到玩家,但我可以看到它的頂點正在根據用戶的鍵輸入而相應地發生變化。當我注釋掉玩家并嘗試更新敵人時,只有敵人沒有更新,但我再次可以看到它的頂點發生了應有的變化。
在主要創建我的 Carte 物件之前,我這樣做了:
// Vertex Shader source code
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
//Fragment Shader source code
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(0.8f, 0.3f, 0.02f, 1.0f);\n"
"}\n\0";
int main()
{
// Initialize GLFW
glfwInit();
// Tell GLFW what version of OpenGL we are using
// In this case we are using OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Tell GLFW we are using the CORE profile
// So that means we only have the modern functions
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create a GLFWwindow object of 800 by 800 pixels, naming it "window"
GLFWwindow* window = glfwCreateWindow(1000, 1000, "Window", NULL, NULL);
// Error check if the window fails to create
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Introduce the window into the current context
glfwMakeContextCurrent(window);
//Load GLAD so it configures OpenGL
gladLoadGL();
// Specify the viewport of OpenGL in the Window
// In this case the viewport goes from x = 0, y = 0, to x = 800, y = 800
//glViewport(0, 0, 1400, 1400);
// Create Vertex Shader Object and get its reference
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
// Attach Vertex Shader source to the Vertex Shader Object
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
// Compile the Vertex Shader into machine code
glCompileShader(vertexShader);
// Create Fragment Shader Object and get its reference
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
// Attach Fragment Shader source to the Fragment Shader Object
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
// Compile the Vertex Shader into machine code
glCompileShader(fragmentShader);
// Create Shader Program Object and get its reference
GLuint shaderProgram = glCreateProgram();
// Attach the Vertex and Fragment Shaders to the Shader Program
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
// Wrap-up/Link all the shaders together into the Shader Program
glLinkProgram(shaderProgram);
// Delete the now useless Vertex and Fragment Shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
在我的 while 回圈中,我正在這樣做:
// Specify the color of the background
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
// Clean the back buffer and assign the new color to it
glClear(GL_COLOR_BUFFER_BIT);
// Tell OpenGL which Shader Program we want to use
glUseProgram(shaderProgram);
int keyW = glfwGetKey(window, GLFW_KEY_W);
int keyA = glfwGetKey(window, GLFW_KEY_A);
int keyS = glfwGetKey(window, GLFW_KEY_S);
int keyD = glfwGetKey(window, GLFW_KEY_D);
carte.update();
if(keyW)
deneme.setPlayerDirection(Directions::UP);
else if(keyA)
deneme.setPlayerDirection(Directions::LEFT);
else if(keyS)
deneme.setPlayerDirection(Directions::DOWN);
else if(keyD)
deneme.setPlayerDirection(Directions::RIGHT);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
// Take care of all GLFW events
glfwPollEvents();
我不明白為什么我不能同時更新我的??兩個物件。為什么當有多個物件頂點發生變化時,當頂點發生變化時,我不能繪制正方形。
編輯以顯示我如何在 Player 中更改我的頂點:
if(getDirection() == Directions::UP){
trans = glm::translate(trans, glm::vec3(0.0f, 0.0002f, 0.0f));
setCenter(center.first,center.second 0.0002f);
}
else if (getDirection() == Directions::LEFT){
trans = glm::translate(trans, glm::vec3(-0.0002f, 0.0f, 0.0f));
setCenter(center.first-0.0002f,center.second);
}
else if (getDirection() == Directions::DOWN){
trans = glm::translate(trans, glm::vec3(0.0f, -0.0002f, 0.0f));
setCenter(center.first,center.second-0.0002f);
}
else if (getDirection() == Directions::RIGHT){
trans = glm::translate(trans, glm::vec3(0.0002f, 0.0f, 0.0f));
setCenter(center.first 0.0002f,center.second);
}
else if (getDirection() == Directions::STOP)
trans = glm::translate(trans, glm::vec3(0.0f, 0.0f, 0.0f));
for(int i=0; i < 4; i ){
glm::vec4 tmp = trans * glm::vec4(getVertices()[i],1);
setVertices(i, tmp.x, tmp.y, tmp.z);
}
glBindBuffer(GL_ARRAY_BUFFER, VAO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
uj5u.com熱心網友回復:
這不是有效代碼: glBindBuffer(GL_ARRAY_BUFFER, VAO); 您正在嘗試將頂點陣列物件系結到頂點緩沖區位置。這是兩種完全不同的型別。您可能打算寫glBindBuffer(GL_ARRAY_BUFFER, VBO);,但只是打錯了。
除非VAOandVBO恰好具有相同的值,否則您要么更新了錯誤的緩沖區,要么根本沒有緩沖區。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471688.html
上一篇:使用狀態模式了解物件的狀態
下一篇:從類屬性創建串列-Python
