Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Template ZPG */
- #include "zpg.h"
- glm::mat4 projectionMatrix; // Store the projection matrix
- glm::mat4 viewMatrix; // Store the view matrix
- glm::mat4 modelMatrix; // Store the model matrix
- int CurrentWidth = 800,
- CurrentHeight = 600,
- WindowHandle = 0;
- int buttonDown =-1;
- GLuint programID,
- vertexID,
- fragmentID;
- GLuint VAO,
- VBO,
- IBO;
- GLuint
- ProjectionMatrixUniformLocation,
- ViewMatrixUniformLocation,
- ModelMatrixUniformLocation,
- LightColor,
- LightLocation;
- float myView=-2;
- vec3 lightPos=vec3(2,0,0);
- float rotationx = 45;
- void DrawCube(void)
- {
- lightPos=lightPos*glm::angleAxis(1.0f,vec3(0,1,0));
- rotationx+=1;
- modelMatrix = glm::rotate(glm::mat4(1.0f),rotationx,glm::vec3(0.0f, 1.0f, 0.0f));
- glUseProgram(programID);
- glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, &viewMatrix[0][0]); // Send our view matrix to the shader
- glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, &modelMatrix[0][0]); // Send our model matrix to the shader
- /*glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid*)0);*/
- glUniform4f(LightColor,1.0,0.5,0.5,1.0);
- glUniform3f(LightLocation,lightPos.x,lightPos.y,lightPos.z);
- glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES,0,36); //nepoužíváme IBO
- glUseProgram(0);
- }
- void RenderFunction(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- DrawCube();
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void ResizeFunction(int Width, int Height)
- {
- CurrentWidth = Width;
- CurrentHeight = Height;
- glViewport(0, 0, CurrentWidth, CurrentHeight);
- projectionMatrix = glm::perspective(60.0f, (float)CurrentWidth / CurrentHeight, 0.1f, 100.0f);
- glUseProgram(programID);
- glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, &projectionMatrix[0][0]);
- glUseProgram(0);
- }
- void KeyboardFunction(unsigned char Key, int X, int Y)
- {
- switch (Key)
- {
- case 'M':
- case 'm':
- {
- printf("Key M\n");
- int i,j;
- for (j=0; j<4; j++){
- for (i=0; i<4; i++){
- printf("%f ",viewMatrix[i][j]);
- }
- printf("\n");
- }
- break;
- }
- case 'V':
- case 'v':
- {
- printf("Key V\n");
- viewMatrix[0][0]=1.0; viewMatrix[1][0]=0.0; viewMatrix[2][0]=0.0; viewMatrix[3][0]=0.0;
- viewMatrix[0][1]=0.0; viewMatrix[1][1]=1.0; viewMatrix[2][1]=0.0; viewMatrix[3][1]=0.0;
- viewMatrix[0][2]=0.0; viewMatrix[1][2]=0.0; viewMatrix[2][2]=1.0; viewMatrix[3][2]=-10.0;
- viewMatrix[0][3]=0.0; viewMatrix[1][3]=0.0; viewMatrix[2][3]=0.0; viewMatrix[3][3]=1.0;
- break;
- }
- case 27: // QUIT
- glutLeaveMainLoop();
- default:
- break;
- }
- }
- void processMouseActiveMotion(int x, int y)
- {
- printf("Active Mouse (%d) motion a x=%d e y=%d\n",buttonDown,x,y);
- }
- void processMousePassiveMotion(int x, int y)
- {
- }
- void processMouse(int button, int state, int x, int y)
- {
- printf("ProcessMouse (%d), state %d a x=%d e y=%d\n",button,state,x,y);
- if ((state == GLUT_DOWN) && (button == GLUT_LEFT_BUTTON))
- {
- buttonDown=button;
- }else buttonDown=-1;
- }
- void myMouseWheel(int wheel, int direction, int x, int y)
- {
- myView+=0.1f*(float)direction;
- viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, myView)); // Create our view matrix which will translate us back 5 units
- }
- void IdleFunction(void)
- {
- glutPostRedisplay();
- }
- void CreateShaders()
- {
- vertexID = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexID, 1, &VertexShader, NULL);
- glCompileShader(vertexID);
- GLint status = GL_FALSE;
- //nactenivysledku kompilace
- glGetShaderiv(vertexID,GL_COMPILE_STATUS,&status);
- //test na chybu
- if(!status){
- fprintf(stderr,"Vertex shader: \n");
- GLchar ErrorLog[1024] = { 0 };
- //nacteni a vypis chyby
- glGetShaderInfoLog(vertexID,sizeof(ErrorLog),NULL,ErrorLog);
- fprintf(stderr,ErrorLog);
- //exit(EXIT_FAILURE);
- }
- fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentID, 1, &FragmentShader, NULL);
- glCompileShader(fragmentID);
- programID = glCreateProgram();
- glAttachShader(programID, vertexID);
- glAttachShader(programID, fragmentID);
- glLinkProgram(programID);
- status = GL_FALSE;
- //nactenivysledkuLinkovani
- glGetProgramiv(programID, GL_LINK_STATUS,&status);
- //test na chybu
- if(!status){
- fprintf(stderr,"Vertex shadeeeer: \n");
- GLchar ErrorLog[1024] = { 0 };
- //nacteni a vypis chyby
- glGetShaderInfoLog(fragmentID,sizeof(ErrorLog),NULL,ErrorLog);
- fprintf(stderr,ErrorLog);
- //exit(EXIT_FAILURE);
- //exit(EXIT_FAILURE);
- }
- LightColor=glGetUniformLocation(programID, "color");
- LightLocation=glGetUniformLocation(programID, "lightPos");
- ModelMatrixUniformLocation = glGetUniformLocation(programID, "ModelMatrix");
- ViewMatrixUniformLocation = glGetUniformLocation(programID, "ViewMatrix");
- ProjectionMatrixUniformLocation = glGetUniformLocation(programID, "ProjectionMatrix");
- }
- void CreateCube()
- {
- glGenVertexArrays(1, &VAO); // generate the VAO
- glBindVertexArray(VAO); //bind the VAO
- glEnableVertexAttribArray(0); //enable vertex attributes
- glEnableVertexAttribArray(1);
- glGenBuffers(1, &VBO); // generate the VBO
- glBindBuffer(GL_ARRAY_BUFFER, VBO); //generate the buffer objects
- glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW); //bind the VBO to the VAO
- //set VAO attributes
- glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)0);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)sizeof(VERTICES[0].Position));
- glBindVertexArray(0);
- }
- void DestroyCube()
- {
- glDetachShader(programID, vertexID);
- glDetachShader(programID, fragmentID);
- glDeleteShader(vertexID);
- glDeleteShader(fragmentID);
- glDeleteProgram(programID);
- glDeleteBuffers(2, &VBO);
- glDeleteVertexArrays(1, &VAO);
- }
- void Initialize(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitContextVersion(3, 3); //Opengl version 3.3
- glutInitContextProfile(GLUT_CORE_PROFILE);
- glutInitContextFlags(GLUT_DEBUG);
- glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS); //glutLeaveMainLoop
- glutInitWindowSize(CurrentWidth, CurrentHeight);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL|GLUT_MULTISAMPLE);
- WindowHandle = glutCreateWindow("Template ZPG");
- if(WindowHandle < 1) {
- printf("ERROR: Could not create a new rendering window.\n");
- exit(EXIT_FAILURE);
- }
- glewExperimental = GL_TRUE;
- GLenum GlewInitResult = glewInit();
- if (GLEW_OK != GlewInitResult) {
- printf("Error: %s\n",glewGetErrorString(GlewInitResult));
- exit(EXIT_FAILURE);
- }
- printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
- printf("Using GLEW %s\n",glewGetString(GLEW_VERSION));
- printf("Vendor %s\n",glGetString(GL_VENDOR));
- printf("Renderer %s\n",glGetString(GL_RENDERER));
- printf("GLSL %s\n",glGetString(GL_SHADING_LANGUAGE_VERSION));
- //Callback Functions
- glutReshapeFunc(ResizeFunction);
- glutDisplayFunc(RenderFunction);
- glutIdleFunc(IdleFunction);
- glutCloseFunc(DestroyCube);
- glutKeyboardFunc(KeyboardFunction);
- glutMouseFunc(processMouse);
- glutMotionFunc(processMouseActiveMotion);
- glutPassiveMotionFunc(processMousePassiveMotion);
- glutMouseWheelFunc(myMouseWheel);
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glFrontFace(GL_CCW);
- viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, myView)); // Create our view matrix which will translate us back 5 units
- CreateShaders();
- CreateCube();
- }
- int main(int argc, char* argv[])
- {
- Initialize(argc, argv);
- glutMainLoop();
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement