PDA

View Full Version : Opengl - Textures problem



shawn619
03-16-2012, 10:51 PM
This problem came when i tried to add textures. I'm only only dealing with .bmp file types atm. So i started by adding the 'imageloader.cpp' and 'imageloader.h', then i added the following variable and function:


GLuint _textureId; //The id of the texture

GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}


And that function code was taken directly from a reputable website(videotutorialsrock.com) so i dont suspect that to be any problem.

Then i added code to my init() function:


void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0);

// images
Image* image = loadBMP("vtr.bmp");
_textureId = loadTexture(image);

delete image;
}


At this point i checked that everything is working fine and there is no problems loading the images.

Next i enable 2d textures to prepare and draw my texture on my quad:


void drawFloor(){

float size = 20.0;
float startX = -60.0f;
float startZ = 60.0f;

//glPushMatrix();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

for(int x = 0;x < 6; x++){
for(int z = 0;z < 6; z++){

glBegin(GL_QUADS);

glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size*z);//bottom-left
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size*z);//bottom-right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size + -size*z);//top-right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size + -size*z);//top-left

glEnd();
}
}

glDisable(GL_TEXTURE_2D);
//glPopMatrix();
}

I should note that there is nothing wrong with the for-loops and structure, it works fine without textures and just color.

This produces my quads without texture, and with a color that i havent defined anywhere previously in my program (Bright Red):
http://oi42.tinypic.com/xbd3mg.jpg

I tried googling this issue and found that it's the result of multiplying colors in my program that i already have and this causes this issue. And people were using "glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);" to overcome this issue, so i did as well, but it isnt working.

When i dont use "glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);", i get a darkish-red color for my quads which is a color i havent previously defined anywhere else in my program:
http://oi39.tinypic.com/11vo314.jpg

Full code:


#include <iostream>
#include <stdlib.h>
#include <gl/glut.h>
#include "imageloader.h"

using namespace std;

//variables
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0;//camera rotation angles
float _angle = -70.0f;
float lastx, lasty;//mouse
GLfloat lightPos1[] = {0.0f, 0.0f, 0.0f, 1.0f};//light position
GLuint _textureId; //The id of the texture

GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &amp;textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}

//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {

if (key=='q')
{
xrot += 1;
if (xrot >360) xrot -= 360;
}

if (key=='z')
{
xrot -= 1;
if (xrot < -360) xrot += 360;
}

if (key=='w')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad)) ;
zpos -= float(cos(yrotrad)) ;
ypos -= float(sin(xrotrad)) ;
}

if (key=='s')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad));
zpos += float(cos(yrotrad)) ;
ypos += float(sin(xrotrad));
}

if (key=='d')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos += float(cos(yrotrad)) * 0.2;
zpos += float(sin(yrotrad)) * 0.2;
}

if (key=='a')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= float(cos(yrotrad)) * 0.2;
zpos -= float(sin(yrotrad)) * 0.2;
}

switch (key) {
case 27: //Escape key
exit(0);
}
}

void mouseMovement(int x, int y) {
int diffx=x-lastx;
int diffy=y-lasty;
lastx=x; //set lastx to the current x position
lasty=y; //set lasty to the current y position
xrot += (float) diffy /1.2;
yrot += (float) diffx /1.2;
}

void camera (void) {
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glTranslated(-xpos,-ypos,-zpos);
}

void lighting (void){
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0004f);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos1);
}

//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0);

// images
Image* image = loadBMP("vtr.bmp");
_textureId = loadTexture(image);

delete image;
}

//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 2000.0);
}

void drawPlane(){

glPushMatrix();

glTranslatef(0.0f, 0.0f, -20.0f);

glColor3f(0.0f, 1.0f, 1.0f);

/* plane */
glBegin(GL_QUADS);

//glNormal3f(0.0f, 0.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-5.0f, 0.0f, 0.0f); //bottom-left
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(-5.0f, 0.0f, -20.0f);//top-left
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(5.0f, 0.0f, -20.0f);//top-right
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(5.0f, 0.0f, 0.0f);//bottom-right

glEnd();

glPopMatrix();
}

void drawBox(){
glPushMatrix();

glTranslatef(0.0f, 0.0f, -20.0f);

glRotatef(_angle, 0.0f, 6.0f, 0.5f);
glColor3f(1.0f, 0.0f, 0.0f);

glBegin(GL_QUADS);

//Front
//glNormal3f(0.0f, 0.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 4.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 4.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 6.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 6.0f, 1.5f);

//Right
//glNormal3f(1.0f, 0.0f, 0.0f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 4.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 6.0f, -1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 6.0f, 1.5f);
glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 4.0f, 1.5f);

//Back
//glNormal3f(0.0f, 0.0f, -1.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 4.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 6.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 6.0f, -1.5f);
glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 4.0f, -1.5f);

//Left
//glNormal3f(-1.0f, 0.0f, 0.0f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 4.0f, -1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 4.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 6.0f, 1.5f);
glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 6.0f, -1.5f);

glEnd();

glPopMatrix();
}

void drawBuilding(){

glPushMatrix();

glBegin(GL_QUADS);

glColor3f(2.0f, 1.0f, 0.0f);
//front - orange
glVertex3f(-60.0f, 0.0f, -60.0f);//bottom-left
glVertex3f(60.0f, 0.0f, -60.0f);//bottom-right
glVertex3f(60.0f, 50.0f, -60.0f);//top-right
glVertex3f(-60.0f, 50.0f, -60.0f);//top-left

glColor3f(0.5f, 1.0f, 0.0f);
//right - green
glVertex3f(60.0f, 0.0f, -60.0f);//bottom-left
glVertex3f(60.0f, 0.0f, 60.0f);//bottom-right
glVertex3f(60.0f, 50.0f, 60.0f);//top-right
glVertex3f(60.0f, 50.0f, -60.0f);//top-left

glColor3f(0.2f, 0.6f, 0.5f);
//back - blue
glVertex3f(60.0f, 0.0f, 60.0f);//bottom left
glVertex3f(-60.0f, 0.0f, 60.0f);//bottom-right
glVertex3f(-60.0f, 50.0f, 60.0f);//top-right
glVertex3f(60.0f, 50.0f, 60.0f);//top-left

glColor3f(0.3f, 0.3f, 0.3f);
//left - gray
glVertex3f(-60.0f, 0.0f, 60.0f);//bottom-left
glVertex3f(-60.0f, 0.0f, -60.0f);//bottom-right
glVertex3f(-60.0f, 50.0f, -60.0f);//top-right
glVertex3f(-60.0f, 50.0f, 60.0f);//top-left

glEnd();

glPopMatrix();
}

void drawFloor(){

float size = 20.0;
float startX = -60.0f;
float startZ = 60.0f;

//glPushMatrix();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

for(int x = 0;x < 6; x++){
for(int z = 0;z < 6; z++){

glBegin(GL_QUADS);

glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size*z);//bottom-left
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size*z);//bottom-right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size + -size*z);//top-right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size + -size*z);//top-left

glEnd();
}
}

glDisable(GL_TEXTURE_2D);
//glPopMatrix();

}

void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//camera();//fixed

//set lighting position
lighting();

//set camera position
camera();//following

/** DRAW **/

drawPlane();
drawBox();
drawBuilding();
drawFloor();

/** END DRAW **/

glutSwapBuffers();
}

void update(int value) {
_angle += 1.5f;
if (_angle > 360) {
_angle -= 360;
}

glutPostRedisplay();
glutTimerFunc(30, update, 0);
}

int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);

//Create the window
glutCreateWindow("Lighting");
initRendering();

//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutPassiveMotionFunc(mouseMovement);

glutTimerFunc(30, update, 0); //Add a timer

glutMainLoop();
return 0;
}

V-man
03-17-2012, 05:58 AM
Did you check for errors with glGetError()?
http://www.opengl.org/wiki/Common_Mistakes#glGetError

shawn619
03-18-2012, 03:40 AM
Solved!

BionicBytes
03-18-2012, 08:11 AM
And the solution was.....?

shawn619
03-18-2012, 08:14 AM
The texcoords were completely off. I pretty much misinterpreted how i read it off a website.