gpt4 book ai didi

c++ - OpenGL 着色器拉伸(stretch)几何体

转载 作者:行者123 更新时间:2023-11-30 03:54:07 25 4
gpt4 key购买 nike

我创建了一个渲染四边形的简单程序

初始化:

Math::float3 vertices[4];
vertices[0] = Math::float3(-0.5f, 0.5f, -1.0f);
vertices[1] = Math::float3(-0.5f, -0.5f, -1.0f);
vertices[2] = Math::float3(0.5f, -0.5f, -1.0f);
vertices[3] = Math::float3(0.5f, 0.5f, -1.0f);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};

glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

渲染:

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Math::float3), 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glDisableVertexAttribArray(0);

它呈现了一个完美的正方形。

我继续添加顶点着色器程序:

顶点着色器

layout (location = 0) in vec3 Position;

void main(){
gl_Position = vec4(Position.x, Position.y, Position.z, 1.0);
}

片段着色器

out vec4 FragColor;

void main()
{
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

如果我再次运行我的程序,四边形似乎在 x 轴上拉伸(stretch)到其大小的两倍左右,并在 Y 轴上稍微拉伸(stretch)。

没有着色器

without shaders

有着色器

with shader

知道是什么原因造成的吗?如果您需要更多代码片段或其他信息,请告诉我:)

更新

一些额外的代码。

开窗/整形

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutCreateWindow(TITLE);

...

if(_h == 0) _h = 1;
float ratio = 1.0f * _w / _h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, _w, _h);
gluPerspective(45.0f, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);

更新 2

遗憾的是,我的问题仍然存在,这里有一些额外的信息:

我的几何学:

Math::float3 vertices[4];
vertices[0] = Math::float3(-0.5, -0.5f, -1.0f);
vertices[1] = Math::float3(0.5f, -0.5f, -1.0f);
vertices[2] = Math::float3(0.5f, 0.5f, -1.0f);
vertices[3] = Math::float3(-0.5f, 0.5f, -1.0f);

unsigned int indices[] = { 0, 1, 2, 2, 3, 0 };

我的顶点着色器

#version 330

layout (location = 0) in vec3 Position;

uniform mat4 MVP;

void main()
{
gl_Position = MVP * vec4(Position.x, Position.y, Position.z, 1.0);
}

设置 View

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

#define FOV 60.0f
#define RATIO 1.0f * SCREEN_WIDTH / SCREEN_HEIGHT
#define ZNEAR 1.0f
#define ZFAR 1000.0f

if(_h == 0) _h = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, _w, _h);
gluPerspective(FOV, RATIO, ZNEAR, ZFAR);
glMatrixMode(GL_MODELVIEW);

初始化

Math::float3 vertices[4];
vertices[0] = Math::float3(-0.5, -0.5f, -1.0f);
vertices[1] = Math::float3(0.5f, -0.5f, -1.0f);
vertices[2] = Math::float3(0.5f, 0.5f, -1.0f);
vertices[3] = Math::float3(-0.5f, 0.5f, -1.0f);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

unsigned int indices[] = { 0, 1, 2, 2, 3, 0 };

glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

sl = new Loader::ShaderLoader();
sl->Initialize();
sl->AddShader("data/shaders/shader.vs", VERTEX_SHADER);
sl->AddShader("data/shaders/shader.fs", FRAGMENT_SHADER);

sl->CompileShaders();

渲染

MVP = glGetUniformLocation(sl->GetShaderProgram(), "MVP");

float m[16];

f -= 0.01f;

m[0] = 1.0f;
m[1] = 0.0f;
m[2] = 0.0f;
m[3] = 0.0f;

m[4] = 0.0f;
m[5] = 1.0f;
m[6] = 0.0f;
m[7] = 0.0f;

m[8] = 0.0f;
m[9] = 0.0f;
m[10] = 1.0f;
m[11] = 0.0f;

m[12] = 0.0f;
m[13] = 0.0f;
m[14] = 0.0f;
m[15] = 1.0f;

glUniformMatrix4fv(MVP, 1, GL_TRUE, m);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glDisableVertexAttribArray(0);

我还打印了每次渲染调用的 MVP 值,但出于某种原因它始终为 0。

再次欢迎任何额外的提示,谢谢!

最佳答案

很可能您有一个使用固定函数设置的非恒等式转换链(非恒等式投影和/或模型 View 矩阵)。使用着色器时,不使用固定函数矩阵,这可以解释差异。

关于c++ - OpenGL 着色器拉伸(stretch)几何体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29744539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com