Sign in to follow this  
Salatinjsh

SDL and opengl textures

Recommended Posts

Hello!

I have decided to move from glut to SDL to make window and handle other things. But now i encounter an issue. When im trying to print texture on objects it does nothing. I have tried different ways of loading textures but it seems not to be the problem

Linker:
SOIL
mingw32
SDLmain
SDL
opengl32
glu32

Im using classes:

Main App:
[source lang="cpp"]#include "CApp.h"

CApp::CApp() {
Surf_Display = NULL;

w = 640;
h = 480;

Running = true;
angle = 0;
}



int CApp::OnExecute() {

if(OnInit() == false) {
return -1;
}

glLoadIdentity();

SDL_Event Event;

while(Running) {
while(SDL_PollEvent(&Event)) {
OnEvent(&Event);
}

OnLoop();
OnRender();
}

OnCleanup();

glGetError();

return 0;
}

int main(int argc, char* argv[]) {
CApp theApp;
return theApp.OnExecute();
}

[/source]
Main header
[source lang="cpp"]#ifndef _CAPP_H_
#define _CAPP_H_

#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/SOIL.h>
#include <iostream>

#include "CEvent.h"

class CApp : public CEvent {
private: // local variables
bool Running;
int w,h; //window width and height

SDL_Surface* Surf_Display;

GLuint texture[3];
GLUquadricObj* quadratic;

GLfloat angle;

public:
CApp();

int OnExecute();

public: // Natural things in game application
bool OnInit();

void OnEvent(SDL_Event* Event);

void OnLoop();

void OnRender();

void OnCleanup();

public: // Events used
void OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode);

void OnExit();
};

#endif
[/source]
Init:
[source lang="cpp"]#include "CApp.h"

void RenderInit();
GLuint LoadTextures(const char* filename);

bool CApp::OnInit() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
return false;
}

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);

SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);

if((Surf_Display = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL)) == NULL) {
return false;
}

SDL_ShowCursor(SDL_DISABLE);

texture[0] = LoadTextures("Crate.bmp");
quadratic=gluNewQuadric();
gluQuadricNormals(quadratic, GLU_SMOOTH);
gluQuadricTexture(quadratic, GL_TRUE);

RenderInit();

glClearColor(0,0,0,1);

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
gluPerspective(45, (double)w / (double)h, 1, 200);

GLfloat AmbientLightColor[] = {0,0,0, 1};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, AmbientLightColor);

GLfloat Light0Color[] = {0.8,0.8,0.8,1};
GLfloat Light0Pos[] = {0.000000000000000000000000000000000000015,0,0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, Light0Color);
glLightfv(GL_LIGHT0, GL_POSITION, Light0Pos);

glMatrixMode(GL_MODELVIEW);

glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);


return true;
}

void RenderInit(){
glNewList(1, GL_COMPILE);
glBegin(GL_QUADS);
glNormal3f(0,0,-1);
glTexCoord2d(0,0);glVertex3d(-1,-1,-1);
glTexCoord2d(1,0);glVertex3d(1,-1,-1);
glTexCoord2d(1,1);glVertex3d(1,1,-1);
glTexCoord2d(0,1);glVertex3d(-1,1,-1);

glNormal3f(0,0,1);
glTexCoord2d(0,0);glVertex3d(-1,-1,1);
glTexCoord2d(1,0);glVertex3d(1,-1,1);
glTexCoord2d(1,1);glVertex3d(1,1,1);
glTexCoord2d(0,1);glVertex3d(-1,1,1);

glNormal3f(-1,0,0);
glTexCoord2d(1,0);glVertex3d(-1,-1,-1);
glTexCoord2d(1,1);glVertex3d(-1,1,-1);
glTexCoord2d(0,1);glVertex3d(-1,1,1);
glTexCoord2d(0,0);glVertex3d(-1,-1,1);

glNormal3f(1,0,0);
glTexCoord2d(1,0);glVertex3d(1,-1,-1);
glTexCoord2d(1,1);glVertex3d(1,1,-1);
glTexCoord2d(0,1);glVertex3d(1,1,1);
glTexCoord2d(0,0);glVertex3d(1,-1,1);
glEnd();
glEndList();
}

GLuint LoadTextures(const char* filename){
GLuint texture = SOIL_load_OGL_texture
(
filename,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);

if(texture == 0)
return false;

glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);



return texture;

}
[/source]
Render:
[source lang="cpp"]#include "CApp.h"

void CApp::OnRender() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslated(0,0,-5);

glTranslated(0,0,-5);
glRotatef(angle,0,1,0);
//glRotatef(90,-1,0,0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
//gluSphere(quadratic,1.3,32,16);
glBegin(GL_QUADS);
glTexCoord2d(0,0);glVertex3d(-1,-1,0);
glTexCoord2d(0,1);glVertex3d(-1,1,0);
glTexCoord2d(1,1);glVertex3d(1,1,0);
glTexCoord2d(1,0);glVertex3d(1,-1,0);
glEnd();

SDL_GL_SwapBuffers();
}
[/source]

ill be glad if someone could help. i've been looking for issue for about week now.

Share this post


Link to post
Share on other sites
I don't see anything wrong with you code but try setting:
[CODE]
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
[/CODE]

in case your texture is too small and is being mapped somewhere you cant see.

also since you're already using SDL you might want to try using SDL_image for image loading instead of SOIL. you use SDL_image to load an image to a surface and then find the format and number of colors. then you use glteximage2d to get the image from surface->pixels.

[CODE]
surface = IMG_Load("texture.png");

GLenum texture_format;
GLint nOfColors;
GLuint texture;
nOfColors = surface->format->BytesPerPixel;

glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );

if (nOfColors == 4) // contains an alpha channel
{
if (surface->format->Rmask == 0x000000ff)
{
texture_format = GL_RGBA;
}
else
{
texture_format = GL_BGRA;
}
}
else if (nOfColors == 3) // no alpha channel
{
if (surface->format->Rmask == 0x000000ff)
{
texture_format = GL_RGB;
}
else
{
texture_format = GL_BGR;
}
}


glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );

SDL_FreeSurface(surface);
[/CODE] Edited by ic0de

Share this post


Link to post
Share on other sites
Well thanks for answer. :)
I have tryed usng soil and SDL but no difference and

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

didn't help. :(
Can it be problem with libraries? can they be at wrong positions?

Share this post


Link to post
Share on other sites
You are using immediate mode, which is deprecated.

Is GLuint LoadTextures(const char* filename) returning successfully? You have it return false when it fails, but you never test to see if it failed, you just assume that it succeeds.

It could be something as simple as not having the correct file path or format of the texture being loaded, and since you just ignore it and try not to set the texture if it fails, you never know. I don't know enough about the bmp format you're using for "Crate.bmp", how SOIL is used, or where the file is located to make a judgment, however I am certain that if you were to fail to load the texture properly and SOIL_load_OGL_texture returned 0, you would experience the issue you are noticing.

Share this post


Link to post
Share on other sites
[quote name='zeidrich' timestamp='1349296455' post='4986524']
You are using immediate mode, which is deprecated.
[/quote]

Immediate mode may be deprecated in OpenGL 3.0 but they are still useful for teaching beginners about OpenGL. teaching beginners OGL 3.0 is one sure way to confuse them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this