OpenGL hazır 3d nesneler ve Işıklandırma

OpenGL hazır 3d nesneler ve Işıklandırma

Sağ tıklayarak istenilen şekle tıklayarak, tel şeklinde veya katı şekilde görüntülemek mümkün.

Ayrıca seri porttan tek pakette  “x=45,y=0,z=0\r” şeklinde gelen verilerle veya klavyedeki ‘w’ , ‘a’ , ‘s’ , ‘d’ , ‘x’ , ‘z’ tuşlarıyla açıları değiştirme imkanı var.

Ekran görüntüleri:

opengl_1

opengl_2

opengl_3

opengl_4

 

 

Program kodları:

#include <string.h>
#include <stdlib.h>
#include "stdio.h"
#include "windows.h"
#include "GL/glut.h"

GLfloat rtri;
GLfloat rquad;

//yeni denemeler
GLfloat x_derece=0;
GLfloat y_derece=0;
GLfloat z_derece=0;

GLfloat x_durum,y_durum,z_durum;
static int hangiSekil = 9; //menudeki degisiklikler bu degiskene yazilir(9=demlik)


//seriport için:
DCB dcb;
HANDLE hCom;
COMMTIMEOUTS CommTimeOuts;
DWORD nWrite;
DWORD nRead;





char SeriPortInit(void){
    // Port açma
    hCom = CreateFile((LPCTSTR)"\\\\.\\COM1", GENERIC_READ|GENERIC_WRITE, 
                       0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    // Açıldımı ?
    if (hCom == INVALID_HANDLE_VALUE)
        return 0;
    
    // Giriş çıkış buffer ayarla
    SetupComm(hCom, 4096, 4096);

    
    // mevcut ayarları oku
    GetCommState(hCom, &dcb);
    
    dcb.BaudRate = 19200;               /* Baudrate at which running       */
    dcb.fBinary = TRUE;                 /* Binary Mode (skip EOF check)    */
    dcb.fParity = FALSE;				/* Enable parity checking          */
    dcb.fOutxCtsFlow = FALSE;           /* CTS handshaking on output       */
    dcb.fOutxDsrFlow = FALSE;           /* DSR handshaking on output       */
    dcb.fDtrControl = DTR_CONTROL_ENABLE; /* DTR Flow control                */
    dcb.fDsrSensitivity = FALSE;        /* DSR Sensitivity              */
    dcb.fTXContinueOnXoff = FALSE;      /* Continue TX when Xoff sent */
    dcb.fOutX = FALSE;                  /* Enable output X-ON/X-OFF        */
    dcb.fInX = FALSE;                   /* Enable input X-ON/X-OFF         */
    dcb.fErrorChar = FALSE;             /* Enable Err Replacement          */
    dcb.fNull = FALSE;                  /* Enable Null stripping           */
    dcb.fRtsControl = RTS_CONTROL_ENABLE; /* Rts Flow control                */
    dcb.fAbortOnError = FALSE;          /* Abort all reads and writes on Error */
    dcb.ByteSize = 8;                   /* Number of bits/byte, 4-8        */
    dcb.Parity = 0;                     /* 0-4=None,Odd,Even,Mark,Space    */
    dcb.StopBits = ONESTOPBIT;          /* 0,1,2 = 1, 1.5, 2               */

    // ayar yap
    SetCommState(hCom, &dcb);

    // Timeout ayarla (2s)
    CommTimeOuts.ReadIntervalTimeout = 0;
    CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
    CommTimeOuts.ReadTotalTimeoutConstant = 100;
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
    CommTimeOuts.WriteTotalTimeoutConstant = 2000;
    
    // ayarları yaz
    SetCommTimeouts(hCom, &CommTimeOuts);
 
    // DTR RTS aktif yap
    EscapeCommFunction(hCom, SETDTR);
    EscapeCommFunction(hCom, SETRTS);


    // buffer leri önceden boşalt
    PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
}




void ayarlar(void)
{
	glClearColor(0.0,0.0,0.0,0.0);
	glMatrixMode(GL_PROJECTION);
	glOrtho(-7.0, 7.0, -2.0, 2.0, -5.0, 5.0);
	
	rtri = 0.0f;
}

katardanFloata(char * katar){
	int i=0;
	//strlen(katar)
	int numaraTut=0,numaraTut2=0,numaraTut3=0;
	int virgulTut=0,virgulTut2=0,virgulTut3=0;
	
	for(i=0;i<strlen(katar);i++){
		if(katar[i]=='='){
			numaraTut=i+1;
			break;
		}
	}
	
	for(i=numaraTut;i<strlen(katar);i++){
		if(katar[i]=='='){
			numaraTut2=i+1;
			break;
		}
	}	
	
	for(i=numaraTut2;i<strlen(katar);i++){
		if(katar[i]=='='){
			numaraTut3=i+1;
			break;
		}
	}		
	//x=45,y=0,z=0\r 2,4,7,8,11,12
	//virgüller
	
	for(i=numaraTut;i<strlen(katar);i++){
		if(katar[i]==','){
			virgulTut=i;
			break;
		}
	}
	for(i=numaraTut2;i<strlen(katar);i++){
		if(katar[i]==','){
			virgulTut2=i;
			break;
		}
	}
	for(i=numaraTut3;i<strlen(katar);i++){
		if(katar[i]=='\r'){
			virgulTut3=i;
			break;
		}
	}	
	
	
	//çevirme işi
	if(virgulTut-numaraTut==1){
		x_derece=(katar[numaraTut]-'0');
	}else if(virgulTut-numaraTut==2){
		x_derece=10*(katar[numaraTut]-'0');
		x_derece+=(katar[numaraTut+1]-'0');
	}else if(virgulTut-numaraTut==3){
		x_derece=100*(katar[numaraTut]-'0');
		x_derece+=10*(katar[numaraTut+1]-'0');
		x_derece+=(katar[numaraTut+2]-'0');
	}else{
	}
	
	if(virgulTut2-numaraTut2==1){
		y_derece=(katar[numaraTut2]-'0');
	}else if(virgulTut2-numaraTut2==2){
		y_derece=10*(katar[numaraTut2]-'0');
		y_derece+=(katar[numaraTut2+1]-'0');
	}else if(virgulTut2-numaraTut2==3){
		y_derece=100*(katar[numaraTut2]-'0');
		y_derece+=10*(katar[numaraTut2+1]-'0');
		y_derece+=(katar[numaraTut2+2]-'0');
	}else{
	}
	
	if(virgulTut3-numaraTut3==1){
		z_derece=(katar[numaraTut3]-'0');
	}else if(virgulTut3-numaraTut3==2){
		z_derece=10*(katar[numaraTut3]-'0');
		z_derece+=(katar[numaraTut3+1]-'0');
	}else if(virgulTut3-numaraTut3==3){
		z_derece=100*(katar[numaraTut3]-'0');
		z_derece+=10*(katar[numaraTut3+1]-'0');
		z_derece+=(katar[numaraTut3+2]-'0');
	}else{
	}		
	 
	
	//printf("%f , %f , %f \r\n",x_derece,y_derece,z_derece);
	
	

}

void idle()
{
		

	float bilgi;
	//char * gelenVeri="x=45,y=0,z=0\r"; //x:dikey, y: kumpas, z: yatay
	
	char * gelenVeri[20];

	//x_durum=1.0; y_durum=1.0; z_durum=0.0; //bu kısım birşey değiştirmiyor, aşağıda "gosterim" fonksiyonunda ayarlanıyor çünkü
	
	//rtri +=0.01f;
	//rquad+=0.15f; //bunun ne işe yaradığını daha çözemedim
 

 
	memset(&gelenVeri[0], 0, sizeof(gelenVeri));
    ReadFile(hCom, gelenVeri, sizeof(gelenVeri), &nRead, NULL);
    if(gelenVeri[0]!=0)katardanFloata(gelenVeri); //gelen veri boş değilse işle
    

	glutPostRedisplay();	
	
	

}


void keyboardCB(unsigned char key, int x, int y)
{
	rtri +=0.5f;
	
    switch(key)
    {
	    case 27: // ESCAPE
	        //clearSharedMem();
	        exit(0);
	        break;

	    case 'w': 
	    	x_derece-=1.0;
	    	x_durum=1.0; y_durum=0.0; z_durum=0.0;
	        break;
	        
	    case 'a': 
	        z_derece+=1.0; 
	        x_durum=0.0; y_durum=0.0; z_durum=0.1;
	        break;
			
	    case 's': 
	    	x_derece+=1.0;
	    	x_durum=0.1; y_durum=0.0; z_durum=0.0;
	        break;
			
	    case 'd': 
	        z_derece-=1.0;
	        x_durum=0.0; y_durum=0.0; z_durum=0.1;
	        break;		
			
		//kumpas:
		case 'z': 
	        y_derece-=1.0;
	        x_durum=0.0; y_durum=0.1; z_durum=0.0;
	        break;	
			
		case 'x': 
	        y_derece+=1.0;
	        x_durum=0.0; y_durum=0.1; z_durum=0.0;
	        break;									        

		//sıfırla
		case 'o': 
			x_durum=0.1;
			y_durum=0.1;
			z_durum=0.1;
			
	        x_derece=45.0;
	        y_derece=0.0;
	        z_derece=0.0;
	        break;		
		
	        
    default:
    	//x_aci=-0.00f; y_aci=-0.00f;  z_aci=-0.00f;   
        ;
    }
    
    //işi sağlama alalım diye yazdık şart değil:
    if(x_derece==360.0 || x_derece==-360.0)x_derece=0.0;
    if(y_derece==360.0 || y_derece==-360.0)x_derece=0.0;
    if(y_derece==360.0 || z_derece==-360.0)x_derece=0.0;
    
    glutPostRedisplay();

}

void gosterim(void)
{
	glEnable(GL_DEPTH_TEST);
	glColor3f(1.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Pencereyi temizle
	
	
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	

	
	//nesneyi taşımak: Nesneyi taşımak: glTranslate{fd}( x, y, z )
	//bilgi: http://ozlemerden.wordpress.com/category/opengl/
	//Nesneyi bir eksen etrafında döndürmek: glRotate{fd}( açı, x, y, z )

	glPopMatrix(); // Transformasyonlari geri yukle
	//glTranslatef(4.0f,0.0f,0.0f); 
	glPushMatrix(); // Matris durumunu kaydet ve donusu yap
	
	
	
	//glRotatef(rtri,x_aci,y_aci,z_aci); //açı dediği aslında ne kadar çok döneceğini ayarlıyor
	
	//if(x_durum==1)glRotatef(rtri,0.1f,0.0f,0.0f);
	//if(y_durum==1)glRotatef(rtri,0.0f,0.1f,0.0f);
	//if(z_durum==1)glRotatef(rtri,0.0f,0.0f,0.1f);
	
	
	glRotatef(x_derece,0.1f,0.0f,0.0f);
	glRotatef(y_derece,0.0f,0.1f,0.0f);
	glRotatef(z_derece,0.0f,0.0f,0.1f);
	

	if(hangiSekil==21)//derece sifirlama
    {
        x_derece=0;
        y_derece=0;
        z_derece=0;
        hangiSekil=9;
    }


//isiklandirma ve golgelendirme (harika sonuc üretiyor)////////////////////////
//degisik ornekler : http://web.media.mit.edu/~gordonw/OpenGL/
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glColor4f(1.0,1.0,1.0,1.0); //rengi beyaz yapar
GLfloat	lightpos[4] = { 5.0, 15.0, 10.0, 1.0 }; 
///////////////////////////////////////////////////////////////////////////////

	switch(hangiSekil)
        {
        case 1:
            glutWireSphere(1.0f, 25, 25);
            break;

        case 2:
            glutWireCube(1.0f);
            break;

        case 3:
            glutWireCone(0.30f, 1.1f, 20, 20);
            break;

        case 4:
            glutWireTorus(0.3f, 1.0f, 10, 25);
            break;

        case 5:
            glutWireDodecahedron();
            break;

        case 6:
            glutWireOctahedron();
            break;

        case 7:
            glutWireTetrahedron();
            break;

        case 8:
            glutWireIcosahedron();
            break;

        case 9:
            glutWireTeapot(2.0f);
            break;

        case 11:
            glutSolidSphere(1.0f, 25, 25);
            break;

        case 12:
            glutSolidCube(1.0f);
            break;

        case 13:
            glutSolidCone(0.30, 1.1f, 20, 20);
            break;
    
        case 14:
            glutSolidTorus(0.3f, 1.0f, 10, 25);
            break;

        case 15:
            glutSolidDodecahedron();
            break;

        case 16:
            glutSolidOctahedron();
            break;

        case 17:
            glutSolidTetrahedron();
            break;

        case 18:
            glutSolidIcosahedron();
            break;
            
        case 19:
            glutSolidTeapot(2.0f);
            break;            
        
        case 20: //kendi cizdigim kup sekli
        	glBegin(GL_QUADS);
			glColor3f(0.0f,1.0f,0.0f);
			glVertex3f( 1.0f, 1.0f,-1.0f);
			glVertex3f(-1.0f, 1.0f,-1.0f);
			glVertex3f(-1.0f, 1.0f, 1.0f);
			glVertex3f( 1.0f, 1.0f, 1.0f);
			glColor3f(1.0f,0.5f,0.0f);
			glVertex3f( 1.0f,-1.0f, 1.0f);
			glVertex3f(-1.0f,-1.0f, 1.0f);
			glVertex3f(-1.0f,-1.0f,-1.0f);
			glVertex3f( 1.0f,-1.0f,-1.0f);
			glColor3f(1.0f,0.0f,0.0f);
			glVertex3f( 1.0f, 1.0f, 1.0f);
			glVertex3f(-1.0f, 1.0f, 1.0f);
			glVertex3f(-1.0f,-1.0f, 1.0f);
			glVertex3f( 1.0f,-1.0f, 1.0f);
			glColor3f(1.0f,1.0f,0.0f);
			glVertex3f( 1.0f,-1.0f,-1.0f);
			glVertex3f(-1.0f,-1.0f,-1.0f);
			glVertex3f(-1.0f, 1.0f,-1.0f);
			glVertex3f( 1.0f, 1.0f,-1.0f);
			glColor3f(0.0f,0.0f,1.0f);
			glVertex3f(-1.0f, 1.0f, 1.0f);
			glVertex3f(-1.0f, 1.0f,-1.0f);
			glVertex3f(-1.0f,-1.0f,-1.0f);
			glVertex3f(-1.0f,-1.0f, 1.0f);
			glColor3f(1.0f,0.0f,1.0f);
			glVertex3f( 1.0f, 1.0f,-1.0f);
			glVertex3f( 1.0f, 1.0f, 1.0f);
			glVertex3f( 1.0f,-1.0f, 1.0f);
			glVertex3f( 1.0f,-1.0f,-1.0f);
			glEnd();
        	break;
        default:
            //glutSolidTeapot(1.0f);
            break;
        }
        

	glPopMatrix(); // Transformasyonlari geri yukle
	glutSwapBuffers(); // Cizim komutlarini akit   	

}







void ProcessMenu(int value) //menu icin
  {
    hangiSekil = value;

  glutPostRedisplay();
  }
  


int main(int argc,char ** argv)
{
	int i;
	
	int nWireMenu; //menu icin
	int nMainMenu; //menu icin
	int nSolidMenu; //menu icin   
	int nDigerMenu; //menu icin   
	
	for(i=0;i<argc;i++)	printf("%d , %s \r\n",i,argv[i]);

	SeriPortInit();
    
    // seri porta bilgi gönder
    WriteFile(hCom, "Merhaba\r\n", (DWORD) strlen("Merhaba\r\n"), &nWrite, NULL);
    
    
    /*
    // port kapat
    CloseHandle(hCom);
    */
    
    
    
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(0,0);
	glutInitWindowSize(900,400);
	glutCreateWindow("OpenGL Uygulaması www.GökhanBeken.com");
	ayarlar();
	
	//Sağ click Menu olustur/////////////////////////////////////////////////////////////////////////////
	  nWireMenu = glutCreateMenu(ProcessMenu);
	  glutAddMenuEntry("Kure",1);
	  glutAddMenuEntry("Kup",2);
	  glutAddMenuEntry("Koni",3);
	  glutAddMenuEntry("Torus",4);
	  glutAddMenuEntry("Dodecahedron",5);
	  glutAddMenuEntry("Sekizgen",6);
	  glutAddMenuEntry("Tetrahedron",7);
	  glutAddMenuEntry("Icosahedron",8);
	  glutAddMenuEntry("Caydanlik",9);
	  
	  nSolidMenu = glutCreateMenu(ProcessMenu);
	  glutAddMenuEntry("Kure",11);
	  glutAddMenuEntry("Kup",12);
	  glutAddMenuEntry("Koni",13);
	  glutAddMenuEntry("Torus",14);
	  glutAddMenuEntry("Dodecahedron",15);
	  glutAddMenuEntry("Sekizgen",16);
	  glutAddMenuEntry("Tetrahedron",17);
	  glutAddMenuEntry("Icosahedron",18);
	  glutAddMenuEntry("Caydanlik",19);  
	  glutAddMenuEntry("Ozel Kup",20);  
	  
	  nDigerMenu = glutCreateMenu(ProcessMenu);
	  glutAddMenuEntry("Sifirla",21);  
	  
	  nMainMenu = glutCreateMenu(ProcessMenu);
	  glutAddSubMenu("Tel", nWireMenu);
	  glutAddSubMenu("Kati", nSolidMenu);
	  glutAddSubMenu("Diger", nDigerMenu);
	  glutAttachMenu(GLUT_RIGHT_BUTTON);
	  //////////////////////////////////////////////////////////////////////////////////////////  
	  
	  
	glutDisplayFunc(gosterim); //sekillerin cizildigi fonksiyon
	glutKeyboardFunc(keyboardCB); //klavye fonksiyonum
	glutIdleFunc(idle); //arka planda değer değiştirmek için genel fonksiyon
	glutMainLoop();
	
	    

    return 0;
}

 

Gökhan BEKEN

One thought on “OpenGL hazır 3d nesneler ve Işıklandırma

  1. Yazınızı ve kodlarınızı inceledim gayet profesyonel duruyor Gökhan Bey,

    OpenGLDerslerini ve Eğitimlerini hazırlayıp sitenizden yayınlasanız yeni başlayanlar için çok yararlı olur kanımca.
    Örnek kodları ve Programları ayrıca paylaşırsanız mükemmel olur.

Bir Cevap Yazın