gpt4 book ai didi

c++ - 在着色器中从 glOrtho 切换到 MVP 矩阵后没有绘制任何东西

转载 作者:行者123 更新时间:2023-11-30 04:46:09 24 4
gpt4 key购买 nike

我以前使用过正交透视法,它能正确地绘制所有内容。我在初始化期间使用了以下代码:

glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);

还有这个着色器:

vec3 actualPos = inPos + cubeVert * (inMass * scale);
gl_Position = vec4(actualPos, 1.0);
Mass = inMass;
UV = cubeVert.xy + vec2(0.5, 0.5);

我一直在尝试改用我的着色器中的 MVP 矩阵,但我的屏幕上没有任何输出,除了我用来清除它的颜色。

我构建它:

glm::mat4 model(1.f);

glm::mat4 projection(1.f);
projection = glm::perspective(glm::radians(30.f), (float)WIDTH / (float)HEIGHT, 0.1f, 100.f);

glm::mat4 view(1.f);
view = glm::lookAt(glm::vec3(-10.f, -10.f, -10.f), glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 1.f, 0.f));

glm::mat4 MVP = projection * view * model;

然后在删除旧代码后在初始化期间应用它:

glViewport(0, 0, WIDTH, HEIGHT);
glUniformMatrix4fv(glGetAttribLocation(program, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));

这是我当前的主要绘图循环:

glClearColor(0.01f, 0.10f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);

glUniformMatrix4fv(glGetAttribLocation(program, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE0, texture);
glUniform1i(sampler, 0);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBOcube);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, (void*)0);

glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, AMOUNT * sizeof(Body), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, AMOUNT * sizeof(Body), bods);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Body), (void*)0);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Body), (void*)sizeof(glm::vec3));

glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 1);
glVertexAttribDivisor(2, 1);

glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, AMOUNT);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);

我尝试四处移动、旋转相机,以防对象被绘制到 View 之外,但我不确定此时是否有任何东西被绘制。

最佳答案

"MVP" 是统一变量。要获取统一变量的位置索引,您必须使用 glGetUniformLocation而不是 glGetAttribLocation .

glLinkProgram(program);
GLint mvp_loc = glGetUniformLocation(program, "MVP"):
glUseProgram(program);
glUniformMatrix4fv(mvp_loc, 1, GL_FALSE, glm::value_ptr(MVP));

关于c++ - 在着色器中从 glOrtho 切换到 MVP 矩阵后没有绘制任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56941647/

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