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


Хотите сделать посильный вклад в развитие Ubuntu и русскоязычного сообщества?
Помогите нам с документацией!

Автор Тема: C - OpenGL - Музыки Эффект Кубик.  (Прочитано 982 раз)

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

Оффлайн victor00000

  • Автор темы
  • Старожил
  • *
  • Сообщений: 15568
  • Глухонемой (Deaf)
    • Просмотр профиля
C - OpenGL - Музыки Эффект Кубик.
« : 01 Марта 2019, 02:59:37 »
1. Начало -> https://forum.ubuntu.ru/index.php?topic=303433.msg2376777#msg2376777

2.
// test_music_gl.c
// ~$ gcc -Wall test_music_gl.c -o test_music_gl -lGL -lGLU -lglut -lpthread || echo "Err: $?"
// ~$ pulsepeak | ./test_music_gl
// victor00000 (c) 2019
#include <stdio.h>
#include <locale.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>
//==================
#define wcolorf(r,g,b,a) gl_set_xcolor (wcolor,r,g,b,a)
//====================
pthread_mutex_t l_pmt;
pthread_cond_t c_pct;
static float cf [4];
float bcolor [4] = {0,0,0,0};
float wcolor [4] = {1,1,1,1};
float lcolor [4] = {1,1,1,1};
float fcolor [4] = {0,0,0,0};

int t1 = 0;
static char pavz [20];
float r = 0, pavzf = 0, pavzf1 = 0;
//=====================
void gl_getm16to3(float *m, float *f);
void gl_getm3(float *f);
void gl_getm16(float *f);
void gl_cube_size(float x, float y, float z);
int gl_ticksleep (int t0);
void gl_get_color ();
void gl_set_color ();

//=======================
void gl_set_xcolor (float *x,
float r, float g, float b,
float a){
x [0] = r;
x [1] = g;
x [2] = b;
x [3] = a;
}

//===================
void *gl_pth (void *a){
char *c;
while (1){
c = pavz;
fgets (c,sizeof (pavz),stdin);
if (*c == '#'){
c++;
pavzf = strtof (c,0);
c--;
fgets (c,sizeof (pavz),stdin);
}
pthread_cond_signal (&c_pct);
// usleep (1000*15);
}
return a;
}
//=====================
void gl_get_color (){
glGetFloatv(GL_CURRENT_COLOR, cf);
}

//====================
void gl_set_color (){
glColor4fv (cf);
}

//==================
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);
gl_get_color ();
glPushMatrix();

glTranslatef(x, y, z);
glutSolidSphere(sz, 10, 10);
glColor4fv (lcolor);
glutWireSphere(sz, 10, 10);
glPopMatrix();
gl_set_color ();
}

//=================
void gl_getm16to3(float *m, float *f)
{
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];
}

//=================
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_cube()
{
gl_get_color ();

glColor4fv(lcolor);
glutWireCube(1);

glColor4fv (wcolor);
glutSolidCube(1);
gl_set_color ();
}

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

//======================
void gl_clear()
{
glClearColor (bcolor[0],bcolor[1],
bcolor[2],bcolor[3]);
glColor4fv (lcolor);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

//===================
void gl_display()
{
gl_clear ();
glColor4f (1,1,1,1);
glTranslatef (0,0,-2);
glRotatef (r,1,1,1);
r += pavzf1;
//==================0
wcolorf (pavzf1, pavzf1, pavzf1,1);
glPushMatrix ();
glScalef (pavzf1, pavzf1,pavzf1);
gl_cube (1,1,1);
glPopMatrix ();
/*
if ( gl_ticksleep (30) == -1 )
// puts ("===| Error ticks loop CPU |====");
{}
else
*/
pthread_cond_wait (&c_pct,&l_pmt);
pavzf1 = pavzf;
glutSwapBuffers();
}

//====================
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)
{
if (k == 27 ) exit (0); // key 'ESC'
}

//==================m0
int main(int ia, char **ca)
{
setlocale(LC_NUMERIC, "C");
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(1.0f-.4, 1.0f-.4,
 1.0f-.4, 1.0f - .4);
glColor4f(0, 0, 0, 0);

glutReshapeFunc(gl_reshape);

glLineWidth(8);

glutKeyboardFunc(gl_kd);

if (ia == 2)
if (strcmp(ca [1],"-f")==0)
glutFullScreen();

pthread_t t;
pthread_create (&t,0,gl_pth,0);

glutMainLoop();
return 0;
}
« Последнее редактирование: 02 Марта 2019, 02:32:55 от victor00000 »
Wars ~.o

Оффлайн victor00000

  • Автор темы
  • Старожил
  • *
  • Сообщений: 15568
  • Глухонемой (Deaf)
    • Просмотр профиля
Re: C - OpenGL - Музыки Эффект Кубик.
« Ответ #1 : 03 Марта 2019, 20:27:46 »

// test_gl_in01.c
// ~$ gcc -Wall test_gl_in01.c -o test_gl_in01 -lGL -lGLU -lglut -lpthread || echo "Err: $?"
// ~$ pulsepeak | ./test_gl_in01
// victor00000 (c) 2019
#include <stdio.h>
#include <locale.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/freeglut.h>
#include <GL/glu.h>
#include <GL/freeglut_ext.h>
//==================
#define lcolorf(r,g,b,a) \
gl_set_xcolor (lcolor,r,g,b,a)

#define wcolorf(r,g,b,a) \
gl_set_xcolor (wcolor,r,g,b,a)
//====================
static float fpav[10];
int tpause = 0;
FILE *fp;
static char str_p[32];
static float m[16];
int window, bt = 0;
pthread_mutex_t l_pmt;
pthread_cond_t c_pct;
static float cf[4];

// float bcolor [4] = {1,1,1,1};
float bcolor[4] = { 0, 0, 0, 0 };

// float wcolor [4] = {1,1,1,1};
float wcolor[4] = { 0, 0, 0, 0 };

// float lcolor [4] = {0,0,0,0};
float lcolor[4] = { 1, 1, 1, 1 };

float fcolor[4] = { 0, 0, 0, 0 };

int t1 = 0;
static char pavz[20];
float r = 0, pavzf = 0, pavzf1 = 0;

//=====================
void gl_getm16to3(float *m, float *f);
void gl_getm3(float *f);
void gl_getm16(float *f);
void gl_cube_size(float x, float y, float z);
int gl_ticksleep(int t0);
void gl_get_color();
void gl_set_color();
void gl_printf(const char *str, ...);
void gl_rect_3d(float sx, float sy, float sz, float sl);
void gl_push();
void gl_pop();
void gl_rect0_2d(float x, float y, float s);
void gl_2d(float x, float y, float cl[4]);
void gl_rect_2d(float x, float y, float s);

//=======================
void *gl_pth_p(void *a)
{
while (1) {
if (tpause == 1)
break;
tpause = 1;
sleep(2);
}
fputs("Err: no signal outputs.\n", stderr);
exit(1);
return a;
}

//=====================
void gl_rotate_0_1(float r1)
{
float x, y, z;

while (1) {
x = rand() % 2 == 1 ? 1.0f : 0.0f;
y = rand() % 2 == 1 ? 1.0f : 0.0f;
z = rand() % 2 == 1 ? 1.0f : 0.0f;
if (x == 0 && y == 0 && z == 0)
continue;
break;
}
glRotatef(r1, x, y, z);
return;
int i = rand() % 7;

if (i == 0)
glRotatef(r1, 0, 0, 1);
if (i == 1)
glRotatef(r1, 0, 1, 0);
if (i == 2)
glRotatef(r1, 0, 1, 1);
if (i == 3)
glRotatef(r1, 1, 0, 0);
if (i == 4)
glRotatef(r1, 1, 0, 1);
if (i == 5)
glRotatef(r1, 1, 1, 0);
if (i == 6)
glRotatef(r1, 1, 1, 1);
if (i == 7)
glRotatef(r1, 0, 0, 0);

}

//===================
float gl_0_1()
{
return rand() % 2 == 0 ? 1.0f : 0.0f;
}

//===================
void gl_rect_2d(float x, float y, float s)
{
gl_push();
gl_push();
glTranslatef(0, -((y / 2.0f) + (s / 2.0f)), 0);
gl_2d(x + (s * 2.0f), s, lcolor);
gl_pop();
//===================1

gl_push();
glTranslatef(0, ((y / 2.0f) + (s / 2.0f)), 0);
gl_2d(x + (s * 2.0f), s, lcolor);
gl_pop();
//===================2

gl_push();
glTranslatef(-((x / 2.0f) + (s / 2.0f)), 0, 0);
gl_2d(s, y, lcolor);
gl_pop();
//=============3

gl_push();
glTranslatef(((x / 2.0f) + (s / 2.0f)), 0, 0);
gl_2d(s, y, lcolor);
gl_pop();
//=====================4
gl_2d(x - s, y - s, wcolor);
gl_pop();
}

//=======================
void gl_2d(float x, float y, float cl[4])
{

gl_get_color();
glBegin(GL_QUADS);
glColor4fv(cl);
glVertex2f(-(x / 2.0f), -(y / 2.0f));
glVertex2f((x / 2.0f), -(y / 2.0f));
glVertex2f((x / 2.0f), (y / 2.0f));
glVertex2f(-(x / 2.0f), (y / 2.0f));
// glVertex2f (-(x/2.0f),-(y/2.0f));
glEnd();
gl_set_color();
}

//========================
void gl_rect0_2d(float x, float y, float s)
{
gl_push();
gl_2d(x, y, lcolor);
glTranslatef(0, 0, -.1f);
gl_2d(x - s, y - s, wcolor);

glTranslatef(0, 0, .2f);
gl_2d(x - s, y - s, wcolor);

gl_pop();
}

//-==============
void gl_pop()
{
glPopMatrix();
}

//=====================
void gl_push()
{
glPushMatrix();
}

//=========================
void gl_rect_3d(float sx, float sy, float sz, float sl)
{

gl_push();
float x, y, z;
int i;

for (i = 0; i < 3; i++) {

if (i == 0 || i == 2) {
x = sz;
y = sy;
z = sx;
}
if (i == 1) {
x = sx;
y = sy;
z = sz;
}
gl_push();
glRotatef(90, 0, 1, 0);
glRotatef(-90 * i, 0, 1, 0);
glTranslatef(0, 0, z / 2.f);
gl_rect_2d(x, y, sl);
gl_pop();
//=======================s2
if (i == 0 || i == 2) {
x = sx;
y = sz;
z = sy;
}
gl_push();
glRotatef(90, 1, 0, 0);
glRotatef(90 * i, 1, 0, 0);
glTranslatef(0, 0, z / 2.f);
gl_rect_2d(x, y, sl);
gl_pop();
//====================s1
}
gl_pop();
}

//==================
void gl_printf(const char *str, ...)
{
glColor4fv(fcolor);
va_list ap;

va_start(ap, str);
vsnprintf(str_p, sizeof(str_p), str, ap);
va_end(ap);
glutStrokeString(GLUT_STROKE_ROMAN, (uint8_t *) str_p);
}

//=======================
void gl_set_xcolor(float *x, float r, float g, float b, float a)
{
x[0] = r;
x[1] = g;
x[2] = b;
x[3] = a;
}

//===================
void *gl_pth(void *a)
{
char *c;

while (1) {
tpause = 0;
c = pavz;

fgets(c, sizeof(pavz), stdin);
if (*c == '#') {
c++;
pavzf = strtof(c, 0);
// pavzf = .99;
// printf ("%.f = %s\n",pavzf,c);
c--;
fgets(c, sizeof(pavz), stdin);
}

pthread_cond_signal(&c_pct);
// usleep (1000*15);
}
return a;
}

//=====================
void gl_get_color()
{
glGetFloatv(GL_CURRENT_COLOR, cf);
}

//====================
void gl_set_color()
{
glColor4fv(cf);
}

//==================
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(8);
gl_get_color();
glPushMatrix();

glTranslatef(x, y, z);
glutSolidSphere(sz, 10, 10);
glColor4fv(lcolor);
glutWireSphere(sz, 10, 10);
glPopMatrix();
gl_set_color();
}

//=================
void gl_getm16to3(float *m, float *f)
{
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];
}

//=================
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_cube()
{
gl_get_color();

glColor4fv(lcolor);
glutWireCube(1);

glColor4fv(wcolor);
glutSolidCube(1);
gl_set_color();
}

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

//======================
void gl_clear()
{
glClearColor(bcolor[0], bcolor[1], bcolor[2], bcolor[3]);
glColor4fv(lcolor);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

//========================f
//============================
void gl_up_3d(float up, float x)
{
// up = up * (up+.5f) / up;
//if (up == 0) up = .1f;
gl_push();
glTranslatef(0, up / 2.f, 0);
gl_cube_size(x, up, .01f);
gl_pop();
glTranslatef(x + .03f, 0, 0);
//==================f1
}

//===================
void gl_display()
{
gl_clear();
if (bt == 0) {
glTranslatef(-.9f, -.75f, -1.9f);
gl_getm16(m);
bt = 1;
}
glLoadMatrixf(m);

int i, i0 = pavzf * 10.f;

glScalef(1, 1.5f, 1);
for (i = 0; i < 10; i++) {
if (i == i0) {
if (pavzf1 == 0)
fpav[i] -= .003f * 2.f;
else
fpav[i] += .03f * 2.f;
} else
fpav[i] -= .003f * 2.f;

if (fpav[i] > 1)
fpav[i] = 1.0f;

if (fpav[i] < 0)
fpav[i] = 0;

wcolorf ( fpav[i]/2.f,
fpav[i]/2.f, fpav[i]/2.f,
fpav[i]/2.f);
// wcolorf (1 - fpav[i], 1 - fpav[i],
// 1 - fpav[i], 1 - fpav[i]);
// wcolorf (.2,.2,.2,.2);
gl_up_3d(fpav[i],
// 1,
// .2f,
.17f);
}
//==========================def

/*
gl_getm16to3 (m,m3);
glLoadIdentity ();
glTranslatef (-.05,-.04,-.1);
glScalef (.0001,.00011,.0001);
gl_printf ("%.01f = %.01f",m3[2],
pavzf1*2.f);
*/
//====================1
/*
if ( gl_ticksleep (30) == -1 )
// puts ("===| Error ticks loop CPU |====");
{}
else
*/
glFlush();
glutSwapBuffers();
pthread_cond_wait(&c_pct, &l_pmt);
pavzf1 = pavzf;
// glutSwapBuffers();
}

//====================
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)
{
if (k == 27)
exit(0); // key 'ESC'
}

//==================m0
int main(int ia, char **ca)
{
srand(time(0));
// setlocale(LC_ALL, getenv("LANG"));
setlocale(LC_NUMERIC, "C");
srand(time(0));
glutInit(&ia, ca);
glutInitDisplayMode(GLUT_DOUBLE);

glutInitWindowSize(640, 400);
// 800, 600);
glutInitWindowPosition(
//1000, 1000);
      50, 50);

// glutReshapeFunc ( gl_reshape );
window = glutCreateWindow(ca[0]);
// 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(1.0f - .4, 1.0f - .4, 1.0f - .4, 1.0f - .4);
glColor4f(0, 0, 0, 0);

glutReshapeFunc(gl_reshape);

glLineWidth(8);

glutKeyboardFunc(gl_kd);

if (ia > 1)
if (strcmp(ca[1], "-f") == 0)
glutFullScreen();

pthread_t t;

pthread_create(&t, 0, gl_pth, 0);
pthread_create(&t, 0, gl_pth_p, 0);

glutMainLoop();
return 0;
}
гораздо! :)
« Последнее редактирование: 08 Марта 2019, 19:31:19 от victor00000 »
Wars ~.o

 

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