REVIEWED: raylib_opengl_interop
This commit is contained in:
parent
b805754aa1
commit
73c8ddc8bf
@ -2,7 +2,7 @@
|
|||||||
*
|
*
|
||||||
* raylib [shaders] example - OpenGL point particle system
|
* raylib [shaders] example - OpenGL point particle system
|
||||||
*
|
*
|
||||||
* This example has been created using raylib 2ad3eb1 (www.raylib.com)
|
* This example has been created using raylib 3.8 (www.raylib.com)
|
||||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Example contributed by Stephan Soller (@arkanis - http://arkanis.de/)
|
* Example contributed by Stephan Soller (@arkanis - http://arkanis.de/)
|
||||||
@ -24,8 +24,9 @@
|
|||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "rlgl.h"
|
#include "rlgl.h" // Required for: rlDrawRenderBatchActive(), rlGetMatrixModelview(), rlGetMatrixProjection()
|
||||||
#include "glad.h"
|
#include "glad.h" // Required for: OpenGL functionality
|
||||||
|
#include "raymath.h" // Required for: MatrixMultiply(), MatrixToFloat()
|
||||||
|
|
||||||
#if defined(PLATFORM_DESKTOP)
|
#if defined(PLATFORM_DESKTOP)
|
||||||
#define GLSL_VERSION 330
|
#define GLSL_VERSION 330
|
||||||
@ -33,6 +34,15 @@
|
|||||||
#define GLSL_VERSION 100
|
#define GLSL_VERSION 100
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define MAX_PARTICLES 1000
|
||||||
|
|
||||||
|
// Particle type
|
||||||
|
typedef struct Particle {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float period;
|
||||||
|
} Particle;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
@ -42,32 +52,34 @@ int main()
|
|||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib - point particles");
|
InitWindow(screenWidth, screenHeight, "raylib - point particles");
|
||||||
|
|
||||||
Shader shader = LoadShader(
|
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION),
|
||||||
TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION),
|
TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION));
|
||||||
TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION));
|
|
||||||
int currentTimeLoc = GetShaderLocation(shader, "currentTime");
|
int currentTimeLoc = GetShaderLocation(shader, "currentTime");
|
||||||
int colorLoc = GetShaderLocation(shader, "color");
|
int colorLoc = GetShaderLocation(shader, "color");
|
||||||
|
|
||||||
// Initialize the vertex buffer for the particles and assign each particle random values
|
// Initialize the vertex buffer for the particles and assign each particle random values
|
||||||
struct { float x, y, period; } particles[10000];
|
Particle particles[MAX_PARTICLES] = { 0 };
|
||||||
const size_t particleCount = sizeof(particles) / sizeof(particles[0]);
|
|
||||||
for (size_t i = 0; i < particleCount; i++)
|
for (int i = 0; i < MAX_PARTICLES; i++)
|
||||||
{
|
{
|
||||||
particles[i].x = GetRandomValue(20, screenWidth - 20);
|
particles[i].x = (float)GetRandomValue(20, screenWidth - 20);
|
||||||
particles[i].y = GetRandomValue(50, screenHeight - 20);
|
particles[i].y = (float)GetRandomValue(50, screenHeight - 20);
|
||||||
// Give each particle a slightly different period. But don't spread it to much. This way the particles line up
|
|
||||||
// every so often and you get a glimps of what is going on.
|
// Give each particle a slightly different period. But don't spread it to much.
|
||||||
particles[i].period = GetRandomValue(10, 30) / 10.0f;
|
// This way the particles line up every so often and you get a glimps of what is going on.
|
||||||
|
particles[i].period = (float)GetRandomValue(10, 30)/10.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a plain OpenGL vertex buffer with the data and an vertex array object that feeds the data from the buffer
|
// Create a plain OpenGL vertex buffer with the data and an vertex array object
|
||||||
// into the vertexPosition shader attribute.
|
// that feeds the data from the buffer into the vertexPosition shader attribute.
|
||||||
GLuint vao = 0, vbo = 0;
|
GLuint vao = 0;
|
||||||
|
GLuint vbo = 0;
|
||||||
glGenVertexArrays(1, &vao);
|
glGenVertexArrays(1, &vao);
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glGenBuffers(1, &vbo);
|
glGenBuffers(1, &vbo);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(particles), particles, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, MAX_PARTICLES*sizeof(Particle), particles, GL_STATIC_DRAW);
|
||||||
// Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION]
|
// Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION]
|
||||||
glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0);
|
glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
@ -89,32 +101,33 @@ int main()
|
|||||||
ClearBackground(WHITE);
|
ClearBackground(WHITE);
|
||||||
|
|
||||||
DrawRectangle(10, 10, 210, 30, MAROON);
|
DrawRectangle(10, 10, 210, 30, MAROON);
|
||||||
DrawText(TextFormat("%zu particles in one vertex buffer", particleCount), 20, 20, 10, RAYWHITE);
|
DrawText(TextFormat("%zu particles in one vertex buffer", MAX_PARTICLES), 20, 20, 10, RAYWHITE);
|
||||||
|
|
||||||
|
rlDrawRenderBatchActive(); // Draw iternal buffers data (previous draw calls)
|
||||||
|
|
||||||
// Switch to plain OpenGL
|
// Switch to plain OpenGL
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// rlglDraw() in raylib 3.5
|
|
||||||
rlDrawRenderBatchActive();
|
|
||||||
glUseProgram(shader.id);
|
glUseProgram(shader.id);
|
||||||
|
|
||||||
glUniform1f(currentTimeLoc, GetTime());
|
glUniform1f(currentTimeLoc, GetTime());
|
||||||
|
|
||||||
Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 });
|
Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 });
|
||||||
glUniform4fv(colorLoc, 1, (float*)&color);
|
glUniform4fv(colorLoc, 1, (float *)&color);
|
||||||
|
|
||||||
// The the current model view projection matrix so the particle system is displayed and transformed
|
// Get the current modelview and projection matrix so the particle system is displayed and transformed
|
||||||
// (e.g. by cameras) just like everything else.
|
|
||||||
// GetMatrixModelview() and GetMatrixProjection() in raylib 3.5
|
|
||||||
Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
|
Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
|
||||||
|
|
||||||
glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection));
|
glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection));
|
||||||
|
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glDrawArrays(GL_POINTS, 0, particleCount);
|
glDrawArrays(GL_POINTS, 0, MAX_PARTICLES);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
glUseProgram(0);
|
glUseProgram(0);
|
||||||
|
|
||||||
// And back to raylib again
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
DrawFPS(screenWidth - 100, 10);
|
DrawFPS(screenWidth - 100, 10);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
@ -124,8 +137,9 @@ int main()
|
|||||||
glDeleteBuffers(1, &vbo);
|
glDeleteBuffers(1, &vbo);
|
||||||
glDeleteVertexArrays(1, &vao);
|
glDeleteVertexArrays(1, &vao);
|
||||||
|
|
||||||
UnloadShader(shader);
|
UnloadShader(shader); // Unload shader
|
||||||
CloseWindow(); // Close window and OpenGL context
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
return 0;
|
return 0;
|
Before Width: | Height: | Size: 263 KiB After Width: | Height: | Size: 263 KiB |
Loading…
x
Reference in New Issue
Block a user