我有一個程式可以渲染 FACET 幾何圖形,然后在相同的 OpenGL 背景關系中在幾何圖形的頂部繪制資料。從定義小平面三角形及其相關顏色的 VBO 按頂點著色時,一切都按預期作業,但是用戶已請求能夠將所有幾何頂點設定為單一顏色(GUI 切換)。
有沒有辦法在不進入并將 VBO 的顏色部分設定為該顏色的情況下執行此操作?看起來效率很低,但我無法從數小時的谷歌搜索中得到任何我嘗試過的東西。
使用 C 、QT6
片段著色器
#version 150 core
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif
in vec3 Color;
out vec4 outColor;
void main()
{
outColor = vec4(Color,1.0f);
}
頂點著色器
#version 150 core
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif
uniform mat4 mvp_matrix;
attribute vec4 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
attribute vec3 color;
varying vec3 Color;
void main()
{
Color = color;
// Calculate vertex position in screen space
gl_Position = mvp_matrix * a_position;
// Pass texture coordinate to fragment shader
// Value will be automatically interpolated to fragments inside polygon faces
v_texcoord = a_texcoord;
}
相關代碼片段:
// initializing the buffers from vectors of structures
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Geometry
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Get a copy of the geometry to reference here
std::vector<storedGeometry::facetData> tGeom = gUseGeom.getGeom();
// Convert vector to array
storedGeometry::facetData* aGeom = tGeom.data();
// Transfer vertex data to VBO 0
geomBuf.bind();
geomBuf.allocate(aGeom, int(tGeom.size() * sizeof(storedGeometry::facetData)));
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Currents
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Get a copy of the currents data to reference here
std::vector<storedGeometry::facetData> tCurrents = gUseCurrents.getGeom();
// Convert vector to array
storedGeometry::facetData* aCurrent = tCurrents.data();
// Transfer vertex data to VBO 1
currentsBuf.bind();
currentsBuf.allocate(aCurrent, int(tCurrents.size() * sizeof(storedGeometry::facetData)));
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// drawing
// Get shader variable locations
int vertexLocation = program->attributeLocation("a_position");
int colorLocation = program->attributeLocation("color");
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Plot Geometry
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (bGeomVisible) {
geomBuf.bind();
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(QVector3D));
program->enableAttributeArray(colorLocation);
if (bCobbleSolid) { // Solid
// This is what I can't get to work ************************************
//program->setAttributeValue(colorLocation, 1.0, 1.0, 1.0);
program->setAttributeValue(colorLocation, QVector3D(1.0,1.0,1.0));
} else { // Cobble
program->setAttributeBuffer(colorLocation, GL_FLOAT, 3*sizeof(QVector3D), 3, sizeof(QVector3D));
}
// Set polygon/wiremesh or solid/filled mode based on fill/wire toggle
if (bFillWire) {
// Polygon = TRUE
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
} else {
// Filled = FALSE
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
}
// Draw geometry
glDrawArrays(GL_TRIANGLES, 0, gUseGeom.gSize() * 6);
}
// Debugging
//out << "gUseGeom.gSize(): " << gUseGeom.gSize() << Qt::endl;
// Draw cube geometry
//glDrawElements(GL_TRIANGLES, gUseGeom.gSize() * 3, GL_UNSIGNED_SHORT, 0);
//glDrawArrays(GL_TRIANGLES, 0, gUseGeom.gSize() * 6);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Plot Currents
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (bCurrentsVisible) {
currentsBuf.bind();
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(QVector3D));
program->enableAttributeArray(colorLocation);
program->setAttributeBuffer(colorLocation, GL_FLOAT, 3*sizeof(QVector3D), 3, sizeof(QVector3D));
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glDrawArrays(GL_TRIANGLES, 0, gUseCurrents.gSize() * 6);
}
uj5u.com熱心網友回復:
當特定的頂點屬性陣列未啟用(使用glEnableVertexAttribArray)時,所有渲染頂點的該屬性的值取自當前頂點屬性值,如glVertexAttrib函式族所定義。
您忘記禁用頂點屬性陣列,因此不使用當前頂點屬性值。您可以按如下方式修復它:
if (bCobbleSolid) { // Solid
program->disableAttributeArray(colorLocation);
program->setAttributeValue(colorLocation, QVector3D(1.0,1.0,1.0));
} else { // Cobble
program->enableAttributeArray(colorLocation);
program->setAttributeBuffer(colorLocation, GL_FLOAT, 3*sizeof(QVector3D), 3, sizeof(QVector3D));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533958.html
標籤:C qtopengl
上一篇:保持所有視窗狀態的QML多視窗
下一篇:為什么docker容器不運行?
