Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I wrote a code but it's just show a cube without texture

C++
#include "Texture.h"

int main(int argc,char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_SetVideoMode(640,480,32,SDL_OPENGL);
    glEnable(GL_DEPTH_TEST);
    glClearColor(0,0,0,1);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,800/600,1,100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(7,7,7,
              0,0,0,
              0,1,0);
    Texture* textre=new Texture((IMG_Load("Texture.png")));
    int rot=0;
    while(1)
    {

        glLoadIdentity();

        gluLookAt(7,7,7,
            0,0,0,
            0,1,0);
        glRotatef(rot,0,1,0);
        glBindTexture(GL_TEXTURE_2D,textre->textureID);
        glBegin(GL_QUADS);

        glTexCoord2f(0,0);
        glVertex3f(-.5f , -.5f , .5f);
        glTexCoord2f(1,0);
        glVertex3f(.5f , -.5f , .5f);
        glTexCoord2f(1,1);
        glVertex3f(.5f , .5f, .5f);
        glTexCoord2f(0,1);
        glVertex3f(-.5f , .5f , .5f);


        glTexCoord2f(0,0);
        glVertex3f(-.5f , -.5f , -.5f);
        glTexCoord2f(1,0);
        glVertex3f(.5f , -.5f , -.5f);
        glTexCoord2f(1,1);
        glVertex3f(.5f , .5f , -.5f);
        glTexCoord2f(0,1);
        glVertex3f(-.5f , .5f , -.5f);



        glVertex3f(-.5f , .5f , -.5f);
        glVertex3f(.5f , .5f , -.5f);
        glVertex3f(.5f , .5f , .5f);
        glVertex3f(-.5f , .5f , .5f);



        glVertex3f(.5f , -.5f , -.5f);
        glVertex3f(.5f , .5f ,-.5f);
        glVertex3f(.5f , .5f , .5f);
        glVertex3f(.5f , -.5f , .5f);



        glVertex3f(-.5f , .5f , .5f);
        glVertex3f(-.5f , -.5f ,.5f);
        glVertex3f(-.5f , -.5f , -.5f);
        glVertex3f(-.5f , .5f , -.5f);


        glVertex3f(.5f , -.5f , .5f);
        glVertex3f(-.5f , -.5f , .5f);
        glVertex3f(-.5f , -.5f , -.5f);
        glVertex3f(.5f , -.5f , -.5f);

        glEnd();
        SDL_GL_SwapBuffers();
        rot++;
        SDL_Delay(16);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }


    return 0;
}



------ texture :

C++
#pragma once

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_opengl.h>

class Texture
{
public:
    Texture(SDL_Surface* surface)
    {
        unsigned int nOfColor=surface->format->BytesPerPixel;
        unsigned int textureFormat;
        if(nOfColor==4)
            if(surface->format->Rmask==0x000000ff)
                textureFormat=GL_RGBA;
            else
                textureFormat=GL_BGRA;
        else
            if (surface->format->Rmask==0x000000ff)
                textureFormat=GL_RGB;
            else
                textureFormat=GL_BGR;
        glGenTextures(1,&textureID);
        glBindTexture(GL_TEXTURE_2D,textureID);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D,0,nOfColor,surface->w,surface->h,0,textureFormat,GL_UNSIGNED_BYTE,surface->pixels);
        SDL_FreeSurface(surface);
    }
    unsigned int textureID;
};
Posted

1 solution

I have two suggestions.
1. GL_DEPTH_TEST is enabled, but DepthBuffer is not cleared. Please call glClear() to clear depth buffer and color buffer.
C++
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );

2. Please enable GL_TEXTURE_2D before texture mapping. Inside Texture class you can enable this by calling
glEnable( GL_TEXTURE_2D )
 
Share this answer
 
Comments
mohammadali1375 25-Jan-13 10:38am    
Oh yea. Thanks alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900