Cześć mam taki problem, próbuję obrócić w OpenGL obiekty o 90 stopni i wszystko robię tak jak na stronie https://learnopengl.com/Getting-started/Transformations obrócony obiekt wygląda tak jak na zdjęciu. Nie mam pojęcia co może być tego przyczyną, w glUniformMatrixfv próbowałem użyć GL_TRUE, ale to dało tylko tyle, że obiekt był tak samo obrócony tylko w inną stronę. Zamieniałem też kolejność: najpierw skalowałem obiekt, a potem rotowałem go wokół osi Z ale to też nic nie dało. Jak to naprawić i dlaczego tak się dzieje?
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
#include <iostream>
#include <fstream>
#include <string>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
static unsigned int CompileShader(unsigned int type, std::string source)
{
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); // Końcókwa iv oznacza typ shaderów "i" od intiger "v" od vector
char* message = (char*)_malloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << "shader" << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}
return id;
}
static unsigned int CreateShader(const std::string& vertexShader, const std::string fragmentShader)
{
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
std::string readFromFile(const char* pathToFile)
{
std::string content;
std::ifstream fileStream(pathToFile, std::ios::in);
if (!fileStream.is_open()) {
std::cerr << "Could not read file " << pathToFile << ". File does not exist." << std::endl;
return "";
}
std::string line;
while (std::getline(fileStream, line)) {
content.append(line + "\n");
}
fileStream.close();
std::cout << content << std::endl;
return content;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glfwSwapInterval(5);
if (glewInit() != GLEW_OK)
std::cout << "Error" << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
float vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
unsigned int indieces[] = {
0, 1, 2,
2, 0, 3
};
unsigned int VAB;
glGenBuffers(1, &VAB);
glBindBuffer(GL_ARRAY_BUFFER, VAB);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
unsigned int VAO;
glGenBuffers(1, &VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VAO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indieces), indieces, GL_STATIC_DRAW);
glm::mat4 trans(1.0);
trans = glm::rotate(trans, 90.0f, glm::vec3(0.0, 0.0, 1.0));
trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5));
std::string vertexShaderSource = readFromFile("vertexShader.shader");
std::string fragmentShaderSource = readFromFile("fragmentShader.shader");
unsigned int shaderProgram = CreateShader(vertexShaderSource, fragmentShaderSource);
unsigned int texture;
glGenBuffers(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_set_flip_vertically_on_load(true);
int width, height, nrChannels;
unsigned char* data = stbi_load("C:/Users/usr/Desktop/container.jpg", &width, &height, &nrChannels, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
unsigned int texture2;
glGenTextures(1, &texture2);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
data = stbi_load("C:/Users/usr/Desktop/face.png", &width, &height, &nrChannels, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
glUseProgram(shaderProgram);
glUniform1i(glGetUniformLocation(shaderProgram, "ourTexture"), 0);
glUniform1i(glGetUniformLocation(shaderProgram, "ourTexture2"), 1);
unsigned int transformLoc = glGetUniformLocation(shaderProgram, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aCords;
out vec3 ourColor;
out vec2 TextureCords;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(aPos, 1.0);
ourColor = aColor;
TextureCords = aCords;
}
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TextureCords;
uniform sampler2D ourTexture;
uniform sampler2D ourTexture2;
void main()
{
FragColor = mix(texture(ourTexture, TextureCords), texture(ourTexture2, TextureCords), 0.2) * vec4(ourColor, 1.0);
}