VBO

#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#pragma comment(lib, "freeglut.lib")
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "OpenGL32.lib")

GLuint vbo;
class Vector3f
{
public:
    Vector3f()
    {

    }
    Vector3f(float x, float y, float z)
        :_x(x)
        ,_y(y)
        ,_z(z)
    {

    }
    float _x;
    float _y;
    float _z;
};
void init()
{
    Vector3f vertices[3];
    vertices[0] = Vector3f(0.0, 0.0, 0.0);
    vertices[1] = Vector3f(1.0, 0.0, 0.0);
    vertices[2] = Vector3f(0.0, 1.0, 0.0);
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);
    glutSwapBuffers();
}

void resize(int w, int h)
{

    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-2, 2, -2, 2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char ** argv)
{
    
    glutInit(&argc, argv);
   
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(35, 35);
    glutInitWindowSize(1024, 800);
    glutCreateWindow("OpenGL");
    GLenum res = glewInit();
    if (res != GLEW_OK)
    {
        std::cout << glewGetErrorString(res) << std::endl;
    }
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(resize);
    glutMainLoop();
    return EXIT_SUCCESS;
}

 

posted @ 2013-12-31 20:22  20118281131  阅读(989)  评论(0编辑  收藏  举报