OpenGL Segfaulting on glDrawArrays

I am making a game in OpenGL on Windows using Code :: Blocks (C ++) and MinGW. All graphics and logic are in order, the game works fine. So far, all of a sudden, this is not so. After some random amount of time playing Segfaults. As is typical of GL segfaults, stack tracing is useless. The only thing I got is that segfault happened in the implementation of the OpenGL provider. I tested this on several video cards from different vendors, and they ALL do this, so the problem is at my end.

I ran the program, although gDEBugger, and got the same useless stack trace. Lol I also used GLIntercept, but nothing jumped at me. So I decided to find segfault in manual mode (BuGLe refuses to compile ...) and basically write trace messages to the log. I narrowed it down to one glDrawArrays call. I did a lot on the Internet and tried to find many fixes that I found.

Since this game was not encoded for portability, I did not dare to launch it in wine and valgrind. Before I go messing around with this wonderful mess, I would appreciate it if you could look at my code and find the error. (I'm sure this is something obvious ...)

(Note: For readability, I deleted all my trace lines)

void PlayerShip::Render() { glPushMatrix(); //Save the current state glTranslatef(m_PosX, m_PosY, 0); glRotatef(vect.Rotation - 90, 0, 0, 1); //Matrixes are applied in reverse order double xl = m_SizeX / 2, yl = m_SizeY / 2; //Get values to add/subract to the midpoint for the corners glEnable(GL_TEXTURE_2D); //Redundant TextureManager::Inst()->BindTexture(TexIDs::Playership01); //Bind the texture glEnableClientState(GL_VERTEX_ARRAY); //These enables and disables are redundant here glEnableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); GLdouble* Verts; Verts = new GLdouble[12] { -xl, -yl, -.5, xl, -yl, -.5, xl, yl, -.5, -xl, yl, -.5}; //Calculate quad vertices GLdouble* Texs; Texs = new GLdouble[8] {0, 0, 1, 0, 1, 1, 0, 1}; //Calculate texture vertices glVertexPointer(3, GL_DOUBLE, 0, Verts); glTexCoordPointer(2, GL_DOUBLE, 0, Texs); glDrawArrays(GL_QUADS, 0, 12); delete [] Verts; delete [] Texs; glPopMatrix(); } 

Interestingly, this code works ... for a while. I printed the values โ€‹โ€‹of all the variables, all of them are correct, the pointers are not equal to zero, calls to glGetError located at the distance of the entire code always return NO_ERROR. Honestly, I'm at a loss.

A note for anyone who suggests I do something else: I need to use vertex arrays to support GL (1.4) GL implementations, and I declare my arrays as pointers so that I can (in the future) reuse them for pulling a few primitives over once (for example, exhaust systems leave the ship)

+4
source share
1 answer

The last argument to glDrawArrays should be 4 (number of elements), not 12 (number of floats).

You are trying to draw 12 elements (= 36 floats) and gain access to the borders of your array.

+6
source

Source: https://habr.com/ru/post/1413085/


All Articles