如何使用glew

来源:互联网 发布:淘宝海外叫什么 编辑:程序博客网 时间:2024/05/16 15:23

使用glew

glew也是一个扩展库,包含了OpenGL中许多核心及扩展函数,现在的版本是1.7,支持OpenGL4.2,可以到这里下载:http://glew.sourceforge.net/
下载了glew库的话,那么可以直接使用该扩展,但是使用之前首先要确定你的显卡是否支持该扩展。方法如下:
首先还是要判断显卡是否支持该扩展

如何判断显卡所支持的扩展:

使用函数glGetString,看一下它的原型:

const     GLubyte   * glGetString( GLenum name )

我们以GL_EXTENSIONS为参数调用该函数,就能获得当前显卡所支持的所有扩展,像下面这样:

const GLubyte *str = glGetString(GL_EXTENSIONS) ;cout << str << endl ;

1.包含头文件glew.h,注意这里和上面不同,这回glut.h要放在glew.h的后面了

像下面这样

#include <GL/glew.h>#include <GL/glut.h>

然后就可以在程序中直接使用glBlendEquation了,其他扩展的判断和使用方法与此类似。

最后还有一点切记!那就是还要调用glewInit() ;来初始化一下方可使用扩展,如下:

GLenum err = glewInit();if (GLEW_OK != err){   MessageBoxA(NULL, "error", "My Window", 1);}

.在project->setting中,打开link选项卡,加上glew.lib 
或在文件中加上#progmma comment(lib, "glew.lib")

好了,现在可以放心使用扩展了,赶快继续你的OpenGL之旅吧!


为了查看显卡到底支持哪些扩展,折腾了好久。

#include <iostream>#include <GL/glew.h>using namespace std;int main(){  GLenum err = glewInit();  if (err != GLEW_OK )  {     fprintf(stderr, "Error: %s\n", glewGetErrorString(err));    exit (-2);  }  int NumberOfExtensions;  glGetIntegerv(GL_NUM_EXTENSIONS, &NumberOfExtensions);  for(int i=0; i<NumberOfExtensions; i++)  {    const GLubyte *ccc=glGetStringi(GL_EXTENSIONS, i);    //Now, do something with ccc    cout << ccc << endl;  }  system("pause");}
这是最初的代码,前面所说的glGetString函数在OpenGL3.0之后废除了,为了使用最新的OpenGL4.2,在官网查到了最新使用方法。

在link->input中写入glew32.lib,编译报link错误,不认识glGetIntegerv函数,但是在glew.h能发现这个函数。google了好久没发现好的解决方案。不过后来在glew官网<说明链接>查到了问题所在,使用glew必须有一个opengl context。最简单的方案是用glut创建一个context。增加了2句话后得到结果。

#include <iostream>#include <GL/glew.h>#include <gl/glut.h>using namespace std;int main(){  glutCreateWindow("GLEW Test");//get an openGL context  GLenum err = glewInit();  if (err != GLEW_OK )  {     fprintf(stderr, "Error: %s\n", glewGetErrorString(err));    exit (-2);  }  int NumberOfExtensions;  glGetIntegerv(GL_NUM_EXTENSIONS, &NumberOfExtensions);  for(int i=0; i<NumberOfExtensions; i++)  {    const GLubyte *ccc=glGetStringi(GL_EXTENSIONS, i);    //Now, do something with ccc    cout << ccc << endl;  }  system("pause");}
结果:(其实我还是不知道显卡到底支不支持最新的OpenGL 4.2)

GL_ARB_blend_func_extendedGL_ARB_color_buffer_floatGL_ARB_compatibilityGL_ARB_copy_bufferGL_ARB_depth_buffer_floatGL_ARB_depth_clampGL_ARB_depth_textureGL_ARB_draw_buffersGL_ARB_draw_elements_base_vertexGL_ARB_draw_instancedGL_ARB_ES2_compatibilityGL_ARB_explicit_attrib_locationGL_ARB_fragment_coord_conventionsGL_ARB_fragment_programGL_ARB_fragment_program_shadowGL_ARB_fragment_shaderGL_ARB_framebuffer_objectGL_ARB_framebuffer_sRGBGL_ARB_geometry_shader4GL_ARB_get_program_binaryGL_ARB_half_float_pixelGL_ARB_half_float_vertexGL_ARB_imagingGL_ARB_instanced_arraysGL_ARB_map_buffer_rangeGL_ARB_multisampleGL_ARB_multitextureGL_ARB_occlusion_queryGL_ARB_occlusion_query2GL_ARB_pixel_buffer_objectGL_ARB_point_parametersGL_ARB_point_spriteGL_ARB_provoking_vertexGL_ARB_robustnessGL_ARB_sampler_objectsGL_ARB_seamless_cube_mapGL_ARB_separate_shader_objectsGL_ARB_shader_bit_encodingGL_ARB_shader_objectsGL_ARB_shading_language_100GL_ARB_shading_language_includeGL_ARB_shadowGL_ARB_syncGL_ARB_texture_border_clampGL_ARB_texture_buffer_objectGL_ARB_texture_compressionGL_ARB_texture_compression_rgtcGL_ARB_texture_cube_mapGL_ARB_texture_env_addGL_ARB_texture_env_combineGL_ARB_texture_env_crossbarGL_ARB_texture_env_dot3GL_ARB_texture_floatGL_ARB_texture_mirrored_repeatGL_ARB_texture_multisampleGL_ARB_texture_non_power_of_twoGL_ARB_texture_rectangleGL_ARB_texture_rgGL_ARB_texture_rgb10_a2uiGL_ARB_texture_swizzleGL_ARB_timer_queryGL_ARB_transpose_matrixGL_ARB_uniform_buffer_objectGL_ARB_vertex_array_bgraGL_ARB_vertex_array_objectGL_ARB_vertex_buffer_objectGL_ARB_vertex_programGL_ARB_vertex_shaderGL_ARB_vertex_type_2_10_10_10_revGL_ARB_viewport_arrayGL_ARB_window_posGL_ATI_draw_buffersGL_ATI_texture_floatGL_ATI_texture_mirror_onceGL_S3_s3tcGL_EXT_texture_env_addGL_EXT_abgrGL_EXT_bgraGL_EXT_bindable_uniformGL_EXT_blend_colorGL_EXT_blend_equation_separateGL_EXT_blend_func_separateGL_EXT_blend_minmaxGL_EXT_blend_subtractGL_EXT_compiled_vertex_arrayGL_EXT_Cg_shaderGL_EXT_depth_bounds_testGL_EXT_direct_state_accessGL_EXT_draw_buffers2GL_EXT_draw_instancedGL_EXT_draw_range_elementsGL_EXT_fog_coordGL_EXT_framebuffer_blitGL_EXT_framebuffer_multisampleGL_EXTX_framebuffer_mixed_formatsGL_EXT_framebuffer_objectGL_EXT_framebuffer_sRGBGL_EXT_geometry_shader4GL_EXT_gpu_program_parametersGL_EXT_gpu_shader4GL_EXT_multi_draw_arraysGL_EXT_packed_depth_stencilGL_EXT_packed_floatGL_EXT_packed_pixelsGL_EXT_pixel_buffer_objectGL_EXT_point_parametersGL_EXT_provoking_vertexGL_EXT_rescale_normalGL_EXT_secondary_colorGL_EXT_separate_shader_objectsGL_EXT_separate_specular_colorGL_EXT_shadow_funcsGL_EXT_stencil_two_sideGL_EXT_stencil_wrapGL_EXT_texture3DGL_EXT_texture_arrayGL_EXT_texture_buffer_objectGL_EXT_texture_compression_dxt1GL_EXT_texture_compression_latcGL_EXT_texture_compression_rgtcGL_EXT_texture_compression_s3tcGL_EXT_texture_cube_mapGL_EXT_texture_edge_clampGL_EXT_texture_env_combineGL_EXT_texture_env_dot3GL_EXT_texture_filter_anisotropicGL_EXT_texture_format_BGRA8888GL_EXT_texture_integerGL_EXT_texture_lodGL_EXT_texture_lod_biasGL_EXT_texture_mirror_clampGL_EXT_texture_objectGL_EXT_texture_shared_exponentGL_EXT_texture_sRGBGL_EXT_texture_swizzleGL_EXT_texture_type_2_10_10_10_REVGL_EXT_timer_queryGL_EXT_vertex_arrayGL_EXT_vertex_array_bgraGL_IBM_rasterpos_clipGL_IBM_texture_mirrored_repeatGL_KTX_buffer_regionGL_NV_alpha_testGL_NV_blend_minmaxGL_NV_blend_squareGL_NV_complex_primitivesGL_NV_conditional_renderGL_NV_copy_depth_to_colorGL_NV_copy_imageGL_NV_depth_buffer_floatGL_NV_depth_clampGL_NV_explicit_multisampleGL_NV_fbo_color_attachmentsGL_NV_fenceGL_NV_float_bufferGL_NV_fog_distanceGL_NV_fragdepthGL_NV_fragment_programGL_NV_fragment_program_optionGL_NV_fragment_program2GL_NV_framebuffer_multisample_coverageGL_NV_geometry_shader4GL_NV_gpu_program4GL_NV_half_floatGL_NV_light_max_exponentGL_NV_multisample_coverageGL_NV_multisample_filter_hintGL_NV_occlusion_queryGL_NV_packed_depth_stencilGL_NV_parameter_buffer_objectGL_NV_parameter_buffer_object2GL_NV_path_renderingGL_NV_pixel_data_rangeGL_NV_point_spriteGL_NV_primitive_restartGL_NV_register_combinersGL_NV_register_combiners2GL_NV_shader_buffer_loadGL_NV_texgen_reflectionGL_NV_texture_barrierGL_NV_texture_compression_vtcGL_NV_texture_env_combine4GL_NV_texture_expand_normalGL_NV_texture_lod_clampGL_NV_texture_multisampleGL_NV_texture_rectangleGL_NV_texture_shaderGL_NV_texture_shader2GL_NV_texture_shader3GL_NV_transform_feedbackGL_NV_vertex_array_rangeGL_NV_vertex_array_range2GL_NV_vertex_buffer_unified_memoryGL_NV_vertex_programGL_NV_vertex_program1_1GL_NV_vertex_program2GL_NV_vertex_program2_optionGL_NV_vertex_program3GL_NVX_conditional_renderGL_NVX_gpu_memory_infoGL_OES_depth24GL_OES_depth32GL_OES_depth_textureGL_OES_element_index_uintGL_OES_fbo_render_mipmapGL_OES_get_program_binaryGL_OES_mapbufferGL_OES_packed_depth_stencilGL_OES_rgb8_rgba8GL_OES_standard_derivativesGL_OES_texture_3DGL_OES_texture_floatGL_OES_texture_float_linearGL_OES_texture_half_floatGL_OES_texture_half_float_linearGL_OES_texture_npotGL_OES_vertex_array_objectGL_OES_vertex_half_floatGL_SGIS_generate_mipmapGL_SGIS_texture_lodGL_SGIX_depth_textureGL_SGIX_shadowGL_SUN_slice_accumGL_WIN_swap_hintWGL_EXT_swap_control


原创粉丝点击