169 satır
7.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - OpenGL point particle system
  4. *
  5. * Example complexity rating: [] 4/4
  6. *
  7. * Example originally created with raylib 3.8, last time updated with raylib 2.5
  8. *
  9. * Example contributed by Stephan Soller (@arkanis) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2021-2025 Stephan Soller (@arkanis) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************
  17. *
  18. * Mixes raylib and plain OpenGL code to draw a GL_POINTS based particle system. The
  19. * primary point is to demonstrate raylib and OpenGL interop.
  20. *
  21. * rlgl batched draw operations internally so we have to flush the current batch before
  22. * doing our own OpenGL work (rlDrawRenderBatchActive()).
  23. *
  24. * The example also demonstrates how to get the current model view projection matrix of
  25. * raylib. That way raylib cameras and so on work as expected.
  26. *
  27. ********************************************************************************************/
  28. #include "raylib.h"
  29. #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL)
  30. #if defined(GRAPHICS_API_OPENGL_ES2)
  31. #include "glad_gles2.h" // Required for: OpenGL functionality
  32. #define glGenVertexArrays glGenVertexArraysOES
  33. #define glBindVertexArray glBindVertexArrayOES
  34. #define glDeleteVertexArrays glDeleteVertexArraysOES
  35. #define GLSL_VERSION 100
  36. #else
  37. #if defined(__APPLE__)
  38. #define GL_SILENCE_DEPRECATION // Silence Opengl API deprecation warnings
  39. #include <OpenGL/gl3.h> // OpenGL 3 library for OSX
  40. #include <OpenGL/gl3ext.h> // OpenGL 3 extensions library for OSX
  41. #else
  42. #include "glad.h" // Required for: OpenGL functionality
  43. #endif
  44. #define GLSL_VERSION 330
  45. #endif
  46. #else // PLATFORM_ANDROID, PLATFORM_WEB
  47. #define GLSL_VERSION 100
  48. #endif
  49. #include "rlgl.h" // Required for: rlDrawRenderBatchActive(), rlGetMatrixModelview(), rlGetMatrixProjection()
  50. #include "raymath.h" // Required for: MatrixMultiply(), MatrixToFloat()
  51. #define MAX_PARTICLES 1000
  52. // Particle type
  53. typedef struct Particle {
  54. float x;
  55. float y;
  56. float period;
  57. } Particle;
  58. //------------------------------------------------------------------------------------
  59. // Program main entry point
  60. //------------------------------------------------------------------------------------
  61. int main(void)
  62. {
  63. // Initialization
  64. //--------------------------------------------------------------------------------------
  65. const int screenWidth = 800;
  66. const int screenHeight = 450;
  67. InitWindow(screenWidth, screenHeight, "raylib - point particles");
  68. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION),
  69. TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION));
  70. int currentTimeLoc = GetShaderLocation(shader, "currentTime");
  71. int colorLoc = GetShaderLocation(shader, "color");
  72. // Initialize the vertex buffer for the particles and assign each particle random values
  73. Particle particles[MAX_PARTICLES] = { 0 };
  74. for (int i = 0; i < MAX_PARTICLES; i++)
  75. {
  76. particles[i].x = (float)GetRandomValue(20, screenWidth - 20);
  77. particles[i].y = (float)GetRandomValue(50, screenHeight - 20);
  78. // Give each particle a slightly different period. But don't spread it to much.
  79. // This way the particles line up every so often and you get a glimps of what is going on.
  80. particles[i].period = (float)GetRandomValue(10, 30)/10.0f;
  81. }
  82. // Create a plain OpenGL vertex buffer with the data and an vertex array object
  83. // that feeds the data from the buffer into the vertexPosition shader attribute.
  84. GLuint vao = 0;
  85. GLuint vbo = 0;
  86. glGenVertexArrays(1, &vao);
  87. glBindVertexArray(vao);
  88. glGenBuffers(1, &vbo);
  89. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  90. glBufferData(GL_ARRAY_BUFFER, MAX_PARTICLES*sizeof(Particle), particles, GL_STATIC_DRAW);
  91. // Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION]
  92. glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0);
  93. glEnableVertexAttribArray(0);
  94. glBindBuffer(GL_ARRAY_BUFFER, 0);
  95. glBindVertexArray(0);
  96. // Allows the vertex shader to set the point size of each particle individually
  97. #ifndef GRAPHICS_API_OPENGL_ES2
  98. glEnable(GL_PROGRAM_POINT_SIZE);
  99. #endif
  100. SetTargetFPS(60);
  101. //--------------------------------------------------------------------------------------
  102. // Main game loop
  103. while (!WindowShouldClose()) // Detect window close button or ESC key
  104. {
  105. // Draw
  106. //----------------------------------------------------------------------------------
  107. BeginDrawing();
  108. ClearBackground(WHITE);
  109. DrawRectangle(10, 10, 210, 30, MAROON);
  110. DrawText(TextFormat("%zu particles in one vertex buffer", MAX_PARTICLES), 20, 20, 10, RAYWHITE);
  111. rlDrawRenderBatchActive(); // Draw iternal buffers data (previous draw calls)
  112. // Switch to plain OpenGL
  113. //------------------------------------------------------------------------------
  114. glUseProgram(shader.id);
  115. glUniform1f(currentTimeLoc, GetTime());
  116. Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 });
  117. glUniform4fv(colorLoc, 1, (float *)&color);
  118. // Get the current modelview and projection matrix so the particle system is displayed and transformed
  119. Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
  120. glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection));
  121. glBindVertexArray(vao);
  122. glDrawArrays(GL_POINTS, 0, MAX_PARTICLES);
  123. glBindVertexArray(0);
  124. glUseProgram(0);
  125. //------------------------------------------------------------------------------
  126. DrawFPS(screenWidth - 100, 10);
  127. EndDrawing();
  128. //----------------------------------------------------------------------------------
  129. }
  130. // De-Initialization
  131. //--------------------------------------------------------------------------------------
  132. glDeleteBuffers(1, &vbo);
  133. glDeleteVertexArrays(1, &vao);
  134. UnloadShader(shader); // Unload shader
  135. CloseWindow(); // Close window and OpenGL context
  136. //--------------------------------------------------------------------------------------
  137. return 0;
  138. }