void OptimizedMeshRenderer::initialize(uint32_t meshId, const Mesh* mesh, float…
- User
void OptimizedMeshRenderer::initialize(uint32_t meshId, const Mesh* mesh, float walkableSlopeAngle, float texScale) { const float* verts = mesh->getVerts(); const int* tris = mesh->getTris(); int ntris = mesh->getTriCount(); const float walkableThr = cosf(walkableSlopeAngle / 180.0f * DU_PI);
std::vector<float> vertexData; vertexData.reserve(ntris * 3 * 6); // 6 флоатів на вершину (3 для позиції, 3 для нормалі) for (int i = 0; i < ntris; ++i) { float norm[3]; mesh->calculateNormal(i, norm); for (int j = 0; j < 3; ++j) { const float* v = &verts[tris[i * 3 + j] * 3]; // Позиція vertexData.insert(vertexData.end(), { v[0], v[1], v[2] }); // Нормаль vertexData.insert(vertexData.end(), { norm[0], norm[1], norm[2] }); } } MeshData meshData; meshData.vertexCount = ntris * 3; meshData.color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); // Updated to white glGenVertexArrays(1, &meshData.vao); glGenBuffers(1, &meshData.vbo); glBindVertexArray(meshData.vao); glBindBuffer(GL_ARRAY_BUFFER, meshData.vbo); glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(float), vertexData.data(), GL_DYNAMIC_DRAW); // Атрибут позиції glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // Атрибут нормалі glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); // Color Attribute glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 12 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); // Texture Coordinates Attribute glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 12 * sizeof(float), (void*)(10 * sizeof(float))); glEnableVertexAttribArray(3); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); meshes[meshId] = meshData; const char* vertexShaderSource = R"(#version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aNormal;
out vec3 FragPos; out vec3 Normal; out vec4 VertexPosition;
uniform mat4 mvpMatrix;
void main() { FragPos = aPos; Normal = aNormal; VertexPosition = mvpMatrix * vec4(aPos, 1.0); gl_Position = VertexPosition; } )";
const char* fragmentShaderSource = R"(#version 330 core in vec3 FragPos; in vec3 Normal;
out vec4 FragColor;
uniform vec3 lightDir = vec3(1.0, 1.0, 1.0); uniform float ambientStrength = 0.3; uniform float diffuseStrength = 0.7; uniform vec4 meshColor; uniform bool isSelected;
void main() { vec3 normalizedLightDir = normalize(lightDir); float diff = abs(dot(Normal, normalizedLightDir)); vec3 diffuse = diff * diffuseStrength * meshColor.rgb; vec3 ambient = ambientStrength * meshColor.rgb;
vec3 result = ambient + diffuse; if (isSelected) { result *= vec3(1.5, 0.5, 0.5); // Increase red, decrease green and blue } FragColor = vec4(result, meshColor.a);} )"; shader = new Shader(vertexShaderSource, fragmentShaderSource); }
- Assistant