You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
6.5 KiB

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