PDA

View Full Version : Problems displaying triangle



Syzygy187
08-06-2015, 02:06 AM
Hi All :)

I am trying to put the below program together. All that it needs to do is display a triangle.



#include "Angel.h"

//----------------------------------------------------------------------------

void
init( void )
{
// sets the color that will be used when clearing the screen
glClearColor( 0.0, 0.0, 0.0, 0.0 );

vec4 vertexPositions[] =
{
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
};

GLuint positionBufferObject;

glGenBuffers(1, &positionBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);

GLuint program = InitShader( "vshader23.glsl", "fshader23.glsl" );
glUseProgram( program );

GLuint loc = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray(loc);

// tell openGL how to interpret the array of data stored in buffer
// this implicitly refers to the buffer currently bound to GL_ARRAY_BUFFER
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
}

//----------------------------------------------------------------------------

void
display( void )
{
// clears the window, sets color to glClearColor()
glClear( GL_COLOR_BUFFER_BIT );

// rendering function
glDrawArrays(GL_TRIANGLES, 0, 3);

// glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
// GL_POINTS, tells the GPU we want the data to be used to display distinct points rather than other primitives such as lines or polygons
// glDrawArrays( GL_POINTS, 0, NumPoints ); // draw the points
// glFlush ensures that all the data are rendered as soon as possible
glFlush();
}

//----------------------------------------------------------------------------

void reshape (int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
}

//----------------------------------------------------------------------------

void
keyboard( unsigned char key, int x, int y )
{

switch ( key ) {
case 033:
exit( EXIT_SUCCESS );
break;
}
}

//----------------------------------------------------------------------------

void demo_menu(int id)
{
switch(id)
{
case 1:
exit(0);
break;
}
glutPostRedisplay();
}

int
main( int argc, char **argv )
{
glutInit( &argc, argv );

// use GLUT_DOUBLE to add in double buffering. Remember parameters are logically or'ed.
// GLUT_DEPTH is used for hidden surface removal.
glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
glutInitWindowSize( 600, 600 );
glutInitWindowPosition(100, 100);
glutCreateWindow( "Assignment 01" );
glewExperimental = GL_TRUE;
glewInit();
init();

glutDisplayFunc( display );
glutKeyboardFunc( keyboard );

glutCreateMenu(demo_menu);
glutAddMenuEntry("quit", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);

glutMainLoop();
return 0;
}




#version 330

out vec4 outputColor;
void main()
{
outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}




#version 330

layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}


Please help, it will be much appreciated. I am not sure where I am going wrong.

tkausl
08-06-2015, 08:07 PM
You don't create and bind a VertexArrayObject and you do no errorhandling at all.

Syzygy187
08-06-2015, 11:13 PM
You don't create and bind a VertexArrayObject and you do no errorhandling at all.

Hi, what sort of error trapping/handling do you recommend?

I am using a book and at this point in the text they do not make use of any error handling whatsoever.

mobeen
08-07-2015, 04:52 AM
Place a glutSwapBuffers call at the end of your display callback after calling glFinish and see if it helps.

GClements
08-07-2015, 07:14 AM
Hi, what sort of error trapping/handling do you recommend?
1. Call glGetError() at the end of each top-level function (i.e. init() and display() in this case) and check that it returns zero (GL_NO_ERROR). If it returns a non-zero value, something failed and you then need to determine which function failed (typically by adding more such calls until you determine the specific function which generated the error);

2. When compiling and linking shaders, check the compilation status with glGetShader(GL_COMPILE_STATUS) and glGetProgram(GL_LINK_STATUS). If they return zero, query the log with glGetShaderInfoLog() or glGetProgramInfoLog() and print it.

Syzygy187
08-07-2015, 03:10 PM
Many thanks for all the help.

I will start using error handling where possible.

I have since implemented all of the suggestions and my triangle is now displaying perfectly! The biggest problem was that I had not used glGenVertexArrays()

tkausl
08-07-2015, 06:51 PM
I will start using error handling where possible.

I use a macro which i place afer every single opengl-call which then, if a error occurs, prints out the error, the file and the line. Thats really helpful while developing, in release-mode i just disable this macro (define it as empty)