Advertisement

OpenGL & SDL & C++ Problems (beginner)

Started by October 15, 2004 05:13 PM
2 comments, last by @XR@ 20 years, 4 months ago
Hi i'm trying to use SDL for OpenGL applications and i having problems with some code the code is the following: WinOpenGL.h #ifndef WINOPENGL_H #define WINOPENGL_H #include <GL/gl.h> #include <GL/glu.h> #include <SDL/SDL.h> #include <iostream> #include <string> using namespace std; #define TRUE 1 #define FALSE 0 #define BOOL int class WinOpenGL { public: WinOpenGL(string title, int width, int height, int bits, BOOL fullscreenflag); //virtual ~WinOpenGL(); ~WinOpenGL(); GLvoid KillGLWindow(GLvoid); BOOL CreateGLWindow(string title, int width, int height, int bits, BOOL fullscreenflag); private: int InitGL(GLvoid); void ReSizeGLScene(GLsizei width, GLsizei height); }; #endif // WINOPENGL_H ================================ WinOpenGL.cpp #include "WinOpenGL.h" WinOpenGL::WinOpenGL(string title, int width, int height, int bits, BOOL fullscreenflag) { if(!CreateGLWindow(title, width, height, bits, fullscreenflag)) { cerr << "OOPS problems to create the window trty again" << endl; KillGLWindow(); exit(1); } } WinOpenGL::~WinOpenGL() { KillGLWindow(); } void WinOpenGL::ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { if (height==0) // Prevent A Divide By Zero By { height=1; // Making Height Equal One } glViewport(0,0,width,height); // Reset The Current Viewport glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix //Estabelece a janela de seleção (left, right, bottom, top) if (width <= height) gluOrtho2D (0.0f, 250.0f, 0.0f, 250.0f*height/width); else gluOrtho2D (0.0f, 250.0f*width/height, 0.0f, 250.0f); // Calculate The Aspect Ratio Of The Window //gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); } int WinOpenGL::InitGL(GLvoid) // All Setup For OpenGL Goes Here { glEnable(GL_TEXTURE_2D); // Enable Texture Mapping glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.00f, 0.00f, 0.00f, 0.0f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations return TRUE; } GLvoid WinOpenGL::KillGLWindow(GLvoid) // Properly Kill The Window { SDL_Quit(); } BOOL WinOpenGL::CreateGLWindow(string title, int width, int height, int bits, BOOL fullscreenflag) { Uint32 flags; int size; const SDL_VideoInfo* info = NULL; /* Initialize SDL */ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { cerr << "Couldn't init SDL: "<< SDL_GetError() << endl; return FALSE; } flags = SDL_OPENGL; if ( fullscreenflag ) { flags |= SDL_FULLSCREEN; } /* Let's get some video information. */ info = SDL_GetVideoInfo( ); if( !info ) { /* This should probably never happen. */ fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) ); KillGLWindow(); } //SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); //SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); //SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 32 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 1 ); if ( SDL_SetVideoMode(width, height, info->vfmt->BitsPerPixel, flags) == NULL ) { return FALSE; } SDL_GL_GetAttribute( SDL_GL_STENCIL_SIZE, &size); ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen if (!InitGL()) // Initialize Our Newly Created GL Window { KillGLWindow(); // Reset The Display return FALSE; // Return FALSE } return TRUE; // Success } ========================= Quadrado.h #ifndef QUADRADO_H #define QUADRADO_H #include <GL/gl.h> #include <GL/glu.h> #include <SDL/SDL.h> #include <iostream> using namespace std; class Quadrado { public: Quadrado(); ~Quadrado(); void draw(); }; #endif // QUADRADO_H =============== Quadrado.cpp #include "Quadrado.h" Quadrado::Quadrado() {} Quadrado::~Quadrado() {} void Quadrado::draw() { cout << "Começou o draw" << endl; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Limpa a janela de visualização com a cor de fundo especificada glClear(GL_COLOR_BUFFER_BIT); // Especifica que a cor corrente é vermelha // R G B glColor3f(1.0f, 0.0f, 0.0f); // Desenha um quadrado preenchido com a cor corrente glBegin(GL_QUADS); glColor3f(1.0f, 0.0f, 0.0f); glVertex2i(100,150); glVertex2i(100,100); // Especifica que a cor corrente é azul glColor3f(0.0f, 0.0f, 1.0f); glVertex2i(150,100); glVertex2i(150,150); glEnd(); // Executa os comandos OpenGL glFlush(); SDL_GL_SwapBuffers(); cout << "Fez o draw() do quadrado" << endl; } ======= Finally main.cpp #include "WinOpenGL.h" // Include the Cone3D Basecode #include "Quadrado.h" int main(int argc, char *argv[]) { Uint8* keys; int done=FALSE; // We aren't done yet, are we? WinOpenGL *win; Quadrado *quad; win = new WinOpenGL("Quadrado Basecode", 640, 480, 32, FALSE); quad = new Quadrado(); SDL_ShowCursor(0); // This is the main loop for the entire program and it will run until done==TRUE while(!done) { // Draw the scene quad->draw(); // And poll for events SDL_Event event; while ( SDL_PollEvent(&event) ) { switch (event.type) { // If a quit event was recieved case SDL_QUIT: // then we're done and we'll end this program done=TRUE; break; default: break; } } // Get the state of the keyboard keys keys = SDL_GetKeyState(NULL); // and check if ESCAPE has been pressed. If so then quit if(keys[SDLK_ESCAPE]) done=TRUE; } // Kill the GL & SDL screens win->KillGLWindow(); // And quit return 0; } ============= But, the program compile and execute but i'm not seen anithyng on screen, what could be wrong ?? What i must or supposed to do to this codee work Thanks && Regards for any Help.
Thanks && RegardsLuís Vitório Cargnini
I can't really tell exactly what the problem is, but perhaps one of these points can help you.
1. if your doing 2D rendering you should probobly not be doing depth testing, it's sort of useless.

2. allthough the near/far value defaults to 0 and 1 respectivley, you should set them to another value 0.1 and 100 is good.
(if you only want to do 2d rendering then perhaps -10 and 10 will work to)

3. it's never wise to render something at depth 0, instead render it some distance away by using something like this.
glVertex3i(???,???,0.5);
it shouldn't really matter, but sometimes it does.

tip: this is a great openGL link, it contains documentation on all that openGL 1.1 can do.
Advertisement
I don't see anywhere that you are moving into the scene to render, or calling glLookAt to reposition the camera. My guess is that you are rendering at (0,0,0) which is not visible. Do a glTranslate() to move into the scene before rendering your quad.
OK people i'll remak the code all again !
thanks for the help.
Thanks && RegardsLuís Vitório Cargnini

This topic is closed to new replies.

Advertisement