Hi!
Here is my shaders :
if (settings.versionMajor >= 3 && settings.versionMinor >= 3) {
glGenBuffers(1, &vboWorldMatrices);
const std::string vertexShader =
R"(#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texCoords;
layout (location = 10) in mat4 worldMat;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 textureMatrix;
out vec2 fTexCoords;
out vec4 frontColor;
void main() {
gl_Position = worldMat * viewMatrix * projectionMatrix * vec4(position, 1.f);
fTexCoords = (textureMatrix * vec4(texCoords, 1.f, 1.f)).xy;
frontColor = color;
}
)";
const std::string fragmentShader =
R"(#version 330 core
uniform sampler2D texture;
in vec4 frontColor;
in vec2 fTexCoords;
void main() {
gl_FragColor = texture2D (texture, fTexCoords.xy) * frontColor;
})";
I’ve putted the first vertices and the world matrix in VBO like this :
RenderStates states:
if (frameBuffer.getSettings().versionMajor >= 3 && frameBuffer.getSettings().versionMinor >= 3) {
math::Matrix4f viewMatrix = view.getViewMatrix().getMatrix();
math::Matrix4f projMatrix = view.getProjMatrix().getMatrix();
shader.setParameter("projectionMatrix", projMatrix);
shader.setParameter("viewMatrix", viewMatrix);
VertexBuffer vb(sf::TrianglesStrip);
std::vector<float> matrices;
for (unsigned int i = 0; i < m_instances.size(); i++) {
if (m_instances[i].getAllVertices().getVertexCount() > 0) {
std::vector<TransformMatrix*> tm = m_instances[i].getTransforms();
for (unsigned int j = 0; j < tm.size(); j++) {
std::array<float, 16> matrix = tm[j]->getMatrix().toGlMatrix();
for (unsigned int n = 0; n < 16; n++) {
matrices.push_back(matrix[n]);
}
}
std::cout<<"vbo world matrices!"<<std::endl;
glCheck(glBindBuffer(GL_ARRAY_BUFFER, vboWorldMatrices));
glCheck(glBufferData(GL_ARRAY_BUFFER, sizeof(matrices), &matrices[0], GL_DYNAMIC_DRAW));
std::cout<<"vbo world matrices filled!"<<std::endl;
if (m_instances[i].getVertexArrays().size() > 0) {
for (unsigned int j = 0; j < m_instances[i].getVertexArrays()[0]->getVertexCount(); j++) {
vb.append((*m_instances[i].getVertexArrays()[0])[j]);
}
}
std::cout<<"vertices filled"<<std::endl;
states.shader = &shader;
std::cout<<"shader set"<<std::endl;
states.texture = m_instances[i].getMaterial().getTexture();
std::cout<<"texture defined"<<std::endl;
if (m_instances[i].getMaterial().getTexture() != nullptr) {
math::Matrix4f texMatrix = m_instances[i].getMaterial().getTexture()->getTextureMatrix();
shader.setParameter("textureMatrix", texMatrix);
std::cout<<"texture set to shader"<<std::endl;
}
std::cout<<"draw instanced"<<std::endl;
frameBuffer.drawInstanced(vb, vboWorldMatrices, sf::TrianglesStrip, 0, 4, tm.size(), states);
}
}
And I set the pointers in the draw function like this :
if (m_cache.lastVboBuffer != &vertexBuffer) {
if (m_versionMajor >= 3 && m_versionMinor >= 3) {
std::cout<<"define pointers"<<std::endl;
glCheck(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.vboVertexBuffer));
glCheck(glEnableVertexAttribArray(0));
glCheck(glEnableVertexAttribArray(1));
glCheck(glEnableVertexAttribArray(2));
glCheck(glVertexAttribPointer(0, 3,GL_FLOAT,GL_FALSE,sizeof(Vertex), (GLvoid*) 0));
glCheck(glVertexAttribPointer(1, 4,GL_UNSIGNED_BYTE,GL_TRUE,sizeof(Vertex),(GLvoid*) 12));
glCheck(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) 16));
glCheck(glDisableVertexAttribArray(0));
glCheck(glDisableVertexAttribArray(1));
glCheck(glDisableVertexAttribArray(2));
glCheck(glBindBuffer(GL_ARRAY_BUFFER, vboWorldMatrices));
for (unsigned int i = 0; i < 4 ; i++) {
glEnableVertexAttribArray(10 + i);
glVertexAttribPointer(10 + i, 4, GL_FLOAT, GL_FALSE, sizeof(math::Matrix4f),
(const GLvoid*)(sizeof(GLfloat) * i * 4));
glVertexAttribDivisor(10 + i, 1);
}
}
m_cache.lastVboBuffer = &vertexBuffer;
}
if (m_versionMajor >= 3 && m_versionMinor >= 3) {
static const GLenum modes[] = {GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES,
GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS};
GLenum mode = modes[type];
std::cout<<"draw instanced"<<std::endl;
glDrawArraysInstanced(mode,start,nb,nbInstances);
std::cout<<"instanced rendered"<<std::endl;
}
It display the message instanced rendered but after this, it blocks…