Hi folks. I know this isn't exactly game-development related, but I was wondering if someone might be able to shed a little light into an issue I'm having with a little tachometer prog in C/GL/SDL.
I can get a square displayed with a texture (GL_QUADS) no problem. When I try to put a triangle (GL_TRIANGLES) over top of it, the whole dang thing becomes the same color of the triangle. The source below is partially from a tutorial. Use any 256x256 24bpp BMP for the texture file.
I'm pulling out what's left of my hair over this.
Thanks in advance!
--Doug
#include <stdio.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
GLfloat xspeed = 0.0f;
GLfloat yspeed = 0.0f;
GLfloat angle = -118.0f;
GLfloat rotationSpeed = 0.0f;
// the ID of the texture we're going to create
GLuint texture;
// =================================================================
int handleInput()
{
SDL_Event event;
while(SDL_PollEvent( &event ))
{
if( event.type == SDL_QUIT ) return 1;
if( event.type == SDL_KEYDOWN )
{
if( event.key.keysym.sym == SDLK_ESCAPE ) return 1;
if( event.key.keysym.sym == SDLK_PAGEUP ) rotationSpeed += 1.0f;
if( event.key.keysym.sym == SDLK_PAGEDOWN ) rotationSpeed -= 1.0f;
}
}
return 0;
}
// =================================================================
void draw()
{
// glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Draw background with texture
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f );
glBindTexture( GL_TEXTURE_2D, texture );
glEnable( GL_TEXTURE_2D );
glBegin(GL_QUADS);
// upper-left corner of square and texture
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( 0.0f, 0.0f, 0.0f );
// upper-right corner
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 256.0f, 0.0f, 0.0f );
// bottom-right corner
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 256.0f, 256.0f, 0.0f );
// bottom-left corner
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( 0.0f, 256.0f, 0.0f );
glEnd();
glDisable( GL_TEXTURE_2D );
glPopMatrix();
// draw the triangle
glPushMatrix();
glTranslatef( 128.0f, 128.0f, 1.0f );
glRotatef(angle,0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0);
glVertex3d( 000,-120,-001);
glVertex3d(-004, 000,-001);
glVertex3d( 004, 000,-001);
glEnd();
glPopMatrix();
SDL_GL_SwapBuffers();
angle += rotationSpeed;
if( angle > 157.0f ) angle = 157.0f;
if( angle < -118.0f ) angle = -118.0f;
}
// =================================================================
int main()
{
SDL_Init( SDL_INIT_VIDEO );
/* SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 8 ); */
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_Surface *screen;
screen = SDL_SetVideoMode( 256, 256, 24, SDL_OPENGL | SDL_HWSURFACE );
glViewport( 0, 0, 256, 256 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glOrtho( 0.0f, 256.0f, 256.0f, 0.0f, 0.0f, -2.0f );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth( 2.0f );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
// generate space for the texture and make it the current texture
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
// load the bitmap and lock the memory
SDL_Surface *tex = SDL_LoadBMP( "tach256_24bpp.bmp" );
SDL_LockSurface( tex );
// create the texture from the bitmap
glTexImage2D( GL_TEXTURE_2D, 0, 3, tex->w, tex->h, 0, GL_BGR, GL_UNSIGNED_BYTE, tex->pixels );
// free up the bitmap memory
SDL_UnlockSurface( tex );
SDL_FreeSurface( tex );
// draw it nicely
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
while( handleInput() == 0 ) draw();
SDL_Quit();
return 0;
} //int main()