-2

I've been trying to make a Snake in c++ with Opengl, the main components work and quite everything is fine, but somehow when the Snake's tail gets bigger, it glitches.

Program's exact behaviour:

Program's behaviour

Basically, I already "made" this but without batch rendering, and this problem didn't appear at all and now i'm just confused about what's happening

EDIT : The glitch only appears after taking when tail's size is an even number

  • Code compiled with Visual Studio Community 2019 in Debug x32,
  • Processor: AMD Ryzen 5 3400G with Radeon Vega Graphics 3.70 GHz
  • Ram: 6Go

Code:

App.cpp (Where the problem most likely is)

#include <glm/glm.h>
#include <glad/glad.h>
#include <glfw/glfw3.h>

int main(){
// Snake Head

// n_Height is 770 and n_Width is 1090
    glm::mat4 proj = glm::ortho(0.0f, float(n_Height), 0.0f, float(n_Height), -1.0f, 1.0f);



Snake::snakeTail = {
        n_Width / 2 - 5, n_Height / 2 - 5, 0.0f,
        n_Width / 2 - 5, n_Height / 2 + 5, 0.0f,
        n_Width / 2 + 5, n_Height / 2 + 5, 0.0f,
        n_Width / 2 + 5, n_Height / 2 - 5, 0.0f
};

/*
  Each snake tail parts is 12 vertices 
*/

// Snake tail 

for (int i = 1; i < Snake::tailSize+1; i++) {
        Snake::snakeTail.push_back(Snake::snakeTail[0] - i * 10);
        Snake::snakeTail.push_back(Snake::snakeTail[1]);
        Snake::snakeTail.push_back(0.0f);

        Snake::snakeTail.push_back(Snake::snakeTail[3] - i * 10);
        Snake::snakeTail.push_back(Snake::snakeTail[4]);
        Snake::snakeTail.push_back(0.0f);

        Snake::snakeTail.push_back(Snake::snakeTail[6] - i * 10);
        Snake::snakeTail.push_back(Snake::snakeTail[7]);
        Snake::snakeTail.push_back(0.0f);

        Snake::snakeTail.push_back(Snake::snakeTail[9]  - i * 10);
        Snake::snakeTail.push_back(Snake::snakeTail[10]);
        Snake::snakeTail.push_back(0.0f);
}

for (int i = 0; i < n_Height/10*n_Width/10; i++) {
        Snake::indices.push_back(i * 4);

        Snake::indices.push_back(i * 4 + 1);

        Snake::indices.push_back(i * 4 + 2);

        Snake::indices.push_back(i * 4 + 2);

        Snake::indices.push_back(i * 4 + 3);

        Snake::indices.push_back(i * 4);
}
}

//Creates vertex array, index buffer, shader program, vertex buffer and renders
// sets uniform mat4 with proj matrix

Snake.cpp (What happens on collision with apple and when snake moves):

 // When snake hits apple 
            tailSize++;
            Snake::snakeTail.push_back(Snake::snakeTail[0] - tailSize * 10);
            Snake::snakeTail.push_back(Snake::snakeTail[1]);
            Snake::snakeTail.push_back(0.0f);

            Snake::snakeTail.push_back(Snake::snakeTail[3] - tailSize * 10);
            Snake::snakeTail.push_back(Snake::snakeTail[4]);
            Snake::snakeTail.push_back(0.0f);

            Snake::snakeTail.push_back(Snake::snakeTail[6] - tailSize * 10);
            Snake::snakeTail.push_back(Snake::snakeTail[7]);
            Snake::snakeTail.push_back(0.0f);

            Snake::snakeTail.push_back(Snake::snakeTail[9] - tailSize * 10);
            Snake::snakeTail.push_back(Snake::snakeTail[10]);
            Snake::snakeTail.push_back(0.0f);

// On render

for (int i = tailSize; i > 0; i--) {
            snakeTail[i * 12] = snakeTail[(i - 1) * 12];
            snakeTail[i * 12 + 1] = snakeTail[(i - 1) * 12 + 1];
            snakeTail[i * 12 + 3] = snakeTail[(i - 1) * 12 + 3];
            snakeTail[i * 12 + 4] = snakeTail[(i - 1) * 12 + 4];
            snakeTail[i * 12 + 6] = snakeTail[(i - 1) * 12 + 6];
            snakeTail[i * 12 + 7] = snakeTail[(i - 1) * 12 + 7];
            snakeTail[i * 12 + 9] = snakeTail[(i - 1) * 12 + 9];
            snakeTail[i * 12 + 10] = snakeTail[(i - 1) * 12 + 10];
}
 // Movement 
        snakeTail[0] += addX;
        snakeTail[1] += addY;
        snakeTail[3] += addX;
        snakeTail[4] += addY;
        snakeTail[6] += addX;
        snakeTail[7] += addY;
        snakeTail[9] += addX;
        snakeTail[10] += addY;
// Sets buffer data to the new array
glBufferData(&Snake::snakeTail[0], snakeTail.size());
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Taka
  • 11
  • 3
  • 3
    At StackOverflow the code must be in the question. Remember the main purpose of a question is to help future readers with the same problem. – drescherjm Feb 09 '20 at 19:25
  • Hi, sorry about the question not being precise enough, i didn't really find a way to "put only the main purpose", and I felt like putting the code inside the question would have been a bit too much, I can fix it right now tho. Have a great day – Taka Feb 09 '20 at 19:29
  • 2
    When the code is too much you need to simplify. The requirement is here: [mcve] – drescherjm Feb 09 '20 at 19:30
  • 1
    ***I don't really know where the problem is in the code, so how can I actually simplify it*** This is where you need to learn how to use your debugger. If you knew how to debug you should be able to execute your code 1 line at a time and see exactly where it's behavior deviates from your expectation. – drescherjm Feb 09 '20 at 19:55
  • Imma try to debug that, but I can't untill tomorrow, i'll give my results when i'm done debugging – Taka Feb 09 '20 at 19:59

1 Answers1

1

So hi, basically the answer was that I did an error that took me hours to find, it was such a simple error and yet it made opengl all act wrong,

I was setting up the buffer by using this :

    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);

while I had to do this :

    glBufferDara(GL_ARRAY_BUFFER, size*sizeof(float), data, GL_STATIC_DRAW);

so yeh that question was useless, sorry for disturbing everything :3

Taka
  • 11
  • 3