Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <cmath>
- #include <iostream>
- #include <windows.h>
- #include <string>
- #include <ctime>
- #include "GL/freeglut.h"
- #define MAXFOV 120
- #define MINFOV 15
- #define RED glColor3f(1,0,0);
- #define GREEN glColor3f(0,1,0);
- #define BLUE glColor3f(0,0,1);
- #define STB_IMAGE_IMPLEMENTATION
- #include "stb/stb_image.h"
- #define STB_IMAGE_WRITE_IMPLEMENTATION
- #include "stb/stb_image_write.h"
- using namespace std;
- int fps = 1000/50;
- int width,height;
- char view;
- struct v3f {
- float x;
- float y;
- float z;
- };
- struct v2i{
- int x;
- int y;
- };
- v3f camPos = {0,0,-6};
- v3f camLook = {-160,0,0};
- v2i mousePos;
- float lightPos[4] = {1,-3,0, 1.0};
- float lightPos2[4] = {0, 0, 0, 1.0};
- int max_carts=3;
- float angle = 0.0f;
- void initGL()
- {
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glEnable(GL_COLOR_MATERIAL);
- }
- void idle()
- {
- glutPostRedisplay(); // Post a re-paint request to activate display()
- }
- void Timer(int value) {
- glutPostRedisplay();
- glutTimerFunc(fps, Timer, 0);
- }
- void drawRails(){
- glPushMatrix();
- glRotatef(90,1,0,0);
- glColor3f(1,1,1);
- glutSolidTorus(0.005,2,30,30);
- glutSolidTorus(0.005,2.2,30,30);
- glRotatef(90,1,0,0);
- glPushMatrix();
- glScalef(0.1,0.05,0.1);
- glColor3f(0.6,0.3,0.1);
- for(int i=0;i<360;i+=12){
- glPushMatrix();
- glRotatef(i,0,1,0);
- glTranslatef(21,0,0);
- glScalef(2.75,1,0.5);
- glutSolidCube(1);
- glPopMatrix();
- }
- glPopMatrix();
- glPopMatrix();
- }
- void drawCarts(){
- glTranslatef(0,-0.11,0);
- glPushMatrix();
- RED;
- glTranslatef(-sin(angle)*2.1,0,-cos(angle)*2.1);
- glRotatef(-angle*55+20,0,1,0);
- glScalef(0.1,0.1,0.1);
- glutSolidCube(2);
- glPopMatrix();
- glPushMatrix();
- BLUE;
- glTranslatef(-sin(angle+0.125)*2.1,0,-cos(angle+0.125)*2.1);
- glRotatef(-angle*55+20,0,1,0);
- glScalef(0.1,0.1,0.1);
- glutSolidCube(2);
- glPopMatrix();
- glPushMatrix();
- GREEN;
- glTranslatef(-sin(angle+0.25)*2.1,0,-cos(angle+0.25)*2.1);
- glRotatef(-angle*55+20,0,1,0);
- glScalef(0.1,0.1,0.1);
- glutSolidCube(2);
- glPopMatrix();
- }
- GLuint tex;
- GLbyte* loadTexture(const char* FileName, GLint* w, GLint* h, GLint* ImComponents, GLenum* ImFormat) {
- GLbyte* image;
- int ch;
- *ImComponents = GL_RGB8;
- *ImFormat=GL_BGR_EXT;
- GLbyte* pbitsperpixel = NULL;
- image = (GLbyte*) stbi_load(FileName, w, h, &ch, 0);
- if (image == NULL) {
- std::cout << "error";
- exit(1);
- }
- switch (ch)
- {
- case 3:
- *ImFormat = GL_BGR_EXT;
- *ImComponents = GL_RGB8;
- break;
- case 4:
- *ImFormat = GL_BGRA_EXT;
- *ImComponents = GL_RGBA8;
- break;
- case 1:
- *ImFormat = GL_LUMINANCE;
- *ImComponents = GL_LUMINANCE8;
- break;
- };
- return image;
- }
- void InitText(const char* FileName) {
- GLbyte* pBytes;
- GLint ImWidth, ImHeight, ImComponents;
- GLenum ImFormat;
- glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);
- pBytes = loadTexture(FileName, &ImWidth, &ImHeight, &ImComponents, &ImFormat);
- glTexImage2D(GL_TEXTURE_2D, 0, ImComponents, ImWidth, ImHeight, 0, ImFormat, GL_UNSIGNED_BYTE, pBytes);
- free(pBytes);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- }
- void light(){
- glPushMatrix();
- glTranslatef(lightPos[0],lightPos[1],lightPos[2]);
- glLightfv(GL_LIGHT1, GL_POSITION, lightPos);
- glPushMatrix();
- glColor3f(1.000, 0.843, 0.000);
- glScalef(0.1,0.1,0.1);
- glutSolidOctahedron();
- glPopMatrix();
- glPushMatrix();
- glRotatef(45,0,1,0);
- glScalef(0.1,0.1,0.1);
- glutSolidOctahedron();
- glPopMatrix();
- glColor3f(1,1,1);
- GLfloat ambient0[]={0.2, 0.2, 0.2, 1.0};
- GLfloat diffuse0[]={0.9, 0.9, 0.9, 1.0};
- GLfloat specular0[]={1.0,1.0, 1.0, 1.0};
- GLfloat emission[]={1.0,1.0, 1.0, 1.0};
- glLightfv(GL_LIGHT1, GL_AMBIENT, ambient0);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse0);
- glLightfv(GL_LIGHT1, GL_SPECULAR, specular0);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT1);
- glPopMatrix();
- }
- void movelight(){
- lightPos[2]=(-sin(angle));
- lightPos[0]=(-cos(angle));
- }
- void plane(){
- glPushMatrix();
- glColor3f(0.502, 0.000, 0.000);
- glBegin(GL_QUADS);
- glVertex3f(-10,0,-10);
- glVertex3f(-10,0,10);
- glVertex3f(10,0,10);
- glVertex3f(10,0,-10);
- glEnd();
- glPopMatrix();
- }
- void display()
- {
- glClearColor(0,0,0,0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- switch(view){
- case '1':
- glTranslatef(camPos.x,camPos.y,camPos.z);
- glRotatef(camLook.x,1.0f,0.0f,0.0f);
- glRotatef(camLook.y,0.0f,1.0f,0.0f);
- glRotatef(camLook.z,0.0f,0.0f,1.0f);
- break;
- case '2': //top
- glTranslatef(-1,-1,-1);
- glRotatef(180,1,0,0);
- glRotatef(90,1,0,0);
- glTranslatef(1,5,1);
- break;
- case '3': //left
- glTranslatef(-1,-1,-1);
- glRotatef(180,1,0,0);
- glRotatef(-90,0,1,0);
- glTranslatef(4,0,-1);
- glRotatef(90,0,1,0);
- break;
- case '4': //left(45)
- glTranslatef(-1,-1,-1);
- glRotatef(180,1,0,0);
- glRotatef(-90,0,1,0);
- glTranslatef(4,0,-1);
- glRotatef(90,0,1,0);
- glRotatef(45,1,0,0);
- glTranslatef(0,0,1);
- break;
- }
- glEnable(GL_TEXTURE_GEN_S);
- InitText("a.jpg");
- plane();
- drawRails();
- drawCarts();
- light();
- movelight();
- angle += 0.02f;
- //glutSwapBuffers();
- glDisable(GL_TEXTURE_GEN_S);
- }
- void reshape(GLsizei width, GLsizei height) {
- if (height == 0) height = 1;
- GLfloat aspect = (GLfloat)width / (GLfloat)height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45, aspect, 0.1f, 100.0f);
- }
- static void key(unsigned char key, int x, int y)
- {
- switch(key){
- case 'a': camPos.x += 0.5f; break;
- case 'd': camPos.x -= 0.5f; break;
- case 'w': camPos.z += 0.5f; break;
- case 's': camPos.z -= 0.5f; break;
- case 'j': camLook.y += 0.75f; break;
- case 'l': camLook.y -= 0.75f; break;
- case 'i': camLook.x += 0.75f; break;
- case 'k': camLook.x -= 0.75f; break;
- case 'Q':
- case 'q':
- case 27: // ESC
- exit(0); break;
- }
- reshape(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
- glutPostRedisplay();
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- //glEnable(GL_TEXTURE_GEN_S);
- glutInitDisplayMode(GLUT_DOUBLE);
- glutInitWindowSize(800, 600);
- glutInitWindowPosition(50, 50);
- width = glutGet(GLUT_WINDOW_WIDTH);
- height = glutGet(GLUT_WINDOW_HEIGHT);
- cout<<"select mode: "<<endl<<"1- free cam (mouse)"<<endl<<"2- top view";
- cout<<endl<<"3- from left"<<endl<<"from left (45deg)"<<endl<<"q-exit"<<endl;
- cin>>view;
- if(view=='q' || view == 'Q') return 0;
- glutCreateWindow("GLUT - POCIAG - FERENC BARTLOMIEJ");
- cout<<"wsad - move (by 0.25)"<<endl;
- cout<<"ijkl - change camera angle"<<endl;
- cout<<"esc to exit"<<endl;
- glEnable(GL_DEPTH_TEST);
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(key);
- //mouse
- glutTimerFunc(0, Timer, 0);
- initGL();
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement