forked from lakshithamrt/CS452-LAB3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab3.cpp
More file actions
167 lines (128 loc) · 3.88 KB
/
Copy pathlab3.cpp
File metadata and controls
167 lines (128 loc) · 3.88 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
#include "initShaders.h"
#include <cstdlib>
using namespace std;
void rotate(GLuint locate);
GLuint vaoID,vboID[2],eboID;
GLuint program;
GLfloat pit,yaw,scalar=1;
glm::vec3 triTran;
GLfloat size=10;
GLfloat vertexarray[]={
-size, -size, size,
size, size, size,
size, -size, -size,
-size, size, -size
};
GLfloat colorarray[]={
0.0f,0.0f,1.0f,1.0f,
1.0f,1.0f,1.0f,1.0f,
1.0f,0.0f,1.0f,1.0f,
0.0f,1.0f,0.0f,1.0f
};
GLubyte elems[]={
0,1,2,3,0,1
};
void init(){
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 600, 600);
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);
glGenBuffers(1,&eboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,eboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(elems),elems,GL_STATIC_DRAW);
ShaderInfo shaders[]={
{ GL_VERTEX_SHADER , "vertexshader.glsl"},
{ GL_FRAGMENT_SHADER , "fragmentshader.glsl"},
{ GL_NONE , NULL}
};
program=initShaders(shaders);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
}
void display(SDL_Window* screen){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glm::mat4 trans;
trans=glm::translate(trans,triTran);//translate the triangle
trans=glm::rotate(trans,pit,glm::vec3(1,0,0));//rotate the triangle around the x axis
trans=glm::rotate(trans,yaw,glm::vec3(0,1,0));//rotate the triangle arround the y axis
trans=glm::scale(trans,glm::vec3(scalar));//scaling the triangle
GLint tempLoc = glGetUniformLocation(program,"modelMatrix");//Matrix that handle the transformations
glUniformMatrix4fv(tempLoc,1,GL_FALSE,&trans[0][0]);
glDrawElements(GL_TRIANGLE_STRIP,24,GL_UNSIGNED_BYTE,NULL);
glFlush();
SDL_GL_SwapWindow(screen);
}
void input(SDL_Window* screen){
SDL_Event event;
while (SDL_PollEvent(&event)){//Handling the keyboard
switch (event.type){
case SDL_QUIT:exit(0);break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_ESCAPE:exit(0);
case SDLK_w:triTran.y+=2;break;
case SDLK_s:triTran.y-=2;break;
case SDLK_a:triTran.x-=2;break;
case SDLK_d:triTran.x+=2;break;
case SDLK_e:scalar+=.1f;break;
case SDLK_q:scalar-=.1f;break;
case SDLK_i:pit+=2;break;
case SDLK_k:pit-=2;break;
case SDLK_j:yaw+=2;break;
case SDLK_l:yaw-=2;break;
}
/*case SDL_MOUSEMOTION:
yaw+=((event.motion.x)-300)/10.0;
pit+=((event.motion.y)-300)/10.0;
SDL_WarpMouseInWindow(screen,300,300);
}
*/
}
}
}
int main(int argc, char **argv){
//SDL window and context management
SDL_Window *window;
if(SDL_Init(SDL_INIT_VIDEO)<0){//initilizes the SDL video subsystem
fprintf(stderr,"Unable to create window: %s\n", SDL_GetError());
SDL_Quit();
exit(1);//die on error
}
//create window
window = SDL_CreateWindow(
"Schwarz - Lab 3", //Window title
SDL_WINDOWPOS_UNDEFINED, //initial x position
SDL_WINDOWPOS_UNDEFINED, //initial y position
500, //width, in pixels
500, //height, in pixels
SDL_WINDOW_OPENGL //flags to be had
);
//check window creation
if(window==NULL){
fprintf(stderr,"Unable to create window: %s\n",SDL_GetError());
}
//creates opengl context associated with the window
SDL_GLContext glcontext=SDL_GL_CreateContext(window);
//initializes glew
glewExperimental=GL_TRUE;
if(glewInit()){
fprintf(stderr, "Unable to initalize GLEW");
exit(EXIT_FAILURE);
}
init();
while(true){
input(window);//keyboard controls
display(window);//displaying
}
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}