Permalink
Browse files

draw a noisy texture

  • Loading branch information...
1 parent 3c97684 commit d42ed1fcce283cdcb83efe00493914a496fdeb5b @dwrensha dwrensha committed Sep 8, 2012
Showing with 73 additions and 39 deletions.
  1. +57 −29 examples/opengl/game.sml
  2. BIN media/graphics/noise.png
  3. +7 −4 opengl/GL.sig
  4. +9 −6 opengl/GL.sml
View
@@ -34,6 +34,28 @@ struct
end
val robot = Graphics.requireimage "media/graphics/robot.png"
+ val noise = Graphics.requireimage "media/graphics/noise.png"
+
+ fun load_texture surface w h =
+ let
+ val texture = glGenSingleTexture ()
+ val mode = case (SDL.get_bytes_per_pixel surface,
+ SDL.is_rgb surface) of
+ (4, true) => GL_RGBA
+ | (4, false) => GL_BGRA
+ | (_, true) => GL_RGB
+ | (_, false) => GL_BGR
+ in
+ glBindTexture GL_TEXTURE_2D texture;
+ glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST;
+ glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST;
+ glTexImage2D GL_TEXTURE_2D 0 4 w h 0 mode GL_UNSIGNED_BYTE (SDL.getpixels surface);
+ texture
+ end
+
+
+ val noise_texture = ref 0;
+ val robot_texture = ref 0;
fun initscreen screen =
(
@@ -43,34 +65,25 @@ struct
glEnable(GL_TEXTURE_2D);
- glClearColor 0.0 0.0 0.0 1.0;
- glClearDepth 1.0;
- glViewport 0 0 width height;
- glClear GL_COLOR_BUFFER_BIT;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho ~5.0 5.0 ~5.0 5.0 5.0 ~5.0;
- glMatrixMode(GL_MODELVIEW);
-
- glLoadIdentity();
- let
- val texture = glGenSingleTexture ()
- val mode = case (SDL.get_bytes_per_pixel robot,
- SDL.is_rgb robot) of
- (4, true) => GL_RGBA
- | (4, false) => GL_BGRA
- | (_, true) => GL_RGB
- | (_, false) => GL_BGR
- in
- glBindTexture GL_TEXTURE_2D texture;
- glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST;
- glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST;
- glTexImage2D GL_TEXTURE_2D 0 4 16 32 0 mode GL_UNSIGNED_BYTE (SDL.getpixels robot);
- ()
- end
+ glClearColor 0.0 0.0 0.0 1.0;
+ glClearDepth 1.0;
+ glViewport 0 0 width height;
+ glClear GL_COLOR_BUFFER_BIT;
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho ~5.0 5.0 ~5.0 5.0 5.0 ~5.0;
+ glMatrixMode(GL_MODELVIEW);
+
+ glLoadIdentity();
+ noise_texture := load_texture noise 8 8;
+ robot_texture := load_texture robot 16 32;
+ ()
)
+
+
+
fun move_right (L {xpos=x, ypos=y}) = L {xpos=x+dpos_robot, ypos=y}
fun move_left (L {xpos=x, ypos=y}) = L {xpos=x-dpos_robot, ypos=y}
fun move_up (L {xpos=x, ypos=y}) = L {xpos=x, ypos=y+dpos_robot}
@@ -86,17 +99,18 @@ struct
(* draw a square *)
glBegin(GL_QUADS);
glColor3f 0.9 1.0 0.0;
- glVertex3f (~ 2.0) 2.0 0.0;
- glVertex3f 2.0 2.0 0.0;
+ glVertex3f (~ 4.0) 4.0 0.0;
+ glVertex3f 4.0 4.0 0.0;
glColor3f 0.0 0.8 0.9;
- glVertex3f 2.0 (~ 2.0) 0.0;
- glVertex3f (~ 2.0) (~ 2.0) 0.0;
+ glVertex3f 4.0 (~ 4.0) 0.0;
+ glVertex3f (~4.0) (~ 4.0) 0.0;
glEnd();
glEnable GL_TEXTURE_2D;
glColor3f 1.0 1.0 1.0;
(* draw the robot *)
+ glBindTexture GL_TEXTURE_2D (!robot_texture);
glBegin(GL_QUADS);
glTexCoord2i 0 1;
glVertex3f sx sy 0.0;
@@ -108,6 +122,20 @@ struct
glVertex3f sx (sy + 4.0) 0.0;
glEnd();
+ (* draw a noisy rectangle *)
+ glBindTexture GL_TEXTURE_2D (!noise_texture);
+ glBegin(GL_QUADS);
+ glTexCoord2i 0 0;
+ glVertex3f (~2.0) 0.0 0.0;
+ glTexCoord2i 0 2;
+ glVertex3f (~4.0) (~2.0) 0.0;
+ glTexCoord2i 4 2;
+ glVertex3f 0.0 (~5.0) 0.0;
+ glTexCoord2i 4 0;
+ glVertex3f (2.0) (~3.0) 0.0;
+ glEnd();
+
+
glFlush();
SDL.glflip();
()
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -823,14 +823,14 @@ signature GL =
val c_glPushAttrib : GLenum -> unit
val glPushAttrib : GLenum -> unit
- val c_glRasterPos2i : int * int -> unit
- val glRasterPos2i : int -> int -> unit
+ val c_glRasterPos2d : GLdouble * GLdouble -> unit
+ val glRasterPos2d : GLdouble -> GLdouble -> unit
val c_glRasterPos2f : GLreal * GLreal -> unit
val glRasterPos2f : GLreal -> GLreal -> unit
- val c_glRasterPos2d : GLdouble * GLdouble -> unit
- val glRasterPos2d : GLdouble -> GLdouble -> unit
+ val c_glRasterPos2i : int * int -> unit
+ val glRasterPos2i : int -> int -> unit
val c_glRotatef: GLreal * GLreal * GLreal * GLreal -> unit
val glRotatef: GLreal -> GLreal -> GLreal -> GLreal -> unit
@@ -841,6 +841,9 @@ signature GL =
val c_glShadeModel : GLenum -> unit
val glShadeModel : GLenum -> unit
+ val c_glTexCoord2f : GLreal * GLreal -> unit
+ val glTexCoord2f : GLreal -> GLreal -> unit
+
val c_glTexCoord2i : int * int -> unit
val glTexCoord2i : int -> int -> unit
View
@@ -857,17 +857,17 @@ structure GL :> GL =
val c_glPushMatrix = _import "glPushMatrix" stdcall: unit -> unit;
fun glPushMatrix () = c_glPushMatrix (): unit;
- val c_glRasterPos2i = _import "glRasterPos2i" stdcall: int * int -> unit;
- fun glRasterPos2i (a:int) (b:int)
- = c_glRasterPos2i (a,b) : unit
+ val c_glRasterPos2d = _import "glRasterPos2d" stdcall: GLdouble * GLdouble -> unit;
+ fun glRasterPos2d (a:GLdouble) (b:GLdouble)
+ = c_glRasterPos2d (a,b) : unit
val c_glRasterPos2f = _import "glRasterPos2f" stdcall: GLreal * GLreal -> unit;
fun glRasterPos2f (a:GLreal) (b:GLreal)
= c_glRasterPos2f (a,b) : unit
- val c_glRasterPos2d = _import "glRasterPos2d" stdcall: GLdouble * GLdouble -> unit;
- fun glRasterPos2d (a:GLdouble) (b:GLdouble)
- = c_glRasterPos2d (a,b) : unit
+ val c_glRasterPos2i = _import "glRasterPos2i" stdcall: int * int -> unit;
+ fun glRasterPos2i (a:int) (b:int)
+ = c_glRasterPos2i (a,b) : unit
val c_glRotated = _import "glRotated" stdcall: GLdouble * GLdouble * GLdouble * GLdouble -> unit;
fun glRotated (a:GLdouble) (b:GLdouble) (c:GLdouble) (d:GLdouble)
@@ -880,6 +880,9 @@ structure GL :> GL =
val c_glShadeModel = _import "glShadeModel" stdcall: GLenum -> unit;
fun glShadeModel (a:GLenum)= c_glShadeModel (a): unit;
+ val c_glTexCoord2f = _import "glTexCoord2f" stdcall: GLreal * GLreal -> unit;
+ fun glTexCoord2f s t = c_glTexCoord2f (s, t) : unit;
+
val c_glTexCoord2i = _import "glTexCoord2i" stdcall: int * int -> unit;
fun glTexCoord2i s t = c_glTexCoord2i (s, t) : unit;

0 comments on commit d42ed1f

Please sign in to comment.