-
Anonymous
В OpenGL этот код переключается с ливных шаров на череды, нижний правый треугольник каждого персонажа неверен, вы можете
Сообщение
Anonymous »
В программе OpenGL мы отображали текст, используя следующее: < /p>
Мы создали бы 6 баллов на символ: < /p>
Код: Выделить всё
coords.push_back({xpos, ypos, s0, t0});
coords.push_back({xpos + w_, ypos, s1, t0});
coords.push_back({xpos, ypos + h_, s0, t1});
coords.push_back({xpos + w_, ypos, s1, t0});
coords.push_back({xpos, ypos + h_, s0, t1});
coords.push_back({xpos + w_, ypos + h_, s1, t1});
< /code>
и рендеринг с: < /p>
void opengl_text::render_and_clear_coords(Shader* shader, std::vector
& coords, font font, glm::vec3 color) {
shader->setVec3("textColor", color.x, color.y, color.z);
glBindTexture(GL_TEXTURE_2D, font.get_texture());
// render
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, coords.size() * sizeof(point), &coords[0], GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, coords.size());
glDisableVertexAttribArray(0);
glBindVertexArray(0);
}
< /code>
Это сработало. В попытке оптимизировать мы предварительно выделяем индексы: < /p>
constexpr int num_points = 65536;
coords.reserve(num_points * 4); // reserve space for 65536 points
point_indices.reserve(num_points * 6);
for(int i = 0; i < num_points; i++){
point_indices.push_back(i * 4 + 0);
point_indices.push_back(i * 4 + 1);
point_indices.push_back(i * 4 + 2);
point_indices.push_back(i * 4 + 1);
point_indices.push_back(i * 4 + 2);
point_indices.push_back(i * 4 + 3);
}
glGenBuffers(1, &vio);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, point_indices.size() * sizeof(uint32_t), point_indices.data(), GL_STATIC_DRAW);
< /code>
, а затем используйте только 4 балла на прямоугольник: < /p>
// Add 4 vertices (for two triangles) for the glyph to the coords vector
coords.push_back((point){xpos, ypos, ch.textureX, ch.height / atlas_height}); // Top left
coords.push_back((point){xpos + w, ypos, ch.textureX + ch.width / atlas_width, ch.height / atlas_height}); // Top right
coords.push_back((point){xpos, ypos + h, ch.textureX, 0}); // Bottom left
coords.push_back((point){xpos + w, ypos + h, ch.textureX + ch.width / atlas_width, 0}); // Bottom right
< /code>
Затем для рендеринга мы используем чертежи, только рисует количество символов * 6 индексов: < /p>
shader->setVec3("textColor", color.x, color.y, color.z);
glBindTexture(GL_TEXTURE_2D, font.get_texture());
// render
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, coords.size() * sizeof(point), &coords[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio);
glDrawElements(GL_TRIANGLES, coords.size()/4*6, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glBindVertexArray(0);
Вот выход:
Подробнее здесь:
https://stackoverflow.com/questions/796 ... right-tria
1750428311
Anonymous
В программе OpenGL мы отображали текст, используя следующее: < /p>
Мы создали бы 6 баллов на символ: < /p>
[code]coords.push_back({xpos, ypos, s0, t0});
coords.push_back({xpos + w_, ypos, s1, t0});
coords.push_back({xpos, ypos + h_, s0, t1});
coords.push_back({xpos + w_, ypos, s1, t0});
coords.push_back({xpos, ypos + h_, s0, t1});
coords.push_back({xpos + w_, ypos + h_, s1, t1});
< /code>
и рендеринг с: < /p>
void opengl_text::render_and_clear_coords(Shader* shader, std::vector
& coords, font font, glm::vec3 color) {
shader->setVec3("textColor", color.x, color.y, color.z);
glBindTexture(GL_TEXTURE_2D, font.get_texture());
// render
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, coords.size() * sizeof(point), &coords[0], GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, coords.size());
glDisableVertexAttribArray(0);
glBindVertexArray(0);
}
< /code>
Это сработало. В попытке оптимизировать мы предварительно выделяем индексы: < /p>
constexpr int num_points = 65536;
coords.reserve(num_points * 4); // reserve space for 65536 points
point_indices.reserve(num_points * 6);
for(int i = 0; i < num_points; i++){
point_indices.push_back(i * 4 + 0);
point_indices.push_back(i * 4 + 1);
point_indices.push_back(i * 4 + 2);
point_indices.push_back(i * 4 + 1);
point_indices.push_back(i * 4 + 2);
point_indices.push_back(i * 4 + 3);
}
glGenBuffers(1, &vio);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, point_indices.size() * sizeof(uint32_t), point_indices.data(), GL_STATIC_DRAW);
< /code>
, а затем используйте только 4 балла на прямоугольник: < /p>
// Add 4 vertices (for two triangles) for the glyph to the coords vector
coords.push_back((point){xpos, ypos, ch.textureX, ch.height / atlas_height}); // Top left
coords.push_back((point){xpos + w, ypos, ch.textureX + ch.width / atlas_width, ch.height / atlas_height}); // Top right
coords.push_back((point){xpos, ypos + h, ch.textureX, 0}); // Bottom left
coords.push_back((point){xpos + w, ypos + h, ch.textureX + ch.width / atlas_width, 0}); // Bottom right
< /code>
Затем для рендеринга мы используем чертежи, только рисует количество символов * 6 индексов: < /p>
shader->setVec3("textColor", color.x, color.y, color.z);
glBindTexture(GL_TEXTURE_2D, font.get_texture());
// render
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, coords.size() * sizeof(point), &coords[0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio);
glDrawElements(GL_TRIANGLES, coords.size()/4*6, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glBindVertexArray(0);
[/code]
Вот выход:
Подробнее здесь: [url]https://stackoverflow.com/questions/79673509/in-opengl-this-code-switched-from-drawarrays-to-drawelements-bottom-right-tria[/url]