Posted on October 9, 2008 in Coding (PHP, C#,...) by Net-LordNo Comments »

Kot sem obljubil, je tu moj prvi C++ programček, ki je preko knjižice “freeglut” povezan z OpenGL. Stvarca nariše vrtečo 3D kocko. V ukazni vrstici se izpisuje tudi število sličic na sekundo, za katerega pa dvomim, da je natančno (čeprav bi načeloma moral biti).

Izvorna koda:

#include <gl/freeglut.h>
#include <windows.h>
#include <iostream>
#include <time.h>

int fps = -1;
int speed = 0;
time_t ts;
time_t te;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glRotatef(0.08, 0.7, 3, 1);

    glBegin(GL_QUADS);
      glColor3f(0,0,1);
      glVertex3f(-0.5, -0.5, -0.5);
      glVertex3f(-0.5, -0.5, 0.5);
      glVertex3f(0.5, -0.5, 0.5);
      glVertex3f(0.5, -0.5, -0.5);

      glColor3f(0,1,0);
      glVertex3f(-0.5, 0.5, -0.5);
      glVertex3f(-0.5, 0.5, 0.5);
      glVertex3f(0.5, 0.5, 0.5);
      glVertex3f(0.5, 0.5, -0.5);

      glColor3f(1,0,0);
      glVertex3f(0.5, 0.5, -0.5);
      glVertex3f(0.5, 0.5, 0.5);
      glVertex3f(0.5, -0.5, 0.5);
      glVertex3f(0.5, -0.5, -0.5);

      glColor3f(0.5,0,0.5);
      glVertex3f(-0.5, 0.5, -0.5);
      glVertex3f(-0.5, 0.5, 0.5);
      glVertex3f(-0.5, -0.5, 0.5);
      glVertex3f(-0.5, -0.5, -0.5);

      glColor3f(0.5,0.5,0);
      glVertex3f(0.5, 0.5, 0.5);
      glVertex3f(0.5, -0.5, 0.5);
      glVertex3f(-0.5, -0.5, 0.5);
      glVertex3f(-0.5, 0.5, 0.5);

      glColor3f(0,0.5,0.5);
      glVertex3f(0.5, 0.5, -0.5);
      glVertex3f(0.5, -0.5, -0.5);
      glVertex3f(-0.5, -0.5, -0.5);
      glVertex3f(-0.5, 0.5, -0.5);
    glEnd();

    te = time (NULL);
    if (te - ts >= 1.0 || fps == -1) {
        ts = time (NULL);
        std::cout << "\rFPS: " << fps; 
        fps = 0;
    }

    fps++;
    //Sleep(10);
    glFlush();
    glutPostRedisplay();
}

int main(int argc, char* argv[])
{
    ts = time (NULL);
    glutInit(&argc,argv);
    glutCreateWindow("Net-Lord 3D");
    glutInitWindowSize(600,600);
    glutInitWindowPosition(10,50);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA|GLUT_DEPTH);
    glEnable(GL_DEPTH_TEST);
    glutDisplayFunc(display);
    std::cout << "Ustvarjam...OK\n\n";
    std::cout << "(fps stevec naj bi bil realen)\n\n";
    std::cout << "\rFPS: ";
    glutMainLoop();
    return 0;
}

ScreenShot:
3D Cube

Download:
OpenGL - cube