static bool arglPixelBufferDataUpload(ARGL_CONTEXT_SETTINGS contextSettings, byte[] bufDataPtr) { if (contextSettings == null) { return(false); } if (contextSettings.textureObjectsHaveBeenSetup || contextSettings.textureGeometryHasBeenSetup || contextSettings.pixSize == 0) { return(false); } GL1.ActiveTexture(All1.Texture0); GL1.BindTexture(All1.Texture2D, contextSettings.texture); GL1.PixelStore(All1.UnpackAlignment, (((contextSettings.bufSizeX * contextSettings.pixSize) & 0x3) == 0 ? 4 : 1)); if (contextSettings.bufSizeIsTextureSize) { GL1.TexImage2D(All1.Texture2D, 0, (OpenTK.Graphics.ES11.All)contextSettings.pixIntFormat, contextSettings.textureSizeX, contextSettings.textureSizeY, 0, (All1)contextSettings.pixFormat, (All1)contextSettings.pixType, bufDataPtr); } else { // Request OpenGL allocate memory internally for a power-of-two texture of the appropriate size. // Then send the NPOT-data as a subimage. GL1.TexImage2D(All1.Texture2D, 0, (OpenTK.Graphics.ES11.All)contextSettings.pixIntFormat, contextSettings.textureSizeX, contextSettings.textureSizeY, 0, (All1)contextSettings.pixFormat, (All1)contextSettings.pixType, IntPtr.Zero); GL1.TexSubImage2D(All1.Texture2D, 0, 0, 0, contextSettings.bufSizeX, contextSettings.bufSizeY, (All1)contextSettings.pixFormat, (All1)contextSettings.pixType, bufDataPtr); } contextSettings.textureDataReady = true; return(true); }
// Set up the texture objects. static bool arglSetupTextureObjects(ARGL_CONTEXT_SETTINGS contextSettings) { int textureWrapMode; // Delete previous textures, unless this is our first time here. if (contextSettings.textureObjectsHaveBeenSetup) { GL1.ActiveTexture(All1.Texture0); GL1.BindTexture(All1.Texture2D, 0); GL1.DeleteTextures(1, ref contextSettings.texture); contextSettings.textureObjectsHaveBeenSetup = false; } GL1.GenTextures(1, out contextSettings.texture); GL1.ActiveTexture(All1.Texture0); GL1.BindTexture(All1.Texture2D, contextSettings.texture); GL1.TexParameterx(All1.Texture2D, All1.TextureMinFilter, (int)All1.Linear); GL1.TexParameterx(All1.Texture2D, All1.TextureMagFilter, (int)All1.Linear); // Decide whether we can use GL_CLAMP_TO_EDGE. //if (arglGLCapabilityCheck(0x0120, (unsigned char *)"GL_SGIS_texture_edge_clamp")) { textureWrapMode = (int)All1.ClampToEdge; //} else { // textureWrapMode = (int)All1.Repeat; //} GL1.TexParameterx(All1.Texture2D, All1.TextureWrapS, textureWrapMode); GL1.TexParameterx(All1.Texture2D, All1.TextureWrapT, textureWrapMode); contextSettings.textureObjectsHaveBeenSetup = true; return(true); }
public static ARGL_CONTEXT_SETTINGS arglSetupForCurrentContext(ARParam cparam, AR_PIXEL_FORMAT pixelFormat) { ARGL_CONTEXT_SETTINGS contextSettings; contextSettings = new ARGL_CONTEXT_SETTINGS(); contextSettings.arParam = cparam; // Copy it. contextSettings.zoom = 1.0f; // Because of calloc used above, these are redundant. //contextSettings.rotate90 = contextSettings.flipH = contextSettings.flipV = false; //contextSettings.disableDistortionCompensation = false; //contextSettings.textureGeometryHasBeenSetup = false; //contextSettings.textureObjectsHaveBeenSetup = false; //contextSettings.textureDataReady = false; // This sets pixIntFormat, pixFormat, pixType, pixSize, and resets textureDataReady. arglPixelFormatSet(contextSettings, pixelFormat); // Set pixel buffer sizes to incoming image size, by default. if (!arglPixelBufferSizeSet(contextSettings, cparam.xsize, cparam.ysize)) { Debug.Print("ARGL: Error setting pixel buffer size.\n"); contextSettings = null; return(null); } return(contextSettings); }
static bool arglGetRotate90(ARGL_CONTEXT_SETTINGS contextSettings) { if (contextSettings == null) { return(false); } return(contextSettings.rotate90); }
static void arglSetFlipV(ARGL_CONTEXT_SETTINGS contextSettings, bool flipV) { if (contextSettings == null) { return; } contextSettings.flipV = flipV; }
static bool arglGetFlipV(ARGL_CONTEXT_SETTINGS contextSettings) { if (contextSettings == null) { return(false); } return(contextSettings.flipV); }
static void arglSetRotate90(ARGL_CONTEXT_SETTINGS contextSettings, bool rotate90) { if (contextSettings == null) { return; } contextSettings.rotate90 = rotate90; }
static bool arglDistortionCompensationGet(ARGL_CONTEXT_SETTINGS contextSettings, ref bool enable) { if (contextSettings == null || !enable) { return(false); } enable = contextSettings.disableDistortionCompensation; return(true); }
static bool arglDistortionCompensationSet(ARGL_CONTEXT_SETTINGS contextSettings, bool enable) { if (contextSettings == null) { return(false); } contextSettings.disableDistortionCompensation = !enable; return(arglSetupTextureGeometry(contextSettings)); }
static bool arglGetPixelZoom(ARGL_CONTEXT_SETTINGS contextSettings, ref float zoom) { if (contextSettings == null) { return(false); } zoom = contextSettings.zoom; return(true); }
static bool arglSetPixelZoom(ARGL_CONTEXT_SETTINGS contextSettings, float zoom) { if (contextSettings == null) { return(false); } contextSettings.zoom = zoom; // Changing the zoom invalidates the geometry, so set it up. return(arglSetupTextureGeometry(contextSettings)); }
static bool arglPixelFormatGet(ARGL_CONTEXT_SETTINGS contextSettings, ref AR_PIXEL_FORMAT format, ref int size) { if (contextSettings == null) { return(false); } format = contextSettings.format; size = contextSettings.pixSize; return(true); }
static void arglCleanup(ARGL_CONTEXT_SETTINGS contextSettings) { if (contextSettings == null) { return; // Sanity check. } if (contextSettings.textureObjectsHaveBeenSetup) { GL1.ActiveTexture(All1.Texture0); GL1.BindTexture(All1.Texture2D, 0); GL1.DeleteTextures(1, ref contextSettings.texture); } if (contextSettings.textureGeometryHasBeenSetup) { GL1.DeleteBuffers(1, ref contextSettings.t2bo); GL1.DeleteBuffers(1, ref contextSettings.v2bo); } contextSettings = null; }
static bool arglPixelBufferSizeSet(ARGL_CONTEXT_SETTINGS contextSettings, int bufWidth, int bufHeight) { if (contextSettings == null) { return(false); } // Check texturing capabilities (sets textureSizeX, textureSizeY, textureSizeMax). GL1.GetInteger(All1.MaxTextureSize, out contextSettings.textureSizeMax); if (bufWidth > contextSettings.textureSizeMax || bufHeight > contextSettings.textureSizeMax) { Debug.Print("Error: ARGL: Your OpenGL implementation and/or hardware's texturing capabilities are insufficient.\n"); return(false); } contextSettings.textureSizeX = bufWidth; contextSettings.textureSizeY = bufHeight; contextSettings.bufSizeIsTextureSize = true; // Changing the size of the data we'll be receiving invalidates the geometry, so set it up. return(arglSetupTextureGeometry(contextSettings)); }
static bool arglPixelBufferSizeGet(ARGL_CONTEXT_SETTINGS contextSettings, ref int bufWidth, ref int bufHeight) { if (contextSettings == null) { return(false); } if (contextSettings.textureGeometryHasBeenSetup) { return(false); } if (contextSettings.bufSizeIsTextureSize) { bufWidth = contextSettings.textureSizeX; bufHeight = contextSettings.textureSizeY; } else { bufWidth = contextSettings.bufSizeX; bufHeight = contextSettings.bufSizeY; } return(true); }
static bool arglPixelFormatSet(ARGL_CONTEXT_SETTINGS contextSettings, AR_PIXEL_FORMAT format) { if (contextSettings == null) { return(false); } switch (format) { case AR_PIXEL_FORMAT.AR_PIXEL_FORMAT_Rgba: contextSettings.pixIntFormat = (int)All1.Rgba; contextSettings.pixFormat = (int)All1.Rgba; contextSettings.pixType = (int)All1.UnsignedByte; contextSettings.pixSize = 4; break; case AR_PIXEL_FORMAT.AR_PIXEL_FORMAT_Bgra: // Windows. contextSettings.pixIntFormat = (int)All1.Rgba; contextSettings.pixFormat = (int)All1.Bgra; contextSettings.pixType = (int)All1.UnsignedByte; contextSettings.pixSize = 4; break; case AR_PIXEL_FORMAT.AR_PIXEL_FORMAT_ABGR: // SGI. contextSettings.pixIntFormat = (int)All1.Rgba; contextSettings.pixFormat = (int)All1.AbgrExt; contextSettings.pixType = (int)All1.UnsignedByte; contextSettings.pixSize = 4; break; case AR_PIXEL_FORMAT.AR_PIXEL_FORMAT_ARGB: // Mac. contextSettings.pixIntFormat = (int)All1.Rgba; contextSettings.pixFormat = (int)All1.Bgra; contextSettings.pixType = (int)All1.UnsignedInt8888; contextSettings.pixSize = 4; break; case AR_PIXEL_FORMAT.AR_PIXEL_FORMAT_RGB: contextSettings.pixIntFormat = (int)All1.Rgb; contextSettings.pixFormat = (int)All1.Rgb; contextSettings.pixType = (int)All1.UnsignedByte; contextSettings.pixSize = 3; break; case AR_PIXEL_FORMAT.AR_PIXEL_FORMAT_BGR: contextSettings.pixIntFormat = (int)All1.Rgb; contextSettings.pixFormat = (int)All1.Rgb; contextSettings.pixType = (int)All1.UnsignedByte; contextSettings.pixSize = 3; break; default: return(false); break; } contextSettings.format = format; contextSettings.textureDataReady = false; if (!arglSetupTextureObjects(contextSettings)) { return(false); } return(true); }
static void arglDispImageStateful(ARGL_CONTEXT_SETTINGS contextSettings) { int texEnvModeSave; int i; if (contextSettings == null) { return; } if (contextSettings.textureObjectsHaveBeenSetup) { return; } if (contextSettings.textureGeometryHasBeenSetup) { return; } if (contextSettings.textureDataReady) { return; } GL1.ActiveTexture(All1.Texture0); GL1.MatrixMode(All1.Texture); GL1.LoadIdentity(); GL1.MatrixMode(All1.Modelview); GL1.BindTexture(All1.Texture2D, contextSettings.texture); GL1.GetTexEnv(All1.TextureEnv, All1.TextureEnvMode, out texEnvModeSave); // Save GL texture environment mode. if (texEnvModeSave != (int)All1.Replace) { GL1.TexEnv(All1.TextureEnv, All1.TextureEnvMode, (int)All1.Replace); } GL1.Enable(All1.Texture2D); GL1.ClientActiveTexture(All1.Texture0); GL1.BindBuffer(All1.ArrayBuffer, contextSettings.t2bo); GL1.TexCoordPointer(2, All1.Float, 0, IntPtr.Zero); GL1.EnableClientState(All1.TextureCoordArray); GL1.BindBuffer(All1.ArrayBuffer, contextSettings.v2bo); GL1.VertexPointer(2, All1.Float, 0, IntPtr.Zero); GL1.EnableClientState(All1.VertexArray); GL1.DisableClientState(All1.NormalArray); if (contextSettings.disableDistortionCompensation) { GL1.DrawArrays(All1.TriangleStrip, 0, 4); } else { for (i = 0; i < 20; i++) { GL1.DrawArrays(All1.TriangleStrip, i * 42, 42); } } GL1.BindBuffer(All1.ArrayBuffer, 0); GL1.DisableClientState(All1.VertexArray); GL1.DisableClientState(All1.TextureCoordArray); GL1.Disable(All1.Texture2D); if (texEnvModeSave != (int)All1.Replace) { GL1.TexEnv(All1.TextureEnv, All1.TextureEnvMode, texEnvModeSave); // Restore GL texture environment mode. } }
public static bool arglSetupTextureGeometry(ARGL_CONTEXT_SETTINGS contextSettings) { float ty_prev, tx, ty; float y_prev, x, y; double x1, x2, y1, y2; float xx1, xx2, yy1, yy2; int i, j; int vertexCount, t2count, v2count; float imageSizeX, imageSizeY; float zoom; // Delete previous geometry, unless this is our first time here. if (contextSettings.textureGeometryHasBeenSetup) { GL1.DeleteBuffers(1, ref contextSettings.t2bo); GL1.DeleteBuffers(1, ref contextSettings.v2bo); contextSettings.textureGeometryHasBeenSetup = false; } // Set up the geometry for the surface which we will texture upon. imageSizeX = (float)contextSettings.arParam.xsize; imageSizeY = (float)contextSettings.arParam.ysize; zoom = contextSettings.zoom; if (contextSettings.disableDistortionCompensation) { vertexCount = 4; } else { vertexCount = 840; // 20 rows of 2 x 21 vertices. } contextSettings.t2 = new float[2 * vertexCount]; contextSettings.v2 = new float[2 * vertexCount]; t2count = v2count = 0; if (contextSettings.disableDistortionCompensation) { contextSettings.t2[t2count++] = 0.0f; // Top-left. contextSettings.t2[t2count++] = 0.0f; contextSettings.v2[v2count++] = 0.0f; contextSettings.v2[v2count++] = imageSizeY * zoom; contextSettings.t2[t2count++] = 0.0f; // Bottom-left. contextSettings.t2[t2count++] = imageSizeY / (float)contextSettings.textureSizeY; contextSettings.v2[v2count++] = 0.0f; contextSettings.v2[v2count++] = 0.0f; contextSettings.t2[t2count++] = imageSizeX / (float)contextSettings.textureSizeX; // Top-right. contextSettings.t2[t2count++] = 0.0f; contextSettings.v2[v2count++] = imageSizeX * zoom; contextSettings.v2[v2count++] = imageSizeY * zoom; contextSettings.t2[t2count++] = imageSizeX / (float)contextSettings.textureSizeX; // Bottom-right. contextSettings.t2[t2count++] = imageSizeY / (float)contextSettings.textureSizeY; contextSettings.v2[v2count++] = imageSizeX * zoom; contextSettings.v2[v2count++] = 0.0f; } else { y = 0.0f; ty = 0.0f; for (j = 1; j <= 20; j++) // Do 20 rows of triangle strips. { y_prev = y; ty_prev = ty; y = imageSizeY * (float)j / 20.0f; ty = y / (float)contextSettings.textureSizeY; for (i = 0; i <= 20; i++) // 21 columns of triangle strip vertices, 2 vertices per column. { x = imageSizeX * (float)i / 20.0f; tx = x / (float)contextSettings.textureSizeX; arParamObserv2Ideal(contextSettings.arParam.dist_factor, (double)x, (double)y_prev, out x1, out y1, contextSettings.arParam.dist_function_version); arParamObserv2Ideal(contextSettings.arParam.dist_factor, (double)x, (double)y, out x2, out y2, contextSettings.arParam.dist_function_version); xx1 = (float)x1 * zoom; yy1 = (imageSizeY - (float)y1) * zoom; xx2 = (float)x2 * zoom; yy2 = (imageSizeY - (float)y2) * zoom; contextSettings.t2[t2count++] = tx; // Top. contextSettings.t2[t2count++] = ty_prev; contextSettings.v2[v2count++] = xx1; contextSettings.v2[v2count++] = yy1; contextSettings.t2[t2count++] = tx; // Bottom. contextSettings.t2[t2count++] = ty; contextSettings.v2[v2count++] = xx2; contextSettings.v2[v2count++] = yy2; } // columns. } // rows. } // Now setup VBOs. GL1.GenBuffers(1, out contextSettings.t2bo); GL1.GenBuffers(1, out contextSettings.v2bo); GL1.BindBuffer(All1.ArrayBuffer, contextSettings.t2bo); GL1.BufferData(All1.ArrayBuffer, new IntPtr((sizeof(float) * 2 * vertexCount)), contextSettings.t2, All1.StaticDraw); GL1.BindBuffer(All1.ArrayBuffer, contextSettings.v2bo); GL1.BufferData(All1.ArrayBuffer, new IntPtr((sizeof(float) * 2 * vertexCount)), contextSettings.v2, All1.StaticDraw); GL1.BindBuffer(All1.ArrayBuffer, 0); contextSettings.textureGeometryHasBeenSetup = true; return(true); }
static void arglDispImage(ARGL_CONTEXT_SETTINGS contextSettings) { float left, right, bottom, top; bool lightingSave; bool depthTestSave; if (contextSettings == null) { return; } // Prepare an orthographic projection, set camera position for 2D drawing, and save GL state. GL1.MatrixMode(All1.Projection); GL1.PushMatrix(); GL1.LoadIdentity(); if (contextSettings.rotate90) { GL1.Rotate(90.0f, 0.0f, 0.0f, -1.0f); } if (contextSettings.flipV) { bottom = (float)contextSettings.arParam.ysize; top = 0.0f; } else { bottom = 0.0f; top = (float)contextSettings.arParam.ysize; } if (contextSettings.flipH) { left = (float)contextSettings.arParam.xsize; right = 0.0f; } else { left = 0.0f; right = (float)contextSettings.arParam.xsize; } GL1.Ortho(left, right, bottom, top, -1.0f, 1.0f); GL1.MatrixMode(All1.Modelview); GL1.PushMatrix(); GL1.LoadIdentity(); lightingSave = GL1.IsEnabled(All1.Lighting); // Save enabled state of lighting. if (lightingSave == true) { GL1.Disable(All1.Lighting); } depthTestSave = GL1.IsEnabled(All1.DepthTest); // Save enabled state of depth test. if (depthTestSave == true) { GL1.Disable(All1.DepthTest); } arglDispImageStateful(contextSettings); if (depthTestSave == true) { GL1.Enable(All1.DepthTest); // Restore enabled state of depth test. } if (lightingSave == true) { GL1.Enable(All1.Lighting); // Restore enabled state of lighting. } // Restore previous projection & camera position. GL1.MatrixMode(All1.Projection); GL1.PopMatrix(); GL1.MatrixMode(All1.Modelview); GL1.PopMatrix(); // Report any errors we generated. int err = (int)GL1.GetError(); while (err != (int)All1.NoError) { Debug.Print("ARGL: GL error 0x%04X\n", (int)err); } }