1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
static const int BufferSize = 2;
static GLuint BufferName[BufferSize]={0,0};
static const GLsizei VertexCount = 3;
static const GLfloat positionData[9] = {0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f};
static const GLfloat colorData[12] = {1.0f, 1.0f, 1.0f, 0.5f,
1.0f, 1.0f, 1.0f, 0.5f,
1.0f, 1.0f, 1.0f, 0.5f};
enum
{
POSITIONS=0,
COLORS=1
};
void init (void)
{
glewInit();
glGenBuffers(2, BufferName);
GLsizeiptr positionSize = VertexCount*3*sizeof(GLfloat);
GLsizeiptr colorSize = VertexCount*4*sizeof(GLfloat);
glBindBuffer(GL_ARRAY_BUFFER, BufferName[COLORS]);
glBufferData(GL_ARRAY_BUFFER, colorSize, colorData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, BufferName[POSITIONS]);
glBufferData(GL_ARRAY_BUFFER, positionSize, positionData, GL_STATIC_DRAW);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glShadeModel(GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glBindBuffer(GL_ARRAY_BUFFER, BufferName[COLORS]);
glColorPointer(4, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, BufferName[POSITIONS]);
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, VertexCount);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glutSwapBuffers();
}
void reshape(int width, int height)
{
int side=0;
if (width < height) side = width; else side=height;
glViewport((width - side) / 2, (height - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (300, 200);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutReshapeFunc (reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
} |
Partager