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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include <stdio.h>
#include <GL/glew.h>
#include <GLUT/glut.h>
#include "math_3d.h"
#include <stdlib.h>
GLuint VBO;
Vector3f Vertices[20];
int VBOSize = 4;
void AddBox()
{
VBOSize += 4; //Will fix later
Vertices[4] = Vector3f(-2.0f, -1.0f, -10.0f);
Vertices[5] = Vector3f(-2.0f, 1.0f, -10.0f);
Vertices[6] = Vector3f(0.0f, 1.0f, -10.0f);
Vertices[7] = Vector3f(0.0f, -1.0f, -10.0f);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
200.0);
}
void keypress(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
default:
AddBox();
glutPostRedisplay();
break;
}
}
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_QUADS, 0, VBOSize);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
glutKeyboardFunc(keypress);
glutReshapeFunc(handleResize);
}
static void CreateVertexBuffer()
{
Vertices[0] = Vector3f(1.0f, -1.0f, -10.0f);
Vertices[1] = Vector3f(1.0f, 1.0f, -10.0f);
Vertices[2] = Vector3f(3.0f, 1.0f, -10.0f);
Vertices[3] = Vector3f(3.0f, -1.0f, -10.0f);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow("Box test");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
|