GLSL不会接受我的隐式演员

时间:2012-10-10 15:00:04

标签: visual-c++ opengl glsl linker-errors

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

教程着色器源

#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);

}

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

error: implicit cast from "vec4" to "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编辑

3 个答案:

答案 0 :(得分:4)

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

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

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驱动程序是最新的吗?

答案 1 :(得分:3)

我遇到了一些GPU(ATi,我相信)在遇到浮点数时不喜欢整数文字的问题。尝试更改

gl_Position =  MVP * vec4(vertexPosition_modelspace,1);

gl_Position =  MVP * vec4(vertexPosition_modelspace, 1.0);

答案 2 :(得分:2)

我刚刚在安装了最新驱动程序的ATI Radeon HD 7900上遇到此错误消息,同时编译了与“虚拟地球仪的3D引擎设计”(http://www.virtualglobebook.com)一书相关的一些示例代码。

< p> 这是原始片段着色器线:
fragmentColor = mix(vec3(0.0, intensity, 0.0), vec3(intensity, 0.0, 0.0), (distanceToContour < dF));

解决方案是将违规的布尔表达式转换为float,如:
fragmentColor = mix(vec3(0.0, intensity, 0.0), vec3(intensity, 0.0, 0.0), float(distanceToContour < dF));

混合手册(http://www.opengl.org/sdk/docs/manglsl)说明

  

对于mix的变体,其中a是genBType,a [i]为false的元素,结果为   元素取自x,其中a [i]为真,它将取自y。

因此,由于编译器会接受布尔混合值而没有评论,我认为这应该是AMD / ATI驱动程序问题。

相关问题