PDA

View Full Version : Background BMP Texture changes colors when key is pressed..



darkenreaper57
02-24-2004, 05:35 PM
Finally i have figured out how to load a bmp to a textured quad and set it to the background. Now, i want to write text, draw shapes, etc, on the screen. I tested drawing a red point, but when i press a key, the entire background changes to red...
I have no idea why this is, can anyone help me out? Here is the source for my main function, where im 99% sure the problem is, and not the other .cpp files:


#include <stdio.h>
#include <windows.h>
#include <gl/glut.h>
#include <gl/glaux.h>
#include <iostream.h>
#include <string.h>
#include "bitmap.h"
#include "texture.h"

// The Following Directive Fixes The Problem With Extra Console Window
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

// Keeps track of windows changing width and height

//----------------------------
// Available fonts |
// |
// GLUT_BITMAP_8_BY_13 |
// GLUT_BITMAP_9_BY_15 |
// GLUT_BITMAP_TIMES_ROMAN_10 |
// GLUT_BITMAP_TIMES_ROMAN_24 |
// GLUT_BITMAP_HELVETICA_10 |
// GLUT_BITMAP_HELVETICA_12 |
// GLUT_BITMAP_HELVETICA_18 |
//----------------------------

GLfloat windowwidth=800;
GLfloat windowheight=600;
GLuint Backgroundtexture;

//************************************************** ***************************************
// Function Prototypes
//************************************************** ***************************************
void display();
void test();
void DispTank(int x1, int y1);
bool init();
void displayfancychar(GLfloat x, GLfloat y, char *text);
void initialsetup();

//************************************************** ***************************************
// Function to display fancier text on the screen, with antialiasing
//************************************************** ***************************************
void displayfancychar(GLfloat x, GLfloat y, char *text)
{
char *p;

glPushMatrix();
glTranslatef(x, y, 0);

glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(20.0);
for (p = text; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopMatrix();
}
//************************************************** ***************************************
// Function to display text on the screen
//************************************************** ***************************************
void bitmapfontoutput(int x, int y, char *string, void *font)
{ int len, i;
glRasterPos2i(x, y); // Locate Raster Position in 2d-space
len = (int) strlen(string); // Find length of string
for (i = 0; i < len; i++) // Loop through plotting all characters in font style
glutBitmapCharacter(font, string[i]);

}

//************************************************** ***************************************
// Displays the intro screen
//************************************************** ***************************************
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Backgroundtexture);

/*
GLubyte *bits;
BITMAPINFO *info;

bits=LoadDIBitmap("c:\\abrams512.bmp", &info);

// Define the 2D texture image.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, 3, info->bmiHeader.biWidth,
info->bmiHeader.biHeight, 0, GL_BGR_EXT,
GL_UNSIGNED_BYTE, bits);
glEnable(GL_TEXTURE_2D);
*/

glBegin(GL_QUADS);
glTexCoord2i(0, 1); // Display the top left vertex
glVertex2i(0, 600);

glTexCoord2i(0,0); // Display the bottom left vertex
glVertex2i(0, 0);

glTexCoord2i(1, 0); // Display the bottom right vertex
glVertex2i(800, 0);

glTexCoord2i(1, 1); // Display the top right vertex
glVertex2i(800, 600);
glEnd();

glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(800,600);
glEnd();
/*
displayfancychar(175,500,"Ballistics");
glColor3f(1.0, 1.0, 1.0);
bitmapfontoutput(750,25,"Version 1.0",GLUT_BITMAP_TIMES_ROMAN_10);
glColor3f(0.0, 0.0, 1.0);
bitmapfontoutput(325,150,"Press Enter",GLUT_BITMAP_TIMES_ROMAN_24);
*/

glDisable(GL_TEXTURE_2D);

glutSwapBuffers(); // Flush drawing commands
}

//************************************************** **********************
// Called by GLUT library when the window has chanaged size
//************************************************** **********************
void ChangeSize(GLsizei w, GLsizei h)
{
// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the clipping volume
gluOrtho2D(0, windowwidth, 0, windowheight);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

//************************************************** ***************************************
// Accepts an initial x and y value for position, and draws a tank facing left
//************************************************** ***************************************
void DispTank(int x1, int y1)
{
// Set current drawing color to brown
// R G B
glColor3f(0.55f, 0.4f, 0.12f);

bitmapfontoutput(10,500,"Here is the tank",GLUT_BITMAP_HELVETICA_18);

// Set Drawing color to green
glColor3f(0.1, 0.9, 0.1);

// Draw lower part of tank
glBegin(GL_POLYGON);
glVertex2i(x1+10, y1+7);
glVertex2i(x1+10, y1+5);
glVertex2i(x1+12, y1+1);
glVertex2i(x1+26, y1+1);
glVertex2i(x1+28, y1+5);
glVertex2i(x1+28, y1+7);
glEnd();

// Draw turret ex1cept gun
glBegin(GL_POLYGON);
glVertex2i(x1+15, y1+7);
glVertex2i(x1+15, y1+9);
glVertex2i(x1+18, y1+11);
glVertex2i(x1+23, y1+11);
glVertex2i(x1+23, y1+7);
glEnd();

//glutSwapBuffers();
}

//************************************************** ***************************************
// Function to check what key is pressed, and determine a result
//************************************************** ***************************************
void HandleKeyboard(unsigned char keyval, int xloc, int yloc)
{

switch(keyval)
{
case 13:
// Set Viewport to window dimensions
//glViewport(0, 0, windowWidth, windowHeight);
/*glColor3i(0,0,0);
glClear(GL_COLOR_BUFFER_BIT);*/

break;
case 27:
exit(0);
break;
default:
break;

}
glutPostRedisplay();

}

//************************************************** ***************************************
// Sets up the initial display properties
//************************************************** ***************************************
void initialsetup()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0,0.0,0.0,1.0);
}


//************************************************** ***************************************
// Main program
//************************************************** ***************************************
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600) ; // set window size
glutInitWindowPosition(0,0) ; // set the window position correctly on screen

glutCreateWindow("Ballistics");
//glutFullScreen();
initialsetup();
glutDisplayFunc(display);
glutKeyboardFunc(HandleKeyboard);
glutReshapeFunc(ChangeSize);

Backgroundtexture=TextureLoad("c:\\abrams512.bmp", GL_FALSE, GL_LINEAR, GL_LINEAR,
GL_CLAMP);

glutMainLoop();
}

Thanks a ton to whoever helps me out http://www.opengl.org/discussion_boards/ubb/smile.gif

mikael_aronsson
02-24-2004, 11:27 PM
Hi !

In your display function you don't set a glColor() so when you render your quad it will use the last color you set with glColor, I guess this is the one you set in key handler code.

Mikael

darkenreaper57
02-25-2004, 03:41 AM
So what color should i set it to? I want the BMP file to display normally... Would i set a NULL color or something?

darkenreaper57
02-25-2004, 04:19 AM
Nevermind, figured it out.