Форум русскоязычного сообщества Ubuntu


Считаете, что Ubuntu недостаточно дружелюбна к новичкам?
Помогите создать новое Руководство для новичков!

Автор Тема: C - OpenGL - Camera + Car (Камеры в машины)  (Прочитано 807 раз)

0 Пользователей и 1 Гость просматривают эту тему.

Оффлайн victor00000

  • Автор темы
  • Старожил
  • *
  • Сообщений: 15568
  • Глухонемой (Deaf)
    • Просмотр профиля
C - OpenGL - Camera + Car (Камеры в машины)
« : 31 Декабря 2018, 20:31:24 »
// camera_car.c
// 0.6
// victor00000 (c) 2018
// Душинский Виктор Викторович.

// ~$ gcc camera_car.c -o camera_car -lGL -lGLU -lglut -lpthread
// ~$ ./camera_car
// keyboard:
// 'space' - goto camera
// 'enter' - goto car
// 'esc' - exit

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/freeglut_ext.h>

//=====================
pthread_cond_t pct;
pthread_mutex_t pmt;
float bcolor[4] = {.6,.6,.6,.6};
float wcolor[4] = {1,1,1,1};
float lcolor[4] = {0,0,0,0};
float fcolor[4] = { 0,0,0,0};

float west [16];
float cv[3], gv[3],gm[3],cm[3];
float m16[16];
int camerai, cr, gr;
typedef struct {
float x, y, z, m[16];
} camera0;
camera0 camera[10];
int btrue = 0, gari;
typedef struct {
float m[16];
float shag, shagm, ltr;
int b;
} gar0;
gar0 gar[20];
float r = 0;
int t1 = 0;

//=================
void gl_getm16to3(float *m, float *f);
void gl_getm3(float *f);
void gl_cube_size(float x, float y, float z);

//======================
void *gl_pth (void *a){
while (1){
usleep (1000 * 20);
pthread_cond_signal (&pct);
}
return a;
}

//=======================
void gl_cube3d (
float xs, float ys,
float zs, float ls){

float x,y,z, xx,yy,zz,
xss,yss,zss;

for (x=-1;x<=1;x++)
for (y=-1;y<=1;y++)
for (z=-1;z<=1;z++){
glPushMatrix ();
xss = 0; yss = 0; zss = 0;
xx = x; yy = y; zz = z;

if ((x == 1 || x == -1) &&
(y == 1 || y == -1) &&
(z == 1 || z == -1)){
xss = ls; yss = ls; zss = ls;
glColor4fv (lcolor);
//==============x1
}
else
//============s2
if (((x == 1 || x == -1 ) &&
y == 0 && ( z == -1 || z == 1)) ||
((z == 1 || z == -1 ) &&
x == 0 && ( y == -1 || y == 1)) ||
((x == 1 || x == -1 ) &&
z == 0 && ( y == -1 || y == 1))){

if ((x == -1 || x== 1) && ( y == -1 ||
y == 1 )){

xss = ls; yss = ls; zss = zs - ls;
}

if ((x == -1 || x == 1)&& y == 0){
xss = ls; yss = ys - ls; zss = ls;
}

if (x == 0 && ( y == -1 ||
y == 1 )){
xss = xs - ls; yss = ls; zss = ls;
}
glColor4fv (lcolor);
//==================x2
}
else {
if (x == -1){
xss = ls; yss = ys - ls;
zss = zs - ls;
}

if (x == 0 && (y == -1 ||
y == 1) ){
xss = xs - ls; yss = ls;
zss = zs - ls;
} else if (x == 0 && !(y == 0 &&
z == 0)){
xss = xs - ls; yss = ys - ls;
zss = ls;
}

if (x == 1){
xss = ls; yss = ys - ls;
zss = zs - ls;
}
glColor4fv (wcolor);
//=================x3
}
xx = ((xs - ls)/2) * x;
yy = ((ys - ls)/2) * y;
zz = ((zs - ls)/2) * z;
glTranslatef (xx,yy,zz);
// gl_cube_size (xss,yss,zss);
glScalef (xss,yss,zss);
glutSolidCube (1);
glPopMatrix ();
}
glColor4fv (wcolor);
}

//==================
void gl_getz (float *z){
glColor4fv (fcolor);
glPushMatrix ();
glLoadIdentity();
glTranslatef (-.05,-.04,-.1);
glScalef (.0001,.00005,.0001);
char *c;
asprintf (&c,"meter: %-4.01f",z[2]*-1.0f);
glutStrokeString(GLUT_STROKE_ROMAN,
(uint8_t*) c);
free (c);
glPopMatrix ();
glColor4fv (wcolor);
}

//================
int gl_ifgg (int l){
int i;
gl_getm16to3 (gar[l].m,gm);
for (i=0;i<gari;i++){
if (l == i) continue;
gl_getm16to3 (gar[i].m,cm);

if (( gm[0] < cm[0] + 1 &&
gm[0] > cm [0] - 1 ) &&
( gm[2] < cm[2] + 1 &&
gm[2] > cm[2] - 1 ) ){
// puts ("===========");
// }else{
return 1;
}
}
return 0;
}

//================
int gl_ifgar (float *f3){
int i;
for (i=0;i<gari;i++){
gl_getm16to3 (gar[i].m,gm);
//====================g2
if ((
gm[0] < f3[0] + .5 &&
gm[0] > f3[0] - .5) &&
(
gm[2] < f3[2] + .5 &&
gm[2] > f3[2] - .5) ){
// }else{
return 1;
}
}
return 0;
}

//===================
int gl_ifcamera (float *f3){
int i;
for (i=0;i<camerai;i++){

if ((f3[0] <  camera [i].x + 1 &&
f3 [0] > camera[i].x - 1)  &&
(f3[2] < camera[i].z + 1 &&
f3[2] > camera[i].z - 1) ){
// printf ("%d\n",i);
return 1;
}
}
return 0;
}

//=========================
int gl_ticksleep (int t0){
if (t1 == 0){
t1 = glutGet ( GLUT_ELAPSED_TIME );
return 0;
}
int t2 = glutGet ( GLUT_ELAPSED_TIME );
int t3 = t0 - (t2-t1);
if (t3 > 0){
// printf ("%d\n",t3);
usleep ( 1000 * t3 );
t1 = glutGet ( GLUT_ELAPSED_TIME );
return 0;
}
t1 = glutGet ( GLUT_ELAPSED_TIME );
return -1;
}

//===================
void gl_getm16(float *m)
{
glGetFloatv(GL_MODELVIEW_MATRIX, m);
}

//=================

//===============
void gl_shar(float sz, float x, float y, float z)
{
glLineWidth(4);

glPushMatrix();

glTranslatef(x, y, z);
glutSolidSphere(sz, 10, 10);
glColor4fv (lcolor);// (0, 0, 0, 0);
glutWireSphere(sz, 10, 10);
//=============a1
glPopMatrix();
glColor4fv (wcolor);
}

//=================
void gl_gar(const char *c)
{


glPushMatrix();
gl_cube_size(1, .05, 2);

glColor4fv (lcolor); // (0, 0, 0, 0);
glLineWidth(4);
// glutWireSphere (1,10,10);
glColor4fv(wcolor);

//=============l1
glPushMatrix();
glTranslatef(0, .1 + (.1 / 2), 0);
gl_cube_size(1, .1, 1);
glPushMatrix();
glTranslatef(0, .1 + (.3 / 2), .5);
gl_cube_size(.8, .3, .5);
glPopMatrix();
//===========l2
glTranslatef(0, .1 + (.2 / 2), -.3);
gl_cube_size(.5, .2, 1);
glPopMatrix();

//=============t1
glPushMatrix();
glColor4fv (fcolor); // (0, 0, 0, 0);
float s = .002, *m3 = malloc(sizeof(float) * 3);

gl_getm3(m3);
glLoadIdentity();
glTranslatef(m3[0], m3[1], m3[2]);
free(m3);
//=========t2
glLineWidth(8);
glTranslatef(0, .6, 0);
glScalef(s, s, s);
glutStrokeString(GLUT_STROKE_ROMAN, (uint8_t *) c);
glPopMatrix();

glLineWidth(3);
glColor4fv(wcolor);
//==========r1
gl_shar(.2, 1.0f / 2, 0, .5 - 2.0f / 2);
//========r2
gl_shar(.2, -1.0f / 2, 0, .5 - 2.0f / 2);

//===========r3
gl_shar(.2, 1.0f / 2, 0, -.5 + 2.0f / 2);
//=========r4
gl_shar(.2, -1.0f / 2, 0, -.5 + 2.0f / 2);

glPopMatrix();
glColor4fv(wcolor);
glLineWidth(8);
}

//=================
void gl_getm16to3(float *m, float *f)
{
// float *m = malloc (sizeof (
// float)*16 );
//glGetFloatv (
// GL_MODELVIEW_MATRIX  , m );
f[0] = m[0] * m[4] * m[8] + m[12];
f[1] = m[1] * m[5] * m[9] + m[13];
f[2] = m[2] * m[6] * m[10] + m[14];
// free (m);
}

//=================
void gl_getm3(float *f)
{
float *m = malloc(sizeof(float) * 16);

glGetFloatv(GL_MODELVIEW_MATRIX, m);
f[0] = m[0] * m[4] * m[8] + m[12];
f[1] = m[1] * m[5] * m[9] + m[13];
f[2] = m[2] * m[6] * m[10] + m[14];
free(m);
}

//=======================
void gl_camera(float up, const char *c)
{
glPushMatrix();
gl_cube_size(1, .1, 1);

glTranslatef(0, .1, 0);
glTranslatef(0, up / 2, 0);
gl_cube_size(.1, up, .1);

glTranslatef(0, up / 2, 0);
glTranslatef(0, .3 / 2, 0);
glColor4fv (fcolor); // (0, 0, 0, 0);
glLineWidth(3);

glutWireCube(.3);
gl_getm16(m16);
glLineWidth(8);
glTranslatef(0, .3 / 2, 0);

// char *c = strdup ("123");
float *m3 = malloc(sizeof(float) * 3);

gl_getm3(m3);
glLoadIdentity();
glTranslatef(m3[0], m3[1], m3[2]);
glTranslatef(-.2, .1, 0);
free(m3);

float s = .002;

glScalef(s, s, s);
glutStrokeString(GLUT_STROKE_ROMAN, (uint8_t *) c);

// free (c);
//================s3
glColor4fv(wcolor);
glPopMatrix();
}

//=================
void gl_cube()
{


glColor4fv (lcolor); // (0, 0, 0, 0);
glutWireCube(1);

glColor4fv(wcolor);
glutSolidCube(1);

}

//==================
void gl_cube_size(float x, float y, float z)
{
gl_cube3d (x,y,z, .05);
return;
glPushMatrix();
glScalef(x, y, z);
gl_cube();
glPopMatrix();
}

//======================
void gl_clear()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

//===================
void gl_display()
{
int i;

gl_clear();
if (btrue == 0) {
gl_getm16 (west);
int l0 = 0;
for (i = 0; i < gari; i++) {
gar[i].b = 0;
glPushMatrix();

glTranslatef(10 + (-2.4f * (i%10)), 0,
l0 * -3 );
if (i%10 == 9 ) l0++;
gl_getm16(gar[i].m);
glPopMatrix();
gar[i].ltr = -1 + (rand() % 3);
if (gar[i].ltr == 0) {
gar[i].shag = 1 + (rand() % 5);
} else {
gar[i].shag = rand() % 90;
}
}

//===============cm1
for (i = 0; i < camerai; i++) {
glPushMatrix();
 cam1:
camera[i].x = -50 + (rand() % 100);
camera[i].z = -50 + (rand() % 100);
camera[i].y = 1 + (rand() % 10);

int ii;

for (ii = 0; ii < i; ii++) {
if (camera[i].x == camera[ii].x &&
    camera[i].z == camera[ii].z)
break;
}
if (ii != i)
goto cam1;

//==================ifc
cm [1] = 0;
cm [0] = camera [i].x;
cm [2] = camera [i].z;
if (gl_ifgar(cm) == 1)
goto cam1;


glTranslatef(camera[i].x, 0, camera[i].z);
gl_camera(camera[i].y, "0");
for (ii = 0; ii < 16; ii++) {
camera[i].m[ii] = m16[ii];
}
glPopMatrix();
}
cr = rand() % camerai;
gr = rand() % gari;
gl_getm16to3(camera[cr].m, cv);
gl_getm16to3(gar[gr].m, gv);

} else {
for (i = 0; i < gari; i++) {
glPushMatrix();
glLoadMatrixf(gar[i].m);

// glTranslatef (0,0,.01f);

if (gar[i].ltr == 0) {

// if (gar[i].b != 0)
// glTranslatef(0, 0, 1);

glTranslatef(0, 0, .3);
gar[i].shag -= .3f;
}
if (gar[i].ltr == -1) {
glRotatef(-4, 0, 1, 0);

if (gar[i].b == 0)
glTranslatef (0,0,.2f);

gar[i].shag -= 4;
}
if (gar[i].ltr == 1) {
glRotatef(4, 0, 1, 0);

if (gar[i].b == 0)
glTranslatef (0,0,.2f);

gar[i].shag -= 4;
}
if (gar[i].shag < 0) {
gar[i].ltr = -1 + (rand() % 3);
if (gar[i].ltr == 0) {
gar[i].shag = 1 + (rand() % 5);
} else {
gar[i].shag = rand() % 90;
}
}
//===============g1
gl_getm3 (gm);
if (
// (gl_ifgg(i) == 0) &&
// (gl_ifcamera (gm) == 0) &&
((gm[0] < 50 && gm[0] > -50 ) &&
( gm[2] < 50 && gm[2] > -50 ))){

// if (gl_ifgg(i) == 0){
gl_getm16(gar[i].m);
gar[i].b = 0;

if (gl_ifcamera (gm) != 0)
gar[i].b = 1;
if (gl_ifgg(i) != 0)
gar[i].b = 1;


// } else gar[i].b = 1;
} else gar[i].b = 1;

glPopMatrix();
}
gl_getm16to3(gar[gr].m, gv);
}

btrue = 1;

gl_clear();
gluLookAt(cv[0], cv[1], cv[2], gv[0], gv[1], gv[2], 0, 1, 0);
glColor4fv (wcolor); // (1, 1, 1, 1);

//====================west
glPushMatrix ();
glMultMatrixf (west);
glTranslatef (0,0,55);
gl_cube_size (5,20,5);

/*
glVertex3f (0,1,0);
glVertex3f (0,1,-2);
glBegin (GL_LINE);
glEnd ();
*/

glPopMatrix ();

//==================pole
glPushMatrix ();
glTranslatef (0,-.2/2,0);
gl_cube_size (100,.2,100);
glPopMatrix ();

// glTranslatef (0,-1,0);

//===============camera
glPushMatrix();
for (i = 0; i < camerai; i++) {
glPushMatrix();

glTranslatef(camera[i].x, 0, camera[i].z);
char *c;

asprintf(&c, "c%d", i + 1);
gl_camera(camera[i].y, c);
free(c);
glPopMatrix();
}
glPopMatrix();

//=================gar
glPushMatrix();
// glTranslatef (0,0,-4);
for (i = 0; i < gari; i++) {
glPushMatrix();

// glLoadMatrixf (gar[i].m);
glMultMatrixf(gar[i].m);
if (i == gr )
{gl_getm3 (gm);
gl_getz (gm);
}
char *c;
asprintf(&c, "%d", i + 1);
gl_gar(c);
free(c);
glPopMatrix();
}
glPopMatrix();

glFlush();
// glutPostRedisplay();
// if ( gl_ticksleep (30) == -1 )
// puts ("===| Error ticks loop CPU |====");
// {}
// else
glutSwapBuffers();
pthread_cond_wait (&pct,&pmt);
}

//====================
void gl_reshape(GLsizei width, GLsizei height)
{

if (height == 0)
height = 1;
GLfloat aspect = (GLfloat) width / (GLfloat) height;

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect,
0.1f, 150.0f);
}

//===============
void gl_kd(uint8_t k, int x, int y)
{
(void)x;
(void)y;
if (k == 13) {
gr += 1;
if (gr > gari - 1)
gr = 0;
gl_getm16to3(camera[cr].m, cv);
}

if (k == ' ') {
cr += 1;
if (cr > camerai - 1)
cr = 0;
gl_getm16to3(camera[cr].m, cv);
}

if (k == 27)
exit(0);
}

//==================m0
int main(int ia, char **ca)
{
gari = sizeof(gar) / sizeof(gar0);
camerai = sizeof(camera) / sizeof(camera0);
srand(time(0));
glutInit(&ia, ca);
glutInitDisplayMode(GLUT_DOUBLE);

glutInitWindowSize(
// 320,200);
  800, 600);
glutInitWindowPosition (
//1000, 1000);
50, 50);

// glutReshapeFunc ( gl_reshape );
glutCreateWindow("GL Camera Gar");
// glutDisplayFunc(gl_display);
glutIdleFunc ( gl_display );

//===========
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearColor (bcolor[0], bcolor[1],
bcolor[2], bcolor[3]);
glColor4fv (lcolor); // (0, 0, 0, 0);

glutReshapeFunc(gl_reshape);

glLineWidth(8);

glutKeyboardFunc(gl_kd);

// glutFullScreen();
pthread_t tt;
pthread_create (&tt,0,gl_pth,0);
pthread_detach (tt);
glutMainLoop();
return 0;
}
« Последнее редактирование: 26 Марта 2019, 18:39:53 от victor00000 »
Wars ~.o

 

Страница сгенерирована за 0.038 секунд. Запросов: 25.