-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathexample.cpp
97 lines (76 loc) · 2.89 KB
/
example.cpp
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
#include "initShaders.h"
using namespace std;
int counter=0;//counter to increase how many vertices are going to be drawn at a given time
GLuint vaoID, vboID[2];//vao and vbo names
GLfloat vertexarray[]={0.5f,0.5f,0.0f,//vertice array
1.0f,0.0f,0.0f,
0.5f,-0.5f,0.0f,
0.0f,-1.0f,0.0f,
-0.5f,-0.5f,0.0f,
-1.0f,0.0f,0.0f,
-0.5f,0.5f,0.0f,
0.0f,1.0f,0.0f
};
GLfloat colorarray[]={1.0f,1.0f,0.0f,1.0f,//color array
0.0f,1.0f,0.0f,1.0f,
1.0f,0.0f,1.0f,1.0f,
0.5f,0.5f,1.0f,1.0f,
1.0f,0.5f,0.5f,1.0f,
0.0f,1.0f,0.5f,1.0f,
0.5f,0.5f,0.5f,1.0f,
1.0f,0.5f,1.0f,1.0f
};
void init(){
ShaderInfo shaders[]={
{ GL_VERTEX_SHADER , "vertexshader.glsl"} ,
{ GL_FRAGMENT_SHADER , "fragmentshader.glsl"},
{ GL_NONE , NULL}
};
initShaders(shaders);
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glGenBuffers(2, vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertexarray),vertexarray,GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, vboID[1]);
glBufferData(GL_ARRAY_BUFFER,sizeof(colorarray),colorarray,GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
}
void drawscene(){
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POLYGON,0,3+(counter%6));
glFlush();
}
void mousepress(int button, int state, int x, int y){
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
exit(0);
else if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
counter++;
drawscene();
}
}
int main(int argc,char ** argv){
glutInit(&argc, argv);
glutCreateWindow("Shapes");//creates the window with the specified name
//initializes glew
glewExperimental=GL_TRUE;
if(glewInit()){
fprintf(stderr, "Unable to initalize GLEW");
exit(EXIT_FAILURE);
}
glutInitContextVersion(4, 3);//specifies the version of opengl
glutInitContextProfile(GLUT_CORE_PROFILE|GLUT_COMPATIBILITY_PROFILE);//specifies what profile your using
init();
//retruns what version of opengl and glsl your computer can use
const GLubyte* version=glGetString(GL_SHADING_LANGUAGE_VERSION);
fprintf(stderr,"Opengl glsl version %s\n", version);
version=glGetString(GL_VERSION);
fprintf(stderr,"Opengl version %s\n", version);
glutDisplayFunc(drawscene);//displays callback draws the shapes
glutMouseFunc(mousepress);//control callback specifies the mouse controls
glutMainLoop();//sets opengl state in a neverending loop
return 0;
}