forked from Loenix/CS452-LAB1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.cpp
More file actions
182 lines (149 loc) · 5.55 KB
/
Copy pathlab1.cpp
File metadata and controls
182 lines (149 loc) · 5.55 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
///////////////////////////////////////////////////////////////////////
// Chris Schwarz
// 1/28/14
// Lab 1
///////////////////////////////////////////////////////////////////////
// -----------------------
// --- I N C L U D E S ---
// -----------------------
#include "initShaders.h"
#include "vec.h"
using namespace std;
int currentObject;
// Vertex IDs
GLuint vaoID, vboID;
// Vertices
GLfloat trianglevertexarray[]={
0.5f,-0.5f,0.0f,
0.0f,0.5f,0.0f,
-0.5f,-0.5f,0.0f
};
GLfloat squarevertexarray[]={
-0.5f,0.5f,0.0f,
-0.5f,-0.5f,0.0f,
0.5f,0.5f,0.0f,
0.5f,-0.5f,0.0f
};
GLfloat pentagonvertexarray[]={
-0.2939f,-0.4045f,0.0f,
-0.4755f,0.1545f,0.0f,
0.0f,0.5f,0.0f,
0.4755f,0.1545f,0.0f,
0.2939f,-0.4045f,0.0f
};
// Indices of triangle
GLubyte indices[3]={0,1,2};
// -----------------------------------------
// --- O B J E C T G E N E R A T I O N ---
// -----------------------------------------
void triangle(){
glClear(GL_COLOR_BUFFER_BIT); // Clears the screen
glGenVertexArrays(1, &vaoID); // Generates object name for Vertex Array Objects
glBindVertexArray(vaoID); // Bind the object to the array
glGenBuffers(1, &vboID); // Generates object name for the Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, vboID); // Bind the object to the array
glBufferData(GL_ARRAY_BUFFER, sizeof(trianglevertexarray), trianglevertexarray, GL_STATIC_DRAW); // Allocates the memory of the vertices
ShaderInfo shaders[]={ // Create the shader specified by my initshaders
{ GL_VERTEX_SHADER , "vertexshader.glsl"} ,
{ GL_FRAGMENT_SHADER , "fragmentshader.glsl"},
{ GL_NONE , NULL}
};
initShaders(shaders); // Creates shaders
glEnableVertexAttribArray(0); // Enables the vertex attribute index
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0); // Specified the start the vertice array used to the draw
glDrawArrays(GL_TRIANGLES, 0, 3); // Draws array
glFlush(); // Makes sure the processes finish
}
void square(){
glClear(GL_COLOR_BUFFER_BIT); // Clears the screen
glGenVertexArrays(1, &vaoID); // Generates object name for Vertex Array Objects
glBindVertexArray(vaoID); // Bind the object to the array
glGenBuffers(1, &vboID); // Generates object name for the Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, vboID); // Bind the object to the array
glBufferData(GL_ARRAY_BUFFER, sizeof(squarevertexarray), squarevertexarray, GL_STATIC_DRAW); // Allocates the memory of the vertices
ShaderInfo shaders[]={ // Create the shader specified by my initshaders
{ GL_VERTEX_SHADER , "vertexshader.glsl"} ,
{ GL_FRAGMENT_SHADER , "fragmentshader2.glsl"},
{ GL_NONE , NULL}
};
initShaders(shaders); // Creates shaders
glEnableVertexAttribArray(0); // Enables the vertex attribute index
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0); // Specified the start the vertice array used to the draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // Draws array
glFlush(); // Makes sure the processes finish
}
void pentagon(){
glClear(GL_COLOR_BUFFER_BIT); // Clears the screen
glGenVertexArrays(1, &vaoID); // Generates object name for Vertex Array Objects
glBindVertexArray(vaoID); // Bind the object to the array
glGenBuffers(1, &vboID); // Generates object name for the Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, vboID); // Bind the object to the array
glBufferData(GL_ARRAY_BUFFER, sizeof(pentagonvertexarray), pentagonvertexarray, GL_STATIC_DRAW); // Allocates the memory of the vertices
ShaderInfo shaders[]={ // Create the shader specified by my initshaders
{ GL_VERTEX_SHADER , "vertexshader.glsl"} ,
{ GL_FRAGMENT_SHADER , "fragmentshader3.glsl"},
{ GL_NONE , NULL}
};
initShaders(shaders); // Creates shaders
glEnableVertexAttribArray(0); // Enables the vertex attribute index
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0); // Specified the start the vertice array used to the draw
glDrawArrays(GL_TRIANGLE_FAN, 0, 5); // Draws array
glFlush(); // Makes sure the processes finish
}
// -----------------------------------
// --- D R A W F U N C T I O N S ---
// -----------------------------------
void drawscene(){
// Display corresponding object
switch(currentObject){
case 0:
glutDisplayFunc(triangle);
glutPostRedisplay(); // Redraw the display
break;
case 1:
glutDisplayFunc(square);
glutPostRedisplay(); // Redraw the display
break;
case 2:
glutDisplayFunc(pentagon);
glutPostRedisplay(); // Redraw the display
break;
}
}
void idle(void){
glutPostRedisplay();
}
// -------------------------------
// --- M O U S E E V E N T S ---
// -------------------------------
void mousepress(int button, int state, int x, int y){
// Right click closes the screen
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN){
exit(0);
}
// Left click changes the object displayed
else if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
currentObject = (currentObject+1) % 3;
drawscene();
}
}
// -----------------------------
// --- M A I N M E T H O D ---
// -----------------------------
int main(int argc, char **argv){
// Initialize to display first object
currentObject = 0;
// Freeglut window and context management
glutInit(&argc, argv);
glutCreateWindow("Chris Schwarz - Lab 1"); // Creates the window with the specified name
// Initializes glew
glewExperimental = GL_TRUE;
if(glewInit()){
fprintf(stderr, "Unable to initalize GLEW");
exit(EXIT_FAILURE);
}
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;
}