Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * GLUT Shapes Demo
- *
- * Written by Nigel Stewart November 2003
- *
- * This program is test harness for the sphere, cone
- * and torus shapes in GLUT.
- *
- * Spinning wireframe and smooth shaded shapes are
- * displayed until the ESC or q key is pressed. The
- * number of geometry stacks and slices can be adjusted
- * using the + and - keys.
- */
- #include <windows.h>
- #include <iostream>
- #include <fstream>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <stdlib.h>
- #include "biblioteka.h"
- using namespace std;
- static int slices = 16;
- static int stacks = 16;
- /* GLUT callback Handlers */
- #define LICZBA_OB_TEXTUR 2
- GLuint obiektyTextur [LICZBA_OB_TEXTUR];
- char *plikiTextur[LICZBA_OB_TEXTUR] = {"C:/Users/Ja/Desktop/grafika/tekstury 2/wood.tga", "C:/Users/Ja//Desktop/grafika/tekstury 2/sand.tga"};
- int lWez, lEle;
- double **Wez;
- int **Ele;
- ///double **kolor;
- void Podloze(){
- glDisable(GL_LIGHTING);
- glBegin(GL_QUADS);
- for(int i=-30;i<25;i++)
- for(int j=-30;j<25;j++)
- {
- if((i+j)%2==0.0)
- glColor4f (1.0f, 0.0f, 0.0f, 0.0f);
- else
- glColor4f (0.0f, 1.0f, 0.0f, 0.0f);
- glVertex3d(0+i,-2,0+j);
- glVertex3d(0+i,-2,1+j);
- glVertex3d(1+i,-2,1+j);
- glVertex3d(1+i,-2,0+j);
- }
- glEnd();
- glEnable(GL_LIGHTING);
- }
- static void resize(int width, int height)
- {
- const float ar = (float) width / (float) height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-ar, ar, -1.0, 1.0, 2.0, 400.0);
- //glOrtho(-8,8,-8,8,-8,8);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- }
- float difamb[]={1.0, 0.5, 0.3,1.0};
- static void display(void)
- {
- const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
- const double a = t*90.0;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3d(1,1,1);
- glPushMatrix();
- glTranslated(0,0,-5);
- glRotatef(a,0,1,1);
- glBindTexture (GL_TEXTURE_2D, obiektyTextur[0]);
- glBegin(GL_TRIANGLES);
- for(int nrEl=0;nrEl<lEle;nrEl++)
- {
- glNormal3f(0.0f, 0.0f, 1.0f);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(Wez[Ele[nrEl][0]][0],Wez[Ele[nrEl][0]][1], Wez[Ele[nrEl][0]][2]);
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(Wez[Ele[nrEl][1]][0],Wez[Ele[nrEl][1]][1], Wez[Ele[nrEl][1]][2]);
- glTexCoord2f(1.0f, 1.0f);
- glVertex3f(Wez[Ele[nrEl][2]][0],Wez[Ele[nrEl][2]][1], Wez[Ele[nrEl][2]][2]);
- }
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslated(0,-12.5,-10);
- glRotated(23,1,0,0);
- glBindTexture (GL_TEXTURE_2D, obiektyTextur[1]);
- glBegin(GL_QUADS);
- glColor3d(1,1,1);
- glTexCoord2f(0, 1);
- glVertex3d(60,0,-60);
- glTexCoord2f(1, 1);
- glVertex3d(60,0,60);
- glTexCoord2f(1, 0);
- glVertex3d(-60,0,60);
- glTexCoord2f(0, 0);
- glVertex3d(-60,0,-60);
- glEnd();
- glPopMatrix();
- glutSwapBuffers();
- }
- static void key(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27 :
- case 'q':
- exit(0);
- break;
- case '+':
- slices++;
- stacks++;
- break;
- case '-':
- if (slices>3 && stacks>3)
- {
- slices--;
- stacks--;
- }
- break;
- }
- glutPostRedisplay();
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- void wczytajPlik(char *nazwaPliku){
- ifstream plik(nazwaPliku);
- int pom1, pom2;
- plik>>lWez;
- Wez = new double* [lWez];
- for(int i=0; i<lWez; i++)
- Wez[i]=new double[3];
- for(int i=0;i<lWez;i++)
- plik>>pom1>>Wez[i][0]>>Wez[i][1]>>Wez[i][2];
- plik>>lEle;
- Ele = new int* [lEle];
- for(int i=0; i<lEle; i++)
- Ele[i]=new int[3];
- for(int i=0; i<lEle;i++){
- plik>>pom1>>pom1>>pom1>>pom1>>pom1>>Ele[i][0]>>Ele[i][1]>>Ele[i][2];
- Ele[i][0]--;
- Ele[i][1]--;
- Ele[i][2]--;
- }
- ///kolor = new double* [lEle];
- /*
- for(int i=0; i<lEle; i++){
- kolor[i]=new double[3];
- kolor[i][0]=rand()%255/255.;
- kolor[i][1]=rand()%255/255.;
- kolor[i][2]=rand()%255/255.;
- }*/
- cout<<"Liczba wezlow liczba elementow: "<<lWez<<"\t"<<lEle;
- plik.close();
- }
- const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
- const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
- const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
- const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat high_shininess[] = { 100.0f };
- /* Program entry point */
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(840,600);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- wczytajPlik("C:/Users/Ja/Desktop/rączka.msh");
- glutCreateWindow("GLUT Shapes");
- glEnable(GL_TEXTURE_2D);
- glGenTextures(LICZBA_OB_TEXTUR, obiektyTextur);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- for (int i = 0; i < LICZBA_OB_TEXTUR; i++)
- {
- GLubyte *pBytes;
- GLint iWidth, iHeight, iComponents;
- GLenum eFormat;
- glBindTexture(GL_TEXTURE_2D, obiektyTextur[i]); // dowiazanie obiektow tekstur
- pBytes = glploadtga(plikiTextur[i], &iWidth, &iHeight, &iComponents, &eFormat); // zaladowanie tekstur
- gluBuild2DMipmaps(GL_TEXTURE_2D, iComponents, iWidth, iHeight, eFormat, GL_UNSIGNED_BYTE, pBytes); // utworzenie mipmap
- free(pBytes); // zwolnienie pamieci
- }
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- glutKeyboardFunc(key);
- glutIdleFunc(idle);
- glClearColor(0.9,0.9,1,1); //kolor tla
- // glEnable(GL_CULL_FACE);
- // glCullFace(GL_BACK);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHTING);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glutMainLoop();
- glDeleteTextures (LICZBA_OB_TEXTUR, obiektyTextur); // usuniecie obiektow tekstur
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement