unable to get VBO to render

I’m trying to render a single triangle using interleaved vertex attributes (position and color). The program does not crash or anything but it does not render the triangle. I know this is a lot of code, but I would appreciate it if someone could look at it:


#define BUFFER_OFFSET(i)((char*)NULL+(i))

#include <GLTools.h>            // OpenGL toolkit
#include <GLShaderManager.h>    // Shader Manager Class
#include <GLFrustum.h>
#include <GL/glut.h>            // Windows FreeGlut equivalent

struct vertexType
{
	GLfloat xPos,yPos,zPos;
	GLfloat rColor,bColor,gColor,aColor;
};

GLShaderManager	shaderManager;
GLFrustum frustum;
vertexType vertexArray[3];
GLuint vertexIndex[3];
GLuint vdataBufferID; //vertex data
GLuint viBufferID; //vertex index buffer

bool zRotationDirection; //Clockwise or Counterclockwise
float zRotationAngle; //Angle to transform to
bool zTranslationDirection; //forward or backward
float zTranslation; //z coord to tranlate to

void loadVertexArray(unsigned int index,GLfloat x,GLfloat y,GLfloat z,GLfloat r,GLfloat g,GLfloat b,GLfloat a)
{
	vertexArray[index].xPos=x;
	vertexArray[index].yPos=y;
	vertexArray[index].zPos=z;
	vertexArray[index].rColor=r;
	vertexArray[index].bColor=b;
	vertexArray[index].gColor=g;
	vertexArray[index].aColor=a;
}
void initTriCube()
{
	loadVertexArray(0, -0.5f,0.5f,0.0f, 1.0f,0.0f,0.0f,1.0f);
	loadVertexArray(1, -0.5f,-0.5f,0.0f, 0.0f,1.0f,0.0f,1.0f);
	loadVertexArray(2, 0.5f,-0.5f,0.0f, 0.0f,0.0f,1.0f,1.0f);
	vertexIndex[0]=0;
	vertexIndex[1]=1;
	vertexIndex[2]=2;
	
	glGenBuffers(1,&vdataBufferID);
	glBindBuffer(GL_ARRAY_BUFFER,vdataBufferID);
	glBufferData(GL_ARRAY_BUFFER,3*sizeof(vertexType),&vertexArray,GL_STATIC_DRAW); //check here later

	glGenBuffers(1,&viBufferID);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,viBufferID);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,3*sizeof(GLuint),&vertexIndex,GL_STATIC_DRAW);
}
void SpecialKeys(int key, int x, int y)
{
	switch(key)
	{
	case GLUT_KEY_UP:		zTranslationDirection=true;
							break;
	case GLUT_KEY_DOWN:		zTranslationDirection=false;
							break;
	case GLUT_KEY_LEFT:		zRotationDirection=true;
							break;
	case GLUT_KEY_RIGHT:	zRotationDirection=false;
							break;
	default:				break;
	}
}
void ChangeSize(int w, int h)
{
	glViewport(0, 0, w, h);
	frustum.SetPerspective(35.0f,float(w)/float(h),1.0f,1000.0f);
}

void SetupRC()
{
	// Blue background
	glClearColor(0.0f, 0.0f, 255.0f, 1.0f );
	glCullFace(GL_BACK);
	glEnable(GL_CULL_FACE);
	glEnable(GL_DEPTH_TEST);
    
	zRotationAngle=0.0f;
	zRotationDirection=false;
	zTranslation=-2.0f;
	zTranslationDirection=true;

	shaderManager.InitializeStockShaders();
	initTriCube();
}
void RenderScene(void)
{
	// Clear the window with current clearing color
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	M3DMatrix44f rotMatrix;
	M3DMatrix44f translationMatrix;
	M3DMatrix44f modelViewMatrix;
	M3DMatrix44f modelViewProjectionMatrix;

	if(zRotationDirection==false)
		zRotationAngle++;
	else
		zRotationAngle--;
	if(zTranslation==-2.0f)
		zTranslationDirection=true;
	if(zTranslation==-100.0f)
		zTranslationDirection=false;

	if(zTranslationDirection==false)
		zTranslation=zTranslation+0.5f;
	else
		zTranslation=zTranslation-0.5f;

	m3dRotationMatrix44(rotMatrix,m3dDegToRad(zRotationAngle),0.0f,0.0f,1.0f);
	m3dTranslationMatrix44(translationMatrix,0.0f,0.0f,zTranslation);
	m3dMatrixMultiply44(modelViewMatrix,rotMatrix,translationMatrix);
	m3dMatrixMultiply44(modelViewProjectionMatrix,frustum.GetProjectionMatrix(),modelViewMatrix);
	
	shaderManager.UseStockShader(GLT_SHADER_SHADED,modelViewProjectionMatrix);
	
	glBindBuffer(GL_ARRAY_BUFFER,vdataBufferID);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(vertexType),BUFFER_OFFSET(0));
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1,4,GL_FLOAT,GL_FALSE,sizeof(vertexType),BUFFER_OFFSET(12));

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,viBufferID);
	glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));

	glDisableVertexAttribArray(0);
	glDisableVertexAttribArray(1);

	glutSwapBuffers();
	glutPostRedisplay();
}
int main(int argc, char* argv[])
{
	gltSetWorkingDirectory(argv[0]);
	
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
	glutSpecialFunc(SpecialKeys);

	GLenum err = glewInit();
	if (GLEW_OK != err) 
	{
		fprintf(stderr, "GLEW Error: %s
", glewGetErrorString(err));
		return 1;
	}
	SetupRC();

	glutMainLoop();
	glDeleteBuffers(1,&vdataBufferID);
	glDeleteBuffers(1,&viBufferID);
	return 0;
}


Can anyone be bothered to look at this and give me a shove in the right direction?

I still can’t figure this out and I don’t know where else to go. I’ve tried using vec4 and vec3 in the structure (as opposed to GLfloats) like the example in the book (GL Super Bible) does but I just get errors when I do that. This is becoming extremely frustrating for me.

Edit: this is what the stock shader does:


// GLT_SHADER_SHADED
// Point light, diffuse lighting only
static const char *szShadedVP =		"uniform mat4 mvpMatrix;"
									"attribute vec4 vColor;"
									"attribute vec3 vVertex;"
									"varying vec4 vFragColor;"
									"void main(void) {"
									"vFragColor = vColor; "
									" gl_Position = mvpMatrix * vec4(vVertex, 1.0); "
									"}";

What size is your index data? You have:

glBufferData(GL_ELEMENT_ARRAY_BUFFER,3*sizeof(GLuint),&vertexIndex,GL_STATIC_DRAW);

And then:

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));

So either you’re allocating too much space for index data, and it should be 3sizeof(GLushort) instead of 3sizeof(GLuint) - this won’t cause any rendering issues though, or you’re using the wrong argument in glDrawElements and should have GL_UNSIGNED_INT instead of GL_UNSIGNED_SHORT - this will cause rendering errors.

Thank you so much! It started working after I switched GL_UNSIGNED_SHORT with GL_UNSIGNED_INT.