diff --git a/include/state.hpp b/include/state.hpp index baa1413..6125e86 100644 --- a/include/state.hpp +++ b/include/state.hpp @@ -34,8 +34,8 @@ class State { vector exposure = {}; uint64_t x; uint64_t y; - uint64_t width; - uint64_t height; + uint64_t w; + uint64_t h; bool active = false; bool ERROR = false; @@ -55,7 +55,7 @@ class State { Mode getMode() { return mode; } string getImage () { return image; } vector getExposure() { return exposure; } - vector getPosition() { return { x, y, width, height }; } + vector getPosition() { return { x, y, w, h }; } bool isError () { return ERROR; } void setActive () { active = true; } bool isActive () { return active; } diff --git a/src/main.cpp b/src/main.cpp index beb90b4..d01bf15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ #include "state.hpp" #ifdef __APPLE__ - #include + #include #include #include #else @@ -25,8 +25,8 @@ using namespace std; const uint16_t PORT = 8081; const uint16_t BUFFER_SIZE = 2048; -GLuint imageTexture; -GLuint blankTexture; +GLuint imageTexture = 0; +GLuint blankTexture = 0; GLint screenWidth = 0; GLint screenHeight = 0; @@ -34,26 +34,24 @@ GLint screenHeight = 0; Image img; State state; - - void actionDisplay () { } void actionLoad () { - string imagePath = state.getImage(); - vector position = state.getPosition(); - Mat image = img.loadImage(imagePath, position[0], position[1], position[2], position[3]); + string imagePath = state.getImage(); + vector position = state.getPosition(); + Mat image = img.loadImage(imagePath, position[0], position[1], position[2], position[3]); glGenTextures(2, &imageTexture); - glBindTexture(GL_TEXTURE_2D, imageTexture); + glBindTexture(GL_TEXTURE_2D, imageTexture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - glTexImage2D(GL_TEXTURE_2D, // Type of texture + glTexImage2D(GL_TEXTURE_2D, // Type of texture 0, // Pyramid level (for mip-mapping) - 0 is the top level GL_RGB, // Internal colour format to convert to image.cols, // Image width i.e. 640 for Kinect in standard mode @@ -69,17 +67,17 @@ void actionStop () { } void loadBlank () { - Mat blank = img.getBlank(); + Mat blank = img.getBlank(); + glGenTextures(1, &blankTexture); - glBindTexture(GL_TEXTURE_2D, blankTexture); + glBindTexture(GL_TEXTURE_2D, blankTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - - glTexImage2D(GL_TEXTURE_2D, // Type of texture + glTexImage2D(GL_TEXTURE_2D, // Type of texture 0, // Pyramid level (for mip-mapping) - 0 is the top level GL_RGB, // Internal colour format to convert to blank.cols, // Image width i.e. 640 for Kinect in standard mode @@ -91,27 +89,32 @@ void loadBlank () { } void display () { - glClear(GL_COLOR_BUFFER_BIT); - glEnable(GL_TEXTURE_2D); - glViewport(0, 0, screenWidth, screenHeight); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); - glBindTexture(GL_TEXTURE_2D, imageTexture); + glClear(GL_COLOR_BUFFER_BIT); + glEnable(GL_TEXTURE_2D); + glViewport(0, 0, screenWidth, screenHeight); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); + glBindTexture(GL_TEXTURE_2D, blankTexture); - glBegin(GL_QUADS); // front face - glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); //bottom right - glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); //top right - glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); //top left - glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); //bottom left - glEnd(); - glDisable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, 0); - glutSwapBuffers(); + glBegin(GL_QUADS); // front face + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); //bottom right + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); //top right + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); //top left + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); //bottom left + glEnd(); + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); + glutSwapBuffers(); //glFlush(); } +void timer(int value) { + glutPostRedisplay(); + glutTimerFunc(1, timer, 0); +} + /** * State Message Variables **/ @@ -133,26 +136,26 @@ condition_variable sendCondVar; void processMessage () { if (receivedMessage) { - cout << "RECEIVED" << endl; - state.receiveMessage(incomingMessage); - receivedMessage = false; + cout << "RECEIVED" << endl; + state.receiveMessage(incomingMessage); + receivedMessage = false; - while (state.isActive()) { - if (state.getAction() == LOAD) { - actionLoad(); - } else if (state.getAction() == DISPLAY) { - actionDisplay(); - } else if (state.getAction() == STOP) { - actionStop(); - } - } + while (state.isActive()) { + if (state.getAction() == LOAD) { + actionLoad(); + } else if (state.getAction() == DISPLAY) { + actionDisplay(); + } else if (state.getAction() == STOP) { + actionStop(); + } + } - if (state.isError()) { - outgoingMessage = state.createMessage(false); - } else { - outgoingMessage = state.createMessage(true); - } - sendMessage = true; + if (state.isError()) { + outgoingMessage = state.createMessage(false); + } else { + outgoingMessage = state.createMessage(true); + } + sendMessage = true; } } @@ -182,28 +185,28 @@ void handleTCPConnection(int clientSocket) { bool localSendMessage; string localOutgoingMessage; - { - unique_lock lock(sendMutex); - sendCondVar.wait(lock, [&] { return sendMessage; }); - localSendMessage = sendMessage; - localOutgoingMessage = outgoingMessage; - } + { + unique_lock lock(sendMutex); + sendCondVar.wait(lock, [&] { return sendMessage; }); + localSendMessage = sendMessage; + localOutgoingMessage = outgoingMessage; + } if (localSendMessage) { - const char* msg = localOutgoingMessage.c_str(); - int bytesSent = send(clientSocket, msg, strlen(msg), 0); - if (bytesSent < 0) { - cerr << "Error sending ACK to client" << endl; - break; - } - } + const char* msg = localOutgoingMessage.c_str(); + int bytesSent = send(clientSocket, msg, strlen(msg), 0); + if (bytesSent < 0) { + cerr << "Error sending ACK to client" << endl; + break; + } + } memset(buffer, 0, BUFFER_SIZE); } } void runTCPServer (int serverSocket) { - while (true) { + while (true) { struct sockaddr_in clientAddr; socklen_t clientAddrLen = sizeof(clientAddr); @@ -220,25 +223,26 @@ void runTCPServer (int serverSocket) { } uint8_t handleArgs (int argc, char** argv) { - if (argc > 1) { + if (argc > 1) { screenWidth = atoi(argv[1]); cout << "Set width: " << screenWidth << endl; } else { - cerr << "Please provide screen width as first argument" << endl; - return 1; + cerr << "Please provide screen width as first argument" << endl; + return 1; } if (argc > 2) { screenHeight = atoi(argv[2]); cout << "Set height: " << screenHeight << endl; } else { - cerr << "Please provide screen height as second argument" << endl; - return 2; + cerr << "Please provide screen height as second argument" << endl; + return 2; } + cout << screenWidth << "x" << screenHeight << endl; return 0; } int main (int argc, char** argv) { - uint8_t argsRet = handleArgs(argc, argv); + uint8_t argsRet = handleArgs(argc, argv); if (argsRet > 0) { return argsRet; @@ -246,15 +250,14 @@ int main (int argc, char** argv) { img.setDimensions( (uint16_t) screenWidth, (uint16_t) screenHeight); - loadBlank(); - glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE); glutCreateWindow("opengl_opencv_example"); glutSetCursor(GLUT_CURSOR_NONE); glutFullScreen(); glutDisplayFunc(display); - glutMainLoop(); + glutTimerFunc(0, timer, 0); + loadBlank(); int serverSocket = socket(AF_INET, SOCK_STREAM, 0); if (serverSocket < 0) { @@ -282,11 +285,12 @@ int main (int argc, char** argv) { cout << "TCP server listening on port " << PORT << endl; thread serverThread(runTCPServer, serverSocket); + + glutMainLoop(); + serverThread.join(); - - close(serverSocket); - return 0; + return 0; } \ No newline at end of file diff --git a/src/state.cpp b/src/state.cpp index e6b988e..9e0af90 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -37,9 +37,9 @@ void State::setMode (json& msgData) { void State::setPosition (json& msgData) { x = msgData["position"]["x"]; y = msgData["position"]["y"]; - width = msgData["position"]["w"]; - height = msgData["position"]["h"]; - cout << "Position[x] = " << x << " [y] = " << y << " [width] = " << width << " [height] = " << height << endl; + w = msgData["position"]["w"]; + h = msgData["position"]["h"]; + cout << "Position[x] = " << x << " [y] = " << y << " [w] = " << w << " [h] = " << h << endl; } void State::setExposure (json& msgData) {