Skip to content

Instantly share code, notes, and snippets.

View kbenzie's full-sized avatar

Kenneth Benzie (Benie) kbenzie

View GitHub Profile
@kbenzie
kbenzie / gist:3961122
Created October 26, 2012 20:01
glewInit
if (glewInit() != GLEW_OK) {
glfwTerminate();
exit(EXIT_FAILURE);
}
@kbenzie
kbenzie / gist:4279480
Created December 13, 2012 20:24
enable_if example
#include <type_traits>
#include <stdio.h>
using namespace std;
template <typename T, typename Ty>
typename enable_if<sizeof(Ty)==sizeof(T), typename T>::type convertToType(Ty type)
{
return *(T*)&type;
}
@kbenzie
kbenzie / test.cpp
Created December 13, 2012 22:38
C++ where T : Type
#include "where_T.h"
#include <iostream>
struct IComponent {
virtual void init = 0;
virtual void update(double dt) = 0;
virtual void render() = 0;
}
struct ConcreateComponent : public IComponent {
@kbenzie
kbenzie / instanced.frag
Created January 18, 2013 10:56
OpenGL instancing a model with multiple sub meshes using a single model matrix transform buffer.
#version 330
struct material_data
{
vec4 emissive;
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
};
@kbenzie
kbenzie / from_string.hpp
Created January 29, 2013 17:34
Safely convert string to template parameter type T
#include <string>
#include <sstream>
// Convert string to type T
template <class T> bool from_string(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&)) {
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
@kbenzie
kbenzie / some_cxx11_features.cpp
Last active December 12, 2015 00:59
Raw string literals, uniform initializers, initializer lists, move semantics, ranged for loops, variadic templates for perfect forwarding, auto, I think that's all the C++11 in there. Also enable_if is_base_of in a class template.
// Some C++11 features in use.
#include <iostream>
#include <string>
#include <vector>
#include <type_traits>
using namespace std;
// Raw string literal
string vertex_shader_source = R"glsl(#version 330
@kbenzie
kbenzie / sse_example
Last active September 3, 2021 12:08
Example of using SSE instructions showing the increase in performance.
#include <xmmintrin.h>
#include <cmath>
#include <iostream>
#include <omp.h>
/*
* void example()
* {
* // Size of float array
* const int size = 4;
@kbenzie
kbenzie / cl_status_codes.h
Created May 30, 2013 20:57
Print OpenCL status codes.
#pragma once
#include <cstdio>
#include <sstream>
#include <iomanip>
#include <CL\cl.h>
void print_cl_status(const cl_int status, const int line) {
if (status != CL_SUCCESS) {
@kbenzie
kbenzie / enable_if_example.cpp
Created June 7, 2013 20:22
Example of using std::enable_if
#include <type_traits>
#include <stdio.h>
using namespace std;
template <typename T, typename Ty>
typename enable_if<sizeof(Ty)==sizeof(T), typename T>::type convertToType(Ty type)
{
return *(T*)&type;
}
@kbenzie
kbenzie / glGetTypeString
Created July 24, 2013 20:28
GLenum type string convenience function
static const char * const glGetTypeString(GLenum type)
{
switch (type)
{
case GL_BYTE : return "GLbyte";
case GL_UNSIGNED_BYTE : return "GLubyte";
case GL_SHORT : return "GLshort";
case GL_UNSIGNED_SHORT : return "GLushort";
case GL_INT : return "GLint";
case GL_UNSIGNED_INT : return "GLuint";