OpenGL(带有OpenTK)不显示正方形

3j86kqsm  于 2022-10-18  发布在  其他
关注(0)|答案(1)|浏览(153)

我正在使用下面的代码在3D环境中显示多个带纹理的正方形,但没有显示一个正方形(OpenGL不输出任何错误),但我可以看到我的背景-颜色:
顶点明暗器:


# version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in mat4 aInstanceMatrix;

out vec2 TexCoords;

uniform mat4 projection;
uniform mat4 view;

void main()
{
    TexCoords = aTexCoords;
    gl_Position = projection * view * aInstanceMatrix * vec4(aPos, 1.0f); 
}

片段着色器:


# version 330 core

out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D texture;

void main()
{
    FragColor = texture(texture, TexCoords);
}

出票人代码:

class Drawer
    {
        private static float[] SquareVertices = new float[]
        {
            //aPos               //aTexCoords
            -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
             0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
             0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
            -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
            -0.5f, -0.5f, -0.5f,  0.0f, 0.0f
        };

        public static unsafe void DrawSquares(Shader shader, Camera camera, Texture texture, List<Matrix4> instanceMatrices)
        {
            //Configure instanced array
            Matrix4[] instanceMatricesArray = instanceMatrices.ToArray();
            int instancedVBO = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, instancedVBO);
            GL.BufferData(BufferTarget.ArrayBuffer, instanceMatricesArray.Length, instanceMatricesArray, BufferUsageHint.StaticDraw);

            int squareVAO = GL.GenVertexArray();

            // set transformation matrices as an instance vertex attribute
            GL.BindVertexArray(squareVAO);
            // set attribute pointers for matrix (4 times vec4)
            GL.EnableVertexAttribArray(3);
            GL.VertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, sizeof(Matrix4), 0);
            GL.EnableVertexAttribArray(4);
            GL.VertexAttribPointer(4, 4, VertexAttribPointerType.Float, false, sizeof(Matrix4), sizeof(Vector4));
            GL.EnableVertexAttribArray(5);
            GL.VertexAttribPointer(5, 4, VertexAttribPointerType.Float, false, sizeof(Matrix4), 2 * sizeof(Vector4));
            GL.EnableVertexAttribArray(6);
            GL.VertexAttribPointer(6, 4, VertexAttribPointerType.Float, false, sizeof(Matrix4), 3 * sizeof(Vector4));

            GL.VertexAttribDivisor(3, 1);
            GL.VertexAttribDivisor(4, 1);
            GL.VertexAttribDivisor(5, 1);
            GL.VertexAttribDivisor(6, 1);

            GL.BindVertexArray(0);

            //Set uniforms
            shader.Use();
            shader.SetMatrix4("projection", camera.GetProjectionMatrix());
            shader.SetMatrix4("view", camera.GetViewMatrix());
            shader.SetInt("texture", 0);
            texture.Use();

            GL.BindVertexArray(squareVAO);
            GL.DrawElementsInstanced(PrimitiveType.Triangles, SquareVertices.Length, DrawElementsType.UnsignedInt, SquareVertices, instanceMatricesArray.Length);
        }
    }

我已经测试了纹理被成功加载,并且着色器程序(这里是Shader类)编译没有错误。我还确保了纹理被正确创建,并且可以通过TextureUnit0中的Texture.Use()使用。在我的示例中,instanceMatches只包含一个标识矩阵。如果有人需要,这是我相机的代码,从0 0 0开始:

public class Camera
{
    private Vector3 _front = -Vector3.UnitZ;
    private Vector3 _up = Vector3.UnitY;
    private Vector3 _right = Vector3.UnitX;

    private float _pitch;
    private float _yaw = -MathHelper.PiOver2;
    private float _fov = MathHelper.PiOver2;

    public Camera(Vector3 position, float aspectRatio)
    {
        Position = position;
        AspectRatio = aspectRatio;
    }

    public Vector3 Position { get; set; }
    public float AspectRatio { private get; set; }

    public Vector3 Front => _front;
    public Vector3 Up => _up;
    public Vector3 Right => _right;

    public float Pitch
    {
        get => MathHelper.RadiansToDegrees(_pitch);
        set
        {
            var angle = MathHelper.Clamp(value, -89f, 89f);
            _pitch = MathHelper.DegreesToRadians(angle);
            UpdateVectors();
        }
    }

    public float Yaw
    {
        get => MathHelper.RadiansToDegrees(_yaw);
        set
        {
            _yaw = MathHelper.DegreesToRadians(value);
            UpdateVectors();
        }
    }

    public float Fov
    {
        get => MathHelper.RadiansToDegrees(_fov);
        set
        {
            var angle = MathHelper.Clamp(value, 1f, 45f);
            _fov = MathHelper.DegreesToRadians(angle);
        }
    }

    public Matrix4 GetViewMatrix()
    {
        return Matrix4.LookAt(Position, Position + _front, _up);
    }

    public Matrix4 GetProjectionMatrix()
    {
        return Matrix4.CreatePerspectiveFieldOfView(_fov, AspectRatio, 0.01f, 100f);
    }

    private void UpdateVectors()
    {
        _front.X = (float)Math.Cos(_pitch) * (float)Math.Cos(_yaw);
        _front.Y = (float)Math.Sin(_pitch);
        _front.Z = (float)Math.Cos(_pitch) * (float)Math.Sin(_yaw);
        _front = Vector3.Normalize(_front);

        _right = Vector3.Normalize(Vector3.Cross(_front, Vector3.UnitY));
        _up = Vector3.Normalize(Vector3.Cross(_right, _front));
    }
}
imzjd6km

imzjd6km1#

你提供的信息不足以让我知道为什么你的屏幕上什么都看不到,然而,我推荐以下几点
1.用颜色属性渲染一个三角形,这将需要你在类库的属性中创建一个着色器类,给它一个程序集名称和默认命名空间,例如LearnOpenTK.Common,如果你成功做到这一点
1.创建着色器和摄影机类的示例,但这将需要访问已创建的命名空间,因此需要使用LearnOpenTK.Common;作为命名空间之一,以便该示例可以访问着色器和摄影机类。
1.如果仍然看不到任何内容,请转到着色器文件,然后将属性转到复制输出目录,并将其设置为始终复制。
如果这不起作用,你能提供足够的信息,关于你可以从一个基本的三角形看到什么,然后尝试变换你的纹理对象。
有关更多信息,请参阅https://opentk.net/

相关问题