gpt4 book ai didi

c++ - OpenGL 顶点属性指针或着色器不工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:55:14 27 4
gpt4 key购买 nike

我在显示使用 Assimp 加载的模型时遇到问题。模型已正确加载并较早显示,但我只使用没有法线等的顶点数据。

现在,当我什至删除整个着色器代码时,它会给我绘制一个白色模型。谁能给我解释一下?

这是网格加载辅助方法:

Model * MeshHelper::loadModelFromObjFile( char * filePath )
{
Model * model = new Model();

const aiScene* scene = aiImportFile ( filePath, aiProcess_Triangulate);

if (!scene) {
fprintf (stderr, "ERROR: reading mesh %s\n", filePath);
return false;
}
printf ("%i animations\n", scene->mNumAnimations);
printf ("%i cameras\n", scene->mNumCameras);
printf ("%i lights\n", scene->mNumLights);
printf ("%i materials\n", scene->mNumMaterials);
printf ("%i meshes\n", scene->mNumMeshes);
printf ("%i textures\n", scene->mNumTextures);

for (unsigned int m_i = 0; m_i < scene->mNumMeshes; m_i++) {
const aiMesh* mesh = scene->mMeshes[m_i];

GLuint vertexBuffer, normalBuffer, textureBuffer;

Mesh * modelMesh = new Mesh();
modelMesh->vertexCount = mesh->mNumVertices;

glGenVertexArrays( 1, &modelMesh->vertexArray );
glBindVertexArray( modelMesh->vertexArray );

for (unsigned int v_i = 0; v_i < mesh->mNumVertices; v_i++)
{
if (mesh->HasPositions ()) {
const aiVector3D* vp = &(mesh->mVertices[v_i]);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->mNumVertices, mesh->mVertices, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL );
glDisableVertexAttribArray(0);
}
// buffer for vertex normals
if (mesh->HasNormals()) {
glGenBuffers(1, &normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->mNumVertices, mesh->mNormals, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, NULL );
glDisableVertexAttribArray(1);
}

// buffer for vertex texture coordinates
if (mesh->HasTextureCoords(0)) {
float *texCoords = (float *)malloc(sizeof(float) * 2 * mesh->mNumVertices);
for (unsigned int k = 0; k < mesh->mNumVertices; ++k) {

texCoords[k*2] = mesh->mTextureCoords[0][k].x;
texCoords[k*2+1] = mesh->mTextureCoords[0][k].y;

}
glGenBuffers(1, &textureBuffer);
glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * mesh->mNumVertices, texCoords, GL_STATIC_DRAW);
//glEnableVertexAttribArray(2);
//glVertexAttribPointer(2, 2, GL_FLOAT, 0, 0, 0);

}
if (mesh->HasTangentsAndBitangents ()) {
// NB: could store/print tangents here
}

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

model->meshCollection->push_back( modelMesh );
}
aiReleaseImport (scene);

return model;
}

模型类包含:

std::vector<Mesh*> * meshCollection;

Mesh 有 VAO 索引和要绘制的点数Mesh的绘制方法

void Mesh::draw()
{
glBindVertexArray( vertexArray );
glDrawArrays( GL_TRIANGLES, 0, vertexCount );
glBindVertexArray(0);
}

这里是顶点着色器和片段

顶点

#version 400

layout( location = 0 ) in vec3 aPosition;
layout( location = 1 ) in vec3 aNormal;

uniform mat4 uMVPMatrix;

out vec3 vNormal;

void main()
{
vNormal = aNormal;
gl_Position = uMVPMatrix * vec4 ( aPosition, 1.0);
};

片段

#version 400

out vec4 frag_colour;
in vec3 vNormal;

void main()
{
frag_colour = vec4 ( vNormal, 1.0) * vec4( 0.5 );
};

着色器加载

GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertexShader, NULL );
glCompileShader (vs);
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragmentShader, NULL);
glCompileShader (fs);

program = glCreateProgram();

glAttachShader (program, fs);
glAttachShader (program, vs);
glLinkProgram (program);

最佳答案

您使用法线作为颜色值。法线用于计算照明过程的结果颜色。例如漫射照明。法线表示来自给定面(三角形)的正交方向 vector 。它用于计算光线方向与表面之间的角度确定亮度强度。

你需要的是从你的网格加载 Material 信息并传递这个片段着色器的信息。

因评论而更新:

也在您的绘制方法中启用 normalBuffer 和 texturebuffer。

这是我的渲染方法的摘录:您可以看到我启用了所有必要的缓冲区以及所有 VertexAttributeArrays因此将正确的 VertexAttributes 传递给着色器程序。为我拥有的每个模型(网格)执行此代码。

void VEModel::render(VEEnvironment &environment, glm::mat4 &parentModel) {

glUseProgram( shaderProgram );

glPolygonMode( GL_FRONT_AND_BACK, polygonModel);

glm::mat4 proj = environment.getProjection();
VECamera* cam = &environment.getActiveCamera();

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo );

GLint refModel = getUniformLocation( shaderProgram, "model" );
GLint refView = getUniformLocation( shaderProgram, "view" );
GLint refProj = getUniformLocation( shaderProgram, "proj" );

glUniformMatrix4fv( refView, 1, GL_FALSE, glm::value_ptr( cam->getCamera() ) );
glUniformMatrix4fv( refProj, 1, GL_FALSE, glm::value_ptr( proj ) );
glUniformMatrix4fv( refModel, 1, GL_FALSE, glm::value_ptr( modelMatrix ) );

applyLights(environment);


GLint posAttrib = getAttributeLocation(shaderProgram, "vertPosition");
GLint normalAttrib = getAttributeLocation(shaderProgram, "vertNormal");
GLint colAttrib = getAttributeLocation(shaderProgram, "vertColor");

glEnableVertexAttribArray( posAttrib );
glVertexAttribPointer( posAttrib, 3, GL_FLOAT, GL_FALSE, vertexAttrLen * sizeof(float), (void*) ( 0 * sizeof(float) ) );


glEnableVertexAttribArray( normalAttrib );
glVertexAttribPointer( normalAttrib, 3, GL_FLOAT, GL_FALSE, vertexAttrLen * sizeof(float), (void*) ( 3 * sizeof(float) ));


glEnableVertexAttribArray( colAttrib );
glVertexAttribPointer( colAttrib, 3, GL_FLOAT, GL_FALSE, vertexAttrLen * sizeof(float), (void*) ( 6 * sizeof(float) ) );

int offset = 0;
int texUnitOffset = 0;
//glDrawElements(GL_TRIANGLES, maxIndices, GL_UNSIGNED_INT, (void*)0);


for(std::vector<VEGeometryObject *>::iterator geometry = geometryObjects.begin(); geometry != geometryObjects.end(); ++geometry) {
if ((*geometry)->hasTexture()) {

for (int i=0; i<(*geometry)->getTextureCount(); i++) {
glActiveTexture( i + texUnitOffset);
glBindTexture( GL_TEXTURE_2D, textures[i+texUnitOffset] );
//glActiveTexture(GL_TEXTURE0); // i + texUnitOffset);

(*geometry)->enableTextureStates();
/*
fprintf(stderr, "Enabled texture buffer TBO %i\n", textures[i+texUnitOffset]);
*/

GLint texAttrib = getAttributeLocation( shaderProgram, "vertUV" );
glEnableVertexAttribArray( texAttrib );
glVertexAttribPointer( texAttrib, 2, GL_FLOAT, GL_FALSE, vertexAttrLen * sizeof(float), (void*)( 9 * sizeof(float) ) );

/*GLint uniTime = getUniformLocation(shaderProgram, "time");
glUniform1f(uniTime, (GLfloat)clock() / (GLfloat)CLOCKS_PER_SEC);*/
glUniform1i( glGetUniformLocation( shaderProgram, "sampler" ), i + texUnitOffset );
}

texUnitOffset++;
}
//glDrawArrays(GL_TRIANGLES, 0, (*geometry)->getVertexCount());
glDrawElements(GL_TRIANGLES, maxIndices, GL_UNSIGNED_INT, (void*) offset) ;
offset += (*geometry)->getFaceCount();
}

glDisableVertexAttribArray(posAttrib);
glDisableVertexAttribArray(normalAttrib);
glDisableVertexAttribArray(colAttrib);

这是一个非常复杂的教程,它解释了以一种非常好的方式缓冲概念:http://open.gl/introduction

关于c++ - OpenGL 顶点属性指针或着色器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20571314/

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