Hi All
I have managed to simplify my code and get some sort of a display.
The cube does not display correctly. It appears as if only two sides of the cube are displaying and these sides run off into the top corner of the screen. Have I made an error with the way I have applied the transformations?
#include "Angel.h"
//----------------------------------------------------------------------------
GLuint program;
void
init( void )
{
vec4 stackCube[] = {
vec4(-1.0f, +1.0f, +1.0f, 1.0), // 0
vec4(+1.0f, +0.0f, +0.0f, 1.0), // Colour
vec4(+1.0f, +1.0f, +1.0f, 1.0), // 1
vec4(+0.0f, +1.0f, +0.0f, 1.0), // Colour
vec4(+1.0f, +1.0f, -1.0f, 1.0), // 2
vec4(+0.0f, +0.0f, +1.0f, 1.0), // Colour
vec4(-1.0f, +1.0f, -1.0f, 1.0), // 3
vec4(+1.0f, +1.0f, +1.0f, 1.0), // Colour
vec4(-1.0f, +1.0f, -1.0f, 1.0), // 4
vec4(+1.0f, +0.0f, +1.0f, 1.0), // Colour
vec4(+1.0f, +1.0f, -1.0f, 1.0), // 5
vec4(+0.0f, +0.5f, +0.2f, 1.0), // Colour
vec4(+1.0f, -1.0f, -1.0f, 1.0), // 6
vec4(+0.8f, +0.6f, +0.4f, 1.0), // Colour
vec4(-1.0f, -1.0f, -1.0f, 1.0), // 7
vec4(+0.3f, +1.0f, +0.5f, 1.0), // Colour
vec4(+1.0f, +1.0f, -1.0f, 1.0), // 8
vec4(+0.2f, +0.5f, +0.2f, 1.0), // Colour
vec4(+1.0f, +1.0f, +1.0f, 1.0), // 9
vec4(+0.9f, +0.3f, +0.7f, 1.0), // Colour
vec4(+1.0f, -1.0f, +1.0f, 1.0), // 10
vec4(+0.3f, +0.7f, +0.5f, 1.0), // Colour
vec4(+1.0f, -1.0f, -1.0f, 1.0), // 11
vec4(+0.5f, +0.7f, +0.5f, 1.0), // Colour
vec4(-1.0f, +1.0f, +1.0f, 1.0), // 12
vec4(+0.7f, +0.8f, +0.2f, 1.0), // Colour
vec4(-1.0f, +1.0f, -1.0f, 1.0), // 13
vec4(+0.5f, +0.7f, +0.3f, 1.0), // Colour
vec4(-1.0f, -1.0f, -1.0f, 1.0), // 14
vec4(+0.4f, +0.7f, +0.7f, 1.0), // Colour
vec4(-1.0f, -1.0f, +1.0f, 1.0), // 15
vec4(+0.2f, +0.5f, +1.0f, 1.0), // Colour
vec4(+1.0f, +1.0f, +1.0f, 1.0), // 16
vec4(+0.6f, +1.0f, +0.7f, 1.0), // Colour
vec4(-1.0f, +1.0f, +1.0f, 1.0), // 17
vec4(+0.6f, +0.4f, +0.8f, 1.0), // Colour
vec4(-1.0f, -1.0f, +1.0f, 1.0), // 18
vec4(+0.2f, +0.8f, +0.7f, 1.0), // Colour
vec4(+1.0f, -1.0f, +1.0f, 1.0), // 19
vec4(+0.2f, +0.7f, +1.0f, 1.0), // Colour
vec4(+1.0f, -1.0f, -1.0f, 1.0), // 20
vec4(+0.8f, +0.3f, +0.7f, 1.0), // Colour
vec4(-1.0f, -1.0f, -1.0f, 1.0), // 21
vec4(+0.8f, +0.9f, +0.5f, 1.0), // Colour
vec4(-1.0f, -1.0f, +1.0f, 1.0), // 22
vec4(+0.5f, +0.8f, +0.5f, 1.0), // Colour
vec4(+1.0f, -1.0f, +1.0f, 1.0), // 23
vec4(+0.9f, +1.0f, +0.2f, 1.0), // Colour
};
// Create a vertex array object
GLuint vao;
// use glGenVertexArray to find an unused name for the buffer
glGenVertexArrays( 1, &vao );
// The first time the function glBindVertexArray is executed for a given name, the object is created
glBindVertexArray( vao );
// Create and initialize a buffer object
GLuint vertexBufferID;
// we use glGenBuffers to give us an unused identifier for our buffer object that is put into the variable buffer.
glGenBuffers( 1, &vertexBufferID );
// The type GL_ARRAY_BUFFER indicates that the data in the buffer will be vertex attribute data rather than
// one of the other storage types that we will encounter later.
glBindBuffer( GL_ARRAY_BUFFER, vertexBufferID );
// with glBufferData, we allocate sufficient memory on the GPU for our data and provide a pointer to the array holding the data
glBufferData( GL_ARRAY_BUFFER, sizeof(stackCube), stackCube, GL_STATIC_DRAW );
// Load shaders and use the resulting shader program
program = InitShader( "vshader.glsl", "fshader.glsl" );
glUseProgram( program );
// Initialize the vertex position attribute from the vertex shader
// The function glGetAttribLocation returns the index of an attribute variable, such as
// the vertex location attribute vPosition in our vertex shader.
GLuint loc = glGetAttribLocation( program, "vPosition" );
// this enables the vertex attributes that are in the shaders
// describe the form of the data in the vertex array
// In glVertexAttribPointer, the second and third parameters specify that the array points to a 4D array of type GL_FLOAT
// The fourth parameter says that we do not want the data normalized to be the range (0.0, 1.0), whereas the fifth if 0, that the values in the array are contiguous/tightly packed
// The last parameter is the address in the buffer where the data begin.
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, BUFFER_OFFSET(0) );
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (char*)(sizeof(float) * 4) );
unsigned short stackIndices[] = {
0, 1, 2, 0, 2, 3, // Top
4, 5, 6, 4, 6, 7, // Front
8, 9, 10, 8, 10, 11, // Right
12, 13, 14, 12, 14, 15, // Left
16, 17, 18, 16, 18, 19, // Back
20, 22, 21, 20, 23, 22, // Bottom
};
GLuint indexBufferID;
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(stackIndices), stackIndices, GL_STATIC_DRAW);
}
//----------------------------------------------------------------------------
void
display( void )
{
glClearColor(0.0f / 255.0f, 0.0f / 255.0f, 0.0f / 255.0f, 1.0f);
glClearDepth(1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the window
mat4 projectionMatrix = Perspective(60.0f, 1.0f, 0.5f, 10.0f);
mat4 translationMatrix = Translate(vec3(0.0f, 0.0f, -10.0f));
mat4 fullTransformMatrix = projectionMatrix * translationMatrix * RotateZ(-90.0f) * RotateY(0.0f) * RotateX(0.0f);
GLint fullTransformMatrixUniformLocation = glGetUniformLocation(program, "fullTransformMatrix");
glUniformMatrix4fv(fullTransformMatrixUniformLocation, 1, GL_FALSE, &fullTransformMatrix[0][0]);
//glDrawArrays( GL_TRIANGLES, 0, 3 ); // draw the points
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_SHORT, 0);
glutSwapBuffers();
// 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 )
{
int w = 800, h = 800;
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE ); // use GLUT_DOUBLE to add in double buffering. Remember parameters are logically or'ed. GLUT_DEPTH is used for hidden surface removal.
glutInitWindowSize( w, h );
glutInitWindowPosition(100, 100);
glutCreateWindow( "Cube Transformation" );
glewExperimental = GL_TRUE;
glewInit();
init();
glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
glutReshapeFunc( reshape );
glutCreateMenu(demo_menu);
glutAddMenuEntry("quit", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}
Vertex Shader:
#version 430
in layout(location = 0) vec4 vPosition;
in layout(location = 1) vec4 vColor;
out vec4 color;
uniform mat4 fullTransformMatrix;
void
main()
{
color = vColor;
gl_Position = fullTransformMatrix * vPosition;
}
Fragment Shader:
#version 430
in vec4 color;
out vec4 fColor;
void
main()
{
fColor = color;
}
Please assist where possible.
Many Thanks