This is a follow-up question on this one: Exposing a C struct with Rcpp?
@DirkEddelbuettel justly criticized that aforementioned question as being too broad in scope so I am here trying to be more specific in my goals, while providing some actual code exemplifying my attempt at this problem.
Description of the problem
First thing: please note that I use (somewhat unfortunately, I will change it eventually) two similar names in my code: GLuint
and Gluint
.
I'm trying to wrap this C OpenGL functionality in R (taken from here: OpenGL Textures objects and parameters):
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
Approach
I am using Rcpp to achieve these goals:
- Provide a way of allocating the
Gluint
object from R; perhaps by instantiating an S4 object that wraps the C object (GLuint
tex) inside? - Then, have an R exposed function named
gl_gen_textures
that takes an integer and that object mentioned in 1. Internally, this functiongl_gen_textures
accesses the memory address of theGLuint
C object and passes it on toglGenTextures
(C library function). - Eventually provide a way of destroying the
Gluint
object created in R and along with it the internalGLuint
C object.
Actual code
#include <Rcpp.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace Rcpp;
class Gluint {
public:
Gluint() {}
GLuint obj;
};
RCPP_MODULE(gluint_module) {
class_<Gluint>("Gluint")
.constructor()
.field("obj", &Gluint::obj)
;
}
//' @export
// [[Rcpp::export]]
void gl_gen_textures(unsigned int n, Gluint& tex) {
uint32_t _n;
_n = (uint32_t) n; // Convert unsigned int to uint32_t.
glGenTextures((GLsizei) _n, &(tex.obj));
}
Errors
Currently this code does not compile. I'm getting these errors:
And here's a snippet of the function gl_gen_textures
as generated in Rcpp::Exports.cpp lines 136-146:
// gl_gen_textures
void gl_gen_textures(unsigned int n, Gluint& tex);
RcppExport SEXP _glfw_gl_gen_textures(SEXP nSEXP, SEXP texSEXP) {
BEGIN_RCPP
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< unsigned int >::type n(nSEXP);
Rcpp::traits::input_parameter< Gluint& >::type tex(texSEXP);
gl_gen_textures(n, tex);
return R_NilValue;
END_RCPP
}