Tutorial 1

First try at OpenGL 3 style application. You are probably better of looking at tutorial 2 since it is more complete. I left this for reference.

/******************************************************************************/
/**

Simple example of opengl 3 usage implmented using opengl 2 for mac.
Obviously, the fixed pipeline stuff should be removed. Consider this a first
test.

Created: 2009-05-11
Modified: 2009-05-11

Original author: filip.wanstrom _at_ gmail.com

Please feel free to use for any purpose. Preferably, add to this so we get an
best practices example.

**/

/******************************************************************************/
#include
#include

int             gLastFrameTime;
float   gRotation;
GLuint  gVBO;
GLuint  gEBO;
GLuint  gVAO;

// VBO macro
#define BUFFER_OFFSET(i) ((char *)NULL + (i))

// An icosahedron
#define _A 0.525731112119133606f
#define _B 0.850650808352039932f

#define NUM_INDICES 20
#define NUM_VERTICES 12

#ifdef __APPLE__
#define glGenVertexArrays(num, id) glGenVertexArraysAPPLE(num, id)
#define glBindVertexArray(id) glBindVertexArrayAPPLE(id)
#endif

static unsigned short icosa_indices[NUM_INDICES][3]=
{
{0,4,1},{0,9,4},{9,5,4},{4,5,8},{4,8,1},
{8,10,1},{8,3,10},{5,3,8},{5,2,3},{2,7,3},
{7,10,3},{7,6,10},{7,11,6},{11,0,6},{0,1,6},
{6,1,10},{9,0,11},{9,11,2},{9,2,5},{7,2,11}
};

static float icosa_verts[NUM_VERTICES][3]=
{
{-_A,0.0,_B},{_A,0.0,_B},{-_A,0.0,-_B},{_A,0.0,-_B},
{0.0,_B,_A},{0.0,_B,-_A},{0.0,-_B,_A},{0.0,-_B,-_A},
{_B,_A,0.0},{-_B,_A,0.0},{_B,-_A,0.0},{-_B,-_A,0.0}
};

/******************************************************************************/

static void draw_icosa()
{
int i;
glBegin(GL_TRIANGLES);
for (i=0;i<20;i++) {
glVertex3fv(icosa_verts[icosa_indices[i][0]]);
glVertex3fv(icosa_verts[icosa_indices[i][1]]);
glVertex3fv(icosa_verts[icosa_indices[i][2]]);
}
glEnd();
}

/******************************************************************************/

static void draw_arrays()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, icosa_verts);
glDrawElements(GL_TRIANGLES, 20*3, GL_UNSIGNED_SHORT, icosa_indices);

glDisableClientState(GL_VERTEX_ARRAY);
}

/******************************************************************************/

static void create_buffers()
{
int sizeInBytes = 0;

glGenVertexArrays(1, &gVAO);
glBindVertexArray(gVAO);

sizeInBytes = sizeof(float)*3*NUM_VERTICES;
glGenBuffers(1, &gVBO);
glBindBuffer(GL_ARRAY_BUFFER, gVBO);
glBufferData(GL_ARRAY_BUFFER, sizeInBytes, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeInBytes, icosa_verts);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));

sizeInBytes = sizeof(unsigned short)*3*NUM_INDICES;
glGenBuffers(1, &gEBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInBytes,NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeInBytes, icosa_indices);

glBindVertexArray(0);

}

/******************************************************************************/

static void draw_vao()
{

glBindVertexArray(gVAO);
glDrawElements(GL_TRIANGLES, NUM_INDICES*3, GL_UNSIGNED_SHORT,
BUFFER_OFFSET(0));
glBindVertexArray(0);

}

/******************************************************************************/

static void draw_vbo()
{
glEnableClientState(GL_VERTEX_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, gVBO);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gEBO);

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

glDisableClientState(GL_VERTEX_ARRAY);
}

/******************************************************************************/

void display(void)
{

// Update
if (gLastFrameTime == 0)
{
gLastFrameTime = glutGet(GLUT_ELAPSED_TIME);
}
int now = glutGet(GLUT_ELAPSED_TIME);
int elapsedMilliseconds = now - gLastFrameTime;
float elapsedTime = elapsedMilliseconds / 1000.0f;
gLastFrameTime = now;
gRotation+=elapsedTime*30.0f;

// Draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslatef(0.0f,0.0f,-10.0f);
glRotatef(gRotation, 0.0f, 0.0f, 1.0f);
draw_vao();

glPopMatrix();
glutSwapBuffers();
}

/******************************************************************************/

void reshape(int width, int height)
{
if (width ==0)
width = 1;
float nearPlane = 0.1f;
float farPlane = 1000.0f;
GLfloat h = (GLfloat) height / (GLfloat) width;
glViewport(0, 0, (GLint) width, (GLint) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glFrustum(-1.0f*nearPlane,
nearPlane,
-h*nearPlane,
h*nearPlane,
nearPlane,
farPlane);

GLfloat aspect = 1.0f/h;
glLoadIdentity();
gluPerspective(90, aspect, nearPlane, farPlane);
glMatrixMode(GL_MODELVIEW);
}

/******************************************************************************/

void idle(void)
{
glutPostRedisplay();
}

/******************************************************************************/

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

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);

glutCreateWindow("GL3 Tutorial 1");

create_buffers();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);

glutMainLoop();
return EXIT_SUCCESS;
}
/******************************************************************************/

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2011 CFW Design Suffusion theme by Sayontan Sinha