Basic OpenGL

Started by
0 comments, last by godmodder 17 years, 12 months ago
How do i "zoomout" on this code?

#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <windows.h>
#include <gl/gl.h>
#include <gl\glu.h>			// Header File For The GLu32 Library

// SDL Events
SDL_Event event;

//Quit flag
int intDone = 0;

// FPS counter
float intThisFrame;
float intLastFrame;
int intTmpFPS;
int intFPS;

//The thread that will be used
SDL_Thread *thread = NULL;

GLfloat	fltR = 0;
GLfloat fltX = 0;
GLfloat fltY = 0;
GLfloat fltZ = 0;

int my_thread( void *data ) {
  SDL_SetVideoMode(640, 480, 0, SDL_OPENGL | SDL_HWSURFACE);
  glViewport(0, 0, 640, 480);
  glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  glClearDepth(1.0f);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_SMOOTH);
  glMatrixMode(GL_PROJECTION);
	glMatrixMode(GL_MODELVIEW);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  //While the program is not over
  while(intDone == 0) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();

      glTranslatef(fltX,fltY,fltZ);
      glRotatef(fltR, 0.0f, 1.0f, 0.0f);
      glBegin(GL_TRIANGLES);
    		glColor3f(1.0f,0.0f,0.0f);			// Red
		    glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Front)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
		    glVertex3f(-1.0f,-1.0f, 1.0f);			// Left Of Triangle (Front)
		    glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f( 1.0f,-1.0f, 1.0f);			// Right Of Triangle (Front)
		    glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Right)
		    glColor3f(0.0f,0.0f,1.0f);			// Blue
		    glVertex3f( 1.0f,-1.0f, 1.0f);			// Left Of Triangle (Right)
     		glColor3f(0.0f,1.0f,0.0f);			// Green
		    glVertex3f( 1.0f,-1.0f, -1.0f);			// Right Of Triangle (Right)
		    glColor3f(1.0f,0.0f,0.0f);			// Red
		    glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Back)
		    glColor3f(0.0f,1.0f,0.0f);			// Green
		    glVertex3f( 1.0f,-1.0f, -1.0f);			// Left Of Triangle (Back)
		    glColor3f(0.0f,0.0f,1.0f);			// Blue
		    glVertex3f(-1.0f,-1.0f, -1.0f);			// Right Of Triangle (Back)
    		glColor3f(1.0f,0.0f,0.0f);			// Red
		    glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Left)
		    glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f(-1.0f,-1.0f,-1.0f);			// Left Of Triangle (Left)
		    glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f(-1.0f,-1.0f, 1.0f);			// Right Of Triangle (Left)
      glEnd();
    	intThisFrame = SDL_GetTicks();
      intTmpFPS++;
      if ((intThisFrame - intLastFrame) > 1000) {
        intFPS = intTmpFPS;
        intTmpFPS = 0;
        intLastFrame = intThisFrame;
      }
      char tmpStr[12];
      sprintf(tmpStr, "FPS: %d", intFPS);

      SDL_WM_SetCaption(tmpStr, NULL);
      SDL_GL_SwapBuffers();
  }
    return 0;
}

int main(int Arg_N, char ** Arg_V) {
  Uint8* keys;
  SDL_Event event;
  SDL_Init(SDL_INIT_VIDEO);

  intLastFrame = SDL_GetTicks();
  //Create and run the thread
  thread = SDL_CreateThread( my_thread, NULL );
  while (intDone == 0) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        intDone = 1;
      }
      if (event.type == SDL_KEYDOWN) {
        if (event.key.keysym.sym == SDLK_ESCAPE) {
          intDone = 1;
        }
      }
    }
    keys = SDL_GetKeyState(NULL);
    if ( keys[SDLK_UP] ) { fltY -= 0.01f; }
    if ( keys[SDLK_DOWN] ) { fltY += 0.01f; }
    if ( keys[SDLK_LEFT] ) { fltX += 0.01f; }
    if ( keys[SDLK_RIGHT] ) { fltX -= 0.01f; }
    if ( keys[SDLK_HOME] ) { fltZ += 0.01f; }
    if ( keys[SDLK_END] ) { fltZ -= 0.01f; }
    if ( keys[SDLK_PAGEUP] ) { fltR += 0.01f; }
    if ( keys[SDLK_PAGEDOWN] ) { fltR -= 0.01f; }
    
  }
  SDL_Delay(1);
  //Stop the thread
  SDL_KillThread(thread);
  SDL_Quit();
  return(0);
}

Advertisement
- Use a glPushMatrix() before your glBegin() and a glPopMatrix() after your glEnd()

- Use glTranslatef() to zoom out in the z direction e.g. glTranslatef(0,0,-2);

This topic is closed to new replies.

Advertisement