Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OpenGL not Texturing the Way I Want  (Read 2604 times)

0 Members and 2 Guests are viewing this topic.

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
OpenGL not Texturing the Way I Want
« on: December 15, 2010, 05:20:30 am »
Well Whenever I use this code which I made myself, it only draws the  texture of the other shape so obviosly these shapes are acting as one and I am wondering how do I give each shape its own caracteristics.

Code: [Select]

#include "Block.h"

Block::~Block()
{
glDeleteTextures(1, &TopTexture);
glDisable(GL_TEXTURE_2D);
}

Block::Block()
{
}

void Block::GetPositionX()
{

}

void Block::GetPositionY()
{

}

void Block::GetPositionZ()
{

}

void Block::GetRotation()
{

}

void Block::GetType()
{

}

void Block::SetPosition(float x,float y, float z)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x,y,z);
}

void Block::Rotation(float angle, float x, float y, float z)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle,x,y,z);
}

void Block::Draw()
{
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);

        // Draw a cube
        float size = 20.f;
        glBegin(GL_QUADS);

glBindTexture(GL_TEXTURE_2D, SideTexture);
glColor4f(1.f, 1.f, 1.f, 1.f);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(-size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(-size, -size,  size);

            glTexCoord2f(0, 0); glVertex3f(size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(size, -size,  size);

glDisable(SideTexture);
glEnable(TopTexture);

glBindTexture(GL_TEXTURE_2D, TopTexture);
glColor4f(1.f, 1.f, 1.f, 1.f);

            glTexCoord2f(0, 1); glVertex3f(-size, -size,  size);
            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, -size,  size);

glBindTexture(GL_TEXTURE_2D, BottomTexture);
glColor4f(1.f, 1.f, 1.f, 1.f);

            glTexCoord2f(0, 1); glVertex3f(-size, size,  size);
            glTexCoord2f(0, 0); glVertex3f(-size, size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, size,  size);

        glEnd();
}

void Block::LoadTexture(BType BlockType)
{
std::string imagefileTop;
std::string imagefileSide;
std::string imagefileBottom;


if(BlockType == Grass) {imagefileTop = "GrassTop.jpg"; imagefileSide = "GrassSide.jpg"; imagefileBottom = "Dirt.jpg";}
if(BlockType == Dirt) {imagefileTop = "Dirt.jpg"; imagefileSide = "Dirt.jpg"; imagefileBottom = "Dirt.jpg";}
if(BlockType == Stone) {imagefileTop = "Stone.jpg"; imagefileSide = "Stone.jpg"; imagefileBottom = "Stone.jpg";}
if(BlockType == Wood) {imagefileTop = "WoodTop.jpg"; imagefileSide = "WoodSide.jpg"; imagefileBottom = "WoodTop.jpg";}

// Load an OpenGL texture.
    // We could directly use a sf::Image as an OpenGL texture (with its Bind() member function),
    // but here we want more control on it (generate mipmaps, ...) so we create a new one from the image pixels
    TopTexture = 0;
    {

        sf::Image image;
        if (!image.LoadFromFile("resources\\" + imagefileTop))
            std::cout << "Could Not Load Texture";
        glGenTextures(1, &TopTexture);
        glBindTexture(GL_TEXTURE_2D, TopTexture);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, image.GetPixelsPtr());
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    }

SideTexture = 0;
    {

        sf::Image image;
        if (!image.LoadFromFile("resources\\" + imagefileSide))
            std::cout << "Could Not Load Texture";
        glGenTextures(1, &SideTexture);
        glBindTexture(GL_TEXTURE_2D, SideTexture);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, image.GetPixelsPtr());
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    }

BottomTexture = 0;
    {

        sf::Image image;
        if (!image.LoadFromFile("resources\\" + imagefileBottom))
            std::cout << "Could Not Load Texture";
        glGenTextures(1, &BottomTexture);
        glBindTexture(GL_TEXTURE_2D, BottomTexture);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, image.GetPixelsPtr());
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    }
}




I call it like this in main..
Block Grass;
Grass.LoadTexture()

in the while loop

Grass.SetPosition(x,y,z); //Cube doesnt display inless this is in while..
Grass.Draw();

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
OpenGL not Texturing the Way I Want
« Reply #1 on: December 15, 2010, 08:04:39 am »
Can you show the definition of your class (what's in the header)?
Laurent Gomila - SFML developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
OpenGL not Texturing the Way I Want
« Reply #2 on: December 16, 2010, 12:10:48 am »
Quote from: "Laurent"
Can you show the definition of your class (what's in the header)?


Here are my files...

http://www.mediafire.com/?ascd4guhsqrvho3

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
OpenGL not Texturing the Way I Want
« Reply #3 on: December 16, 2010, 07:35:52 am »
I don't want the whole project, just want to see Block.h.
Laurent Gomila - SFML developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
OpenGL not Texturing the Way I Want
« Reply #4 on: December 17, 2010, 02:37:26 am »
Quote from: "Laurent"
I don't want the whole project, just want to see Block.h.


Block.h: http://pastebin.com/MhCcWaQD
Block.cpp: http://pastebin.com/0Dxyd6w9

Also if I try to rotate in main in transports me into the block..

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
OpenGL not Texturing the Way I Want
« Reply #5 on: December 17, 2010, 04:24:33 pm »
Quote from: "Fierce_Dutch"
Quote from: "Laurent"
I don't want the whole project, just want to see Block.h.


Block.h: http://pastebin.com/MhCcWaQD
Block.cpp: http://pastebin.com/0Dxyd6w9

Also if I try to rotate in main in transports me into the block..


Ok Now I still have the rotate problem but I am flipping the textures like this and it doesnt display the bg.

glMatrixMode(GL_TEXTURE);
   glLoadIdentity();
   glScalef(1.0, -1.0, 1.0);

I have tried taking out the glLoadIdentity() but it still doesnt work.

 

anything