Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problematic Polygons

Started by Kyretzn Dec 13, 2005 at 2:35 PM 2 replies 850+ views
Original Post
Kyretzn
Kyretzn
Hey all. Ive just started my journey into OpenGL programming. Im running on Mac OS X. anywho, Using just mac OS X GL stuff, I can draw polygons and load bmps and rotate and stuff... But trying to add this functionality to my existing project is not going well. It uses SDL - and ive enabled OpenGL through SDL. Anyways, the screen clears ... and such.. but... polygons arent drawing. you can get the source here : http://homepages.ihug.co.nz/~scroodle/sb_game.cpp This is just a tiny bit of my program. and all the old 2D SDL systems I wrote have been deprecated and removed. Anyways. If anyone can tell me why the quad im trying to draw, wont draw, itd make me an incredibly happy man! ~kyretzn
llvllatrix
llvllatrix
Find comments embeaded in your code. I haven't compiled it so there are probably still problems in the code but I think I've addressed the majority.

#include "sb_game.h"SDL_Surface* theScreen;SDL_Event theEvent;int eventOnlyExit(){    // Main program loop    int terminate = 0;    while(terminate != 1)    {        // Check for quit event.        // Your program's main loop basically stalls here. It continously searches for a quit event and never even gets to the draw code (technically it gets there once right after you quit). Instead try something like what i've listed below.        /*while(SDL_PollEvent(&theEvent))        {            switch(theEvent.type)            {                case SDL_QUIT:                 {                    terminate = 1;                    break;                }            }        }*/        // Process messages.        SDL_PollEvent(&theEvent);        switch(theEvent.type)        {            case SDL_QUIT:             {                    terminate = 1;                    // This should apply to the main program loop, so the draw code isn't called.                    continue;            }        }        // Draw the gl quad.                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        glLoadIdentity();        // Even with your event problems sorted you still wont see the quad because it is at 0.0 on the z axis and your camer's front clipping plane is at 0.1. A simple translation should solve this problem.        glTranslatef(0.0f,0.0f,-1.0f);                // For clarity, coloring code should go outside the Begin/End calls. This way you make a clear distinction between models that have an overall color and models that have color per vertex.        glColor3f(1.0f, 0.0f, 0.0f);        glBegin(GL_QUADS);            glVertex3f(0.0f, 0.0f, 0.0f);            glVertex3f(0.0f, 1.0f, 0.0f);            glVertex3f(1.0f, 1.0f, 0.0f);            glVertex3f(1.0f, 0.0f, 0.0f);            // This vertex isn't necessary; gl quads knows the forth vertex is the last.            // glVertex3f(0.0f, 0.0f, 0.0f);        glEnd();        SDL_GL_SwapBuffers();    } // while(terminate != 1)    return 0;}int programStart(){    eventOnlyExit();    return 0;}int program(){    // Just show the terminal a friendly greeting. :)    printf("Welcome to the game!\r\n--------------------------\r\n");        // Initialize SDL    printf("PROGRAM() - Bringing up Graphic frontend...");        SDL_Init(SDL_INIT_VIDEO);     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );    theScreen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE);    printf(" done!\r\n");    // Setup Opengl    glShadeModel(GL_SMOOTH);    glClearColor(1.0f, 1.0f, 0.0f, 0.0f);    glClearDepth(1.0f);    glEnable(GL_DEPTH_TEST);    glDepthFunc(GL_LEQUAL);    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Setup viewport    glViewport(0, 0, 800, 600);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluPerspective(45.0f, ((GLfloat)800.0/(GLfloat)600), 0.1f, 100.0f);    glMatrixMode(GL_MODELVIEW);        // Run the program.    programStart();    // Deinitialize SDL    SDL_Quit();}


Getting something to show up in a new gl app properly is always tricky. If you have any other questions feel free to ask.

Edit: Removed an extra bracket.

Good luck,
- llvllatrix

[Edited by - llvllatrix on December 13, 2005 6:31:52 PM]
Kyretzn
Kyretzn
I found the problem...
It wasnt the event handler, eithter.

The problem was I never set the camera position..


anyways, Ive got a new problem now.
that im tryin gto solve.

Its to do with Texture mapping.

The code in the function is torn from NeHes tutorial.

Her is the code :
(Id put it in a boxy thing like you did, but I dont know how.)

Also, the problem isnt that the polygon wont draw - its that its totally white... and there is no texture.
Also, The image Im loading IS present. And NehEs SDL lesson gets the same problem if you resize the window.

int presentLogos(GLvoid)
{
glEnable(GL_TEXTURE_2D);

GLuint texCureLogo[0];
SDL_Surface* theTexture = NULL;
iftheTexture == NULL) return -1;

theTexture = SDL_LoadBMP("curelogo.bmp");
glGenTextures( 1, &texCureLogo[0] );

/* Typical Texture Generation Using Data From The Bitmap */
glBindTexture( GL_TEXTURE_2D, texCureLogo[0] );

/* Generate The Texture */
glTexImage2D( GL_TEXTURE_2D, 0, 3, theTexture->w,
theTexture->h, 0, GL_BGR,
GL_UNSIGNED_BYTE, theTexture->pixels );

/* Linear Filtering */
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

SDL_FreeSurface(theTexture);

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

/* Move Into The Screen 5 Units */
glLoadIdentity( );
glTranslatef( 0.0f, 0.0f, -5.0f );


/* Select Our Texture */
glBindTexture( GL_TEXTURE_2D, texCureLogo[0] );

/* NOTE:
* The x coordinates of the glTexCoord2f function need to inverted
* for SDL because of the way SDL_LoadBmp loads the data. So where
* in the tutorial it has glTexCoord2f( 1.0f, 0.0f ); it should
* now read glTexCoord2f( 0.0f, 0.0f );
*/
glBegin(GL_QUADS);
/* Front Face */
/* Bottom Left Of The Texture and Quad */
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, -1.0f, 1.0f );
/* Bottom Right Of The Texture and Quad */
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f, -1.0f, 1.0f );
/* Top Right Of The Texture and Quad */
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, 1.0f, 1.0f );
/* Top Left Of The Texture and Quad */
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, 1.0f, 1.0f );

/* Back Face */
/* Bottom Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
/* Top Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, 1.0f, -1.0f );
/* Top Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, -1.0f );
/* Bottom Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, -1.0f, -1.0f );

/* Top Face */
/* Top Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f, 1.0f, -1.0f );
/* Bottom Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -1.0f, 1.0f, 1.0f );
/* Bottom Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f, 1.0f, 1.0f );
/* Top Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, -1.0f );

/* Bottom Face */
/* Top Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
/* Top Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f, -1.0f, -1.0f );
/* Bottom Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, -1.0f, 1.0f );
/* Bottom Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f );

/* Right face */
/* Bottom Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f, -1.0f, -1.0f );
/* Top Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, -1.0f );
/* Top Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, 1.0f );
/* Bottom Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, -1.0f, 1.0f );

/* Left Face */
/* Bottom Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
/* Bottom Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f );
/* Top Right Of The Texture and Quad */
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, 1.0f, 1.0f );
/* Top Left Of The Texture and Quad */
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f, 1.0f, -1.0f );
glEnd( );

/* Draw it to the screen */
SDL_GL_SwapBuffers( );
return 0;
}


llvllatrix
llvllatrix
You're right, your event handling code is correct. I haven't touched GL and SDL for a while but I'll try my best.

Note: To do the box around the code use {source} and {/source} around the code, but replace { with [.

Your current code shouldn't get past this line:

    GLuint texCureLogo[0];    SDL_Surface* theTexture = NULL;    // It should stop right here.    if(theTexture == NULL)     {        return -1;    }    theTexture = SDL_LoadBMP("curelogo.bmp");    glGenTextures( 1, &texCureLogo[0] );


It should instead look something like this:

    GLuint texCureLogo[0];    SDL_Surface * theTexture = NULL;    theTexture = SDL_LoadBMP("curelogo.bmp");    if(theTexture == NULL)     {        return -1;    }    glGenTextures( 1, &texCureLogo[0] );


In the context of your program, load the texture once and store the texture id somewhere so that it can be used to draw faces over and over again; the texture loading code should not be in the main draw loop.

Cheers,
- llvllatrix

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.