'opengl c# edition: the amount of ram being used keeps increasing
I have made this project so it renders a heart onto a black screen that can move. However it keeps seeming to be increasing the amount of ram it is using even when the heart isnt moving. I think this is due to it repeatedly rendering and then saving something in memory, however i do not know how to make it, in some way, unsave this thing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static OpenGlTutorial.OpenGL.GL;
using GLFW;
using OpenGlTutorial.Rendering.Display;
namespace OpenGlTutorial
{
class TestGame : Game
{
uint vao;
uint vbo;
uint shader;
int posCounter = 1;
public static float[] xHeart = { -0.4f, 0.4f, 0.0f, -0.2f, 0.2f };
public static float[] yHeart = { 0.0f, -0.6f, 0.2f};
public static void keyCallback(Window window, Keys key, int scanCode, InputState state, ModifierKeys mods)
{
if(key == Keys.A && state == InputState.Repeat)
{
xHeart[0] -= 0.01f;
xHeart[1] -= 0.01f;
xHeart[2] -= 0.01f;
xHeart[3] -= 0.01f;
xHeart[4] -= 0.01f;
}
if (key == Keys.D && state == InputState.Repeat)
{
xHeart[0] += 0.01f;
xHeart[1] += 0.01f;
xHeart[2] += 0.01f;
xHeart[3] += 0.01f;
xHeart[4] += 0.01f;
}
if(key == Keys.W && state == InputState.Repeat)
{
yHeart[0] += 0.01f;
yHeart[1] += 0.01f;
yHeart[2] += 0.01f;
}
if(key == Keys.S && state == InputState.Repeat)
{
yHeart[0] -= 0.01f;
yHeart[1] -= 0.01f;
yHeart[2] -= 0.01f;
}
}
public TestGame(int initialWindowHeight, int initialWindowWidth, string initialWindowTitle) : base(initialWindowHeight, initialWindowWidth, initialWindowTitle)
{
}
protected override void Initialize()
{
}
protected unsafe override void LoadContent()
{
string vertexShader = @"#version 330 core
layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec3 aColor;
out vec4 vertexColor;
void main()
{
vertexColor = vec4(aColor.rgb, 1.0);
gl_Position = vec4(aPosition.xy, 0, 1.0);
}";
string fragmentShader = @"#version 330 core
out vec4 FragColor;
in vec4 vertexColor;
void main()
{
FragColor = vertexColor;
}";
uint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, vertexShader);
glCompileShader(vs);
uint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, fragmentShader);
glCompileShader(fs);
shader = glCreateProgram();
glAttachShader(shader, vs);
glAttachShader(shader, fs);
glLinkProgram(shader);
vao = glGenVertexArray();
vbo = glGenBuffer();
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
float[] vertices =
{
xHeart[0], yHeart[0], 1f, 0f, 0f,
xHeart[1], yHeart[0], 1f, 0f, 0f,
xHeart[2], yHeart[1], 1f, 0f, 0f,
xHeart[0], yHeart[0], 1f, 0f, 0f,
xHeart[3], yHeart[2], 1f, 0f, 0f,
xHeart[3], yHeart[0], 1f, 0f, 0f,
xHeart[3], yHeart[2], 1f, 0f, 0f,
xHeart[3], yHeart[0], 1f, 0f, 0f,
xHeart[2], yHeart[0], 1f, 0f, 0f,
xHeart[1], yHeart[0], 1f, 0f, 0f,
xHeart[4], yHeart[2], 1f, 0f, 0f,
xHeart[4], yHeart[0], 1f, 0f, 0f,
xHeart[4], yHeart[2], 1f, 0f, 0f,
xHeart[4], yHeart[0], 1f, 0f, 0f,
xHeart[2], yHeart[0], 1f, 0f, 0f,
};
fixed (float* v = &vertices[0])
{
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertices.Length, v, GL_STATIC_DRAW);
}
glVertexAttribPointer(0, 2, GL_FLOAT, false, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 5 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
protected override void Update()
{
}
protected unsafe override void Render()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader);
if (posCounter % 100 == 0)
{
vao = glGenVertexArray();
vbo = glGenBuffer();
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
float[] vertices =
{
xHeart[0], yHeart[0], 1f, 0f, 0f,
xHeart[1], yHeart[0], 1f, 0f, 0f,
xHeart[2], yHeart[1], 1f, 0f, 0f,
xHeart[0], yHeart[0], 1f, 0f, 0f,
xHeart[3], yHeart[2], 1f, 0f, 0f,
xHeart[3], yHeart[0], 1f, 0f, 0f,
xHeart[3], yHeart[2], 1f, 0f, 0f,
xHeart[3], yHeart[0], 1f, 0f, 0f,
xHeart[2], yHeart[0], 1f, 0f, 0f,
xHeart[1], yHeart[0], 1f, 0f, 0f,
xHeart[4], yHeart[2], 1f, 0f, 0f,
xHeart[4], yHeart[0], 1f, 0f, 0f,
xHeart[4], yHeart[2], 1f, 0f, 0f,
xHeart[4], yHeart[0], 1f, 0f, 0f,
xHeart[2], yHeart[0], 1f, 0f, 0f,
};
fixed (float* v = &vertices[0])
{
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertices.Length, v, GL_STATIC_DRAW);
}
glVertexAttribPointer(0, 2, GL_FLOAT, false, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 5 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
posCounter = 0;
}
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 15);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
Glfw.SwapBuffers(DisplayManager.Window);
posCounter++;
}
}
}
Solution 1:[1]
Do not create a new Vertex Array Object and Vertex Buffer Object, when you just want to update the buffer object's data store. Just use glBufferSubData to update the buffer data:
protected unsafe override void Render()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
if (posCounter % 100 == 0)
{
float[] vertices =
{
xHeart[0], yHeart[0], 1f, 0f, 0f,
// [...]
};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
fixed (float* v = &vertices[0])
{
glBufferSubData(
GL_ARRAY_BUFFER, 0, sizeof(float) * vertices.Length, v);
}
posCounter = 0;
}
glUseProgram(shader);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 15);
glBindVertexArray(0);
Glfw.SwapBuffers(DisplayManager.Window);
posCounter++;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rabbid76 |
