SDL OpenGL rotating cube (where is it?)

Hello,

I’ve got some code and then changed it just for testing.
So now I’ve got textures on a rotating cube (if you like to test you must
create a texture.bmp (that’s for example 1024x1024 or 512x512)).
I can also rotate it using ‘a’ and ‘d’.
What I want to know is how to know when a full image (a cube side) is on the
screen and so that I can stop rotating for a short period of time on an
image. And after a while continue rotating.

Thanks for any suggestions,
Serjan.-------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL.h"
GLint texture;
GLfloat rotate = 0.0f;
int LoadTexture(char* filename)
{
/* Eine Bitmap laden /
SDL_Surface
img;
img = SDL_LoadBMP(“texture.bmp”);
if (!img) {
printf(“Error: %s\n”, SDL_GetError());
exit(1);
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, img->w, img->h, 0, GL_BGR,
GL_UNSIGNED_BYTE, img->pixels);
return 0;
}

int DrawCube()
{
glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);

/* Vorderseite (1, 2, 3, 4) */
glTexCoord2f(-1.0f, -1.0f);glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(-1.0f, 0.0f);glVertex3f(-1.0f,-1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);glVertex3f( 1.0f,-1.0f, 1.0f);
glTexCoord2f(0.0f, -1.0f);glVertex3f( 1.0f, 1.0f, 1.0f);

/* R?ckseite (5, 6, 7, 8) */
glTexCoord2f(1.0f, -1.0f);glVertex3f(-1.0f, 1.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(0.0f, 0.0f);glVertex3f( 1.0f,-1.0f,-1.0f);
glTexCoord2f(0.0f, -1.0f);glVertex3f( 1.0f, 1.0f,-1.0f);

/* Linke Seite (1, 2, 6, 5) */
glTexCoord2f(1.0f, -1.0f);glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);glVertex3f(-1.0f,-1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(0.0f, -1.0f);glVertex3f(-1.0f, 1.0f,-1.0f);

/* Rechte Seite (4, 3, 7, 8) */
glTexCoord2f(-1.0f, -1.0f);glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(-1.0f, 0.0f);glVertex3f( 1.0f,-1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);glVertex3f( 1.0f,-1.0f,-1.0f);
glTexCoord2f(0.0f, -1.0f);glVertex3f( 1.0f, 1.0f,-1.0f);

/* Decke (5, 1, 4, 8) */
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);

/* Boden (6, 2, 3, 7) */
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);

glEnd();
return 0;
}

int main(int argc, char** argv)
{
SDL_Event event;
/* SDL initiieren */
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
printf(“Error: %s\n”, SDL_GetError());
exit(1);
}

atexit(SDL_Quit);

if (!SDL_SetVideoMode(1024, 768, 32, SDL_OPENGL)) {
printf(“Error: %s\n”, SDL_GetError());
exit(1);
}

SDL_WM_SetCaption(“MeinFenster”, “MeinFenster”);

/* OpenGL initiieren */
LoadTexture(“texture.bmp”);

/* OpenGL initiieren */
glViewport(0, 0, 1024, 768);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
//hier geeft 1024x768 een verhouding aan!
//als je b.v. 1 invult worden vierkanten niet vierkant maar rechthoekig.
//gluPerspective(45.0f, (GLfloat) 1024/768, 0.1, 100.0);
// gluPerspective(45.0f, (GLfloat) 1, 0.1, 100.0);
gluPerspective(45.0f, (GLfloat) 1, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//glTranslatef(0.0f, 0.0f, -10.0f);
glTranslatef(0.0f, 0.0f, -3.4f);

while(1) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawCube();
SDL_GL_SwapBuffers();
// glRotatef(0.1f, 1.0f, 1.0f, 0.0f);
// glRotatef(-0.1f, 0.0f, 1.0f, 0.0f);
glRotatef(rotate, 0.0f, 1.0f, 0.0f);
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT: exit(0);
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
exit(0);
if (event.key.keysym.sym == SDLK_d)
// hier bepaald rotate de rotatie snelheid!
rotate += 0.1f;
printf(“rotate = %f\n”, rotate);
if (event.key.keysym.sym == SDLK_a)
rotate -= 0.1f;
printf(“rotate = %f\n”, rotate);
}
}
}

SDL_Delay(2000);
return 0;
}

> What I want to know is how to know when a full image (a cube side) is on the > screen and so that I can stop rotating for a short period of time on an > image. And after a while continue rotating.

Here’s how I would do it. This is off the cuff, I don’t have a
compiler handy at the moment.

  1. Keep track of which direction the user wants it to go. One
    direction will add to “rotate”, the other will subtract from it, and a
    third, “pause”, will hold it still (add 0.0 instead of 0.1). An enum
    might be useful for this, with a “left, right, pause” value set.

  2. Keep your rotation values small enough to work with. glRotatef()
    takes degrees, so you want to clamp the value between 0.0 and 360.0.
    If it goes below 0.0, add 360.0 to it, and if it goes above 360.0,
    subtract 360.0. Only do this when you actually modify the value of
    "rotate".

  3. Now, if “rotate” is a multiple of 90 then you should have a face
    towards the camera.

  4. If you want user pause/move, simply keep track of the last
    direction requested by the user, and add the corresponding value to
    rotate. When the user pauses rotation, you’d be adding 0 to the
    rotation angle, effectively holding it still.

If you want pause/unpause on user command, you’ll need another
variable to store last requested direction. This way, when user
pauses, you store current movement direction and pause rotation. Then
when user unpauses you can go back to the last requested direction.

If you want auto-pause when a face is towards the camera, you’ll
either need an unpause keypress or you’ll need a timer. With this, you
watch the rotation angle every time it’s updated. If it’s 0, 90, 180,
270, or 360 you pause rotation as above, and set your timer to however
long you want it to stay paused. When the timer ends, restore the last
requested direction and it will rotate until the next face is towards
the camera.

HTH,

-JustinOn 4/2/07, Serjan Pruis wrote: