Home » Community » Forums » » OpenGL Type Traits
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 OpenGL Type Traits
Post Reply 
A nice trick, thanks!

 User Rating: 1743   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by swiftcoder
A nice trick, thanks!


seconded

 User Rating: 1430   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

I like it, simple and effective.

This is how it would look in D:

template opengl_traits(T : uint)
{
    enum opengl_traits = GL_UNSIGNED_INT;
}

void vertexPointer(T)(GLint size, GLsizei stride, const T* pointer)
{
    glVertexPointer(size, stride, opengl_traits!(T), pointer);
}



 User Rating: 1283   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

thanks, I am glad you like it :)

just wanted to add that in my code I actually have one more enum in opengl_traits specializations GL_SIZE:

template<>
struct opengl_traits<unsigned int>
{
        enum {GL_TYPE = GL_UNSIGNED_INT, GL_SIZE = 1};
};

template<>
struct opengl_traits<unsigned short>
{
        enum {GL_TYPE = GL_UNSIGNED_SHORT, GL_SIZE = 1};
};

template<>
struct opengl_traits<float>
{
        enum {GL_TYPE = GL_FLOAT, GL_SIZE = 1};
};



and in renderer class:

template <class T>
void vertexSource(const T* ptr, GLsizei stride = 0)
{
  glVertexPointer(opengl_traits<T>::GL_SIZE, opengl_traits<T>::GL_TYPE, stride, ptr);
}



this of course doesn't look good at first but consider this :

// vec3.h - just example

template <class T>
class Vec3
{
  T x, y, z;
};

// opengltraits.h
// this is super cool imho ;)

template <class T>
struct opengl_traits<Vec3<T> >
{
  enum 
  {
    GL_TYPE = opengl_traits<T>::GL_TYPE, // this "redirect" to actual definition
    GL_SIZE = 3 // this is of course number of components in Vec3
  };
};



this ONE templated traits definition enabled Vec3<T> deduction support for all previously defined basic types, so now:

vector<Vec3<float> > vertices;

vertexPointer(&vertices[0]);



voila :)

in fact I have a few traits more, for 2/4 d vectors and colors






 User Rating: 936   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: