未设置LWJGL OpenGL制服

蓬松的狗狗

现在,我处于渲染对象的简单步骤。我在这里遵循gitbook ,尽管设置基本相同,但屏幕上什么也没有。

我试过了:

  • 仔细检查我的视图矩阵数学(这实际上是错误的,但已修复)
  • 尝试不使用着色器进行渲染(效果很好)
  • 尝试使用着色器渲染,但不使用制服(效果很好)

但是,一旦我使用了制服,除了清晰的颜色,我在屏幕上什么也看不到。任何有关如何使这些古怪制服工作的帮助将不胜感激。谢谢!

我的应用程序窗口类的代码:

package flaff.gameengine.internal;

import flaff.gameengine.*;

import java.util.List;

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import static org.lwjgl.opengl.GL30.*;

public class ApplicationWindow
{
    private int width;
    private int height;
    private String title;
    private boolean resized;
    private long window;

    public ApplicationWindow(int width, int height, String title)
    {
        this.width = width;
        this.height = height;
        this.title = title;
    }

    public void start()
    {
        if (!GLFW.glfwInit())
        {
            System.err.println("Error: Couldn't initialize GLFW");
            System.exit(-1);
        }
        this.window = GLFW.glfwCreateWindow(this.width, this.height, this.title, 0, 0);
        if (window == 0)
        {
            System.err.println("Error: Window couldn't be created");
            System.exit(-1);
        }
        GLFW.glfwMakeContextCurrent(this.window);
        GL.createCapabilities();
        GLFW.glfwSetFramebufferSizeCallback(this.window, (unusedW, width, height) ->
        {
            this.width = width;
            this.height = height;
            this.resized = true;
        });
        glClearColor(0.1921569f, 0.3019608f, 0.4745098f, 0f);
        GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
        // centers the window
        GLFW.glfwSetWindowPos(this.window, (videoMode.width() - this.width) / 2, (videoMode.height() - this.height) / 2);
        // show the video
        GLFW.glfwShowWindow(this.window);
        glEnable(GL_DEPTH_TEST);
    }

    public void render()
    {
        if (this.resized)
        {
            glViewport(0, 0, width, height);
            glMatrixMode(GL_MODELVIEW);
            this.resized = false;
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        List<Renderer> rendererList = GObject.findObjectsOfType(Renderer.class);
        for (int index = 0; index < rendererList.size(); index++)
            rendererList.get(index).render();
    }

    public boolean isClosed()
    {
        return GLFW.glfwWindowShouldClose(this.window);
    }

    public void nextFrame()
    {
        // non-relevant code
        GLFW.glfwPollEvents();

        // more non-relevant code

        // render loop
        this.render();

        // more non-relevant code

        GLFW.glfwSwapBuffers(this.window);
    }
}

我的实例类代码:

package flaff.gameengine;

import java.util.List;
import flaff.gameengine.internal.*;
import flaff.gameengine.renderers.MeshRenderer;

public class Instance
{
    private ApplicationWindow window;

    private Instance(int width, int height, String title)
    {
        this.window = new ApplicationWindow(width, height, title);
    }
    public ApplicationWindow getWindow()
    {
        return this.window;
    }
    public void run()
    {
        this.window.start();
        Shader shader = Shader.getDefault();
        shader.createUniform("projectionMatrix");
        MeshRenderer renderer = new MeshRenderer();
        renderer.setShader(shader);
        Mesh mesh = new Mesh(new float[] {
            0.0f,  0.5f, 0f,
            -0.5f, -0.5f, 0f,
            0.5f, -0.5f, 0f
        });
        renderer.setMesh(mesh);
        while (!this.window.isClosed())
        {
            this.window.nextFrame();
        }
        // non-relevant code
    }

    // entry point
    public static void main(String[] args)
    {
        new Instance(800, 600, "Test").run();
    }
}

网格渲染器:

package flaff.gameengine.renderers;

import flaff.gameengine.Application;
import flaff.gameengine.Mesh;
import flaff.utils.geometry.Mathg;
import flaff.utils.geometry.matrix.Matrix4x4f;

public class MeshRenderer extends Renderer
{
    private Mesh mesh;
    private Matrix4x4f perspectiveMatrix;
    
    public MeshRenderer() 
    {
        super();
    }
    
    public void setMesh(Mesh value)
    {
        this.mesh = value;
    }
    
    @Override
    public void render()
    {
        float aspectRatio = (float)Application.getWidth() / Application.getHeight();
        float fov = Mathg.DEG2RAD * 90F;
        float zNear = 0.001F;
        float zFar = 1000F;
        
        if (this.perspectiveMatrix == null)
            this.perspectiveMatrix = Matrix4x4f.createPerspectiveFieldOfView(aspectRatio, fov, zNear, zFar);
        
        shader.bind();
        
        shader.setUniform("projectionMatrix", this.perspectiveMatrix);
        
        glBindVertexArray(mesh.getVaoId());
        glEnableVertexAttribArray(0);
        
        glDrawArrays(GL_TRIANGLES, 0, mesh.getVertexCount());

        // Restore state
        glDisableVertexAttribArray(0);
        glBindVertexArray(0);
        
        shader.unbind();
    }
}

渲染器类:

package flaff.gameengine.renderers;

import flaff.gameengine.Behaviour;
import flaff.gameengine.SerializeField;
import flaff.gameengine.internal.Shader;

public abstract class Renderer extends Behaviour
{
    @SerializeField
    protected Shader shader;
    
    public Renderer()
    {
        super();
    }
    
    public void setShader(Shader shader)
    {
        this.shader = shader;
    }
    
    public abstract void render();
}

着色器类:

package flaff.gameengine.internal;

import static org.lwjgl.opengl.GL30.*;

import java.nio.FloatBuffer;
import java.util.HashMap;
import java.util.Map;

import org.lwjgl.system.MemoryStack;

import flaff.utils.geometry.matrix.Matrix4x4f;

public class Shader
{
    private final int programId;
    private int vertexShaderId;
    private int fragmentShaderId;
    private Map<String, Integer> uniforms;
    
    public Shader(String vertexShader, String fragmentShader)
    {
        this.uniforms = new HashMap<String, Integer>();
        
        this.programId = glCreateProgram();
        if (programId == 0)
            throw new RuntimeException("FML");
        
        this.vertexShaderId = this.createShader(vertexShader, GL_VERTEX_SHADER);
        this.fragmentShaderId = this.createShader(fragmentShader, GL_FRAGMENT_SHADER);
        
        this.link();
    }
    
    private int createShader(String shaderCode, int shaderType)
    {
        int shaderId = glCreateShader(shaderType);
        if (shaderId == 0)
            throw new RuntimeException("Error creating a shader. FML: " + shaderType);
        
        glShaderSource(shaderId, shaderCode);
        glCompileShader(shaderId);
        
        if (glGetShaderi(shaderId, GL_COMPILE_STATUS) == 0)
            throw new RuntimeException("Error compiling Shader code: " + glGetShaderInfoLog(shaderId, 1024));
        
        glAttachShader(this.programId, shaderId);
        
        return shaderId;
    }
    
    public void link()
    {
        glLinkProgram(this.programId);
        if (glGetProgrami(this.programId, GL_LINK_STATUS) == 0)
            throw new RuntimeException("Error linking Shader code: " + glGetProgramInfoLog(programId, 1024));
        
        if (this.vertexShaderId != 0)
            glDetachShader(this.programId, this.vertexShaderId);
        
        if (this.fragmentShaderId != 0)
            glDetachShader(programId, this.fragmentShaderId);
        
        glValidateProgram(this.programId);
        if (glGetProgrami(this.programId, GL_VALIDATE_STATUS) == 0)
            System.err.println("Warning validating Shader code: " + glGetProgramInfoLog(programId, 1024));
    }
    
    public void bind()
    {
        glUseProgram(this.programId);
    }
    
    public void unbind()
    {
        glUseProgram(0);
    }
    
    public void cleanup()
    {
        unbind();
        if (this.programId != 0)
            glDeleteProgram(this.programId);
    }
    
    public void createUniform(String uniformName)
    {
        int uniformLocation = glGetUniformLocation(this.programId, uniformName);
        if (uniformLocation < 0)
            throw new RuntimeException("Could not find uniform: " + uniformName);
        
        uniforms.put(uniformName, uniformLocation);
    }
    
    public void setUniform(String uniformName, Matrix4x4f matrix)
    {
        try (MemoryStack stack = MemoryStack.stackPush())
        {
            FloatBuffer fb = stack.mallocFloat(16);
            matrix.updateBuffer(fb);
            
            glUniformMatrix4fv(uniforms.get(uniformName), false, fb);
        }
    }
    
    public static Shader getDefault()
    {
        return new Shader(
                  "#version 330\n"
                + "\n"
                + "\n"
                + "layout (location=0) in vec3 position;\n"
                + "\n"
                + "uniform mat4 projectionMatrix;\n"
                + "\n"
                + "void main()\n"
                + "{\n"
                + "    gl_Position = projectionMatrix * vec4(position, 1.0);\n"
                + "}", 
    
                  "#version 330\n"
                + "\n"
                + "\n"
                + "out vec4 fragColor;\n"
                + "\n"
                + "void main()\n"
                + "{\n"
                + "    fragColor = vec4(0.5, 0.5, 0.5, 1.0);\n"
                + "}");
    }
}

网格类别:

package flaff.gameengine;

import java.nio.FloatBuffer;
import static org.lwjgl.opengl.GL30.*;
import org.lwjgl.system.MemoryUtil;

public class Mesh extends GObject
{
    private final int vaoId;

    private final int vboId;

    private final int vertexCount;

    public Mesh(float[] positions) //TODO: Implement indices
    {
        FloatBuffer verticesBuffer = null;
        verticesBuffer = MemoryUtil.memAllocFloat(positions.length);
        vertexCount = positions.length / 3;
        verticesBuffer.put(positions).flip();

        vaoId = glGenVertexArrays();
        glBindVertexArray(vaoId);

        vboId = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboId);
        glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);          
        glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
        MemoryUtil.memFree(verticesBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        glBindVertexArray(0);
    }

    public int getVaoId() 
    {
        return vaoId;
    }

    public int getVertexCount() 
    {
        return vertexCount;
    }

    private void onDestroy() 
    {
        glDisableVertexAttribArray(0);

        // Delete the VBO
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glDeleteBuffers(vboId);

        // Delete the VAO
        glBindVertexArray(0);
        glDeleteVertexArrays(vaoId);
    }
}
拉比德76

几何形状(三角形)被“透视投影”的近平面裁剪使用透视投影时,观看的视域就是一个视域裁剪不在视区中,不在近平面和远平面之间的所有几何形状。

您的几何形状(三角形)以(z = 0.0)绘制。Near窗格为0.001,dat平面为1000。因此,将剪切几何图形。沿负z轴移动几何:

Mesh mesh = new Mesh(new float[] {
     0.0f,  0.5f, -5.0f,
    -0.5f, -0.5f, -5.0f,
     0.5f, -0.5f, -5.0f
 });

注意,视图空间坐标系是惯用右手的系统。x轴指向右侧,y轴指向上方。因此,z轴指向视口之外。要在近平面和远平面之间移动几何图形,必须沿z轴在负方向上移动几何图形。
投影矩阵从视图空间转换为剪辑空间。剪辑空间是一个惯用的系统。为了从左手系统转换为右手系统,投影矩阵会“翻转” z轴。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在SPIR-V Shader中的OpenGL中设置sampler2D制服?

来自分类Dev

OpenGL减少制服的使用

来自分类Dev

是否可以安全地假设未设置的sampler2D制服将读取纹理单元0?

来自分类Dev

OpenGL对于存在且已使用的制服返回-1

来自分类Dev

(OpenGL)上下文丢失后是否保留制服?

来自分类Dev

OpenGL LWJGL无效的枚举

来自分类Dev

OpenGL LWJGL无效的枚举

来自分类Dev

lwjgl(opengl)纹理被涂抹

来自分类Dev

WebGL-用于设置属性/制服的WHERE示例

来自分类Dev

在LWJGL中使用OpenGL的方法

来自分类Dev

在LWJGL中使用OpenGL的方法

来自分类Dev

OpenGL索引和位置对于制服和顶点属性是否相同?

来自分类Dev

OpenGL-切换着色器和制服是否经常成为严重的瓶颈?

来自分类Dev

OpenGL ES-未找到矢量制服-仅矩阵有效

来自分类Dev

OpenGL 如何知道将哪个图像单元附加到制服上?

来自分类Dev

CSS:未设置和“未设置”的区别

来自分类Dev

将C ++ OpenGL转换为Java / LWJGL

来自分类Dev

使用lwjgl在现代OpenGL中绘制纹理

来自分类Dev

加载纹理-使用OpenGl,LWJGL和SlickUtil

来自分类Dev

在LWJGL中使用什么OpenGL版本?

来自分类Dev

OpenGL在移动窗口LWJGL时停止绘制

来自分类Dev

LWJGL Opengl着色器错误

来自分类Dev

itextsharp SetFields未设置

来自分类Dev

“未设置对象”错误

来自分类Dev

Laravel邮件未设置

来自分类Dev

未设置本地Cookie

来自分类Dev

未设置对象引用

来自分类Dev

未设置NSURLSessionConfiguration HTTPAdditionalHeaders

来自分类Dev

未设置HOME变量