[HOpenGL] Conversion of GLsizei to GLfloat

Jason Dagit dagitj at gmail.com
Fri Jan 20 15:32:36 CET 2012


On Fri, Jan 20, 2012 at 3:58 AM, Achim Krause <info at achim-krause.de> wrote:
> Hello everyone,
> I'm making my first experiences with OpenGL at the moment. It's really
> nice to use, but I got one issue which really freaks me out:
> Let's say I want to create a square-grid across the whole screen as
> background of a very basic 2D game. The application is meant to run in
> full screen mode. Since I want it to work on different resolutions and
> screen formats I introduced an IORef relSize (of type (GLfloat,GLfloat))
> which is meant to store the size of the squares relative to the current
> window size, and tried to set a reshape callback in the form of
> reshape relS s@(Size w h) = do
>  viewport $= (Position 0 0, s)
>  relS $= (size / w, size / h)
> where size is just a name for the (at another place) defined absolute
> size for the squares. The Problem is that i see absolutely no way to
> convert w and h (which are of type GLsizei) in order to yield a value of
> type GLfloat. Is there any way to extract a Haskell Integer value from
> values of type GLsizei and convert a Haskell Float value to a GLfloat?
> Or is there a more direct way to do what I'm trying to achieve?

Conversion between numeric types is something that trips up everyone
in Haskell when they first encounter it, so don't feel bad.

Almost anytime you are going from some type of integer to something
else you use fromIntegral.  As an example:

GHCi, version 7.2.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> import Graphics.Rendering.OpenGL
Prelude Graphics.Rendering.OpenGL> let a = 1 :: GLsizei
Loading package OpenGLRaw-1.1.0.1 ... linking ... done.
Prelude Graphics.Rendering.OpenGL> let b = 2 :: GLfloat
Prelude Graphics.Rendering.OpenGL> a + b

<interactive>:0:5:
    Couldn't match expected type `GLsizei' with actual type `GLfloat'
    In the second argument of `(+)', namely `b'
    In the expression: a + b
    In an equation for `it': it = a + b
Prelude Graphics.Rendering.OpenGL> fromIntegral a + b
3.0

I hope that helps,
Jason



More information about the HOpenGL mailing list