SFML community forums
Help => Graphics => Topic started by: andariel97 on November 29, 2016, 12:35:45 pm
-
In modern opengl, in order to draw quads fast you create a vertexbuffer and you tell the gpu how your data is organised in those bites. The code looks something like this :
void setupMesh()
{
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex),
&this->vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint),
&this->indices[0], GL_STATIC_DRAW);
// Vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)0);
// Vertex Normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)offsetof(Vertex, Normal));
// Vertex Texture Coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(GLvoid*)offsetof(Vertex, TexCoords));
glBindVertexArray(0);
}
You also use texture units to tell the gpu where are your textures.
glBindTexture(GL_TEXTURE_2D, this->textures.id);
In sfml you can use sf::VertexArray to draw multiple sprites at once. But how do you tell sfml do draw those quads with diferent textures?
-
You can't, the SFML API is not made for that. There's only one set of texture coordinates and one active texture.
-
usually in 3D application, the texture and and light data are gathered with so called material for 3D models. you can write your own data-structure to represent it in your engine/game.
for me i made simple 3D engine to mimic SFML Graphics-package as much as i can, in my approach i have render components as SFML did like RenderWindow, RenderTarget... etc. and for models management i made Mesh, Material classes. the engine depends on programmable pipeline shaders as modern opengl attend to be.
here snippet how the different textures handled via RenderTarget (https://github.com/MORTAL2000/3D-Engine/blob/master/RenderTarget.cpp#L45) and Materiel (https://github.com/MORTAL2000/3D-Engine/blob/master/Material.cpp#L126) classes.