Posted by: bugraphicslab | April 19, 2011

OpenGL Hafta8

15/04/2011 tarihinde ele aldığımız konular:

1- Klavye Veri Girişi Arabellek İşlemi(Keyboard Buffering) devam

2- Kamera’nın hareketli nesneleri takip etmesi (Camera Tracking)

Çalışılması gereken konular ise:

1-Matriks ve Vektörlerin kullanımı ile Kamera sınıfının oluşturulması

2-Bağlı Listeler kullanımı ile dosyadan okunan vertex bilgilerinin OpenGL yardımı ile ekranda gösterimi

3-İmaj dosyalarının okunması


Responses

  1. // hafta4-5.cpp : Defines the entry point for the console application.
    //
    // hafta4.cpp : Defines the entry point for the console application.
    //

    #include “stdafx.h”
    #include “math.h”
    #include “glut.h”

    #define PI 3.14159265

    //Variable Red Blue and variable coordinates
    float vred=1.0,vblue=1.0,vgreen=1.0;
    float vx=0.0, vy=0.0; //X ve y nin degisimi
    float xc=0.0, yc=0.0;
    float rx=0.0,ry=0.0,rz=0.0; //dönüs açilari
    int rotation_status=0;
    int fps=60; //frame per second

    //Mouse Algilanmasi
    int specialKey;
    void processMouse(int button, int state, int x, int y) {

    //SpecialKey State
    specialKey = glutGetModifiers();

    //MOUSE
    if ((state == GLUT_DOWN)&& (specialKey != GLUT_ACTIVE_ALT)){
    if (button == GLUT_LEFT_BUTTON) {
    //LEFT CLICK
    }
    }

    //ALT + MOUSE KULLANIMI

    // if both a mouse button, and the ALT key, are pressed then
    if ((state == GLUT_DOWN) &&
    (specialKey == GLUT_ACTIVE_ALT)) {

    // set the color to pure red for the left button
    if (button == GLUT_LEFT_BUTTON) {
    //ALT+MOUSE LEFT CLICK
    }
    // set the color to pure green for the middle button
    else if (button == GLUT_MIDDLE_BUTTON) {

    }
    // set the color to pure blue for the right button
    else {

    }
    }
    }
    void processMouseActiveMotion(int x, int y) {

    // the ALT key was used in the previous function
    if (specialKey != GLUT_ACTIVE_ALT) {
    }
    }
    void processMousePassiveMotion(int x, int y) {

    // User must press the SHIFT key to change the
    // rotation in the X axis
    if (specialKey != GLUT_ACTIVE_SHIFT) {
    }
    }
    void processMouseEntry(int state) {

    }
    bool keybuffer[256];
    //bool* keyStates=new bool[256];
    void klavyeislemleri(){

    if(keybuffer[27]==true) //Escape key
    exit(0); //Exit the program
    if(keybuffer[‘1’]==true) //Escape key
    { vred=0.0;
    vgreen=0.0;
    vblue=1.0;
    ////glutPostRedisplay();

    }
    if(keybuffer[‘2’]==true)//Escape key
    {if(rotation_status==0)rotation_status=1;
    else rotation_status=0;

    }
    if(keybuffer[‘w’]) //Escape key
    {//vy+=0.10;
    yc+=sin((rz*PI/180))*0.1;
    xc+=cos((rz*PI/180))*(0.1);
    }
    if(keybuffer[‘s’]) //Escape key
    {//vy-=0.10;
    yc-=sin((rz*PI/180))*0.1;
    xc-=cos((rz*PI/180))*(0.1);
    }
    if(keybuffer[‘d’]) //Escape key
    {
    rz-=10;
    if(rz>360)rz=rz/360;
    if(ry360)rz=rz/360;
    if(ry<(-360))rz=rz/360;
    }
    if(keybuffer['e']) //Escape key
    {vx+=0.10;
    //glutPostRedisplay();
    }
    if(keybuffer['q']) //Escape key
    {vx-=0.10;
    //glutPostRedisplay();
    }
    }

    //Called when a key is pressed
    void handleKeypress(unsigned char key,int x, int y) { //The current mouse coordinates
    keybuffer[key]=true;
    }

    void keyUp(unsigned char key, int x, int y){
    keybuffer[key]=false;//set the state of the current key to not pressend
    }

    //Initializes 3D rendering
    void initRendering() {
    //Makes 3D drawing work when something is in front of something else
    glEnable(GL_DEPTH_TEST);
    }

    //Called when the window is resized
    void handleResize(int w, int h) {
    //Tell OpenGL how to convert from coordinates to pixel values
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

    //Set the camera perspective
    glLoadIdentity(); //Reset the camera
    gluPerspective(45.0, //The camera angle
    (double)w / (double)h, //The width-to-height ratio
    1.0, //The near z clipping coordinate
    200.0); //The far z clipping coordinate
    }
    //Draws the 3D scene
    void drawScene() {
    //Clear information from last draw
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
    glLoadIdentity(); //Reset the drawing perspective

    glLoadIdentity();
    /*glTranslatef(vx,vy,0.0);*/
    glTranslatef(xc,yc,0.0);
    glRotatef(-90,0.0,0.0,1.0);
    glRotatef(rx,1.0,0.0,0.0);
    glRotatef(ry,0.0,1.0,0.0);
    glRotatef(rz,0.0,0.0,1.0);

    glColor3f(vred,vgreen,vblue);
    glBegin(GL_TRIANGLE_STRIP);
    //glVertex3f(-0.7f, 1.5f, -5.0f);
    //glVertex3f(-0.3f, 0.7f, -5.0f);
    //glVertex3f(0.4f, 0.5f, -5.0f);
    glVertex3f(0.0f, 0.5f, -5.0f);
    glVertex3f(-0.25f,-0.0f, -5.0f);
    glVertex3f(0.25f, -0.0f, -5.0f);
    glVertex3f(-0.25f,-0.25f, -5.0f);
    glVertex3f(0.25f, -0.25f, -5.0f);
    glEnd(); //End triangle coordinates
    //BACKGROUND
    glLoadIdentity();
    glColor3f(0.5,0.5,0.5);
    glBegin(GL_TRIANGLE_STRIP);
    glVertex3f(-2.0f, 2.0f, -6.0f);
    glVertex3f(-2.0f,-2.0f, -6.0f);
    glVertex3f(2.0f, 2.0f, -6.0f);
    glVertex3f(2.0f,-2.0f, -6.0f);
    glEnd(); //End triangle coordinates

    glutSwapBuffers(); //Send the 3D scene to the screen
    }

    //—————————————————————————–
    /// Desc: Update Function & Idle Update Function
    /// Zaman Fonksiyonlari
    //—————————————————————————–
    void update(int value){
    if(rotation_status==1) rz+=1.6f; // Increase The Rotation of Triangle
    klavyeislemleri();
    glutPostRedisplay(); //tell GLUT redisplay changed scene

    //Tell GLUT to call update in 25 miliseconds
    //glutTimerFunc(16,update,0); // fps:1000/16=62.5 fps
    glutTimerFunc((1000/fps),update,0);
    }
    int now_time=0;
    static int last_time=0;
    int elapsed_time=0;

    void idle(){
    if(rotation_status==1) rz+=1.6f; // Increase The Rotation of Triangle
    //Timer Calculation to calculate FPS
    now_time = glutGet(GLUT_ELAPSED_TIME);
    elapsed_time = now_time – last_time;
    last_time = now_time;
    glutPostRedisplay(); //tell GLUT redisplay changed scene
    }

    //called when a key is pressed

    int main(int argc, char** argv) {
    //Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400); //Set the window size

    //Create the window
    glutCreateWindow("OpenGL – Hafta 4");
    initRendering(); //Initialize rendering

    //Set handler functions for drawing, keypresses, and window resizes
    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutKeyboardUpFunc(keyUp);
    glutReshapeFunc(handleResize);

    //adding here the mouse processing callbacks
    glutMouseFunc(processMouse);
    glutMotionFunc(processMouseActiveMotion);
    glutPassiveMotionFunc(processMousePassiveMotion);
    glutEntryFunc(processMouseEntry);

    //Timing…
    glutTimerFunc((1000/fps),update,0); //Add Timer
    //glutIdleFunc ( display );
    //glutIdleFunc(idle); //Idle Function

    glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
    return 0; //This line is never reached
    }

  2. hafta 4-5 yazsa da keybuffer var :):)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: