SDL入门学习续-在SDL中使用OpenGL

配置好SDL之后,就想在SDL中使用openGL,原以为会像在GLFW中那样简单的,实际上确花费了一整个下午不断查看文档,实例才搞定问题。

相关阅读:SDL入门学习教程 http://www.linuxidc.com/Linux/2012-12/75255.htm

总结如下:

1)SDL对OpenGL进行了部分的封装,一些OpenGL的函数需要用SDL来实现,而不是像GLFW中那样直接用;

2)SDL的事件机制花费了一些时间来理解;

3)在处理OpenGL窗口大小变化的时候,需要先调用SDL_SetVideoMode才可以得到正确结果。

最终基本实现了一个opengl的小型框架,包括一些简单的事件处理。

代码:

  1. /*****************************************************************************
  2. Copyright: 2012, ustc All rights reserved.
  3. contact:k283228391@126.com
  4. File name: main.c
  5. Description:using opengl in SDL.
  6. Author:Silang Quan
  7. Version: 1.0
  8. Date: 2012.12.01
  9. *****************************************************************************/
  10. #include <SDL/SDL.h>
  11. #include <GL/gl.h>
  12. #include <GL/glu.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. SDL_Surface *screen;
  16. void quit( int code )
  17. {
  18. SDL_Quit( );
  19. /* Exit program. */
  20. exit( code );
  21. }
  22. void handleKeyEvent( SDL_keysym* keysym )
  23. {
  24. switch( keysym->sym )
  25. {
  26. case SDLK_ESCAPE:
  27. quit( 0 );
  28. break;
  29. case SDLK_SPACE:
  30. break;
  31. default:
  32. break;
  33. }
  34. }
  35. void resizeGL(int width,int height)
  36. {
  37. if ( height == 0 )
  38. {
  39. height = 1;
  40. }
  41. //Reset View
  42. glViewport( 0, 0, (GLint)width, (GLint)height );
  43. //Choose the Matrix mode
  44. glMatrixMode( GL_PROJECTION );
  45. //reset projection
  46. glLoadIdentity();
  47. //set perspection
  48. gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
  49. //choose Matrix mode
  50. glMatrixMode( GL_MODELVIEW );
  51. glLoadIdentity();
  52. }
  53. void handleEvents()
  54. {
  55. // Our SDL event placeholder.
  56. SDL_Event event;
  57. //Grab all the events off the queue.
  58. while( SDL_PollEvent( &event ) ) {
  59. switch( event.type ) {
  60. case SDL_KEYDOWN:
  61. // Handle key Event
  62. handleKeyEvent( &event.key.keysym );
  63. break;
  64. case SDL_QUIT:
  65. // Handle quit requests (like Ctrl-c).
  66. quit( 0 );
  67. break;
  68. case SDL_VIDEORESIZE:
  69. //Handle resize event
  70. screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 16,
  71. SDL_OPENGL|SDL_RESIZABLE);
  72. if ( screen )
  73. {
  74. resizeGL(screen->w, screen->h);
  75. }
  76. break;
  77. }
  78. }
  79. }
  80.  
  81. void initSDL(int width,int height,int bpp,int flags)
  82. {
  83. // First, initialize SDL's video subsystem.
  84. if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  85. {
  86. fprintf( stderr, "Video initialization failed: %s\n",
  87. SDL_GetError( ) );
  88. quit( 1 );
  89. }
  90. atexit(SDL_Quit);
  91. //Set some Attribute of OpenGL in SDL
  92. SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
  93. SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
  94. SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
  95. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  96. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  97.  
  98. //Set the video mode
  99. screen= SDL_SetVideoMode( width, height, bpp,flags);
  100. if(!screen )
  101. {
  102. fprintf( stderr, "Video mode set failed: %s\n",SDL_GetError( ) );
  103. quit( 1 );
  104. }
  105. resizeGL(screen->w, screen->h);
  106. //Set caption
  107. SDL_WM_SetCaption( "OpenGL Test", NULL );
  108. }
  109. void renderGL()
  110. {
  111. // Clear the color and depth buffers.
  112. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  113. // We don't want to modify the projection matrix. */
  114. glMatrixMode( GL_MODELVIEW );
  115. glLoadIdentity( );
  116. // Move down the z-axis.
  117. glTranslatef( 0.0, 0.0, -5.0 );
  118. //Draw a square
  119. glBegin(GL_QUADS);
  120. glColor3f(1.0f,0.0f,0.0f);
  121. glVertex3f(-1.0f , -1.0f , 1.0f );
  122. glColor3f(0.0f,1.0f,0.0f);
  123. glVertex3f( 1.0f , -1.0f , 1.0f );
  124. glColor3f(0.0f,0.0f,1.0f);
  125. glVertex3f( 1.0f , 1.0f , 1.0f );
  126. glColor3f(1.0f,1.0f,0.0f);
  127. glVertex3f(-1.0f , 1.0f , 1.0f );
  128. glEnd();
  129. SDL_GL_SwapBuffers( );
  130. }
  131. void initGL( int width, int height )
  132. {
  133. float ratio = (float) width / (float) height;
  134. // Our shading model--Gouraud (smooth).
  135. glShadeModel( GL_SMOOTH );
  136. // Set the clear color.
  137. glClearColor( 0, 0, 0, 0 );
  138. // Setup our viewport.
  139. glViewport( 0, 0, width, height );
  140. //Change to the projection matrix and set our viewing volume.
  141. glMatrixMode( GL_PROJECTION );
  142. glLoadIdentity();
  143. gluPerspective( 60.0, ratio, 1.0, 100.0 );
  144. }
  145. int main( int argc, char* argv[] )
  146. {
  147.  
  148. // Dimensions of our window.
  149. int width = 640;
  150. int height = 480;
  151. // Color depth in bits of our window.
  152. int bpp = 32;
  153. int flags= SDL_OPENGL|SDL_RESIZABLE;
  154. //Set the SDL
  155. initSDL(width, height, bpp,flags);
  156. //Set the OpenGL
  157. initGL( width, height );
  158.  
  159. //main loop
  160. while(true)
  161. {
  162. /* Process incoming events. */
  163. handleEvents( );
  164. /* Draw the screen. */
  165. renderGL( );
  166. }
  167. return 0;
  168. }

SDL入门学习续-在SDL中使用OpenGL

linux

转载于:https://my.oschina.net/u/2528742/blog/693454

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值