Programowanie w systemie UNIX/Cpp: Różnice pomiędzy wersjami
Usunięta treść Dodana treść
link wewn |
opengl |
||
Linia 1: | Linia 1: | ||
=Przykłady= |
|||
Przykładowe program w [[C++]] : |
|||
=Hello world= |
|||
<source lang=cpp> |
<source lang=cpp> |
||
Linia 22: | Linia 26: | ||
./a.out |
./a.out |
||
==OpenGl == |
|||
<source lang = cpp> |
|||
// 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> |
|||
unsigned int win_w = 0; |
|||
unsigned int win_h = 0; |
|||
unsigned int mouse_x = 0; |
|||
unsigned int mouse_y = 0; |
|||
void display(void) |
|||
{ |
|||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_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(); |
|||
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(); |
|||
} |
|||
void motion(int x, int y) |
|||
{ |
|||
mouse_x = x; |
|||
mouse_y = win_h - y; |
|||
glutPostRedisplay(); |
|||
} |
|||
void reshape(int w, int h) |
|||
{ |
|||
win_w = w; |
|||
win_h = h; |
|||
glViewport(0, 0, w, h); |
|||
} |
|||
int main(int argc, char **argv) |
|||
{ |
|||
glutInit(&argc, argv); |
|||
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); |
|||
glutInitWindowSize(640,480); |
|||
glutCreateWindow("glReadPixels()"); |
|||
glutDisplayFunc(display); |
|||
glutReshapeFunc(reshape); |
|||
glutPassiveMotionFunc(motion); |
|||
glutMainLoop(); |
|||
return 0; |
|||
} |
|||
</source> |
Wersja z 18:37, 6 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>
unsigned int win_w = 0;
unsigned int win_h = 0;
unsigned int mouse_x = 0;
unsigned int mouse_y = 0;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_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();
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();
}
void motion(int x, int y)
{
mouse_x = x;
mouse_y = win_h - y;
glutPostRedisplay();
}
void reshape(int w, int h)
{
win_w = w;
win_h = h;
glViewport(0, 0, w, h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("glReadPixels()");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutPassiveMotionFunc(motion);
glutMainLoop();
return 0;
}