Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用OpenGL 3.0/GLSL 1.3应用纹理_Opengl_Glsl_Textures_Texture Mapping_Opengl 3 - Fatal编程技术网

使用OpenGL 3.0/GLSL 1.3应用纹理

使用OpenGL 3.0/GLSL 1.3应用纹理,opengl,glsl,textures,texture-mapping,opengl-3,Opengl,Glsl,Textures,Texture Mapping,Opengl 3,目前,我使用以下代码创建三维模型(简化): 我当前(不工作)的纹理代码如下所示: glGenTextures(1, &imgEntity->m_glTexture); glBindTexture(GL_TEXTURE_2D, imgEntity->m_glTexture); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE

目前,我使用以下代码创建三维模型(简化):

我当前(不工作)的纹理代码如下所示:

glGenTextures(1, &imgEntity->m_glTexture);
glBindTexture(GL_TEXTURE_2D, imgEntity->m_glTexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,transImage->GetWidth(),transImage->GetHeight(),0,GL_RGB,GL_UNSIGNED_BYTE,transImage->GetData());
glBindTexture(GL_TEXTURE_2D, imgEntity->m_glTexture);
//An easy way keep track of what locations are assigned for each attribute
enum Attribute_Location 
{
  AL_Vertices = 0,
  AL_DiffuseTexCoords = 1,
  AL_AlphaTexCoords = 2,
  AL_Normals = 3,
};


GLuint uvBuffer;
glGenBuffers(1, &uvBuffer);

//Bind the buffer 
glBindBuffer(
GL_ARRAY_BUFFER, 
uvBuffer);

//Bind the data to the buffer
glBufferData(GL_ARRAY_BUFFER, 
bufferSize, //size of the buffer you are uploading
&diffuseTexCoords[0], //array of texture coords
GL_STATIC_DRAW);

glEnableVertexAttribArray(AL_DiffuseTexCoords);

//Tells OpenGL how to assign data from the texture buffer to the shader
glVertexAttribPointer(AL_DiffuseTexCoords, 
2, 
GL_FLOAT, 
GL_FALSE, 
0, 
0);
显然缺少的是纹理坐标和将纹理指定给创建的模型。所以我的问题是:

如何仅使用OpenGL 3.0和GLSL 1.3将一些有效的纹理坐标应用于对象

如何将这些纹理数据指定给模型,以便在下次调用时绘制它们

glBindVertexArray(element->VAO);
glDrawArrays(element->arrayType,arrayStart,element->arraySize);
这个型号的

谢谢

如何使用将一些有效的纹理坐标应用于对象 只有OpenGL 3.0和GLSL 1.3

纹理坐标通常由第三方程序(如3DS Max或Blender)生成。3D美工人员使用这些程序对其模型进行纹理处理,导出模型时,纹理坐标也会导出到模型文件中。加载模型进行渲染时,将提取每个顶点的纹理坐标,然后我们可以通过着色器属性将这些坐标传递给着色器

如何将这些纹理数据指定给模型

在OpenGL中获取纹理几何体可能是一个过程,因此我将尝试在几个步骤中分解该过程

  • 获取模型的纹理坐标;可以通过编程方式从模型生成或加载

  • 加载纹理,以便OpenGL可以使用它

  • 设置属性数组,以便着色器可以找到纹理坐标

  • 修改顶点着色器和片段着色器以支持纹理几何体

  • 看起来您已经有了第2个机制(加载纹理)

    所以你似乎错过了最后两步

    要获取与顶点数据关联的纹理坐标,可以指定与顶点数据关联的属性

    根据OpenGL文档:

    顶点属性用于从“外部”与顶点着色器通信。与统一变量不同,每个顶点都提供值(而不是所有顶点的全局值)。有内置的顶点属性,如法线或位置,也可以指定自己的顶点属性,如切线或其他自定义值。无法在片段着色器中定义属性

    一些示例代码可能如下所示:

    glGenTextures(1, &imgEntity->m_glTexture);
    glBindTexture(GL_TEXTURE_2D, imgEntity->m_glTexture);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,transImage->GetWidth(),transImage->GetHeight(),0,GL_RGB,GL_UNSIGNED_BYTE,transImage->GetData());
    glBindTexture(GL_TEXTURE_2D, imgEntity->m_glTexture);
    
    //An easy way keep track of what locations are assigned for each attribute
    enum Attribute_Location 
    {
      AL_Vertices = 0,
      AL_DiffuseTexCoords = 1,
      AL_AlphaTexCoords = 2,
      AL_Normals = 3,
    };
    
    
    GLuint uvBuffer;
    glGenBuffers(1, &uvBuffer);
    
    //Bind the buffer 
    glBindBuffer(
    GL_ARRAY_BUFFER, 
    uvBuffer);
    
    //Bind the data to the buffer
    glBufferData(GL_ARRAY_BUFFER, 
    bufferSize, //size of the buffer you are uploading
    &diffuseTexCoords[0], //array of texture coords
    GL_STATIC_DRAW);
    
    glEnableVertexAttribArray(AL_DiffuseTexCoords);
    
    //Tells OpenGL how to assign data from the texture buffer to the shader
    glVertexAttribPointer(AL_DiffuseTexCoords, 
    2, 
    GL_FLOAT, 
    GL_FALSE, 
    0, 
    0);
    
    下面是一个顶点着色器和片段着色器的外观示例,由

    纹理的

    #version 330 core
    
    // Input vertex data, different for all executions of this shader.
    layout(location = 0) in vec3 vertexPosition_modelspace;
    layout(location = 1) in vec2 vertexUV;
    
    // Output data ; will be interpolated for each fragment.
    out vec2 UV;
    
    // 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);
    
        // UV of the vertex. No special space for this one.
        UV = vertexUV;
    }
    
    纹理的.fs

    #version 330 core
    
    // Interpolated values from the vertex shaders
    in vec2 UV;
    
    // Ouput data
    out vec3 color;
    
    // Values that stay constant for the whole mesh.
    uniform sampler2D myTextureSampler;
    
    void main()
    {
    
        // Output color = color of the texture at the specified UV
        color = texture( myTextureSampler, UV ).rgb;
    }
    
    请注意,枚举属性_位置中指定的顶点的属性位置和纹理坐标与顶点着色器中的布局位置匹配:

    enum Attribute_Location 
        {
          AL_Vertices = 0,
          AL_DiffuseTexCoords = 1,
          ...
        }
    
    layout(location = 0) in vec3 vertexPosition_modelspace;
    layout(location = 1) in vec2 vertexUV;
    
    如何使用将一些有效的纹理坐标应用于对象 只有OpenGL 3.0和GLSL 1.3

    纹理坐标通常由第三方程序(如3DS Max或Blender)生成。3D美工人员使用这些程序对其模型进行纹理处理,导出模型时,纹理坐标也会导出到模型文件中。加载模型进行渲染时,将提取每个顶点的纹理坐标,然后我们可以通过着色器属性将这些坐标传递给着色器

    如何将这些纹理数据指定给模型

    在OpenGL中获取纹理几何体可能是一个过程,因此我将尝试在几个步骤中分解该过程

  • 获取模型的纹理坐标;可以通过编程方式从模型生成或加载

  • 加载纹理,以便OpenGL可以使用它

  • 设置属性数组,以便着色器可以找到纹理坐标

  • 修改顶点着色器和片段着色器以支持纹理几何体

  • 看起来您已经有了第2个机制(加载纹理)

    所以你似乎错过了最后两步

    要获取与顶点数据关联的纹理坐标,可以指定与顶点数据关联的属性

    根据OpenGL文档:

    顶点属性用于从“外部”与顶点着色器通信。与统一变量不同,每个顶点都提供值(而不是所有顶点的全局值)。有内置的顶点属性,如法线或位置,也可以指定自己的顶点属性,如切线或其他自定义值。无法在片段着色器中定义属性

    一些示例代码可能如下所示:

    glGenTextures(1, &imgEntity->m_glTexture);
    glBindTexture(GL_TEXTURE_2D, imgEntity->m_glTexture);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,transImage->GetWidth(),transImage->GetHeight(),0,GL_RGB,GL_UNSIGNED_BYTE,transImage->GetData());
    glBindTexture(GL_TEXTURE_2D, imgEntity->m_glTexture);
    
    //An easy way keep track of what locations are assigned for each attribute
    enum Attribute_Location 
    {
      AL_Vertices = 0,
      AL_DiffuseTexCoords = 1,
      AL_AlphaTexCoords = 2,
      AL_Normals = 3,
    };
    
    
    GLuint uvBuffer;
    glGenBuffers(1, &uvBuffer);
    
    //Bind the buffer 
    glBindBuffer(
    GL_ARRAY_BUFFER, 
    uvBuffer);
    
    //Bind the data to the buffer
    glBufferData(GL_ARRAY_BUFFER, 
    bufferSize, //size of the buffer you are uploading
    &diffuseTexCoords[0], //array of texture coords
    GL_STATIC_DRAW);
    
    glEnableVertexAttribArray(AL_DiffuseTexCoords);
    
    //Tells OpenGL how to assign data from the texture buffer to the shader
    glVertexAttribPointer(AL_DiffuseTexCoords, 
    2, 
    GL_FLOAT, 
    GL_FALSE, 
    0, 
    0);
    
    下面是一个顶点着色器和片段着色器的外观示例,由

    纹理的

    #version 330 core
    
    // Input vertex data, different for all executions of this shader.
    layout(location = 0) in vec3 vertexPosition_modelspace;
    layout(location = 1) in vec2 vertexUV;
    
    // Output data ; will be interpolated for each fragment.
    out vec2 UV;
    
    // 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);
    
        // UV of the vertex. No special space for this one.
        UV = vertexUV;
    }
    
    纹理的.fs

    #version 330 core
    
    // Interpolated values from the vertex shaders
    in vec2 UV;
    
    // Ouput data
    out vec3 color;
    
    // Values that stay constant for the whole mesh.
    uniform sampler2D myTextureSampler;
    
    void main()
    {
    
        // Output color = color of the texture at the specified UV
        color = texture( myTextureSampler, UV ).rgb;
    }
    
    请注意,枚举属性_位置中指定的顶点的属性位置和纹理坐标与顶点着色器中的布局位置匹配:

    enum Attribute_Location 
        {
          AL_Vertices = 0,
          AL_DiffuseTexCoords = 1,
          ...
        }
    
    layout(location = 0) in vec3 vertexPosition_modelspace;
    layout(location = 1) in vec2 vertexUV;
    

    你需要第二个属性。在顶点着色器中,在顶点坐标旁边为纹理坐标创建第二个属性。将纹理坐标从顶点着色器传递到片段着色器,并将其用于纹理查找。创建和定义纹理坐标数组,就像为顶点坐标(/
    glVertexAttribPointer
    /
    glEnableVertexAttribArray
    )所做的那样。请参见以下教程。但通常这个问题很宽泛。你需要第二个属性。在顶点着色器中,在顶点坐标旁边为纹理坐标创建第二个属性。将纹理坐标从顶点着色器传递到片段着色器,并将其用于纹理查找。创建和定义纹理坐标数组,就像为顶点坐标(/
    glVertexAttribPointer
    /
    glEnableVertexAttribArray
    )所做的那样。请参见以下教程。但通常这个问题涉及面很广。谢谢你的反馈,但我使用的是OpenGL3.0/GLSL 1.3,所以“布局(位置=0)”的语句对我来说不可用-这也是我提出问题的主要原因,像learnopengl.com这样的页面