glDrawArrays causes Segmantation fault

Started by
1 comment, last by LeeBrowm 9 years, 9 months ago

Hello everyone,

My question may seem pretty newbish for you but I just can't figure it out and it's really bothering me.

So... Having little knowledge about OpenGL I wanted to find myself a tutorial and stumbled upon video series on youtube that was published about half a year ago (so it's fairly recent) and decided to just follow it. I am quite sure that the mentioned tutorial does not have the error I experience (it is shown that it works).

I am using Code::Blocks and the system is Arch Linux x64, the computer has two video cards: 1) integrated Intel graphics card 2) nvidia Geforce GT 620M.

There is this class Mesh in the tutorial

mesh.cpp




#include "mesh.h"
#include <vector>

Mesh::Mesh(Vertex* vertices, unsigned int numVertices){
  m_drawCount = numVertices;
  glGenVertexArrays(1,& m_vertexArrayObject);
  glBindVertexArray(m_vertexArrayObject);
  std::vector<glm::vec3> positions;
  std::vector<glm::vec2> texCoords;
  positions.reserve(numVertices);
  texCoords.reserve(numVertices);

  for(unsigned int i=0; i<numVertices;i++){
    positions.push_back(*vertices[i].GetPos());
    texCoords.push_back(*vertices[i].GetTexCoord());
  }

  glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);

  glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
  glBufferData(GL_ARRAY_BUFFER,numVertices*sizeof(positions[0]), &positions[0],GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);


  glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]);
  glBufferData(GL_ARRAY_BUFFER,numVertices*sizeof(texCoords[0]), &texCoords[0],GL_STATIC_DRAW);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);


  glBindVertexArray(0);

}

Mesh::~Mesh(){
  glDeleteVertexArrays(1,& m_vertexArrayObject);
}

void Mesh::Draw(){
  glBindVertexArray(m_vertexArrayObject);
  glDrawArrays(GL_TRIANGLES, 0, m_drawCount);
  glBindVertexArray(0);
}

And I use it in my main.cpp


#include <iostream>
#include <GL/glew.h>
#include "display.h"
#include "shader.h"
#include "mesh.h"
#include "texture.h"
#include "glm/glm.hpp"
int main(){
  Display display(800, 600, "OpenGL");
  Vertex vertices[] = { Vertex(glm::vec3(-0.5, -0.5, 0), glm::vec2(0.0,0.0)),
			Vertex(glm::vec3(0, 0.5, 0), glm::vec2(0.5,1.0)),
			Vertex(glm::vec3(0.5, -0.5, 0), glm::vec2(1.0,0.0)),};
  Mesh mesh(vertices, sizeof(vertices)/sizeof(vertices[0]));
  Shader shader("./res/basicShader");
  Texture texture("./res/bricks.jpg");
  while(!display.IsClosed()){
    display.Clear(0.0f,0.15f,0.3f,1.0f);
    shader.Bind();
    texture.Bind(0);
    mesh.Draw();
    display.Update();
  }
  return 0;
}

But when I run my app in the debug mode it shows me this

Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: /home/lee/OpenGL/
Adding source dir: /home/lee/OpenGL/
Adding file: /home/lee/OpenGL/bin/Debug/OpenGL
Changing directory to: /home/lee/OpenGL/.
Set variable: LD_LIBRARY_PATH=.:
Starting debugger: /usr/bin/gdb -nx -fullname -quiet -args /home/lee/OpenGL/bin/Debug/OpenGL
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.7.1
Child process PID: 6602
Program received signal SIGSEGV, Segmentation fault.
In __memcpy_sse2_unaligned () (/usr/lib/libc.so.6)
#7 0x0000000000403386 in Mesh::Draw (this=0x7fffffffe700) at /home/lee/OpenGL/mesh.cpp:42
/home/lee/OpenGL/mesh.cpp:42:1256:beg:0x403386
At /home/lee/OpenGL/mesh.cpp:42
#7 0x0000000000403386 in Mesh::Draw (this=0x7fffffffe700) at /home/lee/OpenGL/mesh.cpp:42
/home/lee/OpenGL/mesh.cpp:42:1256:beg:0x403386

The line 42 is glDrawArrays(GL_TRIANGLES, 0, m_drawCount); from mesh.cpp

It is really confusing because ,as I've said, I just typed the whole code as it was done in the tutorial. Unfortunately I can not download the source code because it is not available for this part.

Can it be somehow connected with my hardware specifications?

I apologize beforehand if the answer is too simple and/or obvious.

Advertisement

When you are binding the texture coordinates


glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

You are specifying index 0 in the first parameter. It looks like that should be index 1.

My current game project Platform RPG
Thank you, HappyCoder, it fixed it, that was so stupid of me...

This topic is closed to new replies.

Advertisement