[SDL] SDL OpenGL rotating cube (where is it?)

Serjan Pruis serjan at quicknet.nl
Mon Apr 2 12:31:28 PDT 2007


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;
}



More information about the SDL mailing list