Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h> //Wymagane dla implementacji OpenGL w Visual Studio.
- #include "gl\gl.h"
- #include "gl\glut.h"
- #include "stdio.h" //Przydatne do wypisywania komunikatów na konsoli
- #include "glm\glm.hpp"
- #include "glm\gtc\matrix_transform.hpp"
- #include "glm\gtc\type_ptr.hpp"
- using namespace glm;
- float speed=400; //60 stopni/s
- int lastTime=0;
- float angle;
- int direction;
- void displayFrame(void) {
- glClearColor(0,0,0,1);
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- mat4 P=perspective(100.0f,1.0f,1.0f,50.0f);
- mat4 V=lookAt(vec3(0.0f,0.0f,-5.0f),vec3(0.0f,0.0f,0.0f),vec3(0.0f,1.0f,0.0f));
- mat4 M1 = mat4(1.0);
- mat4 M2 = mat4(1.0);
- glMatrixMode(GL_PROJECTION);
- glLoadMatrixf(value_ptr(P));
- glMatrixMode(GL_MODELVIEW);
- M1=translate(M1,vec3(-2,0,0))*rotate(M1,angle,vec3(1.0f,0.0f,1.0f));
- glLoadMatrixf(value_ptr(V*M1));
- glColor3d(1,0.3,0);
- //glutSolidTorus(0.5,1.5,20,50);
- float tri1[]={
- 1,2,0,
- -1,0 ,1,
- 1,1,0
- };
- float tri1Colors[] = {
- 1, 0, 0,
- 1, 0, 0,
- 1,0, 0
- };
- float tri2[]={
- 1,2,0,
- 2,0 ,1,
- 1,1,0
- };
- float tri2Colors[] = {
- 1, 0, 0,
- 1, 0, 0,
- 1,0, 0
- };
- int tri1VertexCount = 3;
- int tri2VertexCount = 3;
- glEnableClientState(GL_VERTEX_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, tri1);
- glColorPointer(3, GL_FLOAT, 0, tri1Colors);
- glDrawArrays(GL_TRIANGLES, 0, tri1VertexCount);
- glDisableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_VERTEX_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, tri2);
- glColorPointer(3, GL_FLOAT, 0, tri2Colors);
- glDrawArrays(GL_TRIANGLES, 0, tri2VertexCount);
- glDisableClientState(GL_VERTEX_ARRAY);
- M2=translate(M2,vec3(2,0,0))*rotate(M2,-angle,vec3(0.3f,0.5f,0.0f));
- glLoadMatrixf(value_ptr(V*M2));
- glColor3d(1,0,0);
- glutSolidTorus(0.5,1.5,20,50);
- glutSwapBuffers();
- }
- void nextFrame(void) {
- int actTime=glutGet(GLUT_ELAPSED_TIME);
- int interval=actTime-lastTime;
- lastTime=actTime;
- if (direction == 1) angle-=speed*interval/1000.0;
- if (direction == -1) angle+=speed*interval/1000.0;
- if (angle>360) angle-=360;
- glutPostRedisplay();
- }
- void keyDown(int c, int x, int y) {
- if (c == GLUT_KEY_LEFT) direction = 1;
- if (c == GLUT_KEY_RIGHT) direction = -1;
- }
- void keyUp(int c, int x, int y) {
- if (c == GLUT_KEY_LEFT) direction = 0;
- if (c == GLUT_KEY_RIGHT) direction = 0;
- }
- int main(int argc, char* argv[]) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(800,800);
- glutInitWindowPosition(0,0);
- glutCreateWindow("Program OpenGL");
- glutDisplayFunc(displayFrame);
- //Tutaj kod inicjujący
- glutIdleFunc(nextFrame);
- //glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_COLOR_MATERIAL);
- glutSpecialFunc(keyDown);
- glutSpecialUpFunc(keyUp);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement