Skip to content

Instantly share code, notes, and snippets.

@sarnesjo
Last active December 14, 2015 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarnesjo/5116084 to your computer and use it in GitHub Desktop.
Save sarnesjo/5116084 to your computer and use it in GitHub Desktop.
#define GLFW_INCLUDE_GL3
#define GLFW_NO_GLU
#include <GL/glfw.h>
#include <stdio.h>
const char *vertexShaderSource =
"#version 150\n"
"in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position, 1);\n"
"}\n";
const char *fragmentShaderSource =
"#version 150\n"
"out vec4 fragmentColor;\n"
"void main()\n"
"{\n"
"fragmentColor = vec4(1, 1, 1, 1);\n"
"}\n";
const GLfloat vertexData[9] =
{
0.0f, 0.9f, 0.0f,
-0.9f, -0.9f, 0.0f,
0.9f, -0.9f, 0.0f
};
int main()
{
glfwInit();
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwOpenWindow(640, 640, 0, 0, 0, 0, 24, 8, GLFW_WINDOW);
if (glfwGetWindowParam(GLFW_ACCELERATED) != GL_TRUE)
return 1;
int x, y, z;
glfwGetVersion(&x, &y, &z); printf("%d.%d.%d\n", x, y, z);
glfwGetGLVersion(&x, &y, &z); printf("%d.%d.%d\n", x, y, z);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 3 * 3 * 4, vertexData, GL_STATIC_DRAW);
GLuint v = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(v, 1, &vertexShaderSource, NULL);
glCompileShader(v);
GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(f, 1, &fragmentShaderSource, NULL);
glCompileShader(f);
GLuint p = glCreateProgram();
glAttachShader(p, v);
glAttachShader(p, f);
glBindAttribLocation(p, 0, "position");
glBindFragDataLocation(p, 0, "fragmentColor");
glLinkProgram(p);
glUseProgram(p);
glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, NULL);
glEnableVertexAttribArray(0);
while (glfwGetWindowParam(GLFW_OPENED) == GL_TRUE)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers();
}
glfwTerminate();
return 0;
}
import Control.Monad (when)
import Data.Bits ((.|.))
import Foreign.C.String (withCString)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Array (withArray)
import Foreign.Ptr (nullPtr)
import Foreign.Storable (peek)
import Graphics.Rendering.OpenGL.Raw -- OpenGLRaw-1.3.0.0
import Graphics.UI.GLFW -- GLFW-b-0.1.0.5
displayOptions :: DisplayOptions
displayOptions = defaultDisplayOptions
{ displayOptions_width = 640
, displayOptions_height = 640
, displayOptions_numDepthBits = 24
, displayOptions_numStencilBits = 8
, displayOptions_windowIsResizable = False
, displayOptions_openGLVersion = (3, 2)
, displayOptions_openGLProfile = CoreProfile
}
vertexShaderSource :: String
vertexShaderSource = unlines
[ "#version 150"
, "in vec3 position;"
, "void main()"
, "{"
, "gl_Position = vec4(position, 1);"
, "}"
]
fragmentShaderSource :: String
fragmentShaderSource = unlines
[ "#version 150"
, "out vec4 fragmentColor;"
, "void main()"
, "{"
, "fragmentColor = vec4(1, 1, 1, 1);"
, "}"
]
vertexData :: [GLfloat]
vertexData =
[ 0.0 , 0.9 , 0.0
, -0.9 , -0.9 , 0.0
, 0.9 , -0.9 , 0.0
]
while :: Monad m => m Bool -> m () -> m ()
while p a = p >>= flip when (a >> while p a)
main :: IO ()
main = do
True <- initialize
True <- openWindow displayOptions
True <- windowIsHardwareAccelerated
getGlfwVersion >>= (putStrLn . show)
getGlVersion >>= (putStrLn . show)
vao <- alloca $ \ptr -> glGenVertexArrays 1 ptr >> peek ptr
glBindVertexArray vao
vbo <- alloca $ \ptr -> glGenBuffers 1 ptr >> peek ptr
glBindBuffer gl_ARRAY_BUFFER vbo
withArray vertexData $ \ptr -> glBufferData gl_ARRAY_BUFFER (3 * 3 * 4) ptr gl_STATIC_DRAW
v <- glCreateShader gl_VERTEX_SHADER
withCString vertexShaderSource $ \str -> withArray [str] $ \ptr -> glShaderSource v 1 ptr nullPtr
glCompileShader v
f <- glCreateShader gl_FRAGMENT_SHADER
withCString fragmentShaderSource $ \str -> withArray [str] $ \ptr -> glShaderSource f 1 ptr nullPtr
glCompileShader f
p <- glCreateProgram
glAttachShader p v
glAttachShader p f
withCString "position" $ \str -> glBindAttribLocation p 0 str
withCString "fragmentColor" $ \str -> glBindFragDataLocation p 0 str
glLinkProgram p
glUseProgram p
glVertexAttribPointer 0 3 gl_FLOAT 0 0 nullPtr
glEnableVertexAttribArray 0
while windowIsOpen mainLoop
terminate
mainLoop :: IO ()
mainLoop = do
glClear (gl_COLOR_BUFFER_BIT .|. gl_DEPTH_BUFFER_BIT)
glDrawArrays gl_TRIANGLES 0 3
swapBuffers
#include <stdio.h>
int glfwInit();
void glfwOpenWindowHint(int, int);
int glfwOpenWindow(int, int, int, int, int, int, int, int, int);
int glfwGetWindowParam(int);
void glfwGetVersion(int*, int*, int*);
void glfwGetGLVersion(int*, int*, int*);
void glfwTerminate();
int main()
{
glfwInit();
glfwOpenWindowHint(0x00020014, 3);
glfwOpenWindowHint(0x00020015, 2);
glfwOpenWindowHint(0x00020018, 0x00050001);
glfwOpenWindow(640, 640, 0, 0, 0, 0, 24, 8, 0x00010001);
if (glfwGetWindowParam(0x00020004) == 1)
printf("hardware\n");
else
printf("software\n");
int x, y, z;
glfwGetVersion(&x, &y, &z); printf("%d.%d.%d\n", x, y, z);
glfwGetGLVersion(&x, &y, &z); printf("%d.%d.%d\n", x, y, z);
glfwTerminate();
return 0;
}
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign.C.Types
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
foreign import ccall glfwInit :: IO CInt
foreign import ccall glfwOpenWindowHint :: CInt -> CInt -> IO ()
foreign import ccall glfwOpenWindow :: CInt -> CInt -> CInt -> CInt -> CInt -> CInt -> CInt -> CInt -> CInt -> IO CInt
foreign import ccall glfwGetWindowParam :: CInt -> IO CInt
foreign import ccall glfwGetVersion :: Ptr CInt -> Ptr CInt -> Ptr CInt -> IO ()
foreign import ccall glfwGetGLVersion :: Ptr CInt -> Ptr CInt -> Ptr CInt -> IO ()
foreign import ccall glfwTerminate :: IO ()
main :: IO ()
main = do
glfwInit
glfwOpenWindowHint 0x00020014 3
glfwOpenWindowHint 0x00020015 2
glfwOpenWindowHint 0x00020018 0x00050001
glfwOpenWindow 640 640 0 0 0 0 24 8 0x00010001
a <- glfwGetWindowParam 0x00020004
if a == 1
then putStrLn "hardware"
else putStrLn "software"
alloca $ \x -> alloca $ \y -> alloca $ \z -> do
glfwGetVersion x y z
x' <- peek x
y' <- peek y
z' <- peek z
putStrLn $ show (x', y', z')
alloca $ \x -> alloca $ \y -> alloca $ \z -> do
glfwGetGLVersion x y z
x' <- peek x
y' <- peek y
z' <- peek z
putStrLn $ show (x', y', z')
glfwTerminate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment