我写了一个简单的例子,用纹理绘制一个四边形。当我作为 WebAssembly 运行此示例时:https://profound-sunflower-a5633b.netlify.app/ 我在浏览器控制台中看到此消息:
WebGL: this extension has very low support on mobile devices; do not rely on it for rendering effects: WEBGL_polygon_mode
它发生在 JS 文件中的
GLctx.getExtension(ext);
行:
// .getSupportedExtensions() can return null if context is lost, so coerce to empty array.
var exts = GLctx.getSupportedExtensions() || [];
exts.forEach(function(ext) {
// WEBGL_lose_context, WEBGL_debug_renderer_info and WEBGL_debug_shaders are not enabled by default.
if (!ext.includes('lose_context') && !ext.includes('debug')) {
// Call .getExtension() to enable that extension permanently.
GLctx.getExtension(ext);
}
});
如何隐藏此消息?
我的示例:simple-texture-in-one-file-opengl-qt6-cpp.zip
主.cpp
#include <QtWidgets/QApplication>
#include <QtGui/QOpenGLFunctions>
#include <QtOpenGL/QOpenGLBuffer>
#include <QtOpenGL/QOpenGLShaderProgram>
#include <QtOpenGL/QOpenGLTexture>
#include <QtOpenGLWidgets/QOpenGLWidget>
class OpenGLWidget : public QOpenGLWidget, private QOpenGLFunctions
{
public:
OpenGLWidget(QWidget *parent = nullptr)
: QOpenGLWidget(parent)
, m_texture(QOpenGLTexture::Target::Target2D)
{
setWindowTitle("OpenGL, Qt6, C++");
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setFixedSize(350, 350);
}
private:
void initializeGL() override
{
initializeOpenGLFunctions();
// qDebug() << "OpenGL version:" << (const char*) glGetString(GL_VERSION) << "\n";
// qDebug() << "GLSL version: " << (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION) << "\n";
// qDebug() << "Vendor: " << (const char*) glGetString(GL_VENDOR) << "";
m_texture.create();
m_texture.bind();
m_texture.setData(QImage(":/assets/textures/box_texture_512x512.png").mirrored());
m_texture.setMinMagFilters(QOpenGLTexture::Filter::Linear, QOpenGLTexture::Filter::Linear);
m_texture.setWrapMode(QOpenGLTexture::WrapMode::ClampToEdge);
// Create and compile shaders
shaderProgram.addShaderFromSourceCode(QOpenGLShader::ShaderTypeBit::Vertex,
"attribute vec2 vertexPosition;"
"attribute vec2 vertexTexCoord;"
"varying vec2 texCoord;"
"void main()"
"{"
" gl_Position = vec4(vertexPosition, 0.0, 1.0);"
" texCoord = vertexTexCoord;"
"}");
shaderProgram.addShaderFromSourceCode(QOpenGLShader::ShaderTypeBit::Fragment,
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"varying vec2 texCoord;"
"uniform sampler2D textureSampler;"
"void main()"
"{"
" gl_FragColor = texture2D(textureSampler, texCoord);"
"}");
// Create vertex buffer
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f
};
vertexBuffer.create();
vertexBuffer.bind();
vertexBuffer.allocate(vertices, sizeof(vertices));
}
void resizeGL(int w, int h) override
{
glViewport(0, 0, w, h);
}
void paintGL() override
{
// Clear the buffer
glClearColor(0.2f, 0.2f, 0.2f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
// Use shader program
shaderProgram.bind();
// Bind texture
m_texture.bind();
// Bind vertex bufffer
vertexBuffer.bind();
shaderProgram.enableAttributeArray("vertexPosition");
shaderProgram.enableAttributeArray("vertexTexCoord");
shaderProgram.setAttributeBuffer("vertexPosition", GL_FLOAT,
0, 2, sizeof(GLfloat) * 4);
shaderProgram.setAttributeBuffer("vertexTexCoord", GL_FLOAT,
sizeof(GLfloat) * 2, 2, sizeof(GLfloat) * 4);
// Draw quad
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
private:
QOpenGLBuffer vertexBuffer;
QOpenGLShaderProgram shaderProgram;
QOpenGLTexture m_texture;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
OpenGLWidget w;
w.show();
return app.exec();
}
简单纹理在一个文件中-opengl-qt6-cpp.pro
QT += core gui openglwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# disables all the APIs deprecated before Qt 6.0.0
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
SOURCES += \
main.cpp
HEADERS +=
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
assets.qrc
资产.qrc
<RCC>
<qresource prefix="/">
<file>assets/textures/box_texture_512x512.png</file>
</qresource>
</RCC>
资产/纹理/box_texture_512x512.png
这是 Chrome 的问题。阅读 Botje 的 评论。