C : display every x milliseconds (OpenGL) - c++

I've got a C / C++ (only main file is .cpp so I can use OpenGL) program, I use OpenGL (GLUT, GLUI) in it. It already displays something but I want it to move every x ms. I render some circles (known speed and coordinates) and I have already made the function that computes it's next position knowing the refresh rate.
I've tried to put my display callback in a timer callback but the program just freezed.
What can I do in order to run the display callback every x ms ?

I've tried to put my display callback in a timer callback but the program just freezed.
Make sure to re-arm the timer in your timer callback otherwise it will only fire once:
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
static float angle = 0;
angle += 1.0f;
glRotatef(angle, 0, 0, 1);
glColor3ub(255,0,0);
glBegin(GL_TRIANGLES);
glVertex2f(0,0);
glVertex2f(10,0);
glVertex2f(10,10);
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(30, timer, 0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("Timer");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}

Related

Make object disappear and appear in OpenGL

#include <stdio.h> // this library is for standard input and output
#include "glut.h" // this library is for glut the OpenGL Utility Toolkit
#include <math.h>
// left square
void drawShape1(void) {
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(82, 250);
glVertex2f(82, 200);
glVertex2f(140, 200);
glVertex2f(140, 250);
glEnd();
}
// right square
void drawShape2(void) {
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(232, 250);
glVertex2f(232, 200);
glVertex2f(290, 200);
glVertex2f(290, 250);
glEnd();
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
// called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawShape1();
drawShape2();
glutSwapBuffers();
glutPostRedisplay();
}
// the timer code
void update(int value) {
// add code here
glutPostRedisplay();
glutTimerFunc(5, update, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Squares");
initRendering();
glutDisplayFunc(display);
glutReshapeFunc(handleResize);
glutTimerFunc(5, update, 0);
glutMainLoop();
return(0);
}
I have two squares in the middle. One square is on the left and the other square is on the right (see screenshot below). I am trying to make the left square disappear/appear every 5 seconds. I have added the timer code, but I am struggling on how to make the object disappear/appear.
Preview:
The unit of the first parameter of glutTimerFunc milliseconds rather than seconds. So a 5 seconds equals the value 5000.
Create a variable (square1_visible) of type bool which states whether the left square is visible:
bool square1_visible = true;
Change the state of the variable square1_visible every 5 seconds in the timer function update:
void update(int value) {
glutTimerFunc(5000, update, 0);
square1_visible = !square1_visible;
}
Draw the left square dependent on the state of the variable square1_visible:
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if ( square1_visible )
drawShape1();
drawShape2();
glutSwapBuffers();
glutPostRedisplay();
}

Glut subwindow toggle

I have a program and i have created a subwindow from my main window. The problem is that its always there, my goal is to press a key and then get that subwindow "on" and when i press it again , to make it disappear. I managed to destroy it with glutDistroyWindow but then i dont know how to make it appear again. Here is my code:
void init(void)
{
// pregatim o scena noua in opengl
if(glutGetWindow() == mainWindow)
glClearColor(0.0, 0.0, 0.0, 0.0);
else
glClearColor(1.0, 1.0, 1.0, 1.0); fereastra
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
}
void reshape2(int w,int h){
glViewport(0,0,(GLsizei) w,(GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(float)w/h,1.0,40.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
init();
}
void reshape(int w, int h)
{
// Main Window
glViewport(0,0, (GLsizei) w, (GLsizei) h);
// calculare aspect ratio ( Width/ Height )
GLfloat aspect = (GLfloat) w / (GLfloat) h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspect, 1.0, 100);
// init context
init();
if(damageWindow != -1)
glutDestroyWindow(damageWindow);
damageWindow=glutCreateSubWindow(mainWindow,0,0,w/5,h/5);
glutDisplayFunc(display);
glutReshapeFunc(reshape2);
glutKeyboardFunc(keyboard);
glutSpecialFunc(keyboard);
glutKeyboardUpFunc(keyboardup);
glutMouseFunc(mouse);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
int w = 800, h= 600;
glutInitWindowSize(w,h);
glutInitWindowPosition(100,100);
// Main window
mainWindow=glutCreateWindow("Tema4 - Asteroid Attack!");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardup);
glutReshapeFunc(reshape);
glutSpecialFunc(keyboard);
glutMouseFunc(mouse);
// Initializeaza scena 3D
initScene();
glutMainLoop();
return 0;
}
Ok so these are the functions that matters. In my keyboard function i want to toggle damageWindow. How do i do that ? I know how to destroy it but i can't seem to make it again.
LE: I keep getting downvotes because people dont really understand the question. The keyboard function is redundant because there is nothing there , thats what im asking you.But for the sake of you guys here it is:
void keyboard(unsigned char ch,int x,int y){
switch(ch){
case 27: exit(0);break;
case 'n':
view_subwindow=!view_subwindow;
if(view_subwindow == false)
glutDestroyWindow(damageWindow);
else{
//here i want to recreate my window DONT KNOW HOW
damageWindow=glutCreateSubWindow(mainWindow,0,0,w/5,h/5);
glutDisplayFunc(display);
glutReshapeFunc(reshape2);
glutKeyboardFunc(keyboard);
glutSpecialFunc(keyboard);
glutKeyboardUpFunc(keyboardup);
glutMouseFunc(mouse);
}
}
}
I don't think you're supposed to create or destroy any windows after you called glutMainLoop. During initialization you should create the subwindow, then afterwards you should just hide it instead, using glutHideWindow, possibly after calling glutSetWindow to make it the current window. To show it again, call glutShowWindow.
Also, you don't need to call functions like glutReshapeFunc or glutKeyboardFunc multiple times, do that just once during initialization. If you need them to do different things depending on a condition, use if's in the functions you passed to them.

Delta zeta not applied by gltranslatef

The program should simulate a planet rotating around another planet.
I use gltranslatef to let the planet move around the bigger planet, but the problem is that the planet should hide when is over the bigger planet, because dz is -0.5.
But if I test the program I always see the red planet over the blue one.
Another problem I have: the planet rotates too fast, how do I slow it?
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include "utility.h"
GLfloat dx=0.0;
GLfloat dz=-0.5;
bool plus=true;
void init()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glEnable(GLUT_DEPTH);
}
void render()
{
glClearColor(BLACK);
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(BLUE);
glutWireSphere(0.25, 100, 100);
glPushMatrix();
glLoadIdentity();
glTranslatef(-0.5+dx, 0.0, -dz);
glColor4f(RED);
glutWireSphere(0.05, 100, 100);
glPopMatrix();
glFlush();
}
void idle()
{
if(plus)
{
dx+=0.05;
}
else
{
dx-=0.05;
}
if(dx>=1.0)
{
dx=0.5;
plus=false;
}
else if(dx<=-0.0)
{
dx=0.0;
plus=true;
}
glutPostRedisplay();
}
int main(int argc, const char * argv[])
{
glutInit(&argc, (char**)argv);
glutInitWindowSize(500, 500);
glutInitWindowPosition(150, 150);
glutInitWindowPosition(0, 0);
glutCreateWindow("Simple");
glutIdleFunc(idle);
init();
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
I haven't understood well how does the idle function work, why it gets called so many times? Can't I choose a time interval with which the idle function gets called?
More info: RED and BLUE are RGB floats, defined in the utility.h header file.
plus is a bool that is used to know if I have to decrease or increase dx.
Give this a shot:
#include <GL/glut.h>
double GetSeconds()
{
return glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
}
void render()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub(0,0,255);
glutWireSphere(0.25, 100, 100);
glPushMatrix();
glLoadIdentity();
static double prv = GetSeconds();
double cur = GetSeconds();
double delta = cur - prv;
prv = cur;
const float DEG_PER_SEC = 60.0f;
static float angle = 0.0f;
angle += DEG_PER_SEC * delta;
while( angle > 360 ) angle -= 360;
glPushMatrix();
glRotatef( angle, 0, 1, 0 );
glTranslatef( 0.5, 0, 0);
glColor3ub(255,0,0);
glutWireSphere(0.05, 100, 100);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main(int argc, const char * argv[])
{
glutInit(&argc, (char**)argv);
glutInitWindowSize(500, 500);
glutInitWindowPosition(150, 150);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Simple");
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutDisplayFunc(render);
glEnable( GL_DEPTH_TEST );
glutMainLoop();
return 0;
}
Important parts:
Explicit glMatrixMode() calls
Calling glutInitDisplayMode() before glutCreateWindow()
Double-buffering requires glutSwapBuffers()
Clearing the depth buffer via GL_DEPTH_BUFFER_BIT
glEnable( GL_DEPTH_TEST )
glRotatef() for planet rotation
Timer-based animation

OpenGL particle system

I'm trying to simulate a particle system using OpenGl but I can't get it to work, this is what I have so far:
#include <GL/glut.h>
int main (int argc, char **argv){
// data allocation, various non opengl stuff
............
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
glutInitWindowPosition(100,100);
glutInitWindowSize(size, size);
glPointSize (4);
glutCreateWindow("test gl");
............
// initial state, not opengl
............
glViewport(0,0,size,size);
glutDisplayFunc(display);
glutIdleFunc(compute);
glutMainLoop();
}
void compute (void) {
// change state not opengl
glutPostRedisplay();
}
void display (void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for(i = 0; i<nparticles; i++) {
// two types of particles
if (TYPE(particle[i]) == 1) glColor3f(1,0,0);
else glColor3f(0,0,1);
glVertex2f(X(particle[i]),Y(particle[i]));
}
glEnd();
glFlush();
glutSwapBuffers();
}
I get a black window after a couple of seconds (the window has just the title bar before that). Where do I go wrong?
LE: the x and y coordinates of each particle are within the interval (0,size)
Try to make these changes in your code:
move the Main function at the end of the file
glPoinSize call belongs to the Display function
then you should provide a function to handle resizing of the window glutReshapeFunc(reshape), something like this
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
glFlush is called from glutSwapBuffers function so you don't need it there
insert this code (after glutCreateWindow call) to set the initial position for the projection
glClearColor(0.2, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 10, 0.0, 10, -1.0, 1.0);

Resizing glut window

I'm doing 2D drawing in a glut window, and I'm having trouble making the window resize properly.
My glutDisplayFunc and glutReshapeFunc are below. As it is, the drawing disappears when the window is resized. If I delete the glClear() from displayFunc(), new pixels don't "initialize" and there are clipping problems. How do I fix this?
glutDisplayFunc:
void displayFunc() {
glDisable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT );
glPointSize ( 3.0 );
glFlush();
}
glutReshapeFunc:
void windowReshapeFunc( GLint newWidth, GLint newHeight ) {
glViewport( 0, 0, newWidth, newHeight );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, GLdouble (newWidth), 0, GLdouble (newHeight) );
windowWidth = newWidth;
windowHeight = newHeight;
}
I'd try adding a call to glutPostRedisplay() around the end of your reshape function.
This is basic template that I use...
#include <GL/glut.h>
#include <stdio.h>
#include <stdarg.h>
#define WINDOW_WIDTH 300
#define WINDOW_HEIGHT 300
// current Window width and height
int win_width, win_height;
void display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
// write your code here
//
//
glutSwapBuffers();
}
void on_resize(int w, int h)
{
win_width = w;
win_height = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w / 2, w / 2, -h / 2, h / 2, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
display(); // refresh window.
}
void main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("basic resize template");
glutDisplayFunc(display);
glutReshapeFunc(on_resize);
glutMainLoop();
}
You can not call gluOrtho2D twice with the same window, it will break the graphics and so nothing shows up. You would have to init window size and set Ortho to the size of the screen (current display resolution), then draw relative to the size of the window.
I guess your code does not draw everything on scene in display func, you see, if no events occcur ever you have to call display one time and in the first time it has your drawing. But your problem rises when there is an event which says the window is resized! try putting your drawing part in display function. Like so,
void displayFunc() {
glDisable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT );
glPointSize ( 3.0 );
glBegin(GL_POINTS);
{
//Blah blah blah some points here which you expect to draw
}
glEnd();
glFlush();
}
Please post the full code if this was not helpful.
You're not setting the matrix mode back to GL_MODELVIEW at the end of your reshape function.
void reshape(int width, int height) {
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, GLdouble (width), 0, GLdouble (height) );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
It's hard to say if there's something else in your display function without seeing more code. I hope that helps.