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 尝试使用顶点和片段着色器绘制彩色元素以进行绘制时感到困惑_Opengl_Colors_Drawing - Fatal编程技术网

Opengl 尝试使用顶点和片段着色器绘制彩色元素以进行绘制时感到困惑

Opengl 尝试使用顶点和片段着色器绘制彩色元素以进行绘制时感到困惑,opengl,colors,drawing,Opengl,Colors,Drawing,我正在创建一组类,以便从COLLADA文件中读取3d对象。我从一些基本代码开始读取位置和法线,并用opengl绘制它们。我添加了成功缩放顶点的代码,并添加了与colld文件中每个图形元素连接的颜色或纹理中需要读取的所有代码。但是现在我需要添加代码来用颜色绘制顶点。我已经创建了缓冲区对象数组来容纳每个顶点数组和缓冲区对象的颜色数组。 这是我必须根据从COLLADA文件获得的数据构建阵列的代码: 请记住,我仍然在创造这个,它不是完美的 // Set vertex coordinate data

我正在创建一组类,以便从COLLADA文件中读取3d对象。我从一些基本代码开始读取位置和法线,并用opengl绘制它们。我添加了成功缩放顶点的代码,并添加了与colld文件中每个图形元素连接的颜色或纹理中需要读取的所有代码。但是现在我需要添加代码来用颜色绘制顶点。我已经创建了缓冲区对象数组来容纳每个顶点数组和缓冲区对象的颜色数组。 这是我必须根据从COLLADA文件获得的数据构建阵列的代码: 请记住,我仍然在创造这个,它不是完美的

// Set vertex coordinate data
        glBindBuffer(GL_ARRAY_BUFFER, vbosPosition[i]);
        glBufferData(GL_ARRAY_BUFFER, col->vectorGeometry[i].map["POSITION"].size,
            scaledData, GL_STATIC_DRAW);
        free(scaledData);


        loc = glGetAttribLocation(program, "in_coords");//get a GLuint for the attribute and put it into GLuint loc.
        glVertexAttribPointer(loc, col->vectorGeometry[i].map["POSITION"].stride, col->vectorGeometry[i].map["POSITION"].type, GL_FALSE, 0, 0);//glVertexAttribPointer — loc specifies the index of the generic vertex attribute to be modified.
        glEnableVertexAttribArray(0);
#ifdef Testing_Mesh3D
        PrintGLVertex(vbosPosition[i], col->vectorGeometry[i].map["POSITION"].size / 4);
#endif      // Set normal vector data
        glBindBuffer(GL_ARRAY_BUFFER, vbosNormal[i]);
        glBufferData(GL_ARRAY_BUFFER, col->vectorGeometry[i].map["NORMAL"].size, col->vectorGeometry[i].map["NORMAL"].data, GL_STATIC_DRAW);
        loc = glGetAttribLocation(program, "in_normals");
        glVertexAttribPointer(loc, col->vectorGeometry[i].map["NORMAL"].stride, col->vectorGeometry[i].map["NORMAL"].type, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(1);
        glBindBuffer(GL_ARRAY_BUFFER, vbosColor[i]);
        Material* material = col->mapGeometryUrlToMaterial2Effect[col->vectorGeometry[i].id];
        if (material->effect1.size() > 0)
        {
            Effect effect1 = material->effect1[0];
            if (effect1.type == enumEffectTypes::color)
            {
                Color color = effect1.color;
                glBufferData(GL_ARRAY_BUFFER, color.length, color.values, GL_STATIC_DRAW);
                loc = glGetAttribLocation(program, "in_colors");
                glVertexAttribPointer(loc, color.length, color.type, GL_FALSE, 0, 0);

            }
            else
            {

            }

        }

    }
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

// Initialize uniform data
void Mesh3D::InitializeUniforms(GLuint program) {

    GLuint program_index, ubo_index;
    struct LightParameters params;

    // Specify the rotation matrix
    glm::vec4 diff_color = glm::vec4(0.3f, 0.3f, 1.0f, 1.0f);
    GLint location = glGetUniformLocation(program, "diffuse_color");
    glUniform4fv(location, 1, &(diff_color[0]));

    // Initialize UBO data
    params.diffuse_intensity = glm::vec4(0.5f, 0.5f, 0.5f, 1.0f);
    params.ambient_intensity = glm::vec4(0.3f, 0.3f, 0.3f, 1.0f);
    params.light_direction = glm::vec4(-1.0f, -1.0f, 0.25f, 1.0f);

    // Set the uniform buffer object
    glUseProgram(program);
    glGenBuffers(1, &ubo);
    glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    glBufferData(GL_UNIFORM_BUFFER, 3 * sizeof(glm::vec4), &params, GL_STREAM_DRAW);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
    glUseProgram(program);

    // Match the UBO to the uniform block
    glUseProgram(program);
    ubo_index = 0;
    program_index = glGetUniformBlockIndex(program, "LightParameters");
    glUniformBlockBinding(program, program_index, ubo_index);
    glBindBufferRange(GL_UNIFORM_BUFFER, ubo_index, ubo, 0, 3 * sizeof(glm::vec4));
    glUseProgram(program);
这是一个heard文件,包含两个字符串文本,其中包含用于构建顶点和片段着色器的字符串。同样,我是新手,不确定如何修改着色器以包含彩色顶点,我已经开始为四种浮动颜色(包括alpha)添加输入vec4。有什么帮助吗

#pragma once
#ifndef Included_shaders
#define Included_shaders

#include<stdio.h>
#include<iostream>
static std::string  shaderVert = "#version 330\n"
"in vec3 in_coords;\n"
"in vec3 in_normals;\n"
"in vec4 in_colors; \n"//added by me
"out vec3 vertex_normal;\n"
"void main(void) {\n"
"vertex_normal = in_normals;\n"
"gl_Position = vec4(in_coords, 1.0);\n"
"}\n";

static std::string shaderFrag = "#version 330\n"
"in vec3 vertex_normal;\n"
"out vec4 output_color;\n"
"layout(std140) uniform LightParameters{\n"
"vec4 diffuse_intensity;\n"
"vec4 ambient_intensity;\n"
"vec4 light_direction;\n"
"};\n"
"uniform vec4 diffuse_color;\n"
"void main() {\n"
"/* Compute cosine of angle of incidence */\n"
"float cos_incidence = dot(vertex_normal, light_direction.xyz);\n"
"cos_incidence = clamp(cos_incidence, 0, 1);\n"
"/* Compute Blinn term */\n"
"vec3 view_direction = vec3(0, 0, 1);\n"
"vec3 half_angle = normalize(light_direction.xyz + view_direction);\n"
"float blinn_term = dot(vertex_normal, half_angle);\n"
"blinn_term = clamp(blinn_term, 0, 1);\n"
"blinn_term = pow(blinn_term, 1.0);\n"
"/* Set specular color and compute final color */\n"
"vec4 specular_color = vec4(0.25, 0.25, 0.25, 1.0);\n"
"output_color = ambient_intensity * diffuse_color +\n"
"diffuse_intensity * diffuse_color * cos_incidence +\n"
"diffuse_intensity * specular_color * blinn_term;\n"
"}\n";
#endif
#pragma一次
#ifndef包含了\u着色器
#定义包含的着色器
#包括
#包括
静态std::string shadergt=“#版本330\n”
“在vec3 in_coords中;\n”
“在vec3中的法线中;\n”
“在vec4中使用颜色;\n”//由我添加
“输出向量3顶点\u法线;\n”
“void main(void){\n”
“顶点\u法线=在\u法线中;\n”
“gl_Position=vec4(in_coords,1.0);\n”
“}\n”;
静态std::string shaderFrag=“#版本330\n”
“在vec3顶点_法线中;\n”
“输出vec4输出颜色;\n”
“布局(std140)统一灯光参数{\n”
“vec4漫反射强度;\n”
“vec4环境_强度;\n”
“vec4灯光方向;\n”
“};\n”
“均匀vec4漫反射颜色;\n”
“void main(){\n”
“/*计算入射角的余弦*/\n”
float cos_入射=点(顶点_法线,光_方向.xyz);\n
cos_关联=钳位(cos_关联,0,1);\n
“/*计算Blinn项*/\n”
vec3视图_方向=vec3(0,0,1);\n
“vec3半角度=规格化(灯光方向.xyz+视图方向);\n”
浮点blinn_项=点(顶点法向,半角);\n
blinn_项=钳位(blinn_项,0,1);\n
blinn_term=pow(blinn_term,1.0);\n
“/*设置镜面反射颜色并计算最终颜色*/\n”
vec4镜面反射颜色=vec4(0.25,0.25,0.25,1.0);\n
“输出颜色=环境亮度*漫反射颜色+\n”
“漫反射强度*漫反射颜色*cos\u入射率+\n”
“漫反射强度*镜面反射颜色*blinn\u项;\n”
“}\n”;
#恩迪夫
最后,这是我正在修改的函数,用于绘制彩色元素

void Mesh3D::DrawToParent()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Draw elements of each mesh in the vector
    for (int i = 0; i<nVectorGeometry; i++)
    {
        glBindVertexArray(vaos[i]);
        glDrawElements(col->vectorGeometry[i].primitive/*This is 4 for GL_Triangles*/, col->vectorGeometry[i].index_count,
            GL_UNSIGNED_SHORT, col->vectorGeometry[i].indices);
    }
    glBindVertexArray(0);
    glutSwapBuffers();
}
void Mesh3D::DrawToParent()
{
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
//在矢量中绘制每个网格的元素
对于(int i=0;ivectorGeometry[i]。primitive/*对于GL_三角形*/,col->vectorGeometry[i],这是4。索引_计数,
GL_UNSIGNED_SHORT,col->vectorGeometry[i].索引);
}
glBindVertexArray(0);
glutSwapBuffers();
}
我对glVertexAttributePointer和glGetAttributeLocation有点困惑,尽管我想我已经知道了基本的想法。我用对了吗

我是否正确设置颜色的缓冲区对象。我说的对吗?我在这个缓冲区中的每个顶点都有一种颜色,现在我只放置了一种颜色,适用于这个数组中所有相关的缓冲区,可能需要更改它

当我调用gldrawerelements时,如何精确地绘制彩色顶点


不要只是让我参考opengl的参考资料,很多冗长的解释对我来说毫无意义。

使顶点着色器输出颜色,并使片段着色器将其作为输入。颜色将在顶点之间插值

是的,看来您对glAttribPointer的理解是正确的

抽屉元素获取要绘制的顶点的索引。最后一个参数不应包含索引。相反,它可能应该为null。索引应使用
glBindBuffer(GL\u元素\u数组\u缓冲区,…)指定

如果颜色缓冲区绑定正确,则不需要对颜色执行任何特殊的id元素。着色器将获得所有已启用的属性阵列


如果您运行代码并告诉我您遇到了什么问题,我可以更好地帮助您。如果代码更容易阅读,也会有所帮助。如果将其拆分为十行以下的函数,您可能会自己发现一些错误,并可能删除一些重复项。

有点离题:您应该将着色器存储在文件中。使它们更容易编码,如果不硬编码,您可以在运行时交换它们。您说您不需要OpenGL资源,但您尝试过这个吗?已经修改了我的代码人,所以我在另一个问题中再次发布了它,而不是更改这个问题。哦,我留下了最后一个参数,没有它,什么都没有画出来!