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.
Related
So, I am in the process of creating a infinite terrain system similar to Minecraft's. I am making it myself out of personal interest to learn. I have most everything working, and I have made some functions to generate the faces of cubes and then I'm adding it all together into a chunk, but for some reason I am getting a really weird thing where each row of the blocks are randomly much higher than the others, ruining the mesh entire. Anyone able to understand why, because I am completely lost as to the issue.
Note: The highlighted part is supposed to be a singular chunk, and it extends upwards over the entire chunk. Each set of them are a slight bit behind the previous, its supposed to be a completely flat face on the top.
// Face Generator
public enum Face { top, bottom, north, south, west, east };
public Mesh GetMeshDataFromFace(Face face, Vector3 pos)
{
Mesh _face = new Mesh();
Vector3[] _normals = new Vector3[4];
if (face == Face.top)
{
//Create the mesh for a upwards facing surface.
_face.vertices = new Vector3[] {
pos + new Vector3(0, 1, 0),
pos + new Vector3(1, 1, 0),
pos + new Vector3(1, 1, 1),
pos + new Vector3(0, 1, 1)
};
_face.triangles = new int[]{
0, 3, 2, 2, 1, 0
};
_normals = new Vector3[]{
Vector3.up, Vector3.up, Vector3.up, Vector3.up
};
}
if (face == Face.bottom)
{
//Create the mesh for a upwards facing surface.
_face.vertices = new Vector3[] {
pos + new Vector3(0, 0, 0),
pos + new Vector3(1, 0, 0),
pos + new Vector3(1, 0, 1),
pos + new Vector3(0, 0, 1)
};
_normals = new Vector3[]{
Vector3.down, Vector3.down, Vector3.down, Vector3.down
};
_face.triangles = new int[]{
0, 1, 2, 2, 3, 0
};
}
if (face == Face.north)
{
//Create the mesh for a upwards facing surface.
_face.vertices = new Vector3[] {
pos + new Vector3(0, 0, 1),
pos + new Vector3(1, 0, 1),
pos + new Vector3(1, 1, 1),
pos + new Vector3(0, 1, 1)
};
_normals = new Vector3[]{
Vector3.right, Vector3.right, Vector3.right, Vector3.right
};
_face.triangles = new int[]{
0, 1, 2, 2, 3, 0
};
}
if (face == Face.south)
{
//Create the mesh for a upwards facing surface.
_face.vertices = new Vector3[] {
pos + new Vector3(0, 0, 0),
pos + new Vector3(1, 0, 0),
pos + new Vector3(1, 1, 0),
pos + new Vector3(0, 1, 0)
};
_normals = new Vector3[]{
Vector3.left, Vector3.left, Vector3.left, Vector3.left
};
_face.triangles = new int[]{
0, 3, 1, 3, 2, 1
};
}
if (face == Face.west)
{
//Create the mesh for a upwards facing surface.
_face.vertices = new Vector3[] {
pos + new Vector3(0, 0, 0), // 0
pos + new Vector3(0, 1, 0), // 3
pos + new Vector3(0, 1, 1), // 7
pos + new Vector3(0, 0, 1) // 4
};
_normals = new Vector3[]{
Vector3.back, Vector3.back, Vector3.back, Vector3.back
};
_face.triangles = new int[]{
2, 1, 0, 0, 3, 2
};
}
if (face == Face.east)
{
//Create the mesh for a upwards facing surface.
_face.vertices = new Vector3[] {
pos + new Vector3(1, 0, 0),
pos + new Vector3(1, 1, 0),
pos + new Vector3(1, 1, 1),
pos + new Vector3(1, 0, 1)
};
_normals = new Vector3[]{
Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward
};
_face.triangles = new int[]{
0, 1, 2, 2, 3, 0
};
}
_face.SetNormals(_normals);
return _face;
}
Chunks store block data as a singular byte[] array of 4096 indexs.
Here is the code for generating the mesh
public Mesh CreateChunkMesh(ChunkObject chunkObj)
{
Chunk chunkData = chunkObj.chunkData;
Mesh mesh = new Mesh();
List<Mesh> meshesToCombine = new List<Mesh>();
for (int i = 0; i < chunkData.blocks.Length; i++)
{
if (chunkData.blocks[i] != 0)
{
Vector3 blockPos = new Vector3((i % 16), Mathf.Floor(i % 256), Mathf.Floor((i % 256) / 16));
Debug.Log(i + " | " + blockPos);
if (i + 256 >= chunkData.blocks.Length)
{
//This is the top layer of blocks.
if (!_loadedChunkObjs.ContainsKey(chunkObj.chunkPosition + new Vector3Int(0, 1, 0)))
{
//If the block ABOVE this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.top, blockPos));
}
}
if (i < 256)
{
if (!_loadedChunkObjs.ContainsKey(chunkObj.chunkPosition + new Vector3Int(0, -1, 0)))
{
//If the block ABOVE this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.bottom, blockPos));
}
}
if ((i % 256) < 16)
{
//This is the EAST layer of the chunk.
if (!_loadedChunkObjs.ContainsKey(chunkObj.chunkPosition + new Vector3Int(0, 0, 1)))
{
//If the block ABOVE this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.east, blockPos));
}
}
if ((i % 256) >= 240)
{
//This is the WEST layer of the chunk.
if (!_loadedChunkObjs.ContainsKey(chunkObj.chunkPosition + new Vector3Int(0, 0, -1)))
{
//If the block ABOVE this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.west, blockPos));
}
}
if (i % 16 == 0)
{
//This is the NORTH layer of the chunk
if (!_loadedChunkObjs.ContainsKey(chunkObj.chunkPosition + new Vector3Int(1, 0, 0)))
{
//If the block ABOVE this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.north, blockPos));
}
}
if (i % 16 == 1)
{
//This is the SOUTH layer of the chunk.
if (!_loadedChunkObjs.ContainsKey(chunkObj.chunkPosition + new Vector3Int(-1, 0, 0)))
{
//If the block ABOVE this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.south, blockPos));
}
}
//This block is not within any of the faces.
//Check each direction of the block
if (i + 256 <= chunkData.blocks.Length)
{
if (chunkData.blocks[i + 255] == 0) //TOP direction
{
//If the block ABOVE this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.top, blockPos));
}
}
if (i - 256 >= 0)
{
if (chunkData.blocks[i - 256] == 0) //BOTTOM Direction
{
//If the block BELOW this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.bottom, blockPos));
}
}
if (i + 1 <= chunkData.blocks.Length - 1)
{
if (chunkData.blocks[i + 1] == 0) //NORTH Direction
{
//If the block NORTH of this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.north, blockPos));
}
}
if (i - 1 >= 0)
{
if (chunkData.blocks[i - 1] == 0) //SOUTH Direction
{
//If the block SOUTH of this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.south, blockPos));
}
}
if (i + 16 <= chunkData.blocks.Length)
{
if (chunkData.blocks[i + 15] == 0) //WEST Direction
{
//If the block WEST of this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.west, blockPos));
}
}
if (i - 16 >= 0)
{
if (chunkData.blocks[i - 16] == 0) //EAST Direction
{
//If the block EAST of this block is air then draw the face.
meshesToCombine.Add(GetMeshDataFromFace(Face.east, blockPos));
}
}
}
}
CombineInstance[] combine = new CombineInstance[meshesToCombine.Count];
for (int i = 0; i < combine.Length; i++)
{
combine[i].mesh = meshesToCombine[i];
combine[i].transform = chunkObj.transform.localToWorldMatrix;
}
mesh.CombineMeshes(combine);
return mesh;
}
If there are any necessary details I didn't include let me know and ill add them.
I have a problem with my ViewBobScript, it snaps back to the lerps start position for 1 frame.
Here is a Recreation/Video of my problem:
(Due to the framerate of the video not matching up to the game it might look as if the lerp is not smooth, while it is. the problem is the snap at the beginning and end of the video and seems to occur at irregular intervals)
https://youtu.be/UOz2pQcMFjY
And here is my code:
private float aTimer;
private int viewBobPath = 1;
private float startViewBob;
private float viewBobPathTimer;
private void ViewBob()
{
startViewBob = viewBobSpeed / 2;
aTimer += Time.deltaTime;
viewBobPathTimer += Time.deltaTime;
if (aTimer > startViewBob * viewBobPath) { viewBobPath++; }
if (viewBobPathTimer > startViewBob) { viewBobPathTimer = 0; }
if (aTimer < (viewBobSpeed * 2))
{
switch (viewBobPath)
{
case 1:
playerCam.transform.localPosition = Vector3.Lerp(new Vector3(-viewBobHorizontal / 2, 0, 0), new Vector3(0, -viewBobVertical, 0), viewBobPathTimer);
break;
case 2:
playerCam.transform.localPosition = Vector3.Lerp(new Vector3(0, -viewBobVertical, 0), new Vector3(viewBobHorizontal / 2, 0, 0), viewBobPathTimer);
break;
case 3:
playerCam.transform.localPosition = Vector3.Lerp(new Vector3(viewBobHorizontal / 2, 0, 0), new Vector3(0, -viewBobVertical, 0), viewBobPathTimer);
break;
case 4:
playerCam.transform.localPosition = Vector3.Lerp(new Vector3(0, -viewBobVertical, 0), new Vector3(-viewBobHorizontal / 2, 0, 0), viewBobPathTimer);
break;
}
}
else { aTimer = 0; viewBobPathTimer = 0; viewBobPath = 1; }
}
Also, I know the unity character controller has ViewBobbing. but this is not an option for my application.
Thank you so so much for helping!
Edit: This is called from the update function, although I have tried Fixed and late update both don't change, solve or reduce the problem.
Although my script works and I'm continuing with my project, if you know why my code above didn't work/did the Snap I would appreciate knowing so I learn from it!
I would take a different approach, adding Time.deltaTime to one field, then calculating which path and what t param that field corresponds to. Mathf.Repeat is useful for this.
[SerializeField] int pathIndex;
[SerializeField] float pathT;
[SerializeField] float aTimer;
[SerializeField] float pathDuration = 1f; // how long each path takes in seconds
private void ViewBob()
{
// update aTimer but wrap around at pathDuration x 4 ( [0, 4pD) )
aTimer = Mathf.Repeat(aTimer + Time.deltaTime, pathDuration * 4f);
// How many path durations is aTimer along? ( {0, 1, 2, 3} )
pathIndex = Mathf.FloorToInt(aTimer/pathDuration);
// How far into the current path duration is aTimer? ( [0,1) )
pathT = Mathf.Repeat(aTimer, pathDuration) / pathDuration;
Vector3 fromVector;
Vector3 toVector;
// assign to/from vectors based on pathIndex
switch (pathIndex)
{
default:
case 0:
fromVector = new Vector3(-viewBobHorizontal / 2, 0, 0);
toVector = new Vector3(0, -viewBobVertical, 0);
break;
case 1:
fromVector = new Vector3(0, -viewBobVertical, 0);
toVector = new Vector3(viewBobHorizontal / 2, 0, 0);
break;
case 2:
fromVector = new Vector3(viewBobHorizontal / 2, 0, 0);
toVector = new Vector3(0, -viewBobVertical, 0);
break;
case 3:
fromVector = new Vector3(0, -viewBobVertical, 0);
toVector = new Vector3(-viewBobHorizontal / 2, 0, 0);
break;
}
// lerp position based on pathT and the to/from vectors
playerCam.transform.localPosition = Vector3.Lerp(fromVector, toVector, pathT);
}
I am pretty new to the 3D side of XNA and am converting a 2D game.
Its basically designed as a grid and I am drawing with the code below. This works but is a bit laggy, am I doing this completely wrong? Presumably there shouldn't be much lag at this point when we are only talking about a few small models.
protected override void Draw(GameTime gameTime)
{
fpsTimer += gameTime.ElapsedGameTime;
fpsCount++;
if (fpsTimer >= TimeSpan.FromSeconds(1))
{ fpsTimer = TimeSpan.FromSeconds(0); fps = fpsCount; fpsCount = 0; }
GraphicsDevice.Clear(Color.CornflowerBlue);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 1000.0f);
world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
view = Matrix.CreateLookAt(new Vector3(xPos, yPos, zHeight), new Vector3(xPos, yPos + zRotation, 0), new Vector3(0, 5, 0));
// DRAW OBJECTS ON MAP
drawMap(view, world, projection);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.DrawString(font, "" + fps, new Vector2(100, 100), Color.Black);
spriteBatch.End();
base.Draw(gameTime);
}
private void drawMap(Matrix view, Matrix world, Matrix projection)
{
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
int scale = Globals.scale;
int screenWidthScale = Globals.screenwidth / scale;
int screenHeightScale = Globals.screenheight / scale;
int mapXtile = Convert.ToInt32(xPos/2);
int mapYtile = Convert.ToInt32(yPos/2);
for (int i = Convert.ToInt32(xPos/2) - 30; i < Convert.ToInt32(xPos/2) + 30; i++)
{
for (int a = Convert.ToInt32(yPos/2); a < Convert.ToInt32(yPos/2) + 50; a++)
{
if (mapXtile > 0 && mapYtile > 0 && mapXtile < Globals.mapsizex && mapYtile < Globals.mapsizey)
{
int currentTile = Globals.levelArray[mapXtile, mapYtile].TyleType;
// DRAW TREES
if (currentTile == tyleType.tree)
{
if (Globals.levelArray[mapXtile, mapYtile].typeModifier == 1)
{
Matrix worldMatrix = Matrix.CreateScale(0.8f, 0.8f, 0.8f) * Matrix.CreateRotationX(1.5f) * Matrix.CreateTranslation(new Vector3(i * 2 + 0.2f, a * 2 - 0.4f, -0.1f));
tree.Draw(worldMatrix, view, projection);
}
if (Globals.levelArray[mapXtile, mapYtile].typeModifier == 2)
{
Matrix worldMatrix = Matrix.CreateScale(0.8f, 0.8f, 0.8f) * Matrix.CreateRotationX(1.5f) * Matrix.CreateTranslation(new Vector3(i * 2+0.2f, a * 2-0.4f, -0.1f));
tree2.Draw(worldMatrix, view, projection);
}
}
}
mapYtile = mapYtile + 1;
//mapYtile++;
}
mapXtile = mapXtile + 1;
mapYtile = Convert.ToInt32(yPos / 2);
}
}
Hello
This is my function that will detect hit.
private int[] GetSelected(int x, int y)
{
const int max = 512;
var Hit_Buffer = new int[max];
var viewport = new int[4];
Gl.glSelectBuffer(max, Hit_Buffer);
Gl.glRenderMode(Gl.GL_SELECT);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPushMatrix();
Gl.glLoadIdentity();
Glu.gluLookAt(Distance * Math.Cos(beta) * Math.Cos(alpha)
, Distance * Math.Cos(beta) * Math.Sin(alpha)
, Distance * Math.Sin(beta)
, 0, 0, 0
, -Math.Sin(beta) * Math.Cos(alpha)
, -Math.Sin(beta) * Math.Sin(alpha)
, Math.Cos(beta));
Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);
Glu.gluPickMatrix(x, viewport[3] - y, 1, 1, viewport);
Glu.gluPerspective(fovY, ogl1.Width / (double)(ogl1.Height != 0 ? ogl1.Height : 1), 0.1, 100.0);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glInitNames();
// render scene: a TRIANGLE
Gl.glPushName(1);
Gl.glBegin(Gl.GL_TRIANGLES);
Gl.glVertex3d(0, 0, 0);
Gl.glVertex3d(0, 1, 0);
Gl.glVertex3d(1, 0, 0);
Gl.glEnd();
Gl.glPopName();
//
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPopMatrix();
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glFlush();
var hits = Gl.glRenderMode(Gl.GL_RENDER);
Array.Resize(ref Hit_Buffer, hits);
return Hit_Buffer;
}
I am drawing a triangle in xy plane.
In gluLookAt, beta is camera angle from xy plane and alpha is camera angle about z.
But it just works if beta be small ( -15< beta <15 degree )!
What is wrong here?
In OpenGL Y is up
For the first 3 parameters, I'd rather say :
Distance * Math.Cos(beta) * Math.Cos(alpha)
, Distance * Math.Sin(beta)
, Distance * Math.Cos(beta) * Math.Sin(alpha)
And for the 3 last ones, try with (0,1,0) first.
Have you called gluLookAt before gluPickMatrix on purpose. Do you want to apply translations to your pick matrix? If not can you move gluPickMatrix immediately after glLoadIdentity and try if your problem gets resolved?
Hello again
I finally find my answer by myself! (by googling)
Here is my corrected code
private int[] GetSelected(int x, int y, bool debug)
{
var hits = 0; // number of hits
// Define select buffer
const int max = 512;
var Hit_buffer = new int[max];
Gl.glSelectBuffer(max, Hit_buffer);
var viewport = new int[4];
Gl.glViewport(0, 0, Width, (Height != 0 ? Height : 1));
Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);
if (debug)// show real scene in debug mode
GlDraw();
if(!debug)
Gl.glRenderMode(Gl.GL_SELECT);
int s = debug ? 60 : 3; // test region size
Gl.glLoadIdentity();
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPushMatrix();
Gl.glLoadIdentity();
Glu.gluPickMatrix(x, viewport[3] - y, s, s, viewport);
Glu.gluPerspective(fovY, Width / (double)(Height != 0 ? Height : 1), 0.1, 1000.0);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
if (debug) // test region will be shown in left-bottom corner
Gl.glViewport(0, 0, s, s);
#region camera
Gl.glTranslated(Dx, Dy, 0);
var Distance = this.Distance;// *ogl1.Height / 60.0;
var CenterView = this.CenterView.Duplicate();
Glu.gluLookAt(Distance * Math.Cos(beta) * Math.Cos(alpha) + CenterView.x
, Distance * Math.Cos(beta) * Math.Sin(alpha) + CenterView.y
, Distance * Math.Sin(beta) + CenterView.z
, CenterView.x
, CenterView.y
, CenterView.z
, -Math.Sin(beta) * Math.Cos(alpha)
, -Math.Sin(beta) * Math.Sin(alpha)
, Math.Cos(beta));
#endregion
if (debug) // draw a bacground in left-bottom corner
{
ChangeColor(Color.Blue);
Glu.gluSphere(Glu.gluNewQuadric(), 50, 50, 50);
Gl.glBegin(Gl.GL_QUADS);
Gl.glVertex3d(-10, -10, -10);
Gl.glVertex3d(-10, 10, -10);
Gl.glVertex3d(10, 10, -10);
Gl.glVertex3d(10, -10, -10);
Gl.glEnd();
}
Gl.glInitNames();
// render scene
foreach (var b in Bodies)
{
Gl.glPushName(b.id);
var bb = b.Duplicate();
bb.color = Color.Red;
bb.Draw();
Gl.glPopName();
}
//
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPopMatrix();
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glFlush();
if (!debug)
hits = Gl.glRenderMode(Gl.GL_RENDER);
// process hits
int[] Res = { };
int startRecord = 0;
for (int i = 0; i < hits; i++)
{
for (int j = 0; j < Hit_buffer[startRecord]; j++)
{
Array.Resize(ref Res, Res.Length + 1);
Res[Res.Length - 1] = Hit_buffer[startRecord + 3 + j];
}
startRecord += 3 + Hit_buffer[startRecord];
}
return Res;
}
If you set the debug parameter =true, it will draw the select region in left-bottom corner.
The code may be boring, so I am gonna write main parts here
// Define select buffer
const int max = 512;
var Hit_buffer = new int[max];
Gl.glSelectBuffer(max, Hit_buffer);
var viewport = new int[4];
Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);
Gl.glRenderMode(Gl.GL_SELECT);
Gl.glLoadIdentity();
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPushMatrix();
Gl.glLoadIdentity();
Glu.gluPickMatrix(x, viewport[3] - y, 3, 3, viewport);
Glu.gluPerspective(fovY, Width / (double)(Height != 0 ? Height : 1), 0.1, 1000.0);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glInitNames();
// set camera (gluLookAt, ...) & draw scene
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPopMatrix();
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glFlush();
var hits = Gl.glRenderMode(Gl.GL_RENDER);
// process hits
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