data = stbi_load(WayToTexture.data(),&width,&height,&channels,0);
if (data != nullptr) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data); //здесь ловится исключение
}
else {
std::cout << "texture"<< WayToTexture <<"couldn`t loaded"<<std::endl;
}
shaders.push_back(new GL::Program("first", "first") );
shaders[0]->bindAttribute(0, "position");
shaders[0]->bindAttribute(1, "uv");
shaders[0]->bindAttribute(2, "normals");
shaders[0]->link();
shaders[0]->use();
Light light;
glm::mat4 proj = glm::perspective(45.0f, 1.0f / 1.0f, 0.1f, 10000.0f);
Camera cam;
cam.cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
const std::vector<float> vertices = {
-0.5f, 0.5f, 0.f,
-0.5f, -0.5f, 0.f,
0.5f, 0.5f, 0.f,
0.5f, -0.5f, 0.f,
};
const std::vector<float> Uvs = {
0, 1,
0, 0,
1, 1,
1, 0,
};
const std::vector<float> normals = {
0.0f,0.0f,1.0f,
0.0f,0.0f,1.0f,
0.0f,0.0f,1.0f,
0.0f,0.0f,1.0f,
};
const std::vector<unsigned> Indices = {
0, 1, 2,
2, 1, 3
};
Objects.push_back(new Mesh(vertices, Uvs, normals, Indices, glm::vec3(0, 0, 0), glm::vec3(0, 0, 0), glm::vec3(10, 10, 10)));
Objects[0]->SetTexture("res/texture/kartinka2.png");//метод загрузки текстуры
while (!glfwWindowShouldClose(mWindow))
{
glClearColor(0, 0, 0, 1);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
{
if (glfwGetKey(mWindow, GLFW_KEY_LEFT_CONTROL)) {
turn_on_cursor = true;
glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
if (!glfwGetKey(mWindow, GLFW_KEY_LEFT_CONTROL)) {
glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwGetCursorPos(mWindow, &MouseX, &MouseY);
MouseOffsetX = MouseX - PrevMouseX;
MouseOffsetY = MouseY - PrevMouseY;
MouseOffsetX *= sensivity;
MouseOffsetY *= -sensivity;
cam.PovorotX = MouseOffsetX;
cam.PovorotY = MouseOffsetY;
if (cam.PovorotY > 70.0f) {
cam.PovorotY = 70.0f;
MouseOffsetY = 70.0f;
}
if (cam.PovorotY < -70.0f) {
cam.PovorotY = -70.0f;
MouseOffsetY = -70.0f;
}
glm::vec3 front;
front.x = cos(glm::radians(cam.PovorotX)) * cos(glm::radians(cam.PovorotY));
front.y = sin(glm::radians(cam.PovorotY));
front.z = sin(glm::radians(cam.PovorotX)) * cos(glm::radians(cam.PovorotY));
cam.cameraFront = glm::normalize(front);
if (glfwGetKey(mWindow, GLFW_KEY_W)) { cam.position += cam.CamSpeed * cam.cameraFront; }
if (glfwGetKey(mWindow, GLFW_KEY_S)) { cam.position -= cam.CamSpeed * cam.cameraFront; }
if (glfwGetKey(mWindow, GLFW_KEY_A)) { cam.position -= glm::normalize(glm::cross(cam.cameraFront, cam.cameraUp)) * cam.CamSpeed; }
if (glfwGetKey(mWindow, GLFW_KEY_D)) { cam.position += glm::normalize(glm::cross(cam.cameraFront, cam.cameraUp)) * cam.CamSpeed; }
}
}
glfwGetWindowSize(mWindow, &windowWidth, &windowHeight);//узнаем высоту и ширину окна каждый кадр
glViewport(0, 0, windowWidth, windowWidth);
view = glm::lookAt(cam.position, cam.position + cam.cameraFront, cam.cameraUp);
glUniform3fv(glGetUniformLocation(shaders[core_program]->mProgram, "ViewPos"), 1, glm::value_ptr(cam.position));
glUniform3fv(glGetUniformLocation(shaders[core_program]->mProgram, "LightPos"), 1, glm::value_ptr(glm::vec3(100.0,100.0,100.0)));
glUniform3fv(glGetUniformLocation(shaders[core_program]->mProgram, "LightColor"), 1, glm::value_ptr(glm::vec3(1.0f,1.0f,1.0f)));
glUniformMatrix4fv(glGetUniformLocation(shaders[core_program]->mProgram, "ViewMatrix"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shaders[core_program]->mProgram, "projectionMatrix"), 1, GL_FALSE, glm::value_ptr(proj));
Objects[0]->draw(GL_TRIANGLES, shaders[core_program]);
//Objects[1]->draw(GL_TRIANGLES, shaders[core_program]);
glfwSwapBuffers(mWindow);
glfwPollEvents();
}
{
int Index;
for (size_t i = 0; i < PrevIndices.size(); i++) {
bool check = false;
int i = 0;
for (; i < PrevIndices.size(); i++) {
if (vector[i] == PrevIndices[i]) {
check = true;
break;
}
}
if (check == false) {
size_t maxValue = 0;
for (int c = 0; c < IndexVector.size(); c++) {
if (maxValue < IndexVector[c]) {
maxValue = IndexVector[c];
}
return ++maxValue;
}
Index=CheckIndices(PrevIndices, PrevIndices[i], Indices);
Indices.push_back(Index);
}
}
{
for (int StrNumber = 0; StrNumber < PrevIndices.size(); StrNumber++) {
for (int i = 0; i < PrevIndices[StrNumber].size(); i++) {
if (PrevIndices[StrNumber][i] != '/') {
BufferOfIndices.push_back(PrevIndices[StrNumber][i]);
}
else {
ExtraIndices.push_back(vtoi(BufferOfIndices));
BufferOfIndices.clear();
}
}
ExtraIndices.push_back(vtoi(BufferOfIndices));
}
}