protected override void OnLoad(EventArgs e) { base.OnLoad (e); String version = GL.GetString(StringName.Version); Console.WriteLine("GL version: " + version); context = GLGraphics.NewContext(Context, true); program = context.NewProgramFromFiles("Render.vert", "Render.frag"); texture0 = context.NewTexture(TextureMinFilter.Nearest, TextureMagFilter.Nearest, TextureWrapMode.Clamp, TextureWrapMode.Clamp); texture0.UploadImage(sourceImage); sourceImage.Dispose(); texture1 = context.NewTexture(TextureMinFilter.Nearest, TextureMagFilter.Nearest, TextureWrapMode.Clamp, TextureWrapMode.Clamp); texture1.UploadImage(new byte[4 * 4 * 4], 4, 4, PixelInternalFormat.Four, PixelFormat.Rgba, PixelType.UnsignedByte); CreateAndFillBuffers(); paramSpec = ImmutableList<Tuple<String, Object>>.List(new Tuple<String, Object>[] { Tuple.Create<String, Object>("a_position", positions), Tuple.Create<String, Object>("a_texPos", textureCoords), Tuple.Create<String, Object>("u_texture0", texture0), Tuple.Create<String, Object>("u_texture1", texture1)}); }
public GLTexture Process(GLTexture Input) { Filter.SetSize(Input.Width * 2, Input.Height * 2); return Filter.Process((_Shader) => { _Shader.GetUniform("OGL2Texture").Set(GLTextureUnit.CreateAtIndex(0).SetWrap(GLWrap.ClampToEdge).SetFiltering(GLScaleFilter.Nearest).SetTexture(Input)); }); }
public GlyphTextureCache() { _glyphCoords = new Dictionary<char, GlyphInfo>(); _texture = new GLTexture(); _bitmap = new Bitmap(512, 512); _lastTextureCoord = new Point(1, 1); _lastGlyphCount = 0; _maxY = 1; }
public TextRenderer(GLGraphicsContext context, int height, int width, String text) { this.text = text; texture = context.NewEmptyTexture(OpenTK.Graphics.OpenGL.TextureMinFilter.Linear, OpenTK.Graphics.OpenGL.TextureMagFilter.Linear, OpenTK.Graphics.OpenGL.TextureWrapMode.ClampToBorder, OpenTK.Graphics.OpenGL.TextureWrapMode.ClampToBorder, width, height, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, OpenTK.Graphics.OpenGL.PixelType.UnsignedByte); texRenderer = new TextureRenderer(context, texture); Resize (width, height); }
public GLTexture Process(GLTexture InputColor, GLTexture InputDepth) { if (InputColor.Width == 0 || InputColor.Height == 0) throw (new Exception("Smaa can't handle empty textures")); //if (InputColor.Width != InputDepth.Width || InputColor.Height != InputDepth.Height) throw (new Exception("Color.Size != Texture.Size")); SetSize(InputColor.Width, InputColor.Height); var edge_tex = edge_pass(InputColor, InputDepth); var blend_tex = pass_blend(edge_tex); var neighborhood_tex = pass_neighborhood(InputColor, blend_tex); //return edge_tex; //return blend_tex; return neighborhood_tex; }
private void GetTexVram(Action<TexturePair> Action) { if (TexVram == null) { TexVram = GLTexture.Create().SetFormat(TextureFormat.RGBA).SetSize(1, 1); } //Console.WriteLine(TexVram); TexVram.Bind(); if (BufferGraphics == null) { BufferGraphics = Graphics.FromImage(Buffer); //BufferGraphics.Clear(Color.Red); BufferGraphics.Clear(Color.Black); } //if (PspDisplayForm.Singleton.WindowState == FormWindowState.Minimized) //{ // return; //} // //if (!PspDisplayForm.Singleton.EnableRefreshing) //{ // return; //} if (IGuiWindowInfo.EnableRefreshing) { try { int Width = 512; int Height = 272; var FrameAddress = PspDisplay.CurrentInfo.FrameAddress; byte* FrameBuffer = null; byte* DepthBuffer = null; try { FrameBuffer = (byte*)Memory.PspAddressToPointerSafe( FrameAddress, PixelFormatDecoder.GetPixelsSize(PspDisplay.CurrentInfo.PixelFormat, Width * Height) ); } catch (Exception Exception) { Console.Error.WriteLine(Exception); } //Console.Error.WriteLine("FrameBuffer == 0x{0:X}!!", (long)FrameBuffer); if (FrameBuffer == null) { //Console.Error.WriteLine("FrameBuffer == null!!"); } //Console.WriteLine("{0:X}", Address); var Hash = PixelFormatDecoder.Hash( PspDisplay.CurrentInfo.PixelFormat, (void*)FrameBuffer, Width, Height ); if (Hash != LastHash) { LastHash = Hash; Buffer.LockBitsUnlock(System.Drawing.Imaging.PixelFormat.Format32bppArgb, (BitmapData) => { var Count = Width * Height; fixed (OutputPixel* BitmapDataDecodePtr = BitmapDataDecode) { var BitmapDataPtr = (BGRA*)BitmapData.Scan0.ToPointer(); //var LastRow = (FrameBuffer + 512 * 260 * 4 + 4 * 10); //Console.WriteLine("{0},{1},{2},{3}", LastRow[0], LastRow[1], LastRow[2], LastRow[3]); if (FrameBuffer == null) { if (OldFrameBuffer != null) { Console.Error.WriteLine("FrameBuffer == null"); } } else if (BitmapDataPtr == null) { Console.Error.WriteLine("BitmapDataPtr == null"); } else { PixelFormatDecoder.Decode( PspDisplay.CurrentInfo.PixelFormat, (void*)FrameBuffer, BitmapDataDecodePtr, Width, Height ); } // Converts the decoded data to Window's format. for (int n = 0; n < Count; n++) { BitmapDataPtr[n].R = BitmapDataDecodePtr[n].B; BitmapDataPtr[n].G = BitmapDataDecodePtr[n].G; BitmapDataPtr[n].B = BitmapDataDecodePtr[n].R; BitmapDataPtr[n].A = 0xFF; } OldFrameBuffer = FrameBuffer; GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 512, 272, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, BitmapDataPtr); TextureVerticalFlip = true; } }); } //else { //Console.WriteLine("Display not updated!"); } } catch (Exception Exception) { Console.Error.WriteLine(Exception); } } Action(new TexturePair() { Color = TexVram, Depth = GLTexture.Wrap(0) }); return; }
void BindTextureVertex(ShaderProgram shader, GLTexture texture, int slot, int id) { GL.ActiveTexture(TextureUnit.Texture0 + id); texture.Bind(); shader.SetInt($"{ConvertSamplerID(slot, true)}", id); }
private unsafe void Load(int index, int program, PLT0Node palette) { if (_context == null) { return; } Source = null; if (Texture != null) { Texture.Delete(); } Texture = new GLTexture(_context, 0, 0); Texture.Bind(index, program); //ctx._states[String.Format("{0}_TexRef", Name)] = Texture; Bitmap bmp = null; TEX0Node tNode = null; if (_context._states.ContainsKey("_Node_Refs")) { List <ResourceNode> nodes = _context._states["_Node_Refs"] as List <ResourceNode>; List <ResourceNode> searched = new List <ResourceNode>(nodes.Count); foreach (ResourceNode n in nodes) { ResourceNode node = n.RootNode; if (searched.Contains(node)) { continue; } searched.Add(node); //Search node itself first if ((tNode = node.FindChild("Textures(NW4R)/" + Name, true) as TEX0Node) != null) { Source = tNode; if (palette != null) { bmp = tNode.GetImage(0, palette); } else { bmp = tNode.GetImage(0); } } else { //Then search node directory string path = node._origPath; if (path != null) { DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(path)); foreach (FileInfo file in dir.GetFiles(Name + ".*")) { if (file.Name.EndsWith(".tga")) { Source = file.FullName; bmp = TGA.FromFile(file.FullName); break; } else if (file.Name.EndsWith(".png") || file.Name.EndsWith(".tiff") || file.Name.EndsWith(".tif")) { Source = file.FullName; bmp = (Bitmap)Bitmap.FromFile(file.FullName); break; } } } } if (bmp != null) { break; } } searched.Clear(); if (bmp != null) { int w = bmp.Width, h = bmp.Height, size = w * h; Texture._width = w; Texture._height = h; //_context.glTexParameter(GLTextureTarget.Texture2D, GLTextureParameter.MagFilter, (int)GLTextureFilter.LINEAR); //_context.glTexParameter(GLTextureTarget.Texture2D, GLTextureParameter.MinFilter, (int)GLTextureFilter.NEAREST_MIPMAP_LINEAR); //_context.glTexParameter(GLTextureTarget.Texture2D, GLTextureParameter.BaseLevel, 0); //if (tNode != null) // _context.glTexParameter(GLTextureTarget.Texture2D, GLTextureParameter.MaxLevel, tNode.LevelOfDetail); //else // _context.glTexParameter(GLTextureTarget.Texture2D, GLTextureParameter.MaxLevel, 0); BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); try { using (UnsafeBuffer buffer = new UnsafeBuffer(size << 2)) { ARGBPixel *sPtr = (ARGBPixel *)data.Scan0; ABGRPixel *dPtr = (ABGRPixel *)buffer.Address; for (int i = 0; i < size; i++) { *dPtr++ = (ABGRPixel)(*sPtr++); } int res = _context.gluBuild2DMipmaps(GLTextureTarget.Texture2D, GLInternalPixelFormat._4, w, h, GLPixelDataFormat.RGBA, GLPixelDataType.UNSIGNED_BYTE, buffer.Address); if (res != 0) { } } } finally { bmp.UnlockBits(data); bmp.Dispose(); } } } }
private void RenderBackgroundImage() { GL.PushAttrib(AttribMask.AllAttribBits); GL.Disable(EnableCap.DepthTest); GL.Disable(EnableCap.Lighting); GL.Disable(EnableCap.CullFace); GL.MatrixMode(MatrixMode.Projection); GL.PushMatrix(); { GL.LoadIdentity(); Matrix p = Matrix.OrthographicMatrix(0, Region.Width, 0, Region.Height, -1, 1); GL.LoadMatrix((float *)&p); GL.MatrixMode(MatrixMode.Modelview); GL.PushMatrix(); { GL.LoadIdentity(); GL.Color4(Color.White); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Enable(EnableCap.Texture2D); if (_updateImage) { if (_bgImage != null) { _bgImage.Delete(); _bgImage = null; } GL.ClearColor(Color.Black); Bitmap bmp = BackgroundImage as Bitmap; _bgImage = new GLTexture(bmp); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); _bgImage.Bind(); _updateImage = false; } else { GL.BindTexture(TextureTarget.Texture2D, _bgImage._texId); } float *points = stackalloc float[8]; float tAspect = (float)_bgImage.Width / (float)_bgImage.Height; float wAspect = (float)Width / (float)Height; switch (_bgType) { case BGImageType.Stretch: points[0] = points[1] = points[3] = points[6] = 0.0f; points[2] = points[4] = Width; points[5] = points[7] = Height; break; case BGImageType.Center: if (tAspect > wAspect) { points[1] = points[3] = 0.0f; points[5] = points[7] = Height; points[0] = points[6] = Width * ((Width - ((float)Height / _bgImage.Height * _bgImage.Width)) / Width / 2.0f); points[2] = points[4] = Width - points[0]; } else { points[0] = points[6] = 0.0f; points[2] = points[4] = Width; points[1] = points[3] = Height * (((Height - ((float)Width / _bgImage.Width * _bgImage.Height))) / Height / 2.0f); points[5] = points[7] = Height - points[1]; } break; case BGImageType.ResizeWithBars: if (tAspect > wAspect) { points[0] = points[6] = 0.0f; points[2] = points[4] = Width; points[1] = points[3] = Height * (((Height - ((float)Width / _bgImage.Width * _bgImage.Height))) / Height / 2.0f); points[5] = points[7] = Height - points[1]; } else { points[1] = points[3] = 0.0f; points[5] = points[7] = Height; points[0] = points[6] = Width * ((Width - ((float)Height / _bgImage.Height * _bgImage.Width)) / Width / 2.0f); points[2] = points[4] = Width - points[0]; } break; } GL.Begin(BeginMode.Quads); GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(&points[0]); GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(&points[2]); GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(&points[4]); GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(&points[6]); GL.End(); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)MatTextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)MatTextureMagFilter.Linear); GL.Disable(EnableCap.Texture2D); } GL.PopMatrix(); } GL.MatrixMode(MatrixMode.Projection); GL.PopMatrix(); GL.PopAttrib(); }
public void Render(GLCamera cam, bool renderBG) { cam.LoadProjection(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Color4(Color.White); GL.Enable(EnableCap.Texture2D); float halfW = (float)Width / 2.0f, halfH = (float)Height / 2.0f; GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.MatrixMode(MatrixMode.Texture); GL.LoadIdentity(); if (renderBG) { GL.PushAttrib(AttribMask.TextureBit); GLTexture bgTex = TKContext.FindOrCreate <GLTexture>("TexBG", GLTexturePanel.CreateBG); bgTex.Bind(); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); float s = (float)Width / (float)bgTex.Width, t = (float)Height / (float)bgTex.Height; GL.Begin(BeginMode.Quads); GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-halfW, -halfH); GL.TexCoord2(s, 0.0f); GL.Vertex2(halfW, -halfH); GL.TexCoord2(s, t); GL.Vertex2(halfW, halfH); GL.TexCoord2(0.0f, t); GL.Vertex2(-halfW, halfH); GL.End(); GL.PopAttrib(); } if (Tex0 == null) { return; } Tex0.Prepare(_targetMatRef, -1); GLTexture texture = GLTex; if (texture == null || texture._texId <= 0) { return; } MDL0TextureNode.ApplyGLTextureParameters(_targetMatRef); //These are used to match up the UV overlay to the texture underneath Vector2 topLeft = new Vector2(); Vector2 bottomRight = new Vector2(); float texWidth = texture.Width; float texHeight = texture.Height; float tAspect = (float)texWidth / texHeight; float wAspect = (float)Width / Height; float[] texCoord = new float[8]; //These are used to compensate for padding added on an axis float xCorrect = 1.0f, yCorrect = 1.0f; if (tAspect > wAspect) { //Texture is wider, use horizontal fit //X touches the edges of the window, Y has top and bottom padding //X texCoord[0] = texCoord[6] = 0.0f; texCoord[2] = texCoord[4] = 1.0f; //Y texCoord[1] = texCoord[3] = (yCorrect = tAspect / wAspect) / 2.0f + 0.5f; texCoord[5] = texCoord[7] = 1.0f - texCoord[1]; bottomRight = new Vector2(halfW, (((float)Height - ((float)Width / texWidth * texHeight)) / (float)Height / 2.0f - 0.5f) * (float)Height); topLeft = new Vector2(-halfW, -bottomRight._y); } else { //Window is wider, use vertical fit //Y touches the edges of the window, X has left and right padding //Y texCoord[1] = texCoord[3] = 1.0f; texCoord[5] = texCoord[7] = 0.0f; //X texCoord[2] = texCoord[4] = (xCorrect = wAspect / tAspect) / 2.0f + 0.5f; texCoord[0] = texCoord[6] = 1.0f - texCoord[2]; bottomRight = new Vector2(1.0f - (((float)Width - ((float)Height / texHeight * texWidth)) / Width / 2.0f - 0.5f) * (float)Width, -halfH); topLeft = new Vector2(-bottomRight._x, halfH); } //Apply the texcoord bind transform first TextureFrameState state = _targetMatRef._bindState; GL.MultMatrix((float *)&state._transform); //Translate the texture coordinates to match where the user dragged the camera //Divide by width and height to convert window units (0 to w, 0 to h) to texcoord units (0 to 1) //Then multiply by the correction value if the window is bigger than the texture on an axis Vector3 point = cam.GetPoint(); GL.Translate(point._x / Width * xCorrect, -point._y / Height * yCorrect, 0); //Now to scale the texture after translating. //The scale origin is the top left of the texture on the window (not of the window itself), //so we need to translate the center of the texture to that origin, //scale it up or down, then translate it back to where it was. OpenTK.Vector3 trans = new OpenTK.Vector3(-topLeft._x / Width * xCorrect, topLeft._y / Height * yCorrect, 0.0f); GL.Translate(trans); GL.Scale((OpenTK.Vector3)cam._scale); GL.Translate(-trans); //Bind the material ref's texture GL.BindTexture(TextureTarget.Texture2D, texture._texId); //Draw a quad across the screen and render the texture with the calculated texcoords GL.Begin(BeginMode.Quads); GL.TexCoord2(texCoord[0], texCoord[1]); GL.Vertex2(-halfW, -halfH); GL.TexCoord2(texCoord[2], texCoord[3]); GL.Vertex2(halfW, -halfH); GL.TexCoord2(texCoord[4], texCoord[5]); GL.Vertex2(halfW, halfH); GL.TexCoord2(texCoord[6], texCoord[7]); GL.Vertex2(-halfW, halfH); GL.End(); GL.Disable(EnableCap.Texture2D); //Now load the camera transform and draw the UV overlay over the texture cam.LoadModelView(); //Color the lines limegreen, a bright color that probably won't be in a texture GL.Color4(Color.LimeGreen); Vector2 mdlScale = new Vector2(bottomRight._x - topLeft._x, bottomRight._y - topLeft._y); GL.Translate(topLeft._x, topLeft._y, 0.0f); GL.Scale(mdlScale._x, mdlScale._y, 1.0f); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); GL.LineWidth(1); //Render texture coordinates as vertex points foreach (RenderInfo info in _renderInfo) { info.PrepareStream(); } }
public static GLTexture Create(string name, Bitmap bitmap, int width, int height, TextureFlags flags) { if (Exists(name)) { Delete(name); } var actualBitmap = bitmap; if (ForceNonPowerOfTwoResize || !SupportsNonPowerOfTwo()) { var w = NextPowerOfTwo(bitmap.Width); var h = NextPowerOfTwo(bitmap.Height); if (w != bitmap.Width || h != bitmap.Height) actualBitmap = new Bitmap(bitmap, w, h); } var data = actualBitmap.LockBits( new Rectangle(0, 0, actualBitmap.Width, actualBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb ); var tex = CreateAndBindTexture(); SetTextureParameters(); GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat, data.Width, data.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0 ); actualBitmap.UnlockBits(data); if (actualBitmap != bitmap) { actualBitmap.Dispose(); } var texobj = new GLTexture(tex, name, flags) { Width = width, Height = height }; Textures.Add(name, texobj); return texobj; }
public DialogResult ShowDialog(IWin32Window owner, GLTexture texture) { panel.Texture = texture; ClientSize = new Drawing.Size(texture.Width, texture.Height); return(ShowDialog(owner)); }
public static void PrepareMaterial(GLContext context, GLTexture indTexture, GLTexture patternTexture, GLTexture gbuffer, GLTexture linearDepth) { var shader = GlobalShaders.GetShader("LPP_CAUSTICS"); context.CurrentShader = shader; Matrix4 lightSpaceMatrix = Matrix4.LookAt(new Vector3(0, 0, 0.1f), new Vector3(0), new Vector3(0, 1, 0)); Matrix4 lightProj = Matrix4.CreateOrthographicOffCenter(-1, 1, -1, 1, 0.01f, 5.0f); Matrix4 lightVP = lightProj * lightSpaceMatrix; lightVP.Column0 = new Vector4(-0.2408499f, 0.5407615f, 0.8059581f, 0); lightVP.Column1 = new Vector4(1); lightVP.Column2 = new Vector4(1); lightVP.Column3 = new Vector4(-6000, 490, 0.2f, 0.2f); Vector2 projOffset = new Vector2(0); Vector2 projScale = new Vector2(1.0f); shader.SetTexture(indTexture, "IndirectTexture", 1); shader.SetTexture(patternTexture, "PatternTexture", 2); shader.SetTexture(gbuffer, "NormalsTexture", 3); shader.SetTexture(linearDepth, "LinearDepthTexture", 4); shader.SetVector4("viewParams", new OpenTK.Vector4(context.Height, context.Width, 0, 0)); var viewMat = context.Camera.ViewMatrix; var projMat = context.Camera.ProjectionMatrix; var viewProjMatInv = context.Camera.ViewProjectionMatrix.Inverted(); shader.SetMatrix4x4("mtxView", ref viewMat); shader.SetMatrix4x4("mtxProj", ref projMat); shader.SetMatrix4x4("mtxViewProjInv", ref viewProjMatInv); shader.SetMatrix4x4("mtxLightVP", ref lightVP); shader.SetFloat("clipRange", context.Camera.ZFar - context.Camera.ZNear); shader.SetFloat("clipDiv", context.Camera.ZNear / context.Camera.ZFar); shader.SetFloat("clipNear", context.Camera.ZNear); shader.SetFloat("clipFar", context.Camera.ZFar); shader.SetVector2("viewAspect", new OpenTK.Vector2( context.Camera.FactorX, context.Camera.FactorY)); shader.SetVector2("viewSize", new OpenTK.Vector2(context.Width, context.Height)); shader.SetVector3("cameraPos", context.Camera.GetViewPostion()); shader.SetVector3("cameraDir", context.Camera.GetViewDirection()); shader.SetVector2("projectionOffset", projOffset); shader.SetVector2("projectionScale", projScale); shader.SetFloat("clipRange", context.Camera.ZFar - context.Camera.ZNear); shader.SetFloat("fov_x", context.Camera.Fov); shader.SetFloat("fov_y", context.Camera.Fov); GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); }
public static unsafe void RenderBGTexture(int width, int height, GLTexture texture, ref float[] points) { GLTexture bgTex = TKContext.FindOrCreate <GLTexture>("TexBG", CreateBG); bgTex.Bind(); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); //Draw BG float s = (float)width / (float)bgTex.Width, t = (float)height / (float)bgTex.Height; GL.Begin(BeginMode.Quads); GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(0.0f, 0.0f); GL.TexCoord2(s, 0.0f); GL.Vertex2(1.0, 0.0f); GL.TexCoord2(s, t); GL.Vertex2(1.0, 1.0); GL.TexCoord2(0, t); GL.Vertex2(0.0f, 1.0); GL.End(); //Draw texture if ((texture != null) && (texture._texId != 0)) { float tAspect = (float)texture.Width / texture.Height; float wAspect = (float)width / height; if (tAspect > wAspect) //Texture is wider, use horizontal fit { points[0] = points[6] = 0.0f; points[2] = points[4] = 1.0f; points[1] = points[3] = ((height - ((float)width / texture.Width * texture.Height))) / height / 2.0f; points[5] = points[7] = 1.0f - points[1]; } else { points[1] = points[3] = 0.0f; points[5] = points[7] = 1.0f; points[0] = points[6] = (width - ((float)height / texture.Height * texture.Width)) / width / 2.0f; points[2] = points[4] = 1.0f - points[0]; } GL.BindTexture(TextureTarget.Texture2D, texture._texId); GL.Begin(BeginMode.Quads); GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(points[0], points[1]); GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(points[2], points[3]); GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(points[4], points[5]); GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(points[6], points[7]); GL.End(); } }
public TextureRenderer(GLGraphicsContext context, GLTexture texture) { this.context = context; texture = context.NewTexture(OpenTK.Graphics.OpenGL.TextureMinFilter.Nearest, OpenTK.Graphics.OpenGL.TextureMagFilter.Nearest, OpenTK.Graphics.OpenGL.TextureWrapMode.Clamp, OpenTK.Graphics.OpenGL.TextureWrapMode.Clamp); vertices = context.NewVertexBuffer<Vector3>(OpenTK.Graphics.OpenGL.VertexAttribPointerType.Float, 3); vertices.UploadVertices(new Vector3[] { new Vector3(-1, -1, 0), new Vector3(1, -1, 0), new Vector3(-1, 1, 0), new Vector3(1, 1, 0)}); texCoords = context.NewVertexBuffer<Vector2>(OpenTK.Graphics.OpenGL.VertexAttribPointerType.Float, 2); texCoords.UploadVertices(new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1)}); prog = context.NewProgram(MVP_VERTEX_SHADER, TEXTURE_SHADER); paramSpec = ImmutableList<Tuple<String, Object>>.List( Tuple.Create<String, Object>("a_position", vertices), Tuple.Create<String, Object>("a_texPos", texCoords), Tuple.Create<String, Object>("u_texture0", texture)); }
static IRenderableTexture TextureCreationOpenGL(object sender, EventArgs e) { var tex = sender as STGenericTexture; return(GLTexture.FromGenericTexture(tex, tex.Parameters)); }
public static void CreateLightPrepassTexture(GLContext control, GLTexture normalsTexture, GLTexture depthTexture, GLTexture output) { GL.BindTexture(output.Target, 0); if (Filter == null) { Init(control.Width, control.Height); } Filter.Bind(); if (Filter.Width != control.Width || Filter.Height != control.Height) { Filter.Resize(control.Width, control.Height); } if (output.Width != control.Width || output.Height != control.Height) { output.Bind(); if (output is GLTexture2DArray) { GL.TexImage3D(output.Target, 0, output.PixelInternalFormat, control.Width, control.Height, 1, 0, output.PixelFormat, output.PixelType, IntPtr.Zero); } else { GL.TexImage2D(output.Target, 0, output.PixelInternalFormat, control.Width, control.Height, 0, output.PixelFormat, output.PixelType, IntPtr.Zero); } output.Unbind(); } GL.Viewport(0, 0, control.Width, control.Height); for (int i = 0; i < 1; i++) { if (output is GLTexture2DArray) { GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, output.ID, 0, i); } else { GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, output.ID, 0); } } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); SceneLightManager.DrawSceneLights(control.Camera, normalsTexture, depthTexture); CausticLightManager.DrawCaustics(control, normalsTexture, depthTexture); var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } GL.UseProgram(0); Filter.Unbind(); }
public static void SetRenderTarget(GLTexture target) { GL.BindFramebuffer(FramebufferTarget.Framebuffer, target.FrameBufferObject); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, target.Reference, 0); }
private unsafe void Load(int index, int program) { if (TKContext.CurrentContext == null) { return; } bool isStage = false; Source = null; if (Texture != null) { Texture.Delete(); } Texture = new GLTexture(); Texture.Bind(index, program); Bitmap bmp = null; if (RootNode is ARCNode) { isStage = ((ARCNode)RootNode).IsStage; } if (_folderWatcher.EnableRaisingEvents && !String.IsNullOrEmpty(_folderWatcher.Path)) { bmp = SearchDirectory(_folderWatcher.Path + Name); } BRRESNode parentBRRES = null; ARCNode parentARC = null; // Safely search for whether this is part of a BRRES if (_parent != null) { if (_parent._parent != null) { if (_parent._parent._parent != null) { if (_parent._parent._parent._parent != null) { if (_parent._parent._parent._parent is BRRESNode) { parentBRRES = (BRRESNode)_parent._parent._parent._parent; if (parentBRRES._parent != null) { if (parentBRRES._parent is ARCNode) { parentARC = (ARCNode)parentBRRES._parent; } } } } } } } List <ResourceNode> nodes = TKContext.CurrentContext._states["_Node_Refs"] as List <ResourceNode>; TEX0Node tNode = null; if (bmp == null && TKContext.CurrentContext._states.ContainsKey("_Node_Refs") && parentBRRES != null) { ResourceNode node = parentBRRES; //Search node itself first if ((tNode = node.SearchForTextures("Textures(NW4R)/" + Name, true, false) as TEX0Node) != null) { Source = tNode; Texture.Attach(tNode, _palette); return; } } if (bmp == null && TKContext.CurrentContext._states.ContainsKey("_Node_Refs") && parentARC != null) { ResourceNode node = parentARC; //Search node itself first if ((tNode = node.SearchForTextures("Textures(NW4R)/" + Name, true, isStage) as TEX0Node) != null) { Source = tNode; Texture.Attach(tNode, _palette); return; } else //Then search the directory { bmp = SearchDirectory(node._origPath); } } if (bmp != null) { Texture.Attach(bmp); } else { Texture.Default(); } }
public TimImage(TimImageHeader imageHeader, GLTexture layer) { ImageHeader = Exceptions.CheckArgumentNull(imageHeader, "imageHeader"); Layer = Exceptions.CheckArgumentNull(layer, "layer"); }
protected override void Init() { Texture = GLTexture.Create(); }
//Update all existing cubemap uint objects public static void GenerateCubemaps(List <GenericRenderer> targetModels, bool isWiiU) { var texture = isWiiU ? CubeMapTextureArray : CubeMapTexture; if (texture != null) { texture.Dispose(); } if (isWiiU) { texture = GLTexture2DArray.CreateUncompressedTexture(CUBEMAP_SIZE, CUBEMAP_SIZE, MAX_LAYER_COUNT * 6, MAX_MIP_LEVEL, PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.Float); } else { texture = GLTextureCubeArray.CreateEmptyCubemap(CUBEMAP_SIZE, MAX_LAYER_COUNT, MAX_MIP_LEVEL, PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.Float); } GLTextureCube cubemapTexture = GLTextureCube.CreateEmptyCubemap( CUBEMAP_UPSCALE_SIZE, PixelInternalFormat.Rgba16f, PixelFormat.Rgba, PixelType.Float, 9); //Get a list of cubemaps in the scene //The lighting engine has cube map objects with the object placement to draw var lightingEngine = LightingEngine.LightSettings; var cubemapEnvParams = lightingEngine.Resources.CubeMapFiles.FirstOrDefault().Value; var cubeMapUints = cubemapEnvParams.CubeMapObjects; int layer = 0; foreach (var cubeMap in cubeMapUints) { var cUint = cubeMap.CubeMapUint; //Cubemap has no area assigned skip it if (cubeMap.CubeMapUint.Name == string.Empty) { continue; } //Setup the camera to render the cube map faces CubemapCamera camera = new CubemapCamera( new Vector3(cUint.Position.X, cUint.Position.Y, cUint.Position.Z) * GLContext.PreviewScale, cUint.Near, cUint.Far); var context = new GLContext(); context.Camera = camera; GenerateCubemap(context, cubemapTexture, camera, targetModels, MAX_MIP_LEVEL); cubemapTexture.Bind(); cubemapTexture.GenerateMipmaps(); cubemapTexture.Unbind(); //HDR encode and output into the array CubemapHDREncodeRT.CreateCubemap(cubemapTexture, texture, layer, MAX_MIP_LEVEL, false, true); if (SAVE_TO_DISK) { cubemapTexture.SaveDDS(cubeMap.Name + "default.dds"); } layer++; } cubemapTexture.Dispose(); //Just generate mips to keep things easier texture.Bind(); texture.GenerateMipmaps(); texture.Unbind(); if (SAVE_TO_DISK) { texture.SaveDDS("Cubemap_Array_HDR.dds"); } if (isWiiU) { CubeMapTextureArray = texture; } else { CubeMapTexture = texture; } }
private void Form1_Load(object sender, EventArgs e) { #region StartupWindow StartupForm StartForm = new StartupForm(Environment.GetCommandLineArgs()); StartForm.ShowDialog(); if (StartupForm.ApplicationTerminated) { this.Close(); Application.Exit(); return; } #endregion #region WindowSettings this.Size = new Size(StartupForm.W, StartupForm.H); int WindowWidth = StartupForm.W - ClientSize.Width; int WindowHeight = StartupForm.H - ClientSize.Height; this.Size = new Size(StartupForm.W + WindowWidth, StartupForm.H + WindowHeight); this.SetStyle(ControlStyles.StandardDoubleClick, false); FBCaching = StartupForm.NormalsInterpolated; this.MouseWheel += Form1_MouseWheel; #endregion #region FullscreenSettings if (StartupForm.FullscreenSelected) { this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; StartupForm.W = this.Width; StartupForm.H = this.Height; } #endregion #region VertexBufferInitialization STLImporter Importer = new STLImporter(StartupForm.FilePath); float[] vertexpoints = new float[Importer.AllTriangles.Length * 3 * 3]; float[] normalBuffer = new float[Importer.AllTriangles.Length * 3]; for (int i = 0; i < Importer.AllTriangles.Length; i++) { vertexpoints[i * 9] = Importer.AllTriangles[i].vertex1.x; vertexpoints[i * 9 + 1] = Importer.AllTriangles[i].vertex1.y; vertexpoints[i * 9 + 2] = Importer.AllTriangles[i].vertex1.z; vertexpoints[i * 9 + 3] = Importer.AllTriangles[i].vertex2.x; vertexpoints[i * 9 + 4] = Importer.AllTriangles[i].vertex2.y; vertexpoints[i * 9 + 5] = Importer.AllTriangles[i].vertex2.z; vertexpoints[i * 9 + 6] = Importer.AllTriangles[i].vertex3.x; vertexpoints[i * 9 + 7] = Importer.AllTriangles[i].vertex3.y; vertexpoints[i * 9 + 8] = Importer.AllTriangles[i].vertex3.z; normalBuffer[i * 3] = Importer.AllTriangles[i].normals.x; normalBuffer[i * 3 + 1] = Importer.AllTriangles[i].normals.y; normalBuffer[i * 3 + 2] = Importer.AllTriangles[i].normals.z; } #endregion ModelCenter = CalculateCenterOfModel(ref vertexpoints, out DistanceCenter); // vertexpoints = STLImporter.AverageUpFaceNormalsAndOutputVertexBuffer(Importer.AllTriangles, 45); NormalBuffer = new GLBuffer(normalBuffer, 3, MemoryLocation.Heap); VertexBuffer = new GLBuffer(vertexpoints, 3, MemoryLocation.Heap); NormalBuffer = new GLBuffer(renderX.PrimitiveTypes.CubeNormals(), 3, MemoryLocation.Heap); VertexBuffer = new GLBuffer(renderX.PrimitiveTypes.Cube(), 5, MemoryLocation.Heap); //GL._DebugBlit StandardShader = new Shader(null, BasicShader, GLRenderMode.TriangleFlat); //StandardShader.SetOverrideAttributeCopy(true); // Skybox = new GLCubemap("skybox_data"); DisplayTexture = new Shader(CubeShader, TextureShader, GLRenderMode.Triangle); // DisplayTexture.SetOverrideAttributeCopy(false); // DisplayTexture.SetOverrideAttributeCount(3); // GL.BlitInto texture2d = new GLTexture("container2.png", MemoryLocation.Heap, DuringLoad.Flip); TEXTURE_ADDR = (int *)texture2d.GetAddress(); textureHeight = texture2d.Height; textureWidthMinusOne = texture2d.Width - 1; textureHeightMinusOne = texture2d.Height - 1; #region MemoryAddresses nbAddr = (float *)NormalBuffer.GetAddress(); vertexpoints = null; #endregion #region CubeObject CubeVBO = new GLBuffer(renderX.PrimitiveTypes.Cube(), 5, MemoryLocation.Heap); cubeShader = new Shader(CubeVS, CubeFS, GLRenderMode.TriangleFlat); cubeShader.SetOverrideAttributeCount(0); #endregion #region 3D_Grid float[] vpoints = new float[] { 0, 0, 0, 9, 0, 0, 0, 0, 1, 9, 0, 1, 0, 0, 2, 9, 0, 2, 0, 0, 3, 9, 0, 3, 0, 0, 4, 9, 0, 4, 0, 0, 5, 9, 0, 5, 0, 0, 6, 9, 0, 6, 0, 0, 7, 9, 0, 7, 0, 0, 8, 9, 0, 8, 0, 0, 9, 9, 0, 9, 0, 0, 0, 0, 0, 9, 1, 0, 0, 1, 0, 9, 2, 0, 0, 2, 0, 9, 3, 0, 0, 3, 0, 9, 4, 0, 0, 4, 0, 9, 5, 0, 0, 5, 0, 9, 6, 0, 0, 6, 0, 9, 7, 0, 0, 7, 0, 9, 8, 0, 0, 8, 0, 9, 9, 0, 0, 9, 0, 9 }; LineBuffer = new GLBuffer(vpoints, 3, MemoryLocation.Heap); LineShader = new Shader(GridShaderVS, GridShaderFS, GLRenderMode.Line, GLExtraAttributeData.XYZ_CameraSpace); #endregion #region CameraIndicator CameraIndicator = new Shader(CIVS, null, GLRenderMode.TriangleGouraud); CameraIndicator.SetOverrideCameraTransform(true); CameraIndicator.SetOverrideAttributeCopy(true); //Inner Part CICVertex = new GLBuffer(renderX.PrimitiveTypes.CMI(), 3, MemoryLocation.Heap); CICNormals = new GLBuffer(renderX.PrimitiveTypes.CMINormals(), 3, MemoryLocation.Heap); //Outer Part CIPVertex = new GLBuffer(renderX.PrimitiveTypes.CMS(), 3, MemoryLocation.Heap); CIPNormals = new GLBuffer(renderX.PrimitiveTypes.CMSNormals(), 3, MemoryLocation.Heap); CIC_Normals = (float *)CICNormals.GetAddress(); CIP_Normals = (float *)CIPNormals.GetAddress(); #endregion #region Post-Processing SSRShader = new Shader(null, SSR_Fragment, GLRenderMode.Triangle, GLExtraAttributeData.XYZ_XY_Both); SSRShaderPost = new Shader(SSR_Pass); #endregion #region MiniGLIndicator MiniGL = new renderX(130, 128); MiniGL.SetMatrixData(90, 10); MiniGL.SelectShader(CameraIndicator); MiniGL.SetFaceCulling(true, false); MiniGL.InitializeClickBuffer(); MiniGL.SetClickBufferWrite(true); #endregion #region renderX_Initialization GL = new renderX(StartupForm.W, StartupForm.H, this.Handle); GL.SelectBuffer(VertexBuffer); GL.SelectShader(StandardShader); GL.SetMatrixData(90, 10); #endregion #region GLSettings cachedBuffer = new GLCachedBuffer(GL); GL.SetWireFrameOFFSET(-0.1f); GL.SetFaceCulling(true, false); #endregion // GL.SetLinkedWireframe(true, 255, 255, 255); // GL.SetViewportScaling(1920, 1080, InterpolationMethod.NearestNeighbour); GL.SetDebugWireFrameColor(255, 255, 255); // GL.SetLineAntiAliasing(true); GL.SetLineAntiAliasing(true); GL.SetLineThickness(1); // GL.SetLineThickness(3); // GL.SetWireFrameOFFSET(-5000f); // GL.SetLineAntiAliasing(true); // GL.SetLinkedWireframe(true, 0, 0, 255); GL.InitializeVignetteBuffer(VignetteShader); // cameraPosition = new Vector3(-1.16f, 18f, -33.5f); // cameraRotation = new Vector3(0,0,90); cameraPosition = new Vector3(0, 0, -50); cameraRotation = new Vector3(0, 0, 0); #region RenderThreadStart RT = new RenderThread(144); RT.RenderFrame += RT_RenderFrame; RT.Start(); #endregion }
public GLTextureUnit SetTexture(GLTexture GLTexture) { this.GLTexture = GLTexture; return this; }
private unsafe void Load(int index, int program, PLT0Node palette) { if (_context == null) { return; } Source = null; if (Texture != null) { Texture.Delete(); } Texture = new GLTexture(); Texture.Bind(index, program, _context); //ctx._states[String.Format("{0}_TexRef", Name)] = Texture; Bitmap bmp = null; TEX0Node tNode = null; if (_context._states.ContainsKey("_Node_Refs")) { List <ResourceNode> nodes = _context._states["_Node_Refs"] as List <ResourceNode>; List <ResourceNode> searched = new List <ResourceNode>(nodes.Count); foreach (ResourceNode n in nodes) { ResourceNode node = n.RootNode; if (searched.Contains(node)) { continue; } searched.Add(node); //Search node itself first if ((tNode = node.FindChild("Textures(NW4R)/" + Name, true) as TEX0Node) != null) { Source = tNode; if (palette != null) { Texture.Attach(tNode, palette); } else { Texture.Attach(tNode); } return; } else { //Then search node directory string path = node._origPath; if (path != null) { DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(path)); if (dir.Exists && Name != "<null>") { foreach (FileInfo file in dir.GetFiles(Name + ".*")) { if (file.Name.EndsWith(".tga")) { Source = file.FullName; bmp = TGA.FromFile(file.FullName); break; } else if (file.Name.EndsWith(".png") || file.Name.EndsWith(".tiff") || file.Name.EndsWith(".tif")) { Source = file.FullName; bmp = (Bitmap)Bitmap.FromFile(file.FullName); break; } } } } } if (bmp != null) { break; } } searched.Clear(); if (bmp != null) { Texture.Attach(bmp); } } }
public void Show(IWin32Window owner, GLTexture texture) { panel.Texture = texture; ClientSize = new Drawing.Size(texture.Width, texture.Height); base.Show(owner); }
public void Setup() { TextureLUT = GenerateLutTexture(); }
private void ReadTextureFromGifFile(string file) { _texture = GLTextureFactory.FromImageFile(file); _ownTexture = true; }
private GLTexture pass_blend(GLTexture edge_tex) { if (blend_shader == null) { blend_shader = GLShaderFilter.Create(Width, Height, new GLShader( VertexHeader + @" out vec2 texcoord; out vec2 pixcoord; out vec4 offset[3]; out vec4 dummy2; void main() { texcoord = a_texcoords.xy; vec4 dummy1 = vec4(0); SMAABlendingWeightCalculationVS(dummy1, dummy2, texcoord, pixcoord, offset); gl_Position = a_position; } ", FragmentHeader + @" uniform sampler2D edge_tex; uniform sampler2D area_tex; uniform sampler2D search_tex; in vec2 texcoord; in vec2 pixcoord; in vec4 offset[3]; in vec4 dummy2; void main() { gl_FragColor = SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edge_tex, area_tex, search_tex, ivec4(1, 1, 1, 0)); //gl_FragColor = SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edge_tex, area_tex, search_tex, ivec4(0)); } " )); } return blend_shader.Process((Shader) => { Shader.GetUniform("edge_tex").Set(GLTextureUnit.CreateAtIndex(0).SetWrap(GLWrap.ClampToEdge).SetFiltering(GLScaleFilter.Linear).SetTexture(edge_tex)); Shader.GetUniform("area_tex").Set(area_tex_unit.SetIndex(1)); Shader.GetUniform("search_tex").Set(search_tex_unit.SetIndex(2)); }); }
protected virtual void BindTexture(GLTextureUnit aUnit, GLTexture aTexture) { // We want to turn off filtering. We do that by using 'nearest' // we also clamp the coordinates instead of replicating at the edges aUnit.Bind(); aTexture.Bind(); GI.TexParameter(TextureParameterTarget.Texture2d, TextureParameterName.TextureMinFilter, TextureMinFilter.Nearest); GI.TexParameter(TextureParameterTarget.Texture2d, TextureParameterName.TextureMagFilter, TextureMagFilter.Nearest); GI.TexParameter(TextureParameterTarget.Texture2d, TextureParameterName.TextureWrapS, TextureWrapMode.Clamp); GI.TexParameter(TextureParameterTarget.Texture2d, TextureParameterName.TextureWrapT, TextureWrapMode.Clamp); }
private GLTexture pass_neighborhood(GLTexture albedo_tex, GLTexture blend_tex) { if (neighborhood_shader == null) { neighborhood_shader = GLShaderFilter.Create(Width, Height, new GLShader( VertexHeader + @" out vec2 texcoord; out vec4 offset[2]; out vec4 dummy2; void main() { texcoord = a_texcoords.xy; vec4 dummy1 = vec4(0); SMAANeighborhoodBlendingVS(dummy1, dummy2, texcoord, offset); gl_Position = a_position; } ", FragmentHeader + @" uniform sampler2D albedo_tex; uniform sampler2D blend_tex; in vec2 texcoord; in vec4 offset[2]; in vec4 dummy2; void main() { gl_FragColor = SMAANeighborhoodBlendingPS(texcoord, offset, albedo_tex, blend_tex); } " )); } return neighborhood_shader.Process((Shader) => { Shader.GetUniform("albedo_tex").Set(GLTextureUnit.CreateAtIndex(0).SetWrap(GLWrap.ClampToEdge).SetFiltering(GLScaleFilter.Linear).SetTexture(albedo_tex)); Shader.GetUniform("blend_tex").Set(GLTextureUnit.CreateAtIndex(1).SetWrap(GLWrap.ClampToEdge).SetFiltering(GLScaleFilter.Linear).SetTexture(blend_tex)); }); }
protected internal unsafe override void OnRender() { GLTexture _bgTex = _context.FindOrCreate <GLTexture>("TexBG", CreateBG); _bgTex.Bind(); //Draw BG float s = (float)Width / _bgTex.Width, t = (float)Height / _bgTex.Height; _context.glBegin(GLPrimitiveType.Quads); _context.glTexCoord(0.0f, 0.0f); _context.glVertex(0.0f, 0.0f); _context.glTexCoord(s, 0.0f); _context.glVertex(1.0, 0.0f); _context.glTexCoord(s, t); _context.glVertex(1.0, 1.0); _context.glTexCoord(0, t); _context.glVertex(0.0f, 1.0); _context.glEnd(); //Draw texture if ((_currentTexture != null) && (_currentTexture._id != 0)) { float tAspect = (float)_currentTexture.Width / _currentTexture.Height; float wAspect = (float)Width / Height; float *points = stackalloc float[8]; if (tAspect > wAspect) //Texture is wider, use horizontal fit { points[0] = points[6] = 0.0f; points[2] = points[4] = 1.0f; points[1] = points[3] = ((Height - ((float)Width / _currentTexture.Width * _currentTexture.Height))) / Height / 2.0f; points[5] = points[7] = 1.0f - points[1]; } else { points[1] = points[3] = 0.0f; points[5] = points[7] = 1.0f; points[0] = points[6] = (Width - ((float)Height / _currentTexture.Height * _currentTexture.Width)) / Width / 2.0f; points[2] = points[4] = 1.0f - points[0]; } _context.glBindTexture(GLTextureTarget.Texture2D, _currentTexture._id); _context.glBegin(GLPrimitiveType.Quads); _context.glTexCoord(0.0f, 0.0f); _context.glVertex2v(&points[0]); _context.glTexCoord(1.0f, 0.0f); _context.glVertex2v(&points[2]); _context.glTexCoord(1.0f, 1.0f); _context.glVertex2v(&points[4]); _context.glTexCoord(0.0f, 1.0f); _context.glVertex2v(&points[6]); _context.glEnd(); } }
private GLTexture edge_pass(GLTexture albedo_tex, GLTexture depthTex) { if (edge_shader == null) { edge_shader = GLShaderFilter.Create(Width, Height, new GLShader( VertexHeader + @" out vec2 texcoord; out vec4 offset[3]; out vec4 dummy2; void main() { texcoord = a_texcoords.xy; vec4 dummy1 = vec4(0); SMAAEdgeDetectionVS(dummy1, dummy2, texcoord, offset); gl_Position = a_position; } ", FragmentHeader + @" uniform sampler2D albedo_tex; uniform sampler2D depthTex; in vec2 texcoord; in vec4 offset[3]; in vec4 dummy2; void main() { #if SMAA_PREDICATION == 1 gl_FragColor = SMAAColorEdgeDetectionPS(texcoord, offset, albedo_tex, depthTex); #else gl_FragColor = SMAAColorEdgeDetectionPS(texcoord, offset, albedo_tex); #endif } " )); } return edge_shader.Process((Shader) => { Shader.GetUniform("albedo_tex").Set(GLTextureUnit.CreateAtIndex(0).SetWrap(GLWrap.ClampToEdge).SetFiltering(GLScaleFilter.Linear).SetTexture(albedo_tex)); Shader.GetUniform("depthTex").NoWarning().Set(GLTextureUnit.CreateAtIndex(1).SetWrap(GLWrap.ClampToEdge).SetFiltering(GLScaleFilter.Linear).SetTexture(depthTex)); }); }
public GLTextureUnit SetTexture(GLTexture GLTexture) { this.GLTexture = GLTexture; return(this); }
private void GetTexVram(Action <TexturePair> Action) { if (TexVram == null) { TexVram = GLTexture.Create().SetFormat(TextureFormat.RGBA).SetSize(1, 1); } //Console.WriteLine(TexVram); TexVram.Bind(); if (BufferGraphics == null) { BufferGraphics = Graphics.FromImage(Buffer); //BufferGraphics.Clear(Color.Red); BufferGraphics.Clear(Color.Black); } //if (PspDisplayForm.Singleton.WindowState == FormWindowState.Minimized) //{ // return; //} // //if (!PspDisplayForm.Singleton.EnableRefreshing) //{ // return; //} if (IGuiWindowInfo.EnableRefreshing) { try { int Width = 512; int Height = 272; var FrameAddress = PspDisplay.CurrentInfo.FrameAddress; byte *FrameBuffer = null; byte *DepthBuffer = null; try { FrameBuffer = (byte *)Memory.PspAddressToPointerSafe( FrameAddress, PixelFormatDecoder.GetPixelsSize(PspDisplay.CurrentInfo.PixelFormat, Width * Height) ); } catch (Exception Exception) { Console.Error.WriteLine(Exception); } //Console.Error.WriteLine("FrameBuffer == 0x{0:X}!!", (long)FrameBuffer); if (FrameBuffer == null) { //Console.Error.WriteLine("FrameBuffer == null!!"); } //Console.WriteLine("{0:X}", Address); var Hash = PixelFormatDecoder.Hash( PspDisplay.CurrentInfo.PixelFormat, (void *)FrameBuffer, Width, Height ); if (Hash != LastHash) { LastHash = Hash; Buffer.LockBitsUnlock(System.Drawing.Imaging.PixelFormat.Format32bppArgb, (BitmapData) => { var Count = Width * Height; fixed(OutputPixel * BitmapDataDecodePtr = BitmapDataDecode) { var BitmapDataPtr = (BGRA *)BitmapData.Scan0.ToPointer(); //var LastRow = (FrameBuffer + 512 * 260 * 4 + 4 * 10); //Console.WriteLine("{0},{1},{2},{3}", LastRow[0], LastRow[1], LastRow[2], LastRow[3]); if (FrameBuffer == null) { if (OldFrameBuffer != null) { Console.Error.WriteLine("FrameBuffer == null"); } } else if (BitmapDataPtr == null) { Console.Error.WriteLine("BitmapDataPtr == null"); } else { PixelFormatDecoder.Decode( PspDisplay.CurrentInfo.PixelFormat, (void *)FrameBuffer, BitmapDataDecodePtr, Width, Height ); } // Converts the decoded data to Window's format. for (int n = 0; n < Count; n++) { BitmapDataPtr[n].R = BitmapDataDecodePtr[n].B; BitmapDataPtr[n].G = BitmapDataDecodePtr[n].G; BitmapDataPtr[n].B = BitmapDataDecodePtr[n].R; BitmapDataPtr[n].A = 0xFF; } OldFrameBuffer = FrameBuffer; GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 512, 272, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, BitmapDataPtr); TextureVerticalFlip = true; } }); } //else { //Console.WriteLine("Display not updated!"); } } catch (Exception Exception) { Console.Error.WriteLine(Exception); } } Action(new TexturePair() { Color = TexVram, Depth = GLTexture.Wrap(0) }); return; }
public static void CreateCubemap(GLContext control, GLTextureCube cubemapInput, GLTexture cubemapOutput, int layer) { int size = cubemapOutput.Width; Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90), 1.0f, 0.1f, 10.0f); Matrix4[] captureViews = { Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(-1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(0.0f, -1.0f, 0.0f)), }; GL.BindTexture(TextureTarget.TextureCubeMap, 0); //Bind the cubemap's texture into a filtered quad. //Bind the drawn filter to a cubemap array layer Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, size, size, PixelInternalFormat.Rgba32f); frameBuffer.Bind(); GL.Disable(EnableCap.Blend); var cubemapFilter = GlobalShaders.GetShader("CUBEMAP_PREFILTER"); cubemapFilter.Enable(); //Allocate mipmaps cubemapOutput.Bind(); cubemapOutput.GenerateMipmaps(); cubemapOutput.Unbind(); GL.ActiveTexture(TextureUnit.Texture0 + 1); cubemapInput.Bind(); cubemapFilter.SetInt("environmentMap", 1); cubemapFilter.SetMatrix4x4("projection", ref projection); //Quick hack, draw once before rendering (first buffer not updating for some reason??) RenderTools.DrawCube(); GL.Disable(EnableCap.CullFace); for (int mip = 0; mip < cubemapOutput.MipCount; mip++) { int mipWidth = (int)(size * Math.Pow(0.5, mip)); int mipHeight = (int)(size * Math.Pow(0.5, mip)); frameBuffer.Resize(mipWidth, mipHeight); GL.Viewport(0, 0, mipWidth, mipHeight); float roughness = (float)mip / (float)(cubemapOutput.MipCount - 1); cubemapFilter.SetFloat("roughness", roughness); for (int i = 0; i < 6; i++) { //attach face to fbo as color attachment 0 if (cubemapOutput is GLTextureCubeArray) { GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, cubemapOutput.ID, mip, (layer * 6) + i); } else { GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMapPositiveX + i, cubemapOutput.ID, mip); } cubemapFilter.SetMatrix4x4("view", ref captureViews[i]); GL.ClearColor(0, 0, 0, 1); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); RenderTools.DrawCube(); } } var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.Blend); frameBuffer.Dispoe(); frameBuffer.DisposeRenderBuffer(); GL.UseProgram(0); }
void LoadTexture(ShaderProgram shader, GLTexture texture, int location, int id) { GL.ActiveTexture(TextureUnit.Texture0 + id); texture.Bind(); shader.SetInt($"{ConvertSamplerID(location)}", id); }
public static void CreateCubemap(GLContext control, GLTextureCube cubemapInput, GLTexture cubemapOutput, int layer, int numMips) { int size = cubemapInput.Width; Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90), 1.0f, 0.1f, 10.0f); Matrix4[] captureViews = { Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(-1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(0.0f, -1.0f, 0.0f)), }; GL.BindTexture(TextureTarget.TextureCubeMap, 0); //Bind the cubemap's texture into a filtered quad. //Bind the drawn filter to a cubemap array layer Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, size, size, PixelInternalFormat.Rgba32f); frameBuffer.Bind(); GL.Disable(EnableCap.Blend); var cubemapFilter = GlobalShaders.GetShader("CUBEMAP_HDRDECODE"); cubemapFilter.Enable(); GL.ActiveTexture(TextureUnit.Texture0 + 1); cubemapInput.Bind(); cubemapFilter.SetInt("cubemapTexture", 1); cubemapFilter.SetMatrix4x4("projection", ref projection); cubemapFilter.SetFloat("gamma", 2.2f); cubemapFilter.SetFloat("range", 1024.0f); cubemapFilter.SetFloat("scale", 4.0f); GL.Disable(EnableCap.CullFace); for (int mip = 0; mip < numMips; mip++) { int mipWidth = (int)(size * Math.Pow(0.5, mip)); int mipHeight = (int)(size * Math.Pow(0.5, mip)); frameBuffer.Resize(mipWidth, mipHeight); GL.Viewport(0, 0, mipWidth, mipHeight); cubemapFilter.SetFloat("mipLevel", mip); for (int i = 0; i < 6; i++) { cubemapFilter.SetInt("faceLevel", i); //attach face to fbo as color attachment 0 if (cubemapOutput is GLTextureCubeArray) { GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, cubemapOutput.ID, mip, (layer * 6) + i); } else { GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMapPositiveX + i, cubemapOutput.ID, mip); } cubemapFilter.SetMatrix4x4("view", ref captureViews[i]); GL.ClearColor(0, 0, 0, 1); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); RenderTools.DrawCube(); } } var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.Blend); frameBuffer.Dispoe(); frameBuffer.DisposeRenderBuffer(); GL.UseProgram(0); }
public int bind(GLTexture texture) { // todo: what about filters ? tempDesc.set(texture, texture.getMinFilter(), texture.getMagFilter(), texture.getUWrap(), texture.getVWrap()); return(bindTexture(tempDesc, false)); }
public static GLTexture Create(string name, Bitmap bitmap, bool hasTransparency) { if (Exists(name)) { Delete(name); } var data = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb ); var tex = CreateAndBindTexture(); SetTextureParameters(); GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat, data.Width, data.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0 ); bitmap.UnlockBits(data); var texobj = new GLTexture(tex, name) { Width = bitmap.Width, Height = bitmap.Height, HasTransparency = hasTransparency, BitmapImage = (Bitmap)bitmap.Clone() }; Textures.Add(name, texobj); return texobj; }
public static unsafe void RenderBGTexture(int width, int height, GLTexture texture) { float[] points = new float[8]; RenderBGTexture(width, height, texture, ref points); }