您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

131 行
5.7 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - OpenGL point particle system
  4. *
  5. * This example has been created using raylib 2ad3eb1 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Stephan Soller (@arkanis - http://arkanis.de/)
  9. * and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Copyright (c) 2021 Stephan Soller (@arkanis) and Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************
  14. *
  15. * Mixes raylib and plain OpenGL code to draw a GL_POINTS based particle system. The
  16. * primary point is to demonstrate raylib and OpenGL interop.
  17. *
  18. * rlgl batched draw operations internally so we have to flush the current batch before
  19. * doing our own OpenGL work (rlDrawRenderBatchActive()).
  20. *
  21. * The example also demonstrates how to get the current model view projection matrix of
  22. * raylib. That way raylib cameras and so on work as expected.
  23. *
  24. ********************************************************************************************/
  25. #include "raylib.h"
  26. #include "rlgl.h"
  27. #include "glad.h"
  28. #if defined(PLATFORM_DESKTOP)
  29. #define GLSL_VERSION 330
  30. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  31. #define GLSL_VERSION 100
  32. #endif
  33. int main()
  34. {
  35. // Initialization
  36. //--------------------------------------------------------------------------------------
  37. const int screenWidth = 800;
  38. const int screenHeight = 450;
  39. InitWindow(screenWidth, screenHeight, "raylib - point particles");
  40. Shader shader = LoadShader(
  41. TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION),
  42. TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION));
  43. int currentTimeLoc = GetShaderLocation(shader, "currentTime");
  44. int colorLoc = GetShaderLocation(shader, "color");
  45. // Initialize the vertex buffer for the particles and assign each particle random values
  46. struct { float x, y, period; } particles[10000];
  47. const size_t particleCount = sizeof(particles) / sizeof(particles[0]);
  48. for (size_t i = 0; i < particleCount; i++)
  49. {
  50. particles[i].x = GetRandomValue(20, screenWidth - 20);
  51. particles[i].y = GetRandomValue(50, screenHeight - 20);
  52. // Give each particle a slightly different period. But don't spread it to much. This way the particles line up
  53. // every so often and you get a glimps of what is going on.
  54. particles[i].period = GetRandomValue(10, 30) / 10.0f;
  55. }
  56. // Create a plain OpenGL vertex buffer with the data and an vertex array object that feeds the data from the buffer
  57. // into the vertexPosition shader attribute.
  58. GLuint vao = 0, vbo = 0;
  59. glGenVertexArrays(1, &vao);
  60. glBindVertexArray(vao);
  61. glGenBuffers(1, &vbo);
  62. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  63. glBufferData(GL_ARRAY_BUFFER, sizeof(particles), particles, GL_STATIC_DRAW);
  64. // Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION]
  65. glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0);
  66. glEnableVertexAttribArray(0);
  67. glBindBuffer(GL_ARRAY_BUFFER, 0);
  68. glBindVertexArray(0);
  69. // Allows the vertex shader to set the point size of each particle individually
  70. glEnable(GL_PROGRAM_POINT_SIZE);
  71. SetTargetFPS(60);
  72. //--------------------------------------------------------------------------------------
  73. // Main game loop
  74. while (!WindowShouldClose()) // Detect window close button or ESC key
  75. {
  76. // Draw
  77. //----------------------------------------------------------------------------------
  78. BeginDrawing();
  79. ClearBackground(WHITE);
  80. DrawRectangle(10, 10, 210, 30, MAROON);
  81. DrawText(TextFormat("%zu particles in one vertex buffer", particleCount), 20, 20, 10, RAYWHITE);
  82. // Switch to plain OpenGL
  83. //------------------------------------------------------------------------------
  84. // rlglDraw() in raylib 3.5
  85. rlDrawRenderBatchActive();
  86. glUseProgram(shader.id);
  87. glUniform1f(currentTimeLoc, GetTime());
  88. Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 });
  89. glUniform4fv(colorLoc, 1, (float*)&color);
  90. // The the current model view projection matrix so the particle system is displayed and transformed
  91. // (e.g. by cameras) just like everything else.
  92. // GetMatrixModelview() and GetMatrixProjection() in raylib 3.5
  93. Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
  94. glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection));
  95. glBindVertexArray(vao);
  96. glDrawArrays(GL_POINTS, 0, particleCount);
  97. glBindVertexArray(0);
  98. glUseProgram(0);
  99. // And back to raylib again
  100. //------------------------------------------------------------------------------
  101. DrawFPS(screenWidth - 100, 10);
  102. EndDrawing();
  103. //----------------------------------------------------------------------------------
  104. }
  105. // De-Initialization
  106. //--------------------------------------------------------------------------------------
  107. glDeleteBuffers(1, &vbo);
  108. glDeleteVertexArrays(1, &vao);
  109. UnloadShader(shader);
  110. CloseWindow(); // Close window and OpenGL context
  111. //--------------------------------------------------------------------------------------
  112. return 0;
  113. }