Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411512 Posts in 69376 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 11:27:39 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Wit's end
Pages: [1]
Print
Author Topic: Wit's end  (Read 1230 times)
eckertd
Level 0
*


View Profile
« on: February 17, 2009, 12:11:05 PM »

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

Code:
#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()
Logged
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #1 on: February 17, 2009, 12:28:43 PM »

It's because you're calling glColor3f before drawing the triangle, which sets the current vertex drawing color and not changing it before drawing the quad.

Just put glColor3f(1.0,1.0,1.0); before drawing your quad.
« Last Edit: February 17, 2009, 12:32:21 PM by toastie » Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
eckertd
Level 0
*


View Profile
« Reply #2 on: February 17, 2009, 12:41:43 PM »

OMG!  Thank you!  A THOUSAND TIMES THANK YOU!

I think I may have bitten off more than I could chew with my first foray into OpenGL programming. 

Right now, the needle sweep is being done with PgUp/PgDn (so I can test functionality easily).  I have another process that reads analog & digital sensor data from a USB BitWhaker (SparkFun.com) and puts the number of tachometer pulses over the polling interval into a shared memory segment.  The PgUp/PgDn will be replaced by reads/resets of the value inthe shmseg.

This is for a DIY Engine Data Acquisition project I've been working on for my 1967 Mustang.  It's coming along slowly, as the car's getting a 347 stroker kit installed.

Again...THANKS!!!!!!
Logged
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #3 on: February 17, 2009, 12:45:24 PM »

Haha, no problem. That sounds like the most amazing use of OpenGL ever.
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic