gpt4 book ai didi

visual-c++ - GLSL 不会接受我的隐式转换

转载 作者:行者123 更新时间:2023-12-02 06:38:26 25 4
gpt4 key购买 nike

我正在使用一些教程 (http://opengl-tutorial.org) 学习 OpenGL 3.3。在我使用的教程中,有一个顶点着色器执行以下操作:

教程着色器源

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;

// Values that stay constant for the whole mesh.
uniform mat4 MVP;

void main(){

// Output position of the vertex, in clip space : MVP * position
gl_Position = MVP * vec4(vertexPosition_modelspace,1);

}

然而,当我尝试在我的应用程序中模拟相同的行为时,我得到以下信息:

错误:从“vec4”到“vec3”的隐式转换

看到这个后,我不确定是不是因为我使用的是 4.2 版本的着色器而不是 3.3,所以更改了所有内容以匹配作者一直使用的内容,之后仍然收到相同的错误。

所以,我改变了我的着色器来做到这一点:

我的(最新)来源

#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;

uniform mat4 MVP;

void main()
{
vec4 a = vec4(vertexPosition_modelspace, 1);

gl_Position.xyz = MVP * a;
}

当然,这仍然会产生相同的错误。

有谁知道为什么会这样,以及可能的解决方案是什么?我不确定这是否是我的调用代码(我已经发布了,以防万一)。


调用代码

static const GLfloat T_VERTEX_BUF_DATA[] = 
{
// x, y z
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};

static const GLushort T_ELEMENT_BUF_DATA[] =
{ 0, 1, 2 };

void TriangleDemo::Run(void)
{
glClear(GL_COLOR_BUFFER_BIT);

GLuint matrixID = glGetUniformLocation(mProgramID, "MVP");

glUseProgram(mProgramID);

glUniformMatrix4fv(matrixID, 1, GL_FALSE, &mMVP[0][0]); // This sends our transformation to the MVP uniform matrix, in the currently bound vertex shader

const GLuint vertexShaderID = 0;

glEnableVertexAttribArray(vertexShaderID);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glVertexAttribPointer(
vertexShaderID, // Specify the ID of the shader to point to (in this case, the shader is built in to GL, which will just produce a white triangle)
3, // Specify the number of indices per vertex in the vertex buffer
GL_FLOAT, // Type of value the vertex buffer is holding as data
GL_FALSE, // Normalized?
0, // Amount of stride
(void*)0 ); // Offset within the array buffer

glDrawArrays(GL_TRIANGLES, 0, 3); //0 => start index of the buffer, 3 => number of vertices

glDisableVertexAttribArray(vertexShaderID);
}

void TriangleDemo::Initialize(void)
{
glGenVertexArrays(1, &mVertexArrayID);

glBindVertexArray(mVertexArrayID);

glGenBuffers(1, &mVertexBuffer);

glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);

glBufferData(GL_ARRAY_BUFFER, sizeof(T_VERTEX_BUF_DATA), T_VERTEX_BUF_DATA, GL_STATIC_DRAW );

mProgramID = LoadShaders("v_Triangle", "f_Triangle");

glm::mat4 projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f); // field of view, aspect ratio (4:3), 0.1 units near, to 100 units far

glm::mat4 view = glm::lookAt(
glm::vec3(4, 3, 3), // Camera is at (4, 3, 3) in world space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0) // this is the up vector - the head of the camera is facing upwards. We'd use (0, -1, 0) to look upside down
);

glm::mat4 model = glm::mat4(1.0f); // set model matrix to identity matrix, meaning the model will be at the origin

mMVP = projection * view * model;

注意事项

  • 我在使用 Visual Studio 2012
  • 我正在使用 Shader Maker用于 GLSL 编辑

最佳答案

我不能说教程代码有什么问题。

不过在“我的最新来源”中,有

gl_Position.xyz = MVP * a;

这看起来很奇怪,因为您要将 vec4 分配给 vec3


编辑

我无法重现您的问题。

我使用了一个普通的片段着色器来测试...

#version 330 core

void main()
{
}

测试“教程着色器源”:

3.3.11762 Core Profile Context
Log: Vertex shader was successfully compiled to run on hardware.

Log: Fragment shader was successfully compiled to run on hardware.

Log: Vertex shader(s) linked, fragment shader(s) linked.

测试“我的最新来源”:

3.3.11762 Core Profile Context
Log: Vertex shader was successfully compiled to run on hardware.
WARNING: 0:11: warning(#402) Implicit truncation of vector from size 4 to size 3.

Log: Fragment shader was successfully compiled to run on hardware.

Log: Vertex shader(s) linked, fragment shader(s) linked.

gl_Position.xyz 替换为 gl_Position 后警告消失。


你的设置是什么?您有正确版本的 OpenGL 上下文吗? glGetError() 是沉默的吗?

最后,您的 GPU 驱动程序是最新的吗?

关于visual-c++ - GLSL 不会接受我的隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12822442/

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