I am using the below function to split the pdf into two.
Though it is spliting the pdf, the content is appearing upside down. How do I rotate it by 180 degrees.
Please help. below is the code for the same
private static void ExtractPages(string inputFile, string outputFile,
int start, int end)
{
// get input document
PdfReader inputPdf = new PdfReader(inputFile);
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
{
end = pageCount;
}
// load the input document
Document inputDoc =
new Document(inputPdf.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// copy pages from input to output document
for (int i = start; i <= end; i++)
{
inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(1));
inputDoc.NewPage();
PdfImportedPage page =
outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
inputPdf.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
inputDoc.Close();
}
}
I have found the above answers do not rotate correctly for all 4 of the main rotations.
Below is my code to handle 0, 90, 180 and 270 correctly. This has been tested with a PDF rotated in each of these directions.
var pageRotation = reader.GetPageRotation(currentPageIndex);
var pageWidth = reader.GetPageSizeWithRotation(currentPageIndex).Width;
var pageHeight = reader.GetPageSizeWithRotation(currentPageIndex).Height;
switch (pageRotation)
{
case 0:
writer.DirectContent.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
break;
case 90:
writer.DirectContent.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, pageHeight);
break;
case 180:
writer.DirectContent.AddTemplate(importedPage, -1f, 0, 0, -1f, pageWidth, pageHeight);
break;
case 270:
writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
break;
default:
throw new InvalidOperationException(string.Format("Unexpected page rotation: [{0}].", pageRotation));
}
You should try this. It worked for me:
if (rotation == 90 || rotation == 270)
{
if (rotation == 90)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height);
}
if (rotation == 270)
{
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(pagenumber).Width, 0);
}
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
#TimS' answer was very close to perfect, and provided the correct parameters to AddTemplate, but I needed to make a few additions to allow for 90, 180, 270 rotation of a PDF where the pages already have rotation of 0, 90, 180 or 270:
Assuming a parameter of RotateFlipType rotateFlipType is passed to the function to specify the rotation (the handy enum from the GDI+ RotateFlip call):
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.PdfImportedPage page;
int rotation;
int i = 0;
while (i < pageCount)
{
i++;
var pageSize = reader.GetPageSizeWithRotation(i);
// Pull in the page from the reader
page = writer.GetImportedPage(reader, i);
// Get current page rotation in degrees
rotation = pageSize.Rotation;
// Default to the current page size
iTextSharp.text.Rectangle newPageSize = null;
// Apply our additional requested rotation (switch height and width as required)
switch (rotateFlipType)
{
case RotateFlipType.RotateNoneFlipNone:
newPageSize = new iTextSharp.text.Rectangle(pageSize);
break;
case RotateFlipType.Rotate90FlipNone:
rotation += 90;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation);
break;
case RotateFlipType.Rotate180FlipNone:
rotation += 180;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Width, pageSize.Height, rotation);
break;
case RotateFlipType.Rotate270FlipNone:
rotation += 270;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation);
break;
}
// Cap rotation into the 0-359 range for subsequent check
rotation %= 360;
document.SetPageSize(newPageSize);
document.NewPage();
// based on the rotation write out the page dimensions
switch (rotation)
{
case 0:
cb.AddTemplate(page, 0, 0);
break;
case 90:
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, newPageSize.Height);
break;
case 180:
cb.AddTemplate(page, -1f, 0, 0, -1f, newPageSize.Width, newPageSize.Height);
break;
case 270:
cb.AddTemplate(page, 0, 1f, -1f, 0, newPageSize.Width, 0);
break;
default:
throw new System.Exception(string.Format("Unexpected rotation of {0} degrees", rotation));
break;
}
}
Hopefully this will help someone else wishing to correct the rotation of incoming PDFs. Took me 2 days to perfect it.
I tried your code and it worked fine for me; split pages kept their original orientation.
A workaround might be to explicitly rotate your pages 180 degrees.
Replace:
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
With:
cb1.AddTemplate(page, -1f, 0, 0, -1f,
inputPdf.GetPageSizeWithRotation(i).Width,
inputPdf.GetPageSizeWithRotation(i).Height);
If your call to inputPdf.GetPageRotation(i) returns 180 then you can handle this in the if statement that follows (using my suggested code for rotation == 180).
A little change in above code
old code
case 270:
writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
new code
case 270:
writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageHeight, 0);
This will fix the issue with 270 degree rotation
Related
I have the following simplified code for pulling existing 8x10 PDFs from multiple locations, rotating them if need be (almost all need to be), then writing them to a single 11x17 PDF page by page...
while (Page < StackOne.Length)
{
Files++;
using (var strm = new FileStream(RenderPath + "Test_" + Page + ".pdf", FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (var MasterReport = new iTextSharp.text.Document(iTextSharp.text.PageSize._11X17))
{
using (var writer = PdfWriter.GetInstance(MasterReport, strm))
{
MasterReport.Open();
MasterReport.NewPage();
var cb = writer.DirectContent;
for (; Page <= NumPages * Files && Page < StackOne.Length; Page++)
{
var ProductionEntry = StackOne[Page - 1];
var filepath = NetPath + ProductionEntry.UniqueProductId + ".pdf";
if (File.Exists(filepath))
{
var reader = new PdfReader(filepath);
var pagesize = reader.GetPageSize(1);
if (pagesize.Height > pagesize.Width)
{
var ExistingPage = reader.GetPageN(1);
var rotation = ExistingPage.GetAsNumber(PdfName.ROTATE);
int desiredrot = 90;
if (rotation != null)
{
desiredrot += rotation.IntValue;
desiredrot %= 360;
}
ExistingPage.Put(PdfName.ROTATE, new PdfNumber(desiredrot));
}
cb.AddTemplate(writer.GetImportedPage(reader, 1), 50, 50);
}
MasterReport.NewPage();
}
}
}
}
}
However the page rendered doesn't have the pages rotated as they should be, I've verified the height > width branch is indeed being taken but the pages returned are still 8x10 instead of 10x8 written on each 11x17 page.
I searched around for a question this specific but couldn't find one that wasn't just writing to another file or the entire page instead of to a specific location on an 11x17 sheet.
EDIT: So with a little experimenting and looking at other examples I'm able to rotate the 8x10 page and write it to my 11x17 but unfortunately I can't seem to find a way to place it exactly where I want it here's the relevant code snippet:
var reader = new PdfReader(filepath);
var tm = new AffineTransform(1.0F, 0, 0, 1.0F, x, y);
if (reader.GetPageSize(1).Height > reader.GetPageSize(1).Width)
tm.SetTransform(0, -1f, 1f, 0, 0, reader.GetPageSize(1).Height);
writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, 1), tm);
Okay so after tons of searching and sifting through code it appears the answer was quite simple (as usual), as shown above my initial problem was putting a rotate tag on the page didn't actually rotate the page as I expected. After learning about the option of specifying a matrix for the pdftemplate it was pretty simple here's the working code:
var reader = new PdfReader(filepath);
var tm = new AffineTransform();
if (reader.GetPageSize(1).Height > reader.GetPageSize(1).Width)
tm.SetTransform(0, -1f, 1f, 0, 0, reader.GetPageSize(1).Height);
tm.Concatenate(new AffineTransform(1f, 0, 0, 1f, y, x));
writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, 1), tm);
P.S. http://partners.adobe.com/public/developer/en/pdf/PDFReference.pdf P: 162 (143 in physical form) for those who aren't fresh out of algebra
EDIT: As #mkl has stated below the following is really only applicable for rotate entries of 0° or 90° if you have pages at 180° or 270° this code will need some modifications. Also any annotations in the original will be lost.
You may simple add affine transform into AddTemplate procedure.
Like this:
PdfContentByte cb = stamper.getOverContent(pageNumber);
int rotation = reader.getPageRotation(pageNumber);
PdfImportedPage page = stamper.getImportedPage(reader, pageNumber);
if (rotation == 270)
cb.addTemplate(page, 0, 1f, -1f, 0, reader.getPageSizeWithRotation(pageNumber).getWidth(), 0);
else if (rotation == 90) {
cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(pageNumber).getHeight());
} else {
cb.addTemplate(page, 0, 0);
}
Its java, but I think it is not a problem.
I am attempting to create a minimap for an overlay. Currently the radar portion is working, targets rotate around the center point at the correct distance. I am having trouble printing the graphic that holds the minimap to screen. Everything other than the map prints to screen using the SlimDx.Direct3D9 library but based on my research the best way to create sub images based on player location was with graphics and this method avoids the OutOfMemory exception with cloning bitmaps and is more efficient.
if (ShowRadar)
{
//Draw background
//DrawFilledBox(this.GameWindowRect.Right - 225, this.GameWindowRect.Top + 50, 201f, 201f, Color.DarkOliveGreen, RadarTransparency);
RadarCenter = new Vector2(this.GameWindowRect.Right - 125, this.GameWindowRect.Top + 125 + 25);
if (DXTextrureMap != null)
{
//Transpose player posistion to map position and create the current mini map image
x = (int)player_X / 8;// +/- 4000 is max player range /8 = 500 = max map range
z = (int)player_Z / 8;
mini = new Rectangle(x, z, 100, 100);
Graphics g = Graphics.FromImage(originalBitmap);
g.DrawImage(originalBitmap, mini, new Rectangle((int)this.GameWindowRect.Right - 125, (int)this.GameWindowRect.Top + 125, 100, 100), System.Drawing.GraphicsUnit.Pixel);
}//the rest works as it should and displays on screen
if (RadarCenter.Length() > 0f)
{
//Display each entity in correct relational position
foreach (ENTITY entity in Entity)
{
Vector2 pointToRotate = new Vector2(entity.Pos.X, entity.Pos.Z);
Vector2 vector3 = new Vector2(player_X, player_Z);
pointToRotate = vector3 - pointToRotate;
float num30 = pointToRotate.Length() * 0.5f;
num30 = Math.Min(num30, 90f);
pointToRotate.Normalize();
pointToRotate = (Vector2)(pointToRotate * num30);
pointToRotate += RadarCenter;
pointToRotate = RotatePoint(pointToRotate, RadarCenter, player_D, true);
if (entity.Type == 0x04 && RadarPlayers)
{
DrawFilledBox(pointToRotate.X, pointToRotate.Y, 3f, 3f, Color.SkyBlue);
}
if ((entity.Type == 0x0C || entity.Type == 0x14 || entity.Type == 0x50 || entity.Type == 0x5b) && RadarAggressive)
{
DrawFilledBox(pointToRotate.X, pointToRotate.Y, 3f, 3f, Color.Red);
}
if ((entity.Type == 0x13 || entity.Type == 0x55) && RadarAnimals)
{
DrawFilledBox(pointToRotate.X, pointToRotate.Y, 3f, 3f, Color.LightGreen);
}
if ((entity.Type == 0x11 || entity.Type == 0x72 || entity.Type == 0x76) && RadarVehicles)
{
DrawFilledBox(pointToRotate.X, pointToRotate.Y, 3f, 3f, Color.HotPink);
}
}
}
//Draw radar border
DrawBoxAbs(this.GameWindowRect.Right - 225, this.GameWindowRect.Top + 50, 201f, 201f, 1f, Color.Black);
//Draw radar axis
DrawLine(this.GameWindowRect.Right - 125, this.GameWindowRect.Top + 50, this.GameWindowRect.Right - 125, this.GameWindowRect.Top + 251, 1f, Color.Black);
DrawLine(this.GameWindowRect.Right - 225, this.GameWindowRect.Top + 150, this.GameWindowRect.Right - 24, this.GameWindowRect.Top + 150, 1f, Color.Black);
//Center point
DrawFilledBox(RadarCenter.X - 1f, RadarCenter.Y - 1f, 3f, 3f, Color.White);
}
What is the correct way to print my graphic?
Makes more sense when you realize a graphic draws on the image it grabs, also had to dispose of some elements. Time to make it rotate then perhaps add headings.
if (DXTextrureMap != null)
{
DXSprite.Begin(SpriteFlags.None);
//Transpose player posistion to map position and create the current mini map image
x = (int)player_X / 4;// +/- 4000 is max player range and /4 = 1000 = max map range
z = (int)player_Z / 4;
mini = new Rectangle(x-78, z-78, 157, 157);
croppedBitmap = new Bitmap(200, 200);
using (Graphics grD = Graphics.FromImage(croppedBitmap))
{
grD.DrawImage(originalBitmap, new Rectangle(0, 0, 157, 157), mini, GraphicsUnit.Pixel);
}
//croppedBitmap = Copy(originalBitmap, mini);
using (MemoryStream s = new MemoryStream())//s is a MemoryStream
{
croppedBitmap.Save(s, System.Drawing.Imaging.ImageFormat.Png);
s.Seek(0, SeekOrigin.Begin); //must do this, or error is thrown in next line
currentMinimap = Texture.FromStream(DXDevice, s);
s.Dispose();
}
//croppedImage.Dispose();
DXSprite.Draw(currentMinimap, new Vector3(100f, 100f, 0f), new Vector3(this.GameWindowRect.Right - 125, this.GameWindowRect.Top + 135, 0f), Color.White);
DXSprite.End();
currentMinimap.Dispose();
}
I use Tao Framework and I have a problem with using glLoadIdentity(), I'm loading picture in to a gluDisk successfully, I can see the picture, then when I try to change the camera's position with gluLookAt the picture I have loaded on the gluDisk is gone , instead, all I see is a color that reminds the background color of the picture I have loaded, after using debugging I saw that it happens because of the glLoadIdentity() function.
Here is the function that redraw the Disk:
Gl.glClearColor(0F, 0F, 0F, 1.0F);
while (true)
{
world.drawWorld();
Application.DoEvents();
Thread.Sleep(20);
}
here is my code for changing the lookat:
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glMatrixMode(Gl.GL_MODELVIEW); //Switch to the drawing perspective
Gl.glLoadIdentity();
switch (Convert.ToInt32(e.KeyChar))
{
//down arrow
case 76:
rotAngle -= 0.5f;
rotY -= 0.1f;
//rotX = 0;
break;
//up arrow
case 82:
rotAngle += 0.5f;
rotY += 0.1f;
//rotX = 0;
break;
// left arrow
case 85:
rotAngle -= 0.5f;
//rotY = 0;
rotX -= 0.1f;
break;
//right arrow
case 68:
rotAngle += 0.5f;
//rotY = 0;
rotX += 0.1f;
break;
case 'a':
xMove -= 1;
Glu.gluLookAt(xMove, yMove, zMove, xMove + 2, yMove + 2, zMove + 2, 0, 1, 0);
break;
case 'd':
xMove += 1;
Glu.gluLookAt(xMove, yMove, zMove, xMove + 2, yMove + 2, zMove + 2, 0, 1, 0);
break;
case 'w':
yMove +=1;
Glu.gluLookAt(xMove, yMove, zMove, xMove + 2, yMove + 2, zMove + 2, 0, 1, 0);
break;
case 's':
yMove -= 1;
Glu.gluLookAt(xMove, yMove, zMove, xMove + 2, yMove + 2, zMove + 2, 0, 1, 0);
break;
case 'z':
zMove += 1F;
Glu.gluLookAt(xMove, yMove, zMove, 0, 0, 0, 0, 1, 0);
break;
case 'x':
zMove -= 1F;
Glu.gluLookAt(xMove, yMove, zMove, 0, 0, 0, 0, 1, 0);
break;
case '0':
//zMove -= 1F;
Glu.gluLookAt(1, 1, 1, 0, 0, 0, 0, 1, 0);
break;
Here is my drawing code:
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glEnable(Gl.GL_TEXTURE_2D);
LoadTexture();
Glu.GLUquadric qobj = Glu.gluNewQuadric();
Glu.gluQuadricTexture(qobj, Gl.GL_TRUE);
//Glut.glutSetCursor(Glut.GLUT_CURSOR_NONE);
Gl.glPushMatrix();
Gl.glRotatef(80f, 1.0f, 0.0f, 0.0f);
Glu.gluDisk (qobj,0, 800, 900, 300);
Gl.glPopMatrix();
Never(!) put OpenGL state change functions into an event handler! Right now you have exactly this. In your keypress handler change some variable, then issue a redraw event and use that to apropriately setup the matrices in the drawing function.
glLoadIdentity has to do with this only insofar, that you call it at a place, where it does not belong. OpenGL is a drawing API not a scene graph.
I have PDF where some of the text are very closed to the left and right border and I would like to add more white space at the border, top bottom left and right, for each pages in the PDF using iTextSharp.
Is this possible using iTextSharp or there is a better way?
I managed to find the answer. The code below adjusts the size of the pages:
var inputPdf = new PdfReader(inputFile); // Get input document
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
end = pageCount;
var inputDoc = new Document(inputPdf.GetPageSizeWithRotation(1));
using (var fs = new FileStream(outputFile, FileMode.Create))
{
var outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// Copy pages from input to output document
for (int i = start; i <= end; i++)
{
var existingRec = inputPdf.GetPageSizeWithRotation(i);
var newRec = new Rectangle(0.0f, 0.0f, existingRec.Width + 50, existingRec.Height + 25, 0);
inputDoc.SetPageSize(newRec);
inputDoc.NewPage();
PdfImportedPage page = outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, inputPdf.GetPageSizeWithRotation(i).Height);
else cb1.AddTemplate(page, 1f, 0, 0, 1f, 25, 13);
}
inputDoc.Close();
}
Hope this helps someone.
I have a following picture:
Window displays two textures, the left texture is generated by binding it to a FBO and rendering to FBO, 50 random triangles.
The right texture is generated by glTexImage2D with full-color image of Mona Lisa, but as you can see the image is lacking red colors, I think the problem is in OpenGL part, since I've investigated bytes that are passed to glTexImage2D and they do contain Red...
Here is C#/Mono/OpenTK source
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
using OpenTK.Platform.X11;
using OpenTK.Input;
using ManOCL;
namespace GlTest
{
public class MainClass: GameWindow
{
public MainClass()
: base(800, 600, new GraphicsMode(new ColorFormat(0, 0, 0, 0), 32, 0))
{
this.VSync = VSyncMode.Off;
}
public uint fbo;
public uint textureA;
public uint textureB;
public int textureAWidth = 1024;
public int textureAHeight = 1024;
public int textureBWidth;
public int textureBHeight;
Random rand = new Random();
protected override void OnLoad(EventArgs e)
{
GL.Enable(EnableCap.Blend);
GL.ShadeModel(ShadingModel.Smooth);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
GL.Disable(EnableCap.DepthTest);
GL.Disable(EnableCap.CullFace);
// GL.PolygonMode(MaterialFace.Back, PolygonMode.Line);
// Create Color Tex
GL.GenTextures(1, out textureA);
GL.BindTexture(TextureTarget.Texture2D, textureA);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, textureAWidth, textureAHeight, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.ClampToBorder);
GL.GenTextures(1, out textureB);
GL.BindTexture(TextureTarget.Texture2D, textureB);
Bitmap bmp = new Bitmap("/Projects/MonaLisa/MonaLisa.bmp");
Console.WriteLine(textureBWidth = bmp.Width);
Console.WriteLine(textureBHeight = bmp.Height);
//get the data out of the bitmap
System.Drawing.Imaging.BitmapData bmpBits = bmp.LockBits
(
new System.Drawing.Rectangle(0,0,textureBWidth, textureBHeight),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppRgb
);
for (int row = 0; row < 1; row++)
{
for (int col = 0; col < 32; col++)
{
Console.WriteLine
(
"{0}, {1}, {2}, {3}",
Marshal.ReadByte(bmpBits.Scan0, (row * textureBWidth + col) * 4 + 0),
Marshal.ReadByte(bmpBits.Scan0, (row * textureBWidth + col) * 4 + 1),
Marshal.ReadByte(bmpBits.Scan0, (row * textureBWidth + col) * 4 + 2),
Marshal.ReadByte(bmpBits.Scan0, (row * textureBWidth + col) * 4 + 3)
);
}
}
Console.WriteLine(bmpBits.Width);
Console.WriteLine(bmpBits.Height);
GL.TexImage2D
(
TextureTarget.Texture2D,
0,
PixelInternalFormat.Rgba,
textureBWidth,
textureBHeight,
0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba,
PixelType.UnsignedByte,
bmpBits.Scan0
);
// GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, textureBWidth, textureBHeight, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.ClampToBorder);
//free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver
bmp.UnlockBits(bmpBits);
// Create a FBO and attach the textures
GL.Ext.GenFramebuffers(1, out fbo);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fbo);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, textureA, 0);
#region Test for Error
switch (GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt))
{
case FramebufferErrorCode.FramebufferCompleteExt:
{
Console.WriteLine("FBO: The framebuffer is complete and valid for rendering.");
break;
}
case FramebufferErrorCode.FramebufferIncompleteAttachmentExt:
{
Console.WriteLine("FBO: One or more attachment points are not framebuffer attachment complete. This could mean there’s no texture attached or the format isn’t renderable. For color textures this means the base format must be RGB or RGBA and for depth textures it must be a DEPTH_COMPONENT format. Other causes of this error are that the width or height is zero or the z-offset is out of range in case of render to volume.");
break;
}
case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt:
{
Console.WriteLine("FBO: There are no attachments.");
break;
}
/* case FramebufferErrorCode.GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
{
Console.WriteLine("FBO: An object has been attached to more than one attachment point.");
break;
}*/
case FramebufferErrorCode.FramebufferIncompleteDimensionsExt:
{
Console.WriteLine("FBO: Attachments are of different size. All attachments must have the same width and height.");
break;
}
case FramebufferErrorCode.FramebufferIncompleteFormatsExt:
{
Console.WriteLine("FBO: The color attachments have different format. All color attachments must have the same format.");
break;
}
case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt:
{
Console.WriteLine("FBO: An attachment point referenced by GL.DrawBuffers() doesn’t have an attachment.");
break;
}
case FramebufferErrorCode.FramebufferIncompleteReadBufferExt:
{
Console.WriteLine("FBO: The attachment point referenced by GL.ReadBuffers() doesn’t have an attachment.");
break;
}
case FramebufferErrorCode.FramebufferUnsupportedExt:
{
Console.WriteLine("FBO: This particular FBO configuration is not supported by the implementation.");
break;
}
default:
{
Console.WriteLine("FBO: Status unknown. (yes, this is really bad.)");
break;
}
}
// using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering
int[] queryinfo = new int[6];
GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]);
GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]);
GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]);
GL.GetInteger(GetPName.Stereo, out queryinfo[3]);
GL.GetInteger(GetPName.Samples, out queryinfo[4]);
GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]);
Console.WriteLine("max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] +
"\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]);
Console.WriteLine("Last GL Error: " + GL.GetError());
#endregion Test for Error
GL.PushAttrib(AttribMask.ViewportBit);
{
GL.Viewport(0, 0, textureAWidth, textureAHeight);
OpenTK.Matrix4 orthogonal = OpenTK.Matrix4.CreateOrthographicOffCenter(0, 1, 0, 1, -3, 3);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref orthogonal);
Matrix4 lookat = Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref lookat);
// clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
GL.ClearColor(0f, 0f, 0f, 0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
// smack 50 random triangles into the FBO's textures
GL.Begin(BeginMode.Triangles);
{
for (int i = 0; i < 50; i++)
{
GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
}
}
GL.End();
}
GL.PopAttrib();
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO
GL.ClearColor(1f, 1f, 1f, 0.0f);
GL.Color3(1f, 1f, 1f);
GL.Enable(EnableCap.Texture2D); // enable Texture Mapping
GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture
// IntPtr cglContext = Monobjc.OpenGL.CGL.GetCurrentContext();
//
// IntPtr cglShareGroup = Monobjc.OpenGL.CGL.GetShareGroup(cglContext);
//
// ManOCL.Context.ShareWithCGL(cglShareGroup);
//
// Kernel kernel = ManOCL.Kernel.Create
// (
// "kernel1",
// #"__kernel void kernel1(__global int *srcimg, __global int * output, __global int *smp)
// {
// }",
// new Argument[]
// {
// DeviceGlobalMemory.CreateFromArray(new int[1]),
// DeviceGlobalMemory.CreateFromArray(new int[2]),
// DeviceGlobalMemory.CreateFromArray(new int[1])
// }
// );
// Console.WriteLine("Success!");
// Console.WriteLine(kernel);
}
protected override void OnUnload(EventArgs e)
{
// Clean up what we allocated before exiting
GL.DeleteTextures(1, ref textureA);
GL.DeleteTextures(1, ref textureB);
GL.Ext.DeleteFramebuffers(1, ref fbo);
base.OnUnload(e);
}
protected override void OnResize (EventArgs e)
{
GL.Viewport(0, 0, Width, Height);
double aspect_ratio = Width / (double)Height;
OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4 * 3 / 2, (float)aspect_ratio, 1, 64);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref perspective);
Matrix4 lookat = Matrix4.LookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref lookat);
base.OnResize(e);
}
protected override void OnUpdateFrame (FrameEventArgs e)
{
base.OnUpdateFrame(e);
if (Keyboard[Key.Escape])
{
this.Exit();
}
}
protected override void OnRenderFrame(FrameEventArgs e)
{
this.Title = "Frames per Second: " + (1.0 / e.Time);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.PushMatrix();
{
// Draw the Color Texture
GL.Translate(-1f, 0f, 0f);
GL.BindTexture(TextureTarget.Texture2D, textureA);
GL.Begin(BeginMode.Quads);
{
GL.TexCoord2(0f, 1f);
GL.Vertex2(-1.0f, 1.0f);
GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, 1.0f);
}
GL.End();
GL.Translate(2f, 0f, 0f);
GL.BindTexture(TextureTarget.Texture2D, textureB);
GL.Begin(BeginMode.Quads);
{
GL.TexCoord2(0f, 0f);
GL.Vertex2(-1.0f, 1.0f);
GL.TexCoord2(0.0f, 1.0f);
GL.Vertex2(-1.0f, -1.0f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(1.0f, -1.0f);
GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(1.0f, 1.0f);
}
GL.End();
GL.Translate(0f, 0f, 0f);
}
GL.PopMatrix();
this.SwapBuffers();
}
#region public static void Main()
/// <summary>
/// Entry point of this example.
/// </summary>
[STAThread]
public static void Main()
{
using (MainClass example = new MainClass())
{
example.Title = "FrameBufferObjects";
example.Run(1.0, 0.0);
}
}
#endregion
}
}
In the bit of code where you set your texture data:
GL.TexImage2D
(
TextureTarget.Texture2D,
0,
PixelInternalFormat.Rgba,
textureBWidth,
textureBHeight,
0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba,
PixelType.UnsignedByte,
bmpBits.Scan0
);
Change your pixel format to
OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
Windows bitmaps and opengl have a different endianess on pixels i.e. the bytes are reversed.