当前位置: 首页 > 知识库问答 >
问题:

GL11.GLD元件不工作

董良策
2023-03-14

首先,我使用LWJGL 3和OpenGL 3.2

我试图将“索引”与函数GL11一起使用。元素,但窗口中没有渲染任何内容。

缓冲区生成代码(不真正使用索引,但我认为它仍然可以工作):

public void updateBuffers(Game game, int positionsAttrib, int texCoordsAttrib) { // positionsAttrib and texCoordsAttrib are pointer to shader program attribs

    FloatBuffer positionsBuffer = null;
    FloatBuffer texCoordsBuffer = null;

    IntBuffer indicesBuffer = null;

    try {

        this.vertexCount = this.tiles.size() * 4;

        positionsBuffer = MemoryUtil.memAllocFloat( this.tiles.size() * 3 * 4 );
        texCoordsBuffer = MemoryUtil.memAllocFloat( this.tiles.size() * 2 * 4 );
        indicesBuffer = MemoryUtil.memAllocInt( this.vertexCount );

        int i = 0;

        for ( Entry<TilePosition, Tile> tilesEntry : this.tiles.entrySet() ) {

            TilePosition tilePosition = tilesEntry.getKey();
            Tile tile = tilesEntry.getValue();
            String tileTextureIdentifier = tile.getTextureIdentifier();
            TextureDefinition tileTextureDefinition = game.getTexturesManager().getTextureDefinition("tiles");
            Rectangle tileTextureRectangle = tileTextureDefinition.getTilePosition( tileTextureIdentifier );

            if ( tileTextureRectangle == null ) continue;

            positionsBuffer.put( tilePosition.getX() ).put( tilePosition.getY() + 1 ).put( 0 );
            positionsBuffer.put( tilePosition.getX() + 1 ).put( tilePosition.getY() + 1 ).put( 0 );
            positionsBuffer.put( tilePosition.getX() + 1 ).put( tilePosition.getY() ).put( 0 );
            positionsBuffer.put( tilePosition.getX() ).put( tilePosition.getY() ).put( 0 );

            texCoordsBuffer.put( tileTextureRectangle.x ).put( tileTextureRectangle.y );
            texCoordsBuffer.put( tileTextureRectangle.x + tileTextureRectangle.width ).put( tileTextureRectangle.y );
            texCoordsBuffer.put( tileTextureRectangle.x + tileTextureRectangle.width ).put( tileTextureRectangle.y + tileTextureRectangle.height );
            texCoordsBuffer.put( tileTextureRectangle.x ).put( tileTextureRectangle.y + tileTextureRectangle.height );

            indicesBuffer.put( i ).put( i + 1 ).put( i + 2 ).put( i + 3 );

            i += 4;

        }

        positionsBuffer.flip();
        texCoordsBuffer.flip();
        indicesBuffer.flip();

        this.vao.bind(); // vbo and vao are class VertexBufferObject and VertexArrayObject which save internal id of buffers and most usefull functions

        this.positionsVbo.bind( GL15.GL_ARRAY_BUFFER );
        VertexBufferObject.uploadData( GL15.GL_ARRAY_BUFFER, positionsBuffer, GL15.GL_STATIC_DRAW );
        ShaderProgram.pointVertexAttribute( positionsAttrib, 3, 0, 0 );

        this.texCoordsVbo.bind( GL15.GL_ARRAY_BUFFER );
        VertexBufferObject.uploadData( GL15.GL_ARRAY_BUFFER, texCoordsBuffer, GL15.GL_STATIC_DRAW );
        ShaderProgram.pointVertexAttribute( texCoordsAttrib, 2, 0, 0 );

        this.indicesVbo.bind( GL15.GL_ELEMENT_ARRAY_BUFFER );
        VertexBufferObject.uploadData( GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW );

        VertexArrayObject.unbind();

    } finally {

        if ( positionsBuffer != null ) MemoryUtil.memFree( positionsBuffer );
        if ( texCoordsBuffer != null ) MemoryUtil.memFree( texCoordsBuffer );
        if ( indicesBuffer != null ) MemoryUtil.memFree( indicesBuffer );

    }

}

使用的着色器程序:

// scene.vs :

#version 330 // edit : I have to change this line because of OpenGL used version

layout (location=0) in vec3 position;
layout (location=1) in vec2 texCoord;

out vec2 outTexCoord;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

void main() {

    mat4 mvp = projection * view * model;
    gl_Position = mvp * vec4( position, 1.0 );

    outTexCoord = texCoord;

}

// scene.fs :

#version 330

in vec2 outTexCoord;

out vec4 fragColor;

uniform sampler2D textureSampler;

void main() {

    vec3 vertexColor = vec3( 1.0, 1.0, 1.0 );

    vec4 textureColor = texture( textureSampler, outTexCoord );
    fragColor = vec4( vertexColor, 1.0 ) * textureColor;

}

以及渲染功能:

private void beginRender(Game game, int positionsAttrib, int texCoordsAttrib) {

    Texture texture = game.getTexturesManager().getTextureDefinition("tiles").getTexture();
    GL13.glActiveTexture( GL13.GL_TEXTURE0 );
    texture.bind();

    this.vao.bind();

    ShaderProgram.enableVertexAttribute( positionsAttrib );
    ShaderProgram.enableVertexAttribute( texCoordsAttrib );

}

private void endRender(Game game, int positionsAttrib, int texCoordsAttrib) {

    ShaderProgram.disableVertexAttribute( positionsAttrib );
    ShaderProgram.disableVertexAttribute( texCoordsAttrib );

    VertexArrayObject.unbind();

    Texture.unbind();

}

// render is called by render loop between clear and swapbuffer GL functions
public void render(Game game, int positionsAttrib, int texCoordsAttrib) {

    this.beginRender( game, positionsAttrib, texCoordsAttrib );

    GL11.glDrawElements( GL11.GL_QUADS, this.vertexCount, GL11.GL_UNSIGNED_INT, 0 );

    this.endRender( game, positionsAttrib, texCoordsAttrib );

}

我不确定它是否很清楚,尤其是我的近似英语...

共有1个答案

慕容博涛
2023-03-14

您没有详细说明它,但看起来您正在使用核心配置文件(正如您应该的那样)。但是,如果是这样,GL_QUADS将不可用,并且您的绘制调用将导致GL_INVALID_ENUM错误。

顺便说一句:既然你说你使用OpenGL 4.5,我强烈建议你在开发过程中使用OpenGL的调试输出功能,这将使发现和解释任何GL错误更加容易,而且可能还会提供有用的性能提示。

 类似资料:
  • 我试图用类型输入将proptype转换为一个特定的组件类,但这给了我一个警告。 警告:失败的道具类型:提供给MyComponent的道具无效。 MyComponent.js 我的输入组件被剥离了。 子系统上的控制台日志为:

  • 所以我想使用Flux来运行每个Mono任务,并等待每个任务2秒钟。这是我试过的 结果如下 正如您在时间戳delayElement上看到的,即使它在同一个线程上运行(测试工作人员),它似乎也不起作用。我在这里做错了什么? 编辑:我真的不明白它是如何工作的,但是我没有在平面图中添加delayElement,而是在它上面添加delayElements,它工作正常。 结果

  • 我试图列出ftp服务器中特定目录下的所有文件。 虽然目录是一个有效的,但代码卡住,而调用listFiles,可能是什么原因。?进一步我想提到的是,一个单独的netbean项目访问相同的FTP服务器工作正常与相同的代码,但maven项目有问题。请帮助。

  • 我正在尝试设置一个联系人表单,但我正在努力使它与配置项中的SMTP配置一起工作。不过,我已经成功地尝试了基本的'mail'配置。 简而言之,我用以下简单的代码进行测试: 我刚刚询问了我的主机提供商,所有的SMTP配置都是正确的。他们还检查了我的电子邮件帐户是否正常。 事实上,如果我选择: 协议=>“邮件” 消息顺利传递。php mail()函数也使用托管提供程序SMTP,但它只是将它交给服务器,并

  • 当我试图将LWJGL程序的矩阵模式设置为GL_投影时,我遇到了一个错误。 错误是: 异常线程"main"java.lang.IllegalStateException:函数不支持org.lwjgl.BufferChecks.checkFunctionAddress(BufferChecks.java:58)在org.lwjgl.opengl.GL11.glMatrixMode(GL11.java:

  • 我正在尝试让Gradle Artifactory插件来解析工件。 我的build.gradle文件在下面,被替换为正确的主机名 然而,当运行此命令时,它无法解析工件。依赖行是从artiFactory生成的。 我打算使用“旧”发布机制。我的Gradle版本是2.0。 我尝试了一个带有maven2默认值和gradle布局的artifactory存储库。 堆栈跟踪可在以下位置找到:http://text