Adding networking to send agent head rotations

This commit is contained in:
Philip Rosedale 2012-12-12 09:54:13 -08:00
parent b022550eac
commit 1c03d5822a
10 changed files with 172 additions and 134 deletions

View file

@ -8,13 +8,14 @@
#include <iostream>
#include "agent.h"
#include "head.h"
// Structure to hold references to other agents that are nearby
const int MAX_AGENTS = 100;
struct AgentList {
in_addr sin_addr;
glm::vec3 position;
Head head;
} agents[MAX_AGENTS];
int num_agents = 0;
@ -23,8 +24,7 @@ int num_agents = 0;
//
void update_agents(char * data, int length) {
std::string packet(data, length);
//std::string packet("127.0.0.1,");
//std::cout << " Update Agents, string: " << packet << "\n";
std::cout << " Update Agents, string: " << packet << "\n";
size_t spot;
size_t start_spot = 0;
spot = packet.find_first_of (",", 0);
@ -41,6 +41,29 @@ void update_agents(char * data, int length) {
}
}
void render_agents() {
for (int i = 0; i < num_agents; i++) {
glm::vec3 pos = agents[i].head.getPos();
glPushMatrix();
glTranslatef(pos.x, pos.y, pos.z);
agents[i].head.render();
glPopMatrix();
}
}
//
// Update a single agent with data received from that agent's IP address
//
void update_agent(in_addr addr, char * data, int length)
{
std::cout << "Looking for agent: " << inet_ntoa(addr) << "\n";
for (int i = 0; i < num_agents; i++) {
if (agents[i].sin_addr.s_addr == addr.s_addr) {
std::cout << "Updating agent with: " << data << "\n";
}
}
}
//
// Look for an agent by it's IP number, add if it does not exist in local list
//
@ -68,7 +91,7 @@ int add_agent(std::string * IP) {
//
// Broadcast data to all the other agents you are aware of, returns 1 for success
//
int broadcast(int handle, char * data, int length) {
int broadcast_to_agents(int handle, char * data, int length) {
sockaddr_in dest_address;
dest_address.sin_family = AF_INET;
dest_address.sin_port = htons( (unsigned short) UDP_PORT );
@ -81,7 +104,7 @@ int broadcast(int handle, char * data, int length) {
if (sent_bytes != length) {
std::cout << "Broadcast packet fail!\n";
return 0;
}
} else std::cout << "Broadcasted Packet: " << data << "\n";
}
return 1;
}

View file

@ -19,6 +19,8 @@
void update_agents(char * data, int length);
int add_agent(std::string * IP);
int broadcast(int handle, char * data, int length);
int broadcast_to_agents(int handle, char * data, int length);
void update_agent(in_addr addr, char * data, int length);
void render_agents();
#endif

View file

@ -90,7 +90,8 @@ void Cloud::simulate (float deltaTime) {
field_interact(deltaTime, &particles[i].position, &particles[i].velocity, &particles[i].color, FIELD_COUPLE);
// Update color to velocity
particles[i].color = glm::normalize(particles[i].velocity);
particles[i].color = (glm::normalize(particles[i].velocity)*0.5f);
particles[i].color += 0.5f;
// Bounce or Wrap

206
head.cpp
View file

@ -28,6 +28,7 @@ const float DECAY = 0.1;
Head::Head()
{
position.x = position.y = position.z = 0;
PupilSize = 0.10;
interPupilDistance = 0.6;
interBrowDistance = 0.75;
@ -52,7 +53,6 @@ Head::Head()
void Head::reset()
{
position = glm::vec3(0,0,0);
Pitch = Yaw = Roll = 0;
leanForward = leanSideways = 0;
}
@ -167,123 +167,121 @@ void Head::render()
int side = 0;
glEnable(GL_DEPTH_TEST);
glTranslatef(leanSideways, 0.f, leanForward);
glRotatef(Yaw/2.0, 0, 1, 0);
glRotatef(Pitch/2.0, 1, 0, 0);
glRotatef(Roll/2.0, 0, 0, 1);
// Overall scale of head
glScalef(1.5, 2.0, 2.0);
glColor3fv(skinColor);
// Head
glutSolidSphere(1, 30, 30);
// Ears
glPushMatrix();
glLoadIdentity();
glTranslatef(0.f, 0.f, -7.f);
glTranslatef(leanSideways, 0.f, leanForward);
glRotatef(Yaw/2.0, 0, 1, 0);
glRotatef(Pitch/2.0, 1, 0, 0);
glRotatef(Roll/2.0, 0, 0, 1);
// Overall scale of head
glScalef(1.5, 2.0, 2.0);
glColor3fv(skinColor);
// Head
glutSolidSphere(1, 30, 30);
// Ears
glPushMatrix();
glTranslatef(1, 0, 0);
for(side = 0; side < 2; side++)
{
glPushMatrix();
glScalef(0.5, 0.75, 1.0);
glutSolidSphere(0.5, 30, 30);
glPopMatrix();
glTranslatef(-2, 0, 0);
}
glPopMatrix();
// Eyebrows
glPushMatrix();
glTranslatef(-interBrowDistance/2.0,0.4,0.45);
for(side = 0; side < 2; side++)
{
glColor3fv(browColor);
glPushMatrix();
glTranslatef(0, 0.4, 0);
glRotatef(EyebrowPitch[side]/2.0, 1, 0, 0);
glRotatef(EyebrowRoll[side]/2.0, 0, 0, 1);
glScalef(browWidth, browThickness, 1);
glutSolidCube(0.5);
glPopMatrix();
glTranslatef(interBrowDistance, 0, 0);
}
glPopMatrix();
// Mouth
glPushMatrix();
glTranslatef(0,-0.3,0.75);
glColor3fv(mouthColor);
glRotatef(MouthPitch, 1, 0, 0);
glRotatef(MouthYaw, 0, 0, 1);
glScalef(MouthWidth, MouthHeight, 1);
glutSolidCube(0.5);
glPopMatrix();
glTranslatef(0, 1.0, 0);
glTranslatef(-interPupilDistance/2.0,-0.68,0.7);
// Right Eye
glRotatef(-10, 1, 0, 0);
glColor3fv(eyeColor);
glPushMatrix();
glTranslatef(1, 0, 0);
for(side = 0; side < 2; side++)
{
glTranslatef(interPupilDistance/10.0, 0, 0.05);
glRotatef(20, 0, 0, 1);
glScalef(EyeballScaleX, EyeballScaleY, EyeballScaleZ);
glutSolidSphere(0.25, 30, 30);
glPushMatrix();
glScalef(0.5, 0.75, 1.0);
glutSolidSphere(0.5, 30, 30);
glPopMatrix();
glTranslatef(-2, 0, 0);
}
glPopMatrix();
// Right Pupil
glPushMatrix();
glRotatef(EyeballPitch[1], 1, 0, 0);
glRotatef(EyeballYaw[1] + PupilConverge, 0, 1, 0);
glTranslatef(0,0,.25);
glColor3f(0,0,0);
glutSolidSphere(PupilSize, 15, 15);
glPopMatrix();
// Left Eye
glColor3fv(eyeColor);
glTranslatef(interPupilDistance, 0, 0);
glPushMatrix();
{
glTranslatef(-interPupilDistance/10.0, 0, .05);
glRotatef(-20, 0, 0, 1);
glScalef(EyeballScaleX, EyeballScaleY, EyeballScaleZ);
glutSolidSphere(0.25, 30, 30);
}
glPopMatrix();
// Left Pupil
glPushMatrix();
glRotatef(EyeballPitch[0], 1, 0, 0);
glRotatef(EyeballYaw[0] - PupilConverge, 0, 1, 0);
glTranslatef(0,0,.25);
glColor3f(0,0,0);
glutSolidSphere(PupilSize, 15, 15);
glPopMatrix();
glPopMatrix();
}
// Eyebrows
glPushMatrix();
glTranslatef(-interBrowDistance/2.0,0.4,0.45);
for(side = 0; side < 2; side++)
{
glColor3fv(browColor);
glPushMatrix();
glTranslatef(0, 0.4, 0);
glRotatef(EyebrowPitch[side]/2.0, 1, 0, 0);
glRotatef(EyebrowRoll[side]/2.0, 0, 0, 1);
glScalef(browWidth, browThickness, 1);
glutSolidCube(0.5);
glPopMatrix();
glTranslatef(interBrowDistance, 0, 0);
}
glPopMatrix();
// Mouth
glPushMatrix();
glTranslatef(0,-0.3,0.75);
glColor3fv(mouthColor);
glRotatef(MouthPitch, 1, 0, 0);
glRotatef(MouthYaw, 0, 0, 1);
glScalef(MouthWidth, MouthHeight, 1);
glutSolidCube(0.5);
glPopMatrix();
glTranslatef(0, 1.0, 0);
glTranslatef(-interPupilDistance/2.0,-0.68,0.7);
// Right Eye
glRotatef(-10, 1, 0, 0);
glColor3fv(eyeColor);
glPushMatrix();
{
glTranslatef(interPupilDistance/10.0, 0, 0.05);
glRotatef(20, 0, 0, 1);
glScalef(EyeballScaleX, EyeballScaleY, EyeballScaleZ);
glutSolidSphere(0.25, 30, 30);
}
glPopMatrix();
// Right Pupil
glPushMatrix();
glRotatef(EyeballPitch[1], 1, 0, 0);
glRotatef(EyeballYaw[1] + PupilConverge, 0, 1, 0);
glTranslatef(0,0,.25);
glColor3f(0,0,0);
glutSolidSphere(PupilSize, 15, 15);
glPopMatrix();
// Left Eye
glColor3fv(eyeColor);
glTranslatef(interPupilDistance, 0, 0);
glPushMatrix();
{
glTranslatef(-interPupilDistance/10.0, 0, .05);
glRotatef(-20, 0, 0, 1);
glScalef(EyeballScaleX, EyeballScaleY, EyeballScaleZ);
glutSolidSphere(0.25, 30, 30);
}
glPopMatrix();
// Left Pupil
glPushMatrix();
glRotatef(EyeballPitch[0], 1, 0, 0);
glRotatef(EyeballYaw[0] - PupilConverge, 0, 1, 0);
glTranslatef(0,0,.25);
glColor3f(0,0,0);
glutSolidSphere(PupilSize, 15, 15);
glPopMatrix();
}
// Transmit data to agents requesting it
int Head::transmit(char* data)
int Head::getBroadcastData(char* data)
{
// Copy data for transmission to the buffer, return length of data
sprintf(data, "%f6.2", Pitch);
sprintf(data, "H%f,%f,%f,%f,%f,%f", Pitch, Yaw, Roll, position.x, position.y, position.z);
return strlen(data);
}
void Head::recvBroadcastData(char * data, int size)
{
sscanf(data, "H%f,%f,%f,%f,%f,%f", &Pitch, &Yaw, &Roll, &position.x, &position.y, &position.z);
}
void Head::SetNewHeadTarget(float pitch, float yaw)
{
PitchTarget = pitch;

11
head.h
View file

@ -58,16 +58,19 @@ public:
void setNoise (float mag) { noise = mag; }
void setPitch(float p) {Pitch = p; }
void setYaw(float y) {Yaw = y; }
void SetRoll(float r) {Roll = r; };
void setRoll(float r) {Roll = r; };
void addPitch(float p) {Pitch -= p; }
void addYaw(float y){Yaw -= y; }
void addRoll(float r){Roll += r; }
void addLean(float x, float z);
void getPitch(float);
float getPitch() {return Pitch;}
float getRoll() {return Roll;}
float getYaw() {return Yaw;}
void render();
void simulate(float);
int transmit(char*);
void receive(float);
// Send and receive network data
int getBroadcastData(char* data);
void recvBroadcastData(char * data, int size);
void SetNewHeadTarget(float, float);
glm::vec3 getPos() { return position; };
void setPos(glm::vec3 newpos) { position = newpos; };

View file

@ -302,7 +302,7 @@
"$(OTHER_CFLAGS)",
);
PRODUCT_NAME = interface;
SDKROOT = macosx10.7;
SDKROOT = macosx;
};
name = Debug;
};
@ -327,7 +327,7 @@
"$(OTHER_CFLAGS)",
);
PRODUCT_NAME = interface;
SDKROOT = macosx10.7;
SDKROOT = macosx;
};
name = Release;
};

View file

@ -487,8 +487,10 @@ void update_pos(float frametime)
balls.updateHand(myHead.getPos() + myHand.getPos(), glm::vec3(0,0,0), myHand.getRadius());
// Update all this stuff to any agents that are nearby and need to see it!
char test[] = "BXXX";
broadcast(UDP_socket, test, strlen(test));
const int MAX_BROADCAST_STRING = 200;
char broadcast_string[MAX_BROADCAST_STRING];
int broadcast_bytes = myHead.getBroadcastData(broadcast_string);
broadcast_to_agents(UDP_socket, broadcast_string, broadcast_bytes);
}
void display(void)
@ -542,7 +544,17 @@ void display(void)
// Show field vectors
if (display_field) field_render();
if (display_head) myHead.render();
// Render my own head
if (display_head) {
glPushMatrix();
glLoadIdentity();
glTranslatef(0.f, 0.f, -7.f);
myHead.render();
glPopMatrix();
}
// Render heads of other agents
if (!display_head) render_agents();
if (display_hand) myHand.render();
@ -550,10 +562,10 @@ void display(void)
if (!display_head) balls.render();
// Render the world box
if (!display_head) render_world_box();
if (!display_head && stats_on) render_world_box();
glm::vec3 test(0.5, 0.5, 0.5);
render_vector(&test);
//glm::vec3 test(0.5, 0.5, 0.5);
//render_vector(&test);
glPopMatrix();
@ -581,7 +593,7 @@ void display(void)
sprintf(val, "%d,%d", target_x, target_y);
drawtext(target_x, target_y-20, 0.08, 0, 1.0, 0, val, 0, 1, 0);
}
if (display_head_mouse && !display_head)
if (display_head_mouse && !display_head && stats_on)
{
glPointSize(10.0f);
glColor4f(1.0, 1.0, 0.0, 0.8);
@ -717,7 +729,8 @@ void key(unsigned char k, int x, int y)
void read_network()
{
// Receive packets
int bytes_recvd = network_receive(UDP_socket, incoming_packet, delay);
in_addr from_addr;
int bytes_recvd = network_receive(UDP_socket, &from_addr, incoming_packet, delay);
if (bytes_recvd > 0)
{
packetcount++;
@ -742,11 +755,11 @@ void read_network()
// Message from Spaceserver
//
update_agents(&incoming_packet[1], bytes_recvd - 1);
} else if (incoming_packet[0] == 'B') {
} else if (incoming_packet[0] == 'H') {
//
// Broadcast packet from another agent
//
//std::cout << "Got broadcast from agent\n";
update_agent(from_addr, &incoming_packet[1], bytes_recvd - 1);
}
}
}
@ -850,10 +863,11 @@ int main(int argc, char** argv)
incoming_packet = new char[MAX_PACKET_SIZE];
// Test network loopback
in_addr from_addr;
char test_data[] = "Test!";
int bytes_sent = network_send(UDP_socket, test_data, 5);
if (bytes_sent) printf("%d bytes sent.", bytes_sent);
int test_recv = network_receive(UDP_socket, incoming_packet, delay);
int test_recv = network_receive(UDP_socket, &from_addr, incoming_packet, delay);
printf("Received %i bytes\n", test_recv);
// Load textures

View file

@ -116,7 +116,7 @@ int network_send(int handle, char * packet_data, int packet_size)
return sent_bytes;
}
int network_receive(int handle, char * packet_data, int delay /*msecs*/)
int network_receive(int handle, in_addr * from_addr, char * packet_data, int delay /*msecs*/)
{
int received_bytes = recvfrom(handle, (char*)packet_data, MAX_PACKET_SIZE,
0, (sockaddr*)&dest_address, &fromLength );
@ -149,7 +149,4 @@ int network_receive(int handle, char * packet_data, int delay /*msecs*/)
}
}
}

View file

@ -22,7 +22,7 @@ const int UDP_PORT = 30001;
const char DESTINATION_IP[] = "127.0.0.1";
// Address and port of spaceserver process to advertise other agents
const char SPACESERVER_IP[] = "127.0.0.1";
const char SPACESERVER_IP[] = "192.168.1.16";
const int SPACESERVER_PORT = 40000;
// Randomly send a ping packet every N packets sent
@ -30,7 +30,7 @@ const int PING_PACKET_COUNT = 20;
int network_init();
int network_send(int handle, char * packet_data, int packet_size);
int network_receive(int handle, char * packet_data, int delay /*msecs*/);
int network_receive(int handle, in_addr * from_addr, char * packet_data, int delay /*msecs*/);
timeval network_send_ping(int handle);
int notify_spaceserver(int handle, float x, float y, float z);