пытаюсь вывести через OpenGL фрактал Джулия, вроде бы все правильно написал, но у меня вообще ничего не рисует, уже 3 дня читаю разбираюсь. Помогите пж.
P.S. я знаю что есть юнити т.д. но лабу надо сделать на голом opengl
Program.cs
using FractalJulia.ViewModel;
namespace FractalJulia
{
internal class Program
{
static void Main(string[] args)
{
VM viewModel = new VM();
viewModel.Start();
}
}
}
FractalJulia.CS
using FractalJulia.Models.OpenGLEngin;
using System;
namespace FractalJulia.Models.Fractals
{
public class FractalJulia
{
public FractalJulia()
{
}
public void DrawFractalJulia(float startX, float startY, float endX, float endY, OpenGLStart opengl)
{
if (opengl is null)
{
throw new ArgumentNullException(nameof(opengl));
}
float cRe, cIm;
float newRe, newIm, oldRe, oldIm;
float zoom = 1.0f, moveX = 0f, moveY = 0f;
int maxIterations = 300;
cRe = -0.70176f;
cIm = -0.3842f;
for (float x = startX; x < endX; x += 0.001f)
{
for (float y = startY; y < endY; y += 0.001f)
{
newRe = 1.5f * (x - endX / 2.0f) / (0.5f * zoom * endX) + moveX;
newIm = (y - endY / 2.0f) / (0.5f * zoom * endY) + moveY;
int i;
for (i = 0; i < maxIterations; i++)
{
oldRe = newRe;
oldIm = newIm;
newRe = oldRe * oldRe - oldIm * oldIm + cRe;
newIm = 2.0f * oldRe * oldIm + cIm;
if ((newRe * newRe + newIm * newIm) > 4) break;
}
opengl.DrawPoint(x, y, 0.2f, 0.4f, 0.22f, 1.0f);
}
}
}
}
}
OpenGLStart.CS
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Mathematics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Windowing.GraphicsLibraryFramework;
using System.Collections.Generic;
using System.Drawing;
namespace FractalJulia.Models.OpenGLEngin
{
public class OpenGLStart : GameWindow
{
private List<float> verticesColors;
private float[] vertices_colors;
private int _vertexArrayObjectId = 0;
private ShaderProgram shaderProgram;
public OpenGLStart(GameWindowSettings gameWindowSettings,
NativeWindowSettings nativeWindowSettings)
: base(gameWindowSettings, nativeWindowSettings)
{
verticesColors = new List<float>();
VSync = VSyncMode.On;
}
public void DrawPoint(float x, float y, float r, float g, float b, float alfa)
{
verticesColors.Add(x);
verticesColors.Add(y);
verticesColors.Add(r);
verticesColors.Add(g);
verticesColors.Add(b);
verticesColors.Add(alfa);
}
private void ConvertListToArray()
{
vertices_colors = verticesColors.ToArray();
}
protected override void OnRenderFrame(FrameEventArgs args)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
DrawVertexArrayObjectShader();
SwapBuffers();
base.OnRenderFrame(args);
}
protected override void OnUpdateFrame(FrameEventArgs args)
{
var key = KeyboardState;
if (key.IsKeyDown(Keys.Escape))
{
Close();
}
base.OnUpdateFrame(args);
}
protected override void OnLoad()
{
ConvertListToArray();
GL.ClearColor(Color4.Indigo);
GL.PointSize(3.0f);
shaderProgram = new ShaderProgram(@"Models\OpenGLEngin\shaders\shader_base.vert",
@"Models\OpenGLEngin\shaders\shader_base.frag");
_vertexArrayObjectId = CreateVAOShaders();
base.OnLoad();
}
protected override void OnUnload()
{
DeleteVAOShaders();
base.OnUnload();
}
private void DrawVertexArrayObjectShader()
{
shaderProgram.ActiveProgram();
GL.BindVertexArray(_vertexArrayObjectId);
GL.DrawArrays(PrimitiveType.Points,0,2);
shaderProgram.DeleteProgram();
}
private int CreateVBO(float[] data)
{
int vbo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, data.Length * sizeof(float), data, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
return vbo;
}
private int CreateVAOShaders()
{
int vao = GL.GenVertexArray();
GL.BindVertexArray(vao);
int vboVC = CreateVBO(vertices_colors);
int VertexArray = shaderProgram.GetAttribProgram("aPosition");
int ColorArray = shaderProgram.GetAttribProgram("aColor");
GL.EnableVertexAttribArray(VertexArray);
GL.EnableVertexAttribArray(ColorArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, vboVC);
GL.VertexAttribPointer(VertexArray, 2, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);
GL.VertexAttribPointer(ColorArray, 4, VertexAttribPointerType.Float, false, 6 * sizeof(float), 2 * sizeof(float));
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DisableVertexAttribArray(VertexArray);
GL.DisableVertexAttribArray(ColorArray);
return vao;
}
private void DeleteVAOShaders()
{
GL.BindVertexArray(0);
GL.DeleteVertexArray(_vertexArrayObjectId);
}
}
}
ShaderProgram.CS
using System;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using System.IO;
namespace FractalJulia.Models.OpenGLEngin
{
public class ShaderProgram
{
private readonly int _vertexShader = 0;
private readonly int _fragmentShader = 0;
private readonly int _program = 0;
public ShaderProgram(string vertexfile, string fragmentfile)
{
_vertexShader = CreateShader(ShaderType.VertexShader, vertexfile);
_fragmentShader = CreateShader(ShaderType.FragmentShader, fragmentfile);
_program = GL.CreateProgram();
GL.AttachShader(_program, _vertexShader);
GL.AttachShader(_program, _fragmentShader);
GL.LinkProgram(_program);
GL.GetProgram(_program, GetProgramParameterName.LinkStatus, out var code);
if (code != (int)All.True)
{
var infoLog = GL.GetProgramInfoLog(_program);
throw new Exception($"Ошибка линковки шейдерной программы № {_program} \n\n {infoLog}");
}
DeleteShader(_vertexShader);
DeleteShader(_fragmentShader);
}
public void ActiveProgram() => GL.UseProgram(_program);
public void DeactiveProgram() => GL.UseProgram(0);
public void DeleteProgram() => GL.DeleteProgram(_program);
public int GetAttribProgram(string name) => GL.GetAttribLocation(_program, name);
public void SetUniform4(string name, Vector4 vec)
{
int location = GL.GetUniformLocation(_program, name);
GL.Uniform4(location, vec);
}
private int CreateShader(ShaderType shaderType, string shaderFile)
{
string shaderStr = File.ReadAllText(shaderFile);
int shaderID = GL.CreateShader(shaderType);
GL.ShaderSource(shaderID, shaderStr);
GL.CompileShader(shaderID);
GL.GetShader(shaderID, ShaderParameter.CompileStatus, out var code);
if (code != (int)All.True)
{
var infoLog = GL.GetShaderInfoLog(shaderID);
throw new Exception($"Ошибка прикомпиляции шейдера № {shaderID} \n\n {infoLog}");
}
return shaderID;
}
private void DeleteShader(int shader)
{
GL.DetachShader(_program, shader);
GL.DeleteShader(shader);
}
}
}
VM.cs
using FractalJulia.Models.OpenGLEngin;
using System;
using OpenTK.Windowing.Desktop;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
namespace FractalJulia.ViewModel
{
public class VM
{
private OpenGLStart openGL;
private Models.Fractals.FractalJulia fractal;
private NativeWindowSettings nativeWinSettings;
public VM()
{
nativeWinSettings = new NativeWindowSettings()
{
Size = new Vector2i(800, 600),
Location = new Vector2i(370, 300),
WindowBorder = WindowBorder.Resizable,
WindowState = WindowState.Normal,
// Flags = ContextFlags.ForwardCompatible,
Flags = ContextFlags.Default,
APIVersion = new Version(3, 3),
Profile = ContextProfile.Compatability,
// Profile = ContextProfile.Core,
API = ContextAPI.OpenGL,
};
openGL = new OpenGLStart(GameWindowSettings.Default,
nativeWinSettings);
fractal = new Models.Fractals.FractalJulia();
}
public void Start()
{
fractal.DrawFractalJulia(-1.0f, -1.2f, 1.0f, 1.2f, openGL);
openGL.Run();
}
}
}
shader_base.vert
#version 330 core
in vec3 aPosition;
in vec4 aColor;
out vec4 inColorFraf;
uniform vec4 color;
void main()
{
inColorFraf = aColor;
gl_Position = vec4(aPosition, 1.0);
}
shader_base.frag
#version 330 core
in vec4 inColorFraf;
out vec4 outColor;
void main()
{
outColor = inColorFraf;
}