我有一些简单的代码可以在屏幕上绘制一个 30x30 的球。当它全部在 main.cpp 中时它工作得很好,但是当我把它放到它自己单独的类中时,我得到一个非常微弱的灰色框,而不是占据屏幕的右上角。当我将我在 Ball 类中的确切代码粘贴到 main.cpp 中时,它再次完美运行。
当代码在 Ball 类中时:
当我将 Ball 类中的所有代码粘贴到 main 中时(以及 Ball 类之前的情况):
代码可以在这里找到。
#include "Includes.hpp"
static unsigned int Width = 800;
static unsigned int Height = 600;
int main()
{
bool CursorEnabled = true;
bool PolygonMode = false;
//Camera.CameraInit(45.0f , Width , Height , Model); use ortho instead \/
Camera.CameraInit (Width, Height, glm::mat4 (1.0f));
GLuint vao;
glGenVertexArrays (1 , &vao);
glBindVertexArray(vao);
Ball ball;
float LastTime = glfwGetTime(); // (for deltatime)
while (true)
{
float DeltaTime = glfwGetTime() - LastTime; // Deltatime init
LastTime = glfwGetTime(); // update for deltatime
glClearColor (0.0f , 0.0f , 0.0f , 1.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ball.Draw();
glfwSwapBuffers();
if (glfwGetKey (GLFW_KEY_ESC) || !glfwGetWindowParam(GLFW_OPENED))
{
std::cout << "\nShutdown command. Exiting...\n" << std::flush;
return 0;
}
}
}
#ifndef BALL
#define BALL
class Ball
{
private:
//Vertices and UV coordiantes.
std::vector <glm::vec2> ballVert;
std::vector <glm::vec2> ballUV;
//Buffers
GLuint vertBuf;
GLuint uvBuf;
//Image
GLuint ballTex;
//IDs for the shader.
GLuint shader;
GLuint MVPid;
GLuint sampler;
GLuint coord;
GLuint uv;
public:
//Constructor
Ball();
void Draw();
};
#endif
#include "Includes.hpp"
Ball::Ball()
{
//Create ball vertices and UV coords
ballVert.push_back(glm::vec2(0 , 0)); /*left /\*/
ballVert.push_back(glm::vec2(0 , 30)); /*right / \*/
ballVert.push_back(glm::vec2(30 , 0)); /*top ------*/
ballVert.push_back(glm::vec2(0 , 30)); /*left /\*/
ballVert.push_back(glm::vec2(30 , 30)); /*right / \*/
ballVert.push_back(glm::vec2(30 , 0)); /*top ------*/
ballUV.push_back(glm::vec2(0 , 1));
ballUV.push_back(glm::vec2(0 , 0));
ballUV.push_back(glm::vec2(1 , 1));
ballUV.push_back(glm::vec2(0 , 0));
ballUV.push_back(glm::vec2(1 , 0));
ballUV.push_back(glm::vec2(1 , 1));
//Load the image
ballTex = SOIL_load_OGL_texture (
"img/ball.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT);
//Generate buffers
glGenBuffers (1 , &vertBuf);
glBindBuffer (GL_ARRAY_BUFFER , vertBuf);
glBufferData (GL_ARRAY_BUFFER , ballVert.size() * sizeof(glm::vec2) , &ballVert[0] , GL_STATIC_DRAW);
glGenBuffers (1 , &uvBuf);
glBindBuffer (GL_ARRAY_BUFFER , uvBuf);
glBufferData (GL_ARRAY_BUFFER , ballUV.size() * sizeof (glm::vec2) , &ballUV[0] , GL_STATIC_DRAW);
shader = Game.LoadShaders ("src/glsl/ball.vert" , "src/glsl/ball.frag");
MVPid = glGetUniformLocation (shader , "MVP");
sampler = glGetUniformLocation (shader, "sampler");
coord = glGetAttribLocation(shader, "coord");
uv = glGetAttribLocation (shader, "uv");
}
void Ball::Draw()
{
glUseProgram (shader);
glUniformMatrix4fv(MVPid, 1, GL_FALSE, &Camera.GetMVP()[0][0]); // Send MVP to shader
glActiveTexture (GL_TEXTURE0); // bind the ball texture
glBindTexture (GL_TEXTURE_2D, ballTex);
glUniform1i (sampler , 0);
glEnableVertexAttribArray (coord); // sending the vertex data
glBindBuffer(GL_ARRAY_BUFFER, vertBuf);
glVertexAttribPointer(
coord, // The attribute we want to configure
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glEnableVertexAttribArray (uv);
glBindBuffer (GL_ARRAY_BUFFER , uvBuf);
glVertexAttribPointer (uv, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, ballVert.size());
glDisableVertexAttribArray(coord);
glDisableVertexAttribArray(uv);
}