Programowanie w systemie UNIX/Cpp: Różnice pomiędzy wersjami

Z Wikibooks, biblioteki wolnych podręczników.
Usunięta treść Dodana treść
opengl
→‎OpenGl: nowy kod
Linia 40: Linia 40:
#include <iostream>
#include <iostream>


using namespace std;
unsigned int win_w = 0;
unsigned int win_h = 0;
unsigned int mouse_x = 0;
unsigned int mouse_y = 0;


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


glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_PROJECTION);
Linia 68: Linia 65:
glPopMatrix();
glPopMatrix();


unsigned char pixel[3];
glReadPixels(mouse_x, mouse_y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
std::cout << "R: " << (int)pixel[0] << std::endl;
std::cout << "G: " << (int)pixel[1] << std::endl;
std::cout << "B: " << (int)pixel[2] << std::endl;
std::cout << std::endl;

glFlush();
glutSwapBuffers();
glutSwapBuffers();
}
}
Linia 81: Linia 70:
void motion(int x, int y)
void motion(int x, int y)
{
{
y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
mouse_x = x;
mouse_y = win_h - y;
glutPostRedisplay();
}


unsigned char pixel[4];
void reshape(int w, int h)
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
{
cout << "R: " << (int)pixel[0] << endl;
win_w = w;
cout << "G: " << (int)pixel[1] << endl;
win_h = h;
cout << "B: " << (int)pixel[2] << endl;
glViewport(0, 0, w, h);
cout << endl;
}
}


int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
glutInitWindowSize(640,480);
glutInit(&argc, argv);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

glutInitWindowSize(640,480);
glutCreateWindow("glReadPixels()");
glutCreateWindow("glReadPixels()");


glutDisplayFunc(display);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutPassiveMotionFunc(motion);
glutPassiveMotionFunc(motion);
glutMainLoop();
glutMainLoop();

Wersja z 22:13, 7 kwi 2014

Przykłady

Przykładowe program w C++ :


Hello world

#include <iostream> 

int main () {

std::cout << "Hello, world!\n";

return 0;

}

Zapisujemy jako hello.cpp

Kompilujemy :

g++ -Wall hello.cpp

i uruchomiamy :

./a.out


OpenGl

// http://stackoverflow.com/questions/8000921/how-to-get-color-from-the-pixel-opengl
// g++ r.cpp -lGL -lGLU -lglut -Wall
// ./a.out

#include <GL/glut.h>
#include <iostream>

using namespace std;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10, 10, -10, 10, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
        glScalef(5,5,5);
        glBegin(GL_TRIANGLES);
            glColor3ub(255,0,0);
            glVertex2f(-1,-1);
            glColor3ub(0,255,0);
            glVertex2f(1,-1);
            glColor3ub(0,0,255);
            glVertex2f(1,1);
        glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

void motion(int x, int y)
{
    y = glutGet( GLUT_WINDOW_HEIGHT ) - y;

    unsigned char pixel[4];
    glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
    cout << "R: " << (int)pixel[0] << endl;
    cout << "G: " << (int)pixel[1] << endl;
    cout << "B: " << (int)pixel[2] << endl;
    cout << endl;
}

int main(int argc, char **argv)
{
    glutInitWindowSize(640,480);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow("glReadPixels()");

    glutDisplayFunc(display);
    glutPassiveMotionFunc(motion);
    glutMainLoop();
    return 0;
}