public mat4(vec4 a, vec4 b, vec4 c, vec4 d) { this.col0 = a; this.col1 = b; this.col2 = c; this.col3 = d; }
public vec4(vec4 v) { this.x = v.x; this.y = v.y; this.z = v.z; this.w = v.w; }
/// <summary> /// Initializes a new instance of the <see cref="mat4"/> struct. /// The matrix is initialised with the <paramref name="cols"/>. /// </summary> /// <param name="cols">The colums of the matrix.</param> public mat4(vec4[] cols) { this.col0 = cols[0]; this.col1 = cols[1]; this.col2 = cols[2]; this.col3 = cols[3]; }
public override string ToString() { var builder = new System.Text.StringBuilder(); var cols = new vec4[] { col0, col1, col2, col3 }; for (int i = 0; i < cols.Length; i++) { builder.Append(cols[i]); builder.Append(" + "); } return builder.ToString(); }
public float dot(vec4 rhs) { var result = this.x * rhs.x + this.y * rhs.y + this.z * rhs.z + this.w * rhs.w; return result; }
private void InitBuffer() { BufferName = new uint[2]; GL.GenBuffers(BufferName.Length, BufferName); int[] UniformBufferOffset = new int[1]; GL.GetInteger(GetTarget.UniformBufferOffsetAlignment, UniformBufferOffset); int mat4Size = Marshal.SizeOf(typeof(mat4)); int UniformBlockSize = Math.Max(mat4Size, UniformBufferOffset[0]); GL.BindBuffer(BufferTarget.UniformBuffer, BufferName[1]); var buffer = new UnmanagedArray<byte>(UniformBlockSize); GL.BufferData(BufferTarget.UniformBuffer, buffer, BufferUsage.DynamicDraw); GL.BindBuffer(BufferTarget.UniformBuffer, 0); transformBuffer = new uint[1]; GL.GenBuffers(transformBuffer.Length, transformBuffer); GL.BindBuffer(BufferTarget.ArrayBuffer, transformBuffer[0]); UnmanagedArray<vec4> positionData = new UnmanagedArray<vec4>(6); positionData[0] = new vec4(-1.0f, -1.0f, 0.0f, 1.0f); positionData[1] = new vec4(1.0f, -1.0f, 0.0f, 1.0f); positionData[2] = new vec4(1.0f, 1.0f, 0.0f, 1.0f); positionData[3] = new vec4(1.0f, 1.0f, 0.0f, 1.0f); positionData[4] = new vec4(-1.0f, 1.0f, 0.0f, 1.0f); positionData[5] = new vec4(-1.0f, -1.0f, 0.0f, 1.0f); GL.BufferData(BufferTarget.ArrayBuffer, positionData, BufferUsage.StaticDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); feedBackBuffer = new uint[1]; GL.GenBuffers(feedBackBuffer.Length, feedBackBuffer); GL.BindBuffer(BufferTarget.ArrayBuffer, feedBackBuffer[0]); UnmanagedArray<vec4> tmp = new UnmanagedArray<vec4>(2 * 6); GL.BufferData(BufferTarget.ArrayBuffer, tmp, BufferUsage.StaticDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
protected override void DoRender(RenderEventArgs e) { if (start_ticks == 0) { start_ticks = TimerHelper.GetTickCount(); last_ticks = TimerHelper.GetTickCount(); } uint current_ticks = TimerHelper.GetTickCount(); const float factor = 0xFFFFF; float time = ((start_ticks - current_ticks) & 0xFFFFF) / factor;// *1.0f / 0.075f; float delta_time = (float)(current_ticks - last_ticks) * 0.075f; IntPtr attractors = GL.MapBufferRange(GL.GL_UNIFORM_BUFFER, 0, 32 * Marshal.SizeOf(typeof(vec4)), GL.GL_MAP_WRITE_BIT | GL.GL_MAP_INVALIDATE_BUFFER_BIT); unsafe { vec4* array = (vec4*)attractors.ToPointer(); for (int i = 0; i < 32; i++) { array[i] = new vec4( (float)Math.Sin(time * (float)(i + 4) * 7.5f * 20.0f) * 50.0f, (float)Math.Cos(time * (float)(i + 7) * 3.9f * 20.0f) * 50.0f, (float)(Math.Sin(time * (float)(i + 3) * 5.3f * 20.0f) * Math.Cos(time * (float)(i + 5) * 9.1f) * 100.0f), attractor_masses[i] ); } } GL.UnmapBuffer(GL.GL_UNIFORM_BUFFER); // If dt is too large, the system could explode, so cap it to // some maximum allowed value if (delta_time >= 2.0f) { delta_time = 2.0f; } // Activate the compute program and bind the position and velocity buffers GL.UseProgram(compute_prog); //GL.BindImageTexture(0, velocity_tbo, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); GL.BindImageTexture(0, tbos[1], 0, false, 0, GL.GL_READ_WRITE, GL.GL_RGBA32F); //GL.BindImageTexture(1, position_tbo, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); GL.BindImageTexture(1, tbos[0], 0, false, 0, GL.GL_READ_WRITE, GL.GL_RGBA32F); // Set delta time GL.Uniform1(dt_location, delta_time); // Dispatch GL.DispatchCompute(PARTICLE_GROUP_COUNT, 1, 1); GL.MemoryBarrier(MemoryBarrierFlags.ShaderImageAccessBarrier); //vmath::mat4 mvp = vmath::perspective(45.0f, aspect_ratio, 0.1f, 1000.0f) * //vmath::translate(0.0f, 0.0f, -60.0f) * //vmath::rotate(time * 1000.0f, vmath::vec3(0.0f, 1.0f, 0.0f)); //int[] viewport = new int[4]; //GL.GetInteger(GetTarget.Viewport, viewport); //mat4 projection = glm.perspective((float)(45.0f * Math.PI / 180.0f), (float)viewport[2] / (float)viewport[3], 0.1f, 1000.0f); //mat4 view1 = glm.translate(mat4.identity(), new vec3(0.0f, 0.0f, -60.0f)); //mat4 view2 = glm.rotate(mat4.identity(), time * 1000.0f, new vec3(0.0f, 1.0f, 0.0f)); //mat4 mvp = projection * view1 * view2; mat4 mvp = e.Camera.GetProjectionMat4() * e.Camera.GetViewMat4(); // Clear, select the rendering program and draw a full screen quad //GL.Disable(GL.GL_DEPTH_TEST); GL.UseProgram(render_prog); GL.UniformMatrix4(0, 1, false, mvp.to_array()); GL.BindVertexArray(render_vao[0]); GL.Enable(GL.GL_BLEND); GL.BlendFunc(GL.GL_ONE, GL.GL_ONE); // GL.PointSize(2.0f); GL.DrawArrays(GL.GL_POINTS, 0, PARTICLE_COUNT); GL.Disable(GL.GL_BLEND); last_ticks = current_ticks; }
protected override void DoInitialize() { // Initialize our compute program compute_prog = GL.CreateProgram(); ShaderHelper.vglAttachShaderSource(compute_prog, ShaderType.ComputerShader, compute_shader_source); GL.LinkProgram(compute_prog); dt_location = GL.GetUniformLocation(compute_prog, "dt"); GL.GenVertexArrays(1, render_vao); GL.BindVertexArray(render_vao[0]); GL.GenBuffers(2, buffers); { GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[0]);//position buffer UnmanagedArray<vec4> tmp = new UnmanagedArray<vec4>(PARTICLE_COUNT); GL.BufferData(BufferTarget.ArrayBuffer, tmp, BufferUsage.DynamicCopy); tmp.Dispose(); IntPtr positions = GL.MapBufferRange(GL.GL_ARRAY_BUFFER, 0, PARTICLE_COUNT * Marshal.SizeOf(typeof(vec4)), GL.GL_MAP_WRITE_BIT | GL.GL_MAP_INVALIDATE_BUFFER_BIT); unsafe { vec4* array = (vec4*)positions.ToPointer(); for (int i = 0; i < PARTICLE_COUNT; i++) { array[i] = new vec4(Vec3Helper.GetRandomVec3(), (float)random.NextDouble()); } } GL.UnmapBuffer(BufferTarget.ArrayBuffer); GL.VertexAttribPointer(0, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero); GL.EnableVertexAttribArray(0); } { GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[1]);// velocity buffer UnmanagedArray<vec4> tmp = new UnmanagedArray<vec4>(PARTICLE_COUNT); GL.BufferData(BufferTarget.ArrayBuffer, tmp, BufferUsage.DynamicCopy); tmp.Dispose(); IntPtr velocities = GL.MapBufferRange(GL.GL_ARRAY_BUFFER, 0, PARTICLE_COUNT * Marshal.SizeOf(typeof(vec4)), GL.GL_MAP_WRITE_BIT | GL.GL_MAP_INVALIDATE_BUFFER_BIT); unsafe { vec4* array = (vec4*)velocities.ToPointer(); for (int i = 0; i < PARTICLE_COUNT; i++) { array[i] = new vec4(Vec3Helper.GetRandomVec3(), (float)random.NextDouble()); } } GL.UnmapBuffer(BufferTarget.ArrayBuffer); } { GL.GenTextures(2, tbos); for (int i = 0; i < 2; i++) { GL.BindTexture(GL.GL_TEXTURE_BUFFER, tbos[i]); GL.TexBuffer(GL.GL_TEXTURE_BUFFER, GL.GL_RGBA32F, buffers[i]); } } { GL.GenBuffers(1, attractor_buffer); GL.BindBuffer(BufferTarget.UniformBuffer, attractor_buffer[0]); UnmanagedArray<vec4> tmp = new UnmanagedArray<vec4>(32); GL.BufferData(BufferTarget.UniformBuffer, tmp, BufferUsage.StaticDraw); tmp.Dispose(); for (int i = 0; i < MAX_ATTRACTORS; i++) { attractor_masses[i] = 0.5f + (float)random.NextDouble() * 0.5f; } GL.BindBufferBase(TransformFeedbackBufferTarget.UniformBuffer, 0, attractor_buffer[0]); } { render_prog = GL.CreateProgram(); ShaderHelper.vglAttachShaderSource(render_prog, ShaderType.VertexShader, render_vs); ShaderHelper.vglAttachShaderSource(render_prog, ShaderType.FragmentShader, render_fs); GL.LinkProgram(render_prog); } }
private void InitVAO() { this.axisPrimitiveMode = DrawMode.QuadStrip; GLColor[] colors = this.ColorPalette.Colors; float[] coords = this.ColorPalette.Coords; this.numbers = new PointSpriteStringElement[coords.Length]; this.vertexCount = coords.Length * 2; this.vao = new uint[1]; float coordLength = coords[coords.Length - 1] - coords[0]; { GL.GenVertexArrays(1, vao); GL.BindVertexArray(vao[0]); // Create a vertex buffer for the vertex data. { UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(this.vertexCount); positionArray[0] = new vec3(-0.5f, -0.5f, 0); positionArray[1] = new vec3(-0.5f, 0.5f, 0); for (int i = 1; i < coords.Length; i++) { float x = (coords[i] - coords[0]) / coordLength - 0.5f; positionArray[i * 2 + 0] = new vec3(x, -0.5f, 0); positionArray[i * 2 + 1] = new vec3(x, 0.5f, 0); } uint[] ids = new uint[1]; GL.GenBuffers(1, ids); GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]); GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw); GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero); GL.EnableVertexAttribArray(in_PositionLocation); positionArray.Dispose(); } // Now do the same for the colour data. { var colorArray = new UnmanagedArray<vec4>(this.vertexCount); for (int i = 0; i < colors.Length; i++) { GLColor color = colors[i]; colorArray[i * 2 + 0] = new vec4(color.R, color.G, color.B, color.A); colorArray[i * 2 + 1] = new vec4(color.R, color.G, color.B, color.A); } uint[] ids = new uint[1]; GL.GenBuffers(1, ids); GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]); GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw); GL.VertexAttribPointer(in_ColorLocation, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero); GL.EnableVertexAttribArray(in_ColorLocation); colorArray.Dispose(); } // Unbind the vertex array, we've finished specifying data for it. GL.BindVertexArray(0); } // prepare numbers { const float numberPosY = -0.6f; this.numbers[0] = new PointSpriteStringElement( this.Min.ToShortString(), new vec3(-0.5f, numberPosY, 0)); this.numbers[0].Initialize(); for (int i = 1; i < coords.Length; i++) { float x = (coords[i] - coords[0]) / coordLength - 0.5f; if (i + 1 == coords.Length) { this.numbers[i] = new PointSpriteStringElement( (this.Min + i * this.Step).ToShortString(), new vec3(x, numberPosY, 0)); } else { this.numbers[i] = new PointSpriteStringElement( this.Max.ToShortString(), new vec3(x, numberPosY, 0)); } this.numbers[i].Initialize(); } } }
protected override void DoInitialize() { render_prog = GL.CreateProgram(); ShaderHelper.vglAttachShaderSource(render_prog, ShaderType.VertexShader, render_vs); ShaderHelper.vglAttachShaderSource(render_prog, ShaderType.FragmentShader, render_fs); GL.LinkProgram(render_prog); GL.UseProgram(render_prog); view_matrix_loc = GL.GetUniformLocation(render_prog, "view_matrix"); projection_matrix_loc = GL.GetUniformLocation(render_prog, "projection_matrix"); vboObject.LoadFromVBM(@"media\armadillo_low.vbm", 0, 1, 2); // Bind its vertex array object so that we can append the instanced attributes vboObject.BindVertexArray(); // Get the locations of the vertex attributes in 'prog', which is the // (linked) program object that we're going to be rendering with. Note // that this isn't really necessary because we specified locations for // all the attributes in our vertex shader. This code could be made // more concise by assuming the vertex attributes are where we asked // the compiler to put them. int position_loc = GL.GetAttribLocation(render_prog, "position"); int normal_loc = GL.GetAttribLocation(render_prog, "normal"); int color_loc = GL.GetAttribLocation(render_prog, "color"); int matrix_loc = GL.GetAttribLocation(render_prog, "model_matrix"); // Generate the colors of the objects var colors = new UnmanagedArray<vec4>(INSTANCE_COUNT); for (int n = 0; n < INSTANCE_COUNT; n++) { float a = (float)(n) / 4.0f; float b = (float)(n) / 5.0f; float c = (float)(n) / 6.0f; colors[n] = new vec4( (float)(0.5f + 0.25f * (Math.Sin(a + 1.0f) + 1.0f)), (float)(0.5f + 0.25f * (Math.Sin(b + 2.0f) + 1.0f)), (float)(0.5f + 0.25f * (Math.Sin(c + 3.0f) + 1.0f)), (float)(1.0f) ); } GL.GenBuffers(1, color_buffer); GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer[0]); GL.BufferData(BufferTarget.ArrayBuffer, colors, BufferUsage.DynamicDraw); colors.Dispose(); // Now we set up the color array. We want each instance of our geometry // to assume a different color, so we'll just pack colors into a buffer // object and make an instanced vertex attribute out of it. GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer[0]); GL.VertexAttribPointer((uint)color_loc, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero); GL.EnableVertexAttribArray((uint)color_loc); // This is the important bit... set the divisor for the color array to // 1 to get OpenGL to give us a new value of 'color' per-instance // rather than per-vertex. GL.VertexAttribDivisor((uint)color_loc, 1); // Likewise, we can do the same with the model matrix. Note that a // matrix input to the vertex shader consumes N consecutive input // locations, where N is the number of columns in the matrix. So... // we have four vertex attributes to set up. UnmanagedArray<mat4> tmp = new UnmanagedArray<mat4>(INSTANCE_COUNT); GL.GenBuffers(1, model_matrix_buffer); GL.BindBuffer(BufferTarget.ArrayBuffer, model_matrix_buffer[0]); GL.BufferData(BufferTarget.ArrayBuffer, tmp, BufferUsage.DynamicDraw); tmp.Dispose(); // Loop over each column of the matrix... for (int i = 0; i < 4; i++) { // Set up the vertex attribute GL.VertexAttribPointer((uint)(matrix_loc + i), // Location 4, GL.GL_FLOAT, false, // vec4 Marshal.SizeOf(typeof(mat4)), // Stride new IntPtr(Marshal.SizeOf(typeof(vec4)) * i)); // Start offset // Enable it GL.EnableVertexAttribArray((uint)(matrix_loc + i)); // Make it instanced GL.VertexAttribDivisor((uint)(matrix_loc + i), 1); } // Done (unbind the object's VAO) GL.BindVertexArray(0); }
public vec3(vec4 v) { this.x = v.x; this.y = v.y; this.z = v.z; }
public static vec4 ToVec4(this float[] array, int startIndex = 0) { vec4 result = new vec4(array[startIndex], array[startIndex + 1], array[startIndex + 2], array[startIndex + 3]); return result; }
/// <summary> /// Map the specified window coordinates (win.x, win.y, win.z) into object coordinates. /// </summary> /// <param name="win">The win.</param> /// <param name="model">The model.</param> /// <param name="proj">The proj.</param> /// <param name="viewport">The viewport.</param> /// <returns></returns> public static vec3 unProject(vec3 win, mat4 model, mat4 proj, vec4 viewport) { mat4 Inverse = glm.inverse(proj * model); vec4 tmp = new vec4(win, (1f)); tmp.x = (tmp.x - (viewport[0])) / (viewport[2]); tmp.y = (tmp.y - (viewport[1])) / (viewport[3]); tmp = tmp * (2f) - new vec4(1, 1, 1, 1); vec4 obj = Inverse * tmp; obj /= obj.w; return new vec3(obj); }
/// <summary> /// Map the specified object coordinates (obj.x, obj.y, obj.z) into window coordinates. /// </summary> /// <param name="obj">The object.</param> /// <param name="model">The model.</param> /// <param name="proj">The proj.</param> /// <param name="viewport">The viewport.</param> /// <returns></returns> public static vec3 project(vec3 obj, mat4 model, mat4 proj, vec4 viewport) { vec4 tmp = new vec4(obj, (1f)); tmp = model * tmp; tmp = proj * tmp; tmp /= tmp.w; tmp = tmp * 0.5f + new vec4(0.5f, 0.5f, 0.5f, 0.5f); tmp[0] = tmp[0] * viewport[2] + viewport[0]; tmp[1] = tmp[1] * viewport[3] + viewport[1]; return new vec3(tmp.x, tmp.y, tmp.z); }
/// <summary> /// Define a picking region. /// </summary> /// <param name="center">The center.</param> /// <param name="delta">The delta.</param> /// <param name="viewport">The viewport.</param> /// <returns></returns> /// <exception cref="System.ArgumentOutOfRangeException"></exception> public static mat4 pickMatrix(vec2 center, vec2 delta, vec4 viewport) { if (delta.x <= 0 || delta.y <= 0) throw new ArgumentOutOfRangeException(); var Result = new mat4(1.0f); if (!(delta.x > (0f) && delta.y > (0f))) return Result; // Error vec3 Temp = new vec3( ((viewport[2]) - (2f) * (center.x - (viewport[0]))) / delta.x, ((viewport[3]) - (2f) * (center.y - (viewport[1]))) / delta.y, (0f)); // Translate and scale the picked region to the entire window Result = translate(Result, Temp); return scale(Result, new vec3((viewport[2]) / delta.x, (viewport[3]) / delta.y, (1))); }