Hi everyone,
I’m working on a project where I need to integrate OpenGL code into the JUCE framework. I’ve only worked with the older version of OpenGL (using functions like glBegin()
, glVertex()
, and gluPerspective()
), but JUCE uses modern OpenGL, and I’m struggling to understand how to modify my code to fit into this.
I’ve read the JUCE OpenGL tutorial, but I’m still very lost on how to make the transition. I have some specific code that renders a simple guitar pick and responds to mouse input to rotate the view and move the pick on screen.
Here’s my original code:
#define _USE_MATH_DEFINES
#include <GL/glut.h>
#include <iostream>
void SetupRC(void);
void RenderScene(void);
void TimerFunc(int value);
void ChangeSize(int w, int h);
void Mouse(int button, int state, int x, int y);
void MouseMotion(int x, int y);
// Window Dimensions
const int INI_WINDOW_WIDTH = 1500;
const int INI_WINDOW_HEIGHT = 750;
const float INI_WINDOW_DEPTH = 1000.0f;
const float DEPTH = 600.0f;
// Pick Dimensions
const float PICK_WIDTH = 27.94; // Pick width (mm)
const float PICK_LENGTH = 32; // Pick length (mm)
const float PICK_THICKNESS = 0.73; // Pick thickness (mm)
// Guitar Dimensions
const float SCALE_LENGTH = 647.7f; // Scale length (mm)
// Mouse Motion
bool mouseLeftDown;
bool mouseRightDown;
float mouseX;
float mouseY;
float preMouseY; // Previous mouse Y coordinate
float rotateX;
float rotateY;
float cameraAngleX = 0.0f;
float cameraAngleY = 0.0f;
float cameraDistance;
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(INI_WINDOW_WIDTH, INI_WINDOW_HEIGHT);
glutCreateWindow("Guitar Simulator");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutMouseFunc(Mouse);
glutMotionFunc(MouseMotion);
glutTimerFunc(1, TimerFunc, 1);
SetupRC();
glutMainLoop();
return 0;
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f, 0.0f, -DEPTH);
glRotatef(cameraAngleX, 1, 0, 0);
glRotatef(cameraAngleY, 0, 1, 0);
glTranslatef(-SCALE_LENGTH / 2.0f, 0.0f, 0.0f);
// Pick
glPushMatrix();
glColor3f(1.0f, 0.0f, 0.0f); // red
glBegin(GL_TRIANGLES);
glVertex3f(mouseX, mouseY, 0.0f); // Tip of the pick
glVertex3f(mouseX - PICK_WIDTH / 2, mouseY - 10.0f, PICK_LENGTH); // One end of the base
glVertex3f(mouseX + PICK_WIDTH / 2, mouseY - 10.0f, PICK_LENGTH); // The other end of the base
glEnd();
glPopMatrix();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void SetupRC(void)
{
// Antialiasing
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
// Set material properties
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// Background
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White
}
void TimerFunc(int value)
{
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(1, TimerFunc, 1);
}
void ChangeSize(int w, int h)
{
GLfloat aspectRatio;
// Prevent a divide by zero
if (h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish perspective projection
aspectRatio = (GLfloat)w / (GLfloat)h;
gluPerspective(45.0, aspectRatio, 1.0, INI_WINDOW_DEPTH);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Mouse(int button, int state, int x, int y)
{
if (button == GLUT_RIGHT_BUTTON)
{
rotateX = x;
rotateY = y;
if (state == GLUT_DOWN)
mouseRightDown = true;
else if (state == GLUT_UP)
mouseRightDown = false;
}
else if (button == GLUT_LEFT_BUTTON)
{
mouseX = (float)x / glutGet(GLUT_WINDOW_WIDTH) * SCALE_LENGTH;
mouseY = -(float)y + glutGet(GLUT_WINDOW_HEIGHT) / 2.0f;
if (state == GLUT_DOWN)
mouseLeftDown = true;
else if (state == GLUT_UP)
mouseLeftDown = false;
}
}
void MouseMotion(int x, int y)
{
if (mouseRightDown)
{
cameraAngleY += (x - rotateX) * 0.1f;
cameraAngleX += (y - rotateY) * 0.1f;
rotateX = x;
rotateY = y;
}
else if (mouseLeftDown)
{
mouseX = (float)x / (float)glutGet(GLUT_WINDOW_WIDTH) * SCALE_LENGTH;
mouseY = -(float)y + (float)glutGet(GLUT_WINDOW_HEIGHT) / 2.0f;
preMouseY = mouseY;
}
}