Answer the question
In order to leave comments, you need to log in
How is it possible to set the parameters, namely the texture separately for each triangle?
I have a model in which the coordinates of the vertices and the index buffer are stored separately along with the texture ID, the model is not divided into separate parts by texture. And the question is how to apply the texture separately to each triangle? Through the texture atlas will not work, because the textures are different in size.
// Std. Includes
#include <iostream>
#include <map>
#include <string>
#include <vector>
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
// GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/string_cast.hpp>
#include <glm/gtc/type_ptr.hpp>
// GL includes
#include "Shader.h"
#include "Texture.h"
#define DEBUG
#include "Model.h"
static uint32_t WIDTH = 900;
static uint32_t HEIGHT = 700;
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "TestProgram", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glewExperimental = true;
glewInit();
Shader shader = Shader("shaders/text.vert", "shaders/text.frag");
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
float vertices[] = {
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, // top right
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // bottom right
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // bottom left
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // top left
};
uint32_t indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
uint32_t VBO = 0;
uint32_t VAO = 0;
uint32_t EBO = 0;
#pragma pack(push, 1)
struct Vec2
{
float x;
float y;
};
struct Vertex {
mod::Vector3 pos;
Vec2 uv;
Vec2 dat;
};
struct Index {
uint32_t a;
uint32_t b;
uint32_t c;
};
#pragma pack(pop)
std::vector<Vertex> data = {
//{ 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f }, // top right
//{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }, // bottom right
//{ 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f }, // bottom left
//{ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f }, // top left
};
std::vector<Index> index = {
//{ 0, 1, 3 },
//{ 1, 2, 3 }
};
mod::Frame* mod = mod::Model::_()->loadModel("model.of");
std::cout << "(I)Name: " << mod->name << std::endl;
mod::Geometry m = *(mod->geom);
for (int i = 0; i < m.header.vertice_count; i++) {
mod::Vector3 v = m.vertexs[i];
data.push_back({ v, 1.0f, 1.0f, 1.0f, 1.0f });
}
for (int i = 0; i < m.header.triangle_count; i++) {
mod::Triangle t = m.triangles[i];
index.push_back({ t.index_a, t.index_b, t.index_c });
t.print();
}
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(Vertex), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index.size() * sizeof(Index), &index[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(5 * sizeof(float)));
glEnableVertexAttribArray(2);
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)WIDTH / (float)HEIGHT, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 2.0f, 5.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 model = glm::mat4(1.0f);
Texture* texture = new Texture("image.jpg");
Texture* texture1 = new Texture("image1.jpg");
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shader.use();
shader.setMat4("projection", projection);
shader.setMat4("view", view);
shader.setMat4("model", model);
shader.setInt("texture1", 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture->ID());
shader.setInt("texture2", 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture1->ID());
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 3*8, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
in vec2 test;
uniform sampler2D texture1;
uniform sampler2D texture2;
void main()
{
if(test.x == 1.0)
FragColor = texture(texture1, TexCoord);
else
FragColor = texture(texture2, TexCoord);
}
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
layout (location = 2) in vec2 aTest;
out vec2 TexCoord;
out vec2 test;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0f);
TexCoord = aTexCoord;
test = aTest;
}
Answer the question
In order to leave comments, you need to log in
Assign different texture coordinates to different faces and you're done.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question