opengl_timing_example/main.cpp

161 lines
4.0 KiB
C++

#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/gl3.h>
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <string>
#include <arpa/inet.h>
#include <unistd.h>
#include "json/single_include/nlohmann/json.hpp"
#include <mutex>
#include <thread>
#include <condition_variable>
using namespace std;
using namespace std::chrono;
using namespace nlohmann;
volatile int DISPLAY_DURATION = 2000;
const int PORT = 8081;
steady_clock::time_point startTime;
bool shouldDisplayQuad = false;
mutex quadMutex;
condition_variable quadCondVar;
void displayQuad() {
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glViewport(0, 0, 500, 500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
auto currentTime = steady_clock::now();
auto elapsedTime = duration_cast<milliseconds>(currentTime - startTime).count();
bool localShouldDisplayQuad;
{
unique_lock<mutex> lock(quadMutex);
quadCondVar.wait(lock, [&] { return shouldDisplayQuad; });
localShouldDisplayQuad = shouldDisplayQuad;
}
if (localShouldDisplayQuad && elapsedTime < DISPLAY_DURATION) {
glColor3f(1.0f, 0.0f, 0.0f);
} else {
glColor3f(0.0f, 0.0f, 0.0f);
}
glBegin(GL_QUADS);
glVertex2f(-0.5f, -0.5f);
glVertex2f(-0.5f, 0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
glutSwapBuffers();
}
void timer(int value) {
glutPostRedisplay();
glutTimerFunc(1, timer, 0);
}
void handleTCPConnection(int serverSocket) {
struct sockaddr_in clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket < 0) {
cerr << "Error accepting client connection" << endl;
return;
}
char buffer[1024] = {0};
int bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
if (bytesRead < 0) {
cerr << "Error reading from client socket" << endl;
close(clientSocket);
return;
}
string jsonString(buffer, bytesRead);
try {
json jsonData = json::parse(jsonString);
cout << "Received JSON: " << jsonData << endl;
if (jsonData.contains("rgb")) {
DISPLAY_DURATION = jsonData["rgb"];
cout << "Set exposure time to " << DISPLAY_DURATION << "ms" << endl;
}
{
lock_guard<mutex> lock(quadMutex);
shouldDisplayQuad = true;
startTime = steady_clock::now();
quadCondVar.notify_all();
}
} catch (const exception& e) {
cerr << "Error parsing JSON: " << e.what() << endl;
}
close(clientSocket);
}
void runTCPServer(int serverSocket) {
while (true) {
handleTCPConnection(serverSocket);
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Red Quad");
glutDisplayFunc(displayQuad);
glutTimerFunc(0, timer, 0);
startTime = steady_clock::now();
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0) {
cerr << "Error creating socket" << endl;
return 1;
}
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
int bindResult = ::bind(serverSocket, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr));
if (bindResult < 0) {
std::cerr << "Error binding socket" << std::endl;
close(serverSocket);
return 1;
}
if (listen(serverSocket, 5) < 0) {
cerr << "Error listening on socket" << endl;
return 1;
}
cout << "TCP server listening on port " << PORT << endl;
thread serverThread(runTCPServer, serverSocket);
glutMainLoop();
serverThread.join();
close(serverSocket);
return 0;
}