Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ OpenGL
Hi.
I wrote a code but it's just show a cube without texture
 
#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 :
 
#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 21-Jan-13 5:41am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
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 )
  Permalink  
Comments
mohammadali1375 at 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)

  Print Answers RSS
0 Schatak 394
1 OriginalGriff 355
2 Sergey Alexandrovich Kryukov 119
3 _Amy 115
4 Rob Philpott 100
0 OriginalGriff 7,097
1 Sergey Alexandrovich Kryukov 5,623
2 Maciej Los 3,504
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100