I'm trying to access the OpenGL function glCreateVertexArrays
. Consider the following small C++ program:
#include <GL/glx.h>
#include <GLFW/glfw3.h>
int main() {
// Init GLFW
glfwInit();
// Set OpenGL Version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Select core profile
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
// Create Window
auto window = glfwCreateWindow(400, 400, "Test", nullptr, nullptr);
glfwMakeContextCurrent(window);
void (*glCreateVertexArrays)(unsigned int, unsigned int*) = nullptr;
glCreateVertexArrays = (decltype(glCreateVertexArrays))glXGetProcAddress((const GLubyte*)"glCreateVertexArrays");
unsigned int vao;
glCreateVertexArrays(1, &vao); // <-- This is the important part
return 0;
}
This application doesn't segfault; now I want to recreate the same in Rust:
extern crate libc;
use std::ffi::CString;
use std::os;
use std::ptr;
enum GLFWwindow {}
enum GLFWmonitor {}
#[link(name = "glfw")]
extern "C" {
fn glfwInit() -> bool;
fn glfwWindowHint(target: libc::uint32_t, hint: libc::int32_t);
fn glfwCreateWindow(
width: libc::c_int,
height: libc::c_int,
title: *const os::raw::c_char,
monitor: *mut GLFWmonitor,
window: *mut GLFWwindow,
) -> *mut GLFWwindow;
fn glfwMakeContextCurrent(window: *mut GLFWwindow);
}
#[link(name = "GL")]
extern "C" {
fn glXGetProcAddress(procname: *const os::raw::c_char) -> extern "C" fn();
}
static GLFW_CONTEXT_VERSION_MAJOR: u32 = 0x22002;
static GLFW_CONTEXT_VERSION_MINOR: u32 = 0x22003;
static GLFW_OPENGL_FORWARD_COMPAT: u32 = 0x22006;
static GLFW_OPENGL_PROFILE: u32 = 0x22008;
static GLFW_OPENGL_CORE_PROFILE: i32 = 0x00032001;
type FnCreateVertexArrays = extern "C" fn(n: libc::c_int, arrays: *mut libc::c_uint);
fn main() {
let w_name = CString::new("Test").unwrap();
let fn_name = CString::new("glCreateVertexArrays").unwrap();
unsafe {
glfwInit();
// Set OpenGL Version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Select core profile
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1);
// Create Window
let window = glfwCreateWindow(
400,
400,
w_name.as_ptr(),
ptr::null_mut(),
ptr::null_mut(),
);
glfwMakeContextCurrent(window);
}
let create_vertex_arrays: FnCreateVertexArrays = unsafe {
glXGetProcAddress(fn_name.as_ptr())
};
let mut vao: libc::c_uint = 0;
unsafe {
(create_vertex_arrays)(1, &mut vao);
}
println!("Hello, world!");
}
However, the cast is not correct:
error[E0308]: mismatched types
--> src/main.rs:63:9
|
63 | glXGetProcAddress(fn_name.as_ptr())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect number of function parameters
|
= note: expected type `extern "C" fn(i32, *mut u32)`
found type `extern "C" fn()`
The main problem here is that the OpenGL function glXGetProcAddress
returns a void*()
pointer, which has to be cast to the appropriate function pointer type. How could that be done?
I know that there are already wrappers and engines for OpenGL/GLFW, but I want to do it for the sake of learning.
Here's a slightly smaller reproduction:
One solution is The Big Hammer of "make this type into that type":
mem::transmute
:But
mem::transmute
really is supposed to be used as the last resort. None of the other conversion techniques I'm aware of seem to apply here...See also: