我正在使用 OpenGL SuperBible 第 6 版,并学习有关曲面细分着色器的知识。我INVALID_OPERATION
从gl::DrawArrays
. 这是我的代码:
extern crate libc;
extern crate glutin;
extern crate gl;
use std::io::Read;
fn main() {
unsafe {
let win = glutin::WindowBuilder::new()
.with_gl_profile(glutin::GlProfile::Core)
.build_strict()
.unwrap();
win.make_current().unwrap();
gl::load_with(|s| win.get_proc_address(s));
gl::DebugMessageCallback(
std::mem::transmute(gl_debug_message),
std::ptr::null_mut());
let program = build_shader_program();
gl::UseProgram(program);
let mut vao = std::mem::uninitialized();
gl::GenVertexArrays(1, &mut vao);
gl::BindVertexArray(vao);
gl::PolygonMode(gl::FRONT_AND_BACK, gl::LINE);
let red = [1.0, 0.0, 0.0, 1.0];
let mut running = true;
while running {
for event in win.poll_events() {
if let glutin::Event::Closed = event {
running = false;
}
}
win.swap_buffers().unwrap();
gl::ClearBufferfv(gl::COLOR, 0, &red[0]);
gl::DrawArrays(gl::TRIANGLES, 0, 3);
}
}
}
extern "system"
fn gl_debug_message(_source: u32, _type: u32, _id: u32, _sev: u32,
_len: i32, message: *const libc::c_char,
_param: *mut libc::c_void)
{
unsafe {
let s = cstring_to_string(message);
panic!("OpenGL Debug message: {}", s);
}
}
fn build_shader_program() -> u32 {
let vert = load_and_compile_shader("a.vert", gl::VERTEX_SHADER);
let tesc = load_and_compile_shader("a.tesc", gl::TESS_CONTROL_SHADER);
let tese = load_and_compile_shader("a.tese", gl::TESS_EVALUATION_SHADER);
let frag = load_and_compile_shader("a.frag", gl::FRAGMENT_SHADER);
unsafe {
let program_id = gl::CreateProgram();
gl::AttachShader(program_id, vert);
gl::AttachShader(program_id, tesc);
gl::AttachShader(program_id, tese);
gl::AttachShader(program_id, frag);
gl::LinkProgram(program_id);
let mut result = std::mem::uninitialized();
gl::GetProgramiv(program_id, gl::LINK_STATUS, &mut result);
assert_eq!(result, gl::TRUE as i32);
program_id
}
}
unsafe fn cstring_to_string(mut cs: *const libc::c_char) -> String {
let mut v : Vec<u8> = Vec::new();
while *cs != 0 {
v.push(*cs as u8);
cs = cs.offset(1);
}
String::from_utf8(v).expect("c-string not utf8")
}
fn load_file_as_cstring(path: &str) -> std::ffi::CString {
let mut contents = Vec::new();
let mut file = std::fs::File::open(path).unwrap();
file.read_to_end(&mut contents).unwrap();
std::ffi::CString::new(contents).unwrap()
}
fn load_and_compile_shader(path: &str, shader_type: u32) -> u32 {
let contents = load_file_as_cstring(path);
unsafe {
let shader_id = gl::CreateShader(shader_type);
let source_ptr = contents.as_ptr();
gl::ShaderSource(shader_id, 1, &source_ptr, std::ptr::null());
gl::CompileShader(shader_id);
let mut result = std::mem::uninitialized();
gl::GetShaderiv(shader_id, gl::COMPILE_STATUS, &mut result);
assert_eq!(result, gl::TRUE as i32);
shader_id
}
}
运行此程序会导致一条调试消息:
OpenGL Debug message: glDrawArrays has generated an error (GL_INVALID_OPERATION)
我省略了着色器(顶点着色器、片段着色器、镶嵌控制着色器和镶嵌评估着色器),但如果有必要,我可以显示它们。它们非常基本,我只是不想使问题混乱。事实上,它们就是sb6 git 存储库中的示例程序中的那些。他们编译和链接没有错误。
如果我删除对两个曲面细分着色器的调用gl::AttachShader
,则不会出现错误,并且会绘制一个简单的线框三角形。使用曲面细分着色器,它应该绘制相同的三角形,只是切割成更小的三角形。
我查看了glDrawArrays
此站点上的文档,并且我的代码似乎没有做任何可能导致INVALID_OPERATION
错误的事情,afaict。