[英]OpenGL - Trying to add texture to triangle
I am trying to add a stop sign texture to a triangle in opengl.我正在尝试向 opengl 中的三角形添加停止标志纹理。
But for some reason the image is coming up weird like I have the coordinates in the wrong order and the image is 1, mirrored and 2, not angled correctly:但是由于某种原因,图像变得很奇怪,就像我的坐标顺序错误一样,图像是 1,镜像和 2,角度不正确:
I believe I set the texture coordinates correct but I am unsure.我相信我设置的纹理坐标是正确的,但我不确定。 Have i got the texture coordinates in the wrong order?
我是否以错误的顺序获得了纹理坐标?
Here is the code I have for it:这是我的代码:
#include <glm\glm.hpp>
#include <graphics_framework.h>
#include <memory>
using namespace std;
using namespace graphics_framework;
using namespace glm;
mesh m;
effect eff;
target_camera cam;
texture tex;
bool load_content() {
// Construct geometry object
geometry geom;
// Create triangle data
// Positions
vector<vec3> positions{vec3(0.0f, 1.0f, 0.0f), vec3(-1.0f, -1.0f, 0.0f), vec3(1.0f, -1.0f, 0.0f)};
// *********************************
// Define texture coordinates for triangle
vector<vec2> tex_coords{ vec2(0.0f, 0.0f), vec2(1.0f, 0.0f), vec2(0.5f, 1.0f) };
// *********************************
// Add to the geometry
geom.add_buffer(positions, BUFFER_INDEXES::POSITION_BUFFER);
// *********************************
// Add texture coordinate buffer to geometry
geom.add_buffer(tex_coords, BUFFER_INDEXES::TEXTURE_COORDS_0);
// *********************************
// Create mesh object
m = mesh(geom);
// Load in texture shaders here
eff.add_shader("27_Texturing_Shader/simple_texture.vert", GL_VERTEX_SHADER);
eff.add_shader("27_Texturing_Shader/simple_texture.frag", GL_FRAGMENT_SHADER);
// *********************************
// Build effect
eff.build();
// Load texture "textures/sign.jpg"
tex = texture("textures/sign.jpg");
// *********************************
// Set camera properties
cam.set_position(vec3(10.0f, 10.0f, 10.0f));
cam.set_target(vec3(0.0f, 0.0f, 0.0f));
auto aspect = static_cast<float>(renderer::get_screen_width()) / static_cast<float>(renderer::get_screen_height());
cam.set_projection(quarter_pi<float>(), aspect, 2.414f, 1000.0f);
return true;
}
bool update(float delta_time) {
// Update the camera
cam.update(delta_time);
return true;
}
bool render() {
// Bind effect
renderer::bind(eff);
// Create MVP matrix
auto M = m.get_transform().get_transform_matrix();
auto V = cam.get_view();
auto P = cam.get_projection();
auto MVP = P * V * M;
// Set MVP matrix uniform
glUniformMatrix4fv(eff.get_uniform_location("MVP"), // Location of uniform
1, // Number of values - 1 mat4
GL_FALSE, // Transpose the matrix?
value_ptr(MVP)); // Pointer to matrix data
// *********************************
// Bind texture to renderer
renderer::bind(tex, 0);
// Set the texture value for the shader here
glUniform1i(eff.get_uniform_location("tex"), 0);
// *********************************
// Render the mesh
renderer::render(m);
return true;
}
void main() {
// Create application
app application("27_Texturing_Shader");
// Set load content, update and render methods
application.set_load_content(load_content);
application.set_update(update);
application.set_render(render);
// Run application
application.run();
}
That's because the order of your vertex positions and texture coordinates do not match.那是因为您的顶点位置和纹理坐标的顺序不匹配。 The first position is the top corner (assuming y is up), but the first texture coordinate is the bottom left corner of the image.
第一个位置是顶角(假设 y 向上),但第一个纹理坐标是图像的左下角。 Moving the last texture coordinate to the front should do the trick.
将最后一个纹理坐标移到前面应该可以解决问题。
Your texture is probably mirrored along the y-axis.您的纹理可能沿 y 轴镜像。 OpenGL expects the texture lines to be bottom-up, but most image libraries provide them top-down.
OpenGL 期望纹理线是自下而上的,但大多数图像库提供自上而下的纹理线。 Depends on what library you use to load the image.
取决于您用于加载图像的库。 And of course, it will depend on the side from which you view the triangle.
当然,这取决于您查看三角形的一侧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.