Witam, próbowałem stworzyć w C++ własną klasę za pomocą, której można w prosty sposób rysować sześciany w OpenGL. Wszystko działa ok, kiedy rysuję jeden obiekt lub kilka oddzielnych, ale gdy za pomocą jednego obiektu chcę narysować kilka sześcianów żaden nie pojawia się w oknie aplikacji. Próbowałem, zmieniać kolejność bindowania VAO w funkcji rysującej sześcian, ale to nic nie daje. Dlaczego funkcja render nie działa tak jak chcę?
main.cpp
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "VBO.h"
#include "VAO.h"
#include "EBO.h"
#include "Shader.h"
#include "Cube.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Hello Window!", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to initialize GLFW" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST);
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)
};
Cube myCube("vertexShader.vs", "fragmentShader.fs");
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (int i = 0; i < 10; i++)
{
myCube.move(cubePositions[i]);
float angle = 20.0f * i;
myCube.rotate(angle, glm::vec3(1.0f, 1.0f, 0.0f));
myCube.render();
}
glfwSwapBuffers(window);
glfwPollEvents();
}
myCube.~Cube();
glfwTerminate();
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
Cube.cpp
#include "Cube.h"
Cube::Cube(const char* vertexPath, const char* fragmentPath) : Entity(vertexPath, fragmentPath)
{
float points[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
for (int i = 0; i < 108; i++)
vertices[i] = points[i];
}
Cube::~Cube()
{
vao.Delete();
vbo.Delete();
shaderProgram.~Shader();
}
void Cube::render()
{
vbo.InitVBO(vertices, sizeof(vertices));
vao.Bind();
vao.LinkAttrib(vbo, 0, 3, GL_FLOAT, 3 * sizeof(float), (void*)0);
shaderProgram.use();
this->setMatrixes();
vao.Bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
vao.Unbind();
vbo.Unbind();
}
Entity.cpp
#include "Entity.h"
Entity::Entity() {}
Entity::Entity(const char* vertexPath, const char* fragmentPath)
{
position = glm::vec3(0.0f);
model = glm::mat4(1.0f);
view = glm::mat4(1.0f);
projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
shaderProgram.InitShader(vertexPath, fragmentPath);
}
void Entity::move(glm::vec3 offset)
{
view = glm::translate(view, offset);
shaderProgram.setMat4("view", view);
position += offset;
}
void Entity::rotate(GLfloat angle, glm::vec3 axes)
{
model = glm::rotate(model, glm::radians(angle), axes);
}
glm::vec3 Entity::getPosition()
{
return this->position;
}
void Entity::setMatrixes()
{
shaderProgram.setMat4("model", model);
shaderProgram.setMat4("view", view);
shaderProgram.setMat4("projection", projection);
}