/* Virtual City*/
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <gl\glaux.h>
#include <iostream>
using namespace std;
/* Prototypes */
void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);
void drawcube(int, int, int);
void mouse(int, int, int, int);
void idleEvent(void);
/* Global variables*/
int is_depth, counter, h, keyp_x; /* depth testing flag */
float slide = 1.0f;
float temp_slide = 0.0f;
float const LR_DRIFT = 10.0f;
int color = 1;
GLuint texture[1]; // storage for *one* texture
/* Bitmap File Loader */
AUX_RGBImageRec *LoadBMP(char *Filename){
FILE *File = NULL;
if (!Filename) {
return NULL;
}
File = fopen(Filename,"r");
if (File){
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}
/*Texture builder returns 0 for false and 1 for true*/
void LoadGLTextures(){
int Status = FALSE;
AUX_RGBImageRec *TextureImage[1]; // Create Storage space for the texture
memset(TextureImage, 0, sizeof(void *)*1); // Set pointer to null
if (TextureImage[0] = LoadBMP("img/House.bmp")) {
Status = TRUE;
glGenTextures(1, &texture[0]); // create the texture
glBindTexture(GL_TEXTURE_2D, texture[0]); //use data from bitmap
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // linear filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // linear filtering
}
/*Free up RAM */
if (TextureImage[0]){
if (TextureImage[0]->data){
free(TextureImage[0] -> data);
}
free(TextureImage[0]);
}
}
int main (int argc, char **argv){
glEnable(GL_TEXTURE_2D);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(600, 400);
glutCreateWindow("Virtual Icy World");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutIdleFunc(idleEvent);
/* this time we're going to keep the aspect ratio
constant by trapping the window resizes */
glutReshapeFunc(resize);
glutMainLoop();
return 0;
}
void init(void){
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
is_depth = 1;
glMatrixMode(GL_MODELVIEW);
//New portion for lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//Portion for texture
LoadGLTextures();
//glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
//glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
//glClearDepth(1.0f);
//glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void display(void){
//GLfloat position [] = {1.0, 5.0, 1.0, 1.0}; //original
//GLfloat position[] = {-50.0, -50.0, -50.0, -11.0};
GLfloat position[] = {0.0, 0.0, 1.0, 14.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};
if (is_depth)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
else
glClear(GL_COLOR_BUFFER_BIT);
/* set lightning */
//glLightfv(GL_LIGHT0, GL_POSITION, position);
/* draw the floor */
glBegin(GL_QUADS);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-100.0, 0.0, -100.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-100.0, 0.0, 100.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(100.0, 0.0, 100.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
/* draw 12 cubes with different colors*/
glPushMatrix();
glDisable(GL_LIGHTING);
drawcube(75, 57, 2);
drawcube(-65, -12, 3);
drawcube(50, -50, color);
drawcube(-56, 17, 2);
drawcube(67, 12, 3);
drawcube(-87, 32, 1);
drawcube(-26, 75, 2);
drawcube(57, 82, 3);
drawcube(-3, 12, 1);
drawcube(46, 35, 2);
drawcube(37, -2, 3);
glPopMatrix();
glPushMatrix();
glLightfv(GL_LIGHT0, GL_POSITION, position);
glDisable(GL_LIGHTING);
glColor3f(0.0, 1.0, 1.0);
//glTranslatef((x_deg*0.2)*-18.0, 2.0, -11.0);
if (slide == 0) {
glTranslatef(temp_slide, 2.0, -12.0);
} else {
glTranslatef(slide, 2.0, -12.0);
}
glScalef(1.0, 1.0, 2.0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glutSolidCube(4.0);
//glutSolidSphere(4.0, 3.5, 7);
//glEnable(GL_LIGHTING);
glPopMatrix();
glPushMatrix();
glDisable(GL_LIGHTING);
glColor3f(1.0, 0.0, 0.0);
if (slide == 0) {
glTranslatef(temp_slide, 3.0, -11.0);
} else {
glTranslatef(slide, 3.0, -11.0);
}
glutSolidCube(4.0);
glEnable(GL_LIGHTING);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
/* This time the controls are:
"a": move left
"d": move right
"w": move forward
"s": move back
"t": toggle depth-testing
*/
switch (key)
{
case 'a':
case 'A':
glTranslatef(5.0, 0.0, 0.0);
break;
case 'e':
case 'E':
glRotatef(-5, 0.0, 1.0, 0.0);
break;
case 'q':
case 'Q':
glRotatef(5, 0.0, 1.0, 0.0);
break;
case 'x':
case 'X':
if (keyp_x <= 89) {
keyp_x = keyp_x + 1;
glRotatef(1, 0.0, 0.0, 1.0);
}
break;
case 'c':
case 'C':
if (keyp_x >= -89) {
keyp_x = keyp_x - 1;
glRotatef(-1, 0.0, 0.0, 1.0);
}
break;
case 'd':
case 'D':
glTranslatef(-5.0, 0.0, 0.0);
break;
case 'w':
case 'W':
glTranslatef(0.0, 0.0, 5.0);
break;
case 's':
case 'S':
glTranslatef(0.0, 0.0, -5.0);
break;
case 'r':
case 'R':
glTranslatef(0.0, -5.0, -150.0);
case 't':
case 'T':
if (is_depth)
{
is_depth = 0;
glDisable(GL_DEPTH_TEST);
}
else
{
is_depth = 1;
glEnable(GL_DEPTH_TEST);
}
}
display();
}
void idleEvent(){
if (counter == 1700000){
h = h + 1;
if (color == 1) {
color =4;
} else {
color = 1;
}
if (keyp_x > 0) {
slide = (temp_slide - (keyp_x/LR_DRIFT)); // we factorize keyp_x
temp_slide = slide;
} else if (keyp_x < 0) {
slide = (temp_slide - (keyp_x/LR_DRIFT));
temp_slide = slide;
} else {
slide = 0;
}
counter = 0;
glutPostRedisplay();
} else {
counter++;
}
}
void mouse(int button, int state, int x, int y){
//lowers and raises the view
switch(button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
glTranslatef(0.0, -2.0, 0.0);
glutPostRedisplay();
}
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN){
glTranslatef(0.0, 2.0, 0.0);
glutPostRedisplay();
}
break;
default:
break;
}
}
void resize(int width, int height)
{
if (height == 0) height = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* note we divide our width by our height to get the aspect ratio */
gluPerspective(45.0, width / height, 1.0, 400.0);
/* set initial position */
glTranslatef(0.0, -5.0, -150.0);
glMatrixMode(GL_MODELVIEW);
}
void drawcube(int x_offset, int z_offset, int color){
/* this function draws a cube centerd at (x_offset, z_offset)
x and z _big are the back and rightmost points, x and z _small are
the front and leftmost points */
float x_big = (float)x_offset + 5;
float z_big = (float)z_offset + 5;
float x_small = (float)x_offset - 5;
float z_small = (float)z_offset - 5;
switch(color)
{
case 1:
glColor3f(1.0,0.0,0.0); //red
break;
case 2:
glColor3f(0.0,1.0,0.0); //green
break;
case 3:
glColor3f(0.0,0.0,1.0); //blue
break;
case 4:
glColor3f(0.0, 0.0, 0.0); //black
}
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x_small,10.0,z_big); /* front */
glTexCoord2f(1.0f, 0.0f); glVertex3f(x_small,0.0,z_big);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x_big,0.0,z_big);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x_big,10.0,z_big);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x_big,10.0,z_small); /* back */
glTexCoord2f(1.0f, 1.0f); glVertex3f(x_big,0.0,z_small);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x_small,0.0,z_small);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x_small,10.0,z_small);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x_big,10.0,z_big); /* right */
glTexCoord2f(1.0f, 1.0f); glVertex3f(x_big,0.0,z_big);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x_big,0.0,z_small);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x_big,10.0,z_small);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x_small,10.0,z_small); /* left */
glTexCoord2f(1.0f, 0.0f); glVertex3f(x_small,0.0,z_small);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x_small,0.0,z_big);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x_small,10.0,z_big);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x_small,10.0,z_big); /* top */
glTexCoord2f(0.0f, 0.0f); glVertex3f(x_big,10.0,z_big);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x_big,10.0,z_small);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x_small,10.0,z_small);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x_small,0.0,z_small); /* bottom */
glTexCoord2f(0.0f, 1.0f); glVertex3f(x_big,0.0,z_small);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x_big,0.0,z_big);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x_small,0.0,z_big);
glEnd();
}
[\source]
Thanks in advance!
Texture mapping
Hi,
after having applied notions from NeHE Lesson 6 to my c++ code, I still don't grasp why textures aren't applied:
Here goes the code:
[Edited by - pi6502 on January 14, 2006 4:30:22 PM]
First of all, you need to put your source code in [#source]CODE IN HERE[#/source] tags or else you're going to get people complaining. Remove the hashes to make the tags work.
Second, your problem is that you haven't enabled texturing:
Hope that helps!
Second, your problem is that you haven't enabled texturing:
// enable texturingglEnable(GL_TEXTURE_2D);// DRAW TEXTURED STUFF// disable texturingglDisable(GL_TEXTURE_2D);// DRAW UNTEXTURED STUFF
So in your display function, enable texturing just before you call your drawcube function (only once!) and then disable it after your last call to drawcube. This presumes you only want your cubes textured.Hope that helps!
--
Cheers,
Darren Clark
Cheers,
Darren Clark
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement