I am using Legacy OpenGL to make a fish tank:
#include <GL/glut.h>
#include <stdio.h>
float fishPosX = 0.0f;
float fishDirection = -1.0f;
float rotationAngle = 0.0f;
int rotating = 0;
void setupGraphics() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glDisable(GL_CULL_FACE); // Disable back-face culling for debugging
glDisable(GL_LIGHTING); // Disable lighting temporarily for visibility
glClearColor(0.0, 0.0, 0.0, 1.0); // Black background for contrast
glLineWidth(3.0); // Increase wireframe visibility
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
printf("Rendering frame. Fish position: %f, Rotation: %f\n", fishPosX, rotationAngle);
// Draw large fish
glPushMatrix();
glTranslatef(fishPosX, 0.0f, -400.0f);
glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f);
glutWireOctahedron();
glPopMatrix();
glutSwapBuffers();
}
void update(int value) {
if (!rotating) {
fishPosX += 5.0f * fishDirection;
if (fishPosX < -396.0f || fishPosX > 396.0f) {
rotating = 1;
}
} else {
rotationAngle += 5.0f * fishDirection;
if (rotationAngle >= 180.0f || rotationAngle <= -180.0f) {
rotationAngle = 0.0f;
fishDirection *= -1.0f;
rotating = 0;
}
}
glutPostRedisplay();
glutTimerFunc(50, update, 0);
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400, 400, -400, 400, -900, -100); // Ensure correct projection setup
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y) {
if (key == 'q' || key == 'Q') {
printf("Exiting simulation.\n");
exit(0);
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glutCreateWindow("Fish Tank Simulation");
setupGraphics();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutTimerFunc(50, update, 0);
glutMainLoop();
return 0;
}
It seems the fish are being created but I just cannot see them. What is causing my fish to not be displayed in the canvas? Maybe it's a camera positioning issue?
I have tried using transforms to move the camera, didn't work. I have tried altering colors, didn't work. I tried gluLookAt, which errors out the program for some reason. I tried displaying a green square to the middle of the screen, and when that didn't work I realized it may be a camera placement issue or something of the sort.