source: trunk/packages/vizservers/nanovis/socket/RenderClient.cpp @ 2096

Last change on this file since 2096 was 2096, checked in by ldelgass, 13 years ago

Normalize line endings, set eol-style to native on *.cpp, *.h files

  • Property svn:eol-style set to native
File size: 15.0 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  RenderClient.cpp: client connecting to the RenderServer
4 *
5 * ======================================================================
6 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
7 *           Purdue Rendering and Perceptualization Lab (PURPL)
8 *
9 *  Copyright (c) 2004-2006  Purdue Research Foundation
10 *
11 *  See the file "license.terms" for information on usage and
12 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * ======================================================================
14 */
15#include <GL/freeglut.h>
16#include "RenderClient.h"
17#include <stdlib.h>
18#include <sstream>
19#include <string.h>
20#include <time.h>
21#include <iostream>
22#include <assert.h>
23
24#include "../Event.h"
25
26using namespace std;
27
28Event* event[5000];
29int cur_event = 0;
30
31
32int width, height;
33RenderClient::RenderClient(){}
34
35RenderClient::RenderClient(std::string& remote_host, int port_num){
36        //screen_size = sizeof(float)*4*512*512;        //float
37        screen_size = 3*width*height;   //unsigned byte
38        screen_buffer = new char[screen_size];
39
40        socket_num = port_num;
41        host = remote_host;
42
43        //init socket server
44        std::cout << "client running....\n";
45
46        try
47        {
48          // Create the socket
49          client_socket = new ClientSocket(host, socket_num);
50          //client_socket->set_non_blocking(true);
51          fprintf(stderr, "client socket initialized\n");
52        }
53        catch ( SocketException& e )
54        {
55          std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
56        }
57}
58
59void RenderClient::send(std::string& msg){
60      try
61        {
62          //printf("client: send()\n");
63          *client_socket << msg;
64        }
65      catch ( SocketException& ) {}
66}
67
68void RenderClient::receive(std::string& msg){
69      std::string buf="";
70      try
71        {
72          fprintf(stderr, "client: receive()\n");
73          fflush(stdout);
74          *client_socket >> buf;
75          msg = buf;
76        }
77      catch ( SocketException& ) {}
78}
79
80
81bool RenderClient::receive(char* s, int size){
82      try
83        {
84          //printf("client: receive()\n");
85          bool ret = client_socket->recv(s, size);
86          return true;
87        }
88      catch ( SocketException& ) { return false;}
89}
90
91
92bool RenderClient::send(char* s, int size){
93      try
94        {
95          //printf("client: receive()\n");
96          bool ret = client_socket->send(s, size);
97          return true;
98        }
99      catch ( SocketException& ) { return false;}
100}
101
102
103RenderClient* client;
104
105bool loaded = false;
106
107float live_rot_x = 0.;
108float live_rot_y = 0.;
109float live_rot_z = 0.;
110
111float live_obj_x = 0.;
112float live_obj_y = 0.;
113float live_obj_z = -3.;
114
115int left_last_x, left_last_y, right_last_x, right_last_y;
116bool left_down, right_down;
117bool render_done = true;
118
119/* GLUT callback Handlers */
120void resize(int _width, int _height)
121{
122   delete[] client->screen_buffer;
123   client->screen_size = 3*_width*_height;
124   client->screen_buffer = new char[client->screen_size];
125
126    width = _width;
127    height = _height;
128
129    glViewport(0, 0, width, height);
130    glMatrixMode(GL_PROJECTION);
131    glLoadIdentity();
132    gluPerspective(60, float(width)/height, 0.1, 50.0);
133    //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
134
135    glMatrixMode(GL_MODELVIEW);
136    glLoadIdentity() ;
137    glTranslatef(live_obj_x, live_obj_y, live_obj_z);
138    glRotated(live_rot_x, 1., 0., 0.);
139    glRotated(live_rot_y, 0., 1., 0.);
140    glRotated(live_rot_z, 0., 0., 1.);
141}
142
143
144void draw_buttons(){
145  glPushMatrix();
146
147  glViewport(0, 0, width, height);
148  glMatrixMode(GL_PROJECTION);
149  glLoadIdentity();
150  glOrtho(0, width, 0, height, -0.09, 1);
151  glMatrixMode(GL_MODELVIEW);
152  glLoadIdentity();
153
154  glDisable(GL_BLEND);
155  glEnable(GL_BLEND);
156 
157  //loaddata button
158  glColor4f(0.5, 0.5, 0.5, 0.5);
159  glBegin(GL_QUADS);
160    glVertex2d(10, 10);
161    glVertex2d(60, 10);
162    glVertex2d(60, 40);
163    glVertex2d(10, 40);
164  glEnd();
165 
166
167  glDisable(GL_BLEND);
168  glPopMatrix();
169}
170
171
172
173void display()
174{
175
176       
177   if(loaded){
178     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
179     //glDrawPixels(width, height, GL_RGBA, GL_FLOAT, client->screen_buffer);   
180     //bzero(client->screen_buffer, client->screen_size);
181     glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, client->screen_buffer);     
182     glFlush();
183     glutSwapBuffers();
184     return;
185   }
186
187 
188    render_done = false;
189
190    //const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
191
192    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
193
194    glViewport(0, 0, width, height);
195    glMatrixMode(GL_PROJECTION);
196    glLoadIdentity();
197    gluPerspective(60, float(width)/height, 0.1, 50.0);
198    //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
199
200    glMatrixMode(GL_MODELVIEW);
201    glLoadIdentity() ;
202    glTranslatef(live_obj_x, live_obj_y, live_obj_z);
203    glRotated(live_rot_x, 1., 0., 0.);
204    glRotated(live_rot_y, 0., 1., 0.);
205    glRotated(live_rot_z, 0., 0., 1.);
206
207    glEnable(GL_DEPTH_TEST);
208
209    glEnable(GL_LIGHTING);
210    //glutSolidCone(1,1,slices,stacks);
211    glutSolidCube(1.);
212    glDisable(GL_LIGHTING);
213
214    //draw buttons
215    //draw_buttons();
216
217    glFlush();
218    glutSwapBuffers();
219
220    render_done = true;
221}
222
223
224void key(unsigned char key, int x, int y)
225{
226    cerr << "client: key()" << endl;
227    std::stringstream msgstream;
228    std::string msg;
229    std::string msg2;
230
231    switch (key)
232    {
233        case 27 :
234        case 'q':
235          exit(0);
236          break;
237
238        case 'l':
239          cerr << "client: load" << endl;
240          //msgstream << "command=0" << endl;
241          msgstream << "load" << endl;
242          msg = msgstream.str();
243          client->send(msg);
244          loaded = true;
245          break;
246
247        case 'o':
248          msgstream << "command=" << "1" << endl;
249          msg = msgstream.str();
250          client->send(msg);
251          break;
252
253        case '+':
254          msgstream << "command=" << "4" << endl;
255          msg = msgstream.str();
256          client->send(msg);
257          break;
258
259        case '-':
260          msgstream << "command=" << "5" << endl;
261          msg = msgstream.str();
262          client->send(msg);
263          break;
264
265        case 'x':
266          //msgstream << "and=" << "5" << endl;
267          msgstream << "cut x " << "off" << endl;
268          msg = msgstream.str();
269          cerr << "client: " << msg << endl;
270          client->send(msg);
271          break;
272
273        case 'X':
274          msgstream << "cut x " << " on" << endl;
275          msg = msgstream.str();
276          cerr << "client: " << msg << endl;
277          client->send(msg);
278          break;
279
280        case 'y':
281          msgstream << "cut y " << "off" << endl;
282          msg = msgstream.str();
283          cerr << "client: " << msg << endl;
284          client->send(msg);
285          break;
286
287        case 'Y':
288          msgstream << "cut y " << "on" << endl;
289          msg = msgstream.str();
290          cerr << "client: " << msg << endl;
291          client->send(msg);
292          break;
293
294        case 'z':
295          msgstream << "cut z " << "off" << endl;
296          msg = msgstream.str();
297          cerr << "client: " << msg << endl;
298          client->send(msg);
299          break;
300
301        case 'Z':
302          msgstream << "cut z " << "on" << endl;
303          msg = msgstream.str();
304          cerr << "client: " << msg << endl;
305          client->send(msg);
306          break;
307
308        default:
309          return;
310    }
311
312   
313    /*
314    for(int j=0; j<512; j=j+1){
315      cerr << "client read: " << j << endl;
316      client->receive(client->screen_buffer + j*4*512, 4*512);  //unsigned byte
317    }
318    */
319   
320    client->receive(client->screen_buffer, client->screen_size);        //unsigned byte
321
322    //cin >> msg;
323    //strncpy(client->screen_buffer, msg.c_str(), 512*512*4);
324       
325    display();
326    //glutPostRedisplay();
327
328    cerr << "client: key() done" << endl;
329}
330
331
332void update_rot(int delta_x, int delta_y){
333        live_rot_x += delta_x;
334        live_rot_y += delta_y;
335
336        if(live_rot_x > 360.0)
337                live_rot_x -= 360.0;   
338        else if(live_rot_x < -360.0)
339                live_rot_x += 360.0;
340
341        if(live_rot_y > 360.0)
342                live_rot_y -= 360.0;   
343        else if(live_rot_y < -360.0)
344                live_rot_y += 360.0;
345}
346
347void update_trans(int delta_x, int delta_y, int delta_z){
348        live_obj_x += delta_x*0.01;
349        live_obj_y += delta_y*0.01;
350        live_obj_z += delta_z*0.01;
351}
352
353bool test_in_box(int box_x0, int box_y0, int box_x1, int box_y1, int x, int y){
354        return (box_x0<=x && box_x1>=x && box_y0<=y && box_y1>=y);
355}
356
357void mouse(int button, int state, int x, int y){
358  if(button==GLUT_LEFT_BUTTON){
359    left_last_x = x;
360    left_last_y = y;
361
362    if(state==GLUT_DOWN){
363      left_down = true;
364      right_down = false;
365    }
366    else{
367      left_down = false;
368      right_down = true;
369    }
370  }
371  else{
372    right_last_x = x;
373    right_last_y = y;
374
375    if(state==GLUT_DOWN){
376      left_down = false;
377      right_down = true;
378    }
379    else{
380      left_down = true;
381      right_down = false;
382    }
383  }
384}
385
386void motion(int x, int y){
387  cerr << "client: motion() " << endl;
388
389  if(render_done){
390    int old_x, old_y;   
391
392    if(left_down){
393      old_x = left_last_x;
394      old_y = left_last_y;   
395    }
396    else{
397      old_x = right_last_x;
398      old_y = right_last_y;   
399    }
400
401    int delta_x = x - old_x;
402    int delta_y = y - old_y;
403
404    //more coarse event handling
405    if(abs(delta_x)<5 && abs(delta_y)<5)
406      return;
407
408    if(left_down){
409      left_last_x = x;
410      left_last_y = y;
411
412      update_rot(delta_y, delta_x);
413      std::stringstream msgstream;
414      //msgstream << "command=" << "2" << "&delta_x=" << delta_y << "&delta_y=" << delta_x << endl;
415      msgstream << "camera " << live_rot_x << " " << live_rot_y << " " << live_rot_z << " " << live_obj_z << endl;
416      std::string msg;
417      msg = msgstream.str();
418      std::cout << "client msg: " << msg <<"\n";
419      //client->send(msg);
420      client->send((char*) msg.c_str(), (int) strlen(msg.c_str()));
421    }
422    else{
423      right_last_x = x;
424      right_last_y = y;
425
426      update_trans(0, 0, delta_y);
427      std::stringstream msgstream;
428      //msgstream << "command=" << "3" << "&delta_x=" << delta_y << "&delta_y=" << delta_x << endl;
429      msgstream << "camera " << live_rot_x << " " << live_rot_y << " " << live_rot_z << " " << live_obj_z << endl;
430      std::string msg;
431      msg = msgstream.str();
432      std::cout << "client msg: " << msg <<"\n";
433      //client->send(msg);
434      client->send((char*) msg.c_str(), (int) strlen(msg.c_str()));
435    }
436
437    client->receive(client->screen_buffer, client->screen_size);
438
439    /*
440    for(int j=0; j<512; j=j+4){
441      unsigned int r,g,b,a;
442      r = client->screen_buffer[j];
443      g = client->screen_buffer[j+1];
444      b = client->screen_buffer[j+2];
445      a = client->screen_buffer[j+3];
446      //fprintf(stderr, "(%d %d %d %d) ", r,g,b,a);     //unsigned byte
447      fprintf(stderr, "(%X %X %X %X) ", r,g,b,a);       //unsigned byte
448    }
449    */
450
451    display();
452    cerr << "client: motion() done " << endl;
453  }
454}
455
456void idle(void)
457{
458/*
459    struct timespec ts;
460    ts.tv_sec = 0;
461    ts.tv_nsec = 100000000;
462
463    nanosleep(&ts, 0);
464*/
465
466  //send requests
467  Event* cur = event[cur_event];
468  std::stringstream msgstream;
469  std::string msg;
470
471  switch(cur->type){
472    case 0: //rotate
473      msgstream << "camera " << cur->parameter[0] << " "
474                    << cur->parameter[1] << " "
475                    << cur->parameter[2] << " " << endl;
476      break;
477
478    case 1: //move
479      msgstream << "move " << cur->parameter[0] << " "
480                    << cur->parameter[1] << " "
481                    << cur->parameter[2] << " " << endl;
482      break;
483
484    case 2: //other
485      msgstream << "refresh " << cur->parameter[0] << " "
486                    << cur->parameter[1] << " "
487                    << cur->parameter[2] << " " << endl;
488      break;
489
490    default:
491      return;
492  }
493
494  msg = msgstream.str();
495  //std::cout << "client msg: " << msg <<"\n";
496 
497  //start timer
498  client->send((char*) msg.c_str(), (int) strlen(msg.c_str()));
499  client->receive(client->screen_buffer, client->screen_size);
500  //end timer
501
502}
503
504const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
505const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
506const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
507const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
508
509const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
510const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
511const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
512const GLfloat high_shininess[] = { 100.0f };
513
514
515void init_client(char* host, char* port, char* file){
516
517  //load the event file
518  FILE* fd = fopen(file, "r");
519  //load 5000 events
520  for(int i=0; i<5000; i++){
521    int type;
522    float param[3];
523    fscanf(fd, "%d %f %f %f\n", &type, param, param+1, param+2);
524    event[i] = new Event(type, param, 0);
525    fprintf(stderr, "%d %f %f %f\n", type, param[0], param[1], param[2]);
526  }
527  fclose(fd);
528
529  //std::string host = "localhost";
530  //hostname -i
531  //std::string host = "128.46.137.192";
532 
533  std::string hostname = host;
534  client = new RenderClient(hostname, atoi(port));
535
536  //point stdin stdout to socket
537  /*
538  close(0);
539  close(1);
540  dup2(client->Socket::m_sock, 0);
541  dup2(client->Socket::m_sock, 1);
542  */
543 
544  std::string msg1 = "hello";
545  std::string msg2 = " ";
546
547  //cout << msg1;
548  //cin >> msg2;
549
550  client->send(msg1);
551  client->receive(msg2);
552 
553  cerr << "client: msg received - " << msg2 << endl;
554  cerr << "connection to server established" << endl;
555}
556
557
558
559void print_gl_info(){
560  fprintf(stderr, "OpenGL vendor: %s %s\n", glGetString(GL_VENDOR), glGetString(GL_VERSION));
561  fprintf(stderr, "OpenGL renderer: %s\n", glGetString(GL_RENDERER));
562}
563
564
565void menu_cb(int entry){
566
567  std::stringstream msgstream;
568  std::string msg;
569  std::string msg2;
570  switch(entry)
571  {
572    case 0:
573        msgstream << "command=" << "0";
574        msgstream >> msg;
575        client->send(msg);
576
577        client->receive(msg2);
578       
579        loaded = true;
580        break;
581
582    case 1:
583        msgstream << "command=" << "1";
584        msgstream >> msg;
585        client->send(msg);
586
587        client->receive(msg2);
588        break;
589
590    case 2:
591        msgstream << "command=" << "2";
592        msgstream >> msg;
593        client->send(msg);
594
595        client->receive(msg2);
596        break;
597  }
598}
599
600void help(const char *argv0)
601{
602  fprintf(stderr,
603          "Syntax: %s addr port eventfile\n",
604          argv0);
605  exit(1);
606}
607
608/* Program entry point */
609int main(int argc, char *argv[])
610{
611    //parameters:  hostip and port and event file
612    if(argc!=4) help(argv[0]);
613
614    width =512; height=512;
615
616    init_client(argv[1], argv[2], argv[3]);
617
618    glutInit(&argc, argv);
619    glutInitWindowSize(width,height);
620    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
621
622    glutCreateWindow("Client");
623
624    /*
625    glutCreateMenu(menu_cb);
626    glutAddMenuEntry("load data ", 0);
627    glutAddMenuEntry("toggle mode (on/off screen)", 1);
628    glutAttachMenu(GLUT_RIGHT_BUTTON);
629    */
630
631    glutReshapeFunc(resize);
632    glutDisplayFunc(display);
633    glutMouseFunc(mouse);
634    glutMotionFunc(motion);
635    glutKeyboardFunc(key);
636    glutIdleFunc(idle);
637
638    glClearColor(0.,0.,0.,0.);
639
640    glDepthFunc(GL_LESS);
641    glDepthRange(0,1);
642    glDisable(GL_DEPTH_TEST);
643
644    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
645    glDisable(GL_BLEND);
646
647    glDisable(GL_ALPHA_TEST);
648
649    glEnable(GL_LIGHT0);
650    glEnable(GL_LIGHTING);
651
652    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
653    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
654    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
655    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
656
657    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
658    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
659    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
660    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
661
662    print_gl_info();
663
664    left_down = false;
665    right_down = false;
666
667    glutMainLoop();
668
669    return 0;
670}
671
Note: See TracBrowser for help on using the repository browser.