Thread: SDL and OpenGL problems

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    SDL and OpenGL problems

    Hi guys. I wonder has anyone has had this problem before. I would like to poing out that SDL is properly installed. But for some reseaon it has a problem with the glTexImage2D(). It has an issue with the GL_UNSIGNED_SHORT_5_6_5 section. At first it was saying that it didnot recognized glTexImage2D, so I included glext.h still have that same issue though. Here is the code below. I hope you guys can help. On the linux machine I don't even need to include glext.h, but ont he windows machine I cann't get anything to show.

    I'll also attached files.
    Code:
    /*Applying Textures to Surfaces: This Program Works Great
      Good example video OpenGL (SDL,C++) tutorial 7 - Textures (part 1) - YouTube
    
      The linux computer at school contains SDL libraries, using -lSDL */
    /* Rotating Cube with Textures and Mouse click interactions
    
    Rendered images are stored in OpenGL pictures in the UAB School Folder
    This program is a intro to texturing. It use a 3D cube and textures it's sureface with colors defined in the color array RGB.
    This program also introcudes a simple way to interact with cube using the mouse buttons.*/
    
    //****Important Note put SDL.dll file in operating directory and link libraries
    #include <stdlib.h>
    #include <GL/glut.h>
    #include <SDL/SDL.h>
    #include <GL/glext.h> //
    
    //Prototypes
    unsigned int loadTexture(const char* filename); //I had to put this in since the initRendering function was using loadTexture function before it was declared
    
    //note:  notice that the range is from -1 to 1, and colors are from 0 to 1, while textures are from 0 to 1
    GLfloat vertices[][3] = {{-1.0,-1.0,1.0},{-1.0,1.0,1.0}, 
    						{1.0,1.0,1.0}, {1.0,-1.0,1.0},
    						{-1.0,-1.0,-1.0},{-1.0,1.0,-1.0},
    						{1.0,1.0,-1.0},{1.0,-1.0,-1.0}};
    
    GLfloat colors[][3] = {{1.0,0.0,0.0},{0.0,1.0,1.0},
    					  {1.0,1.0,0.0},{0.0,1.0,0.0},
    					  {0.0,0.0,1.0},{1.0,0.0,1.0}};
    
    unsigned int tex;  //global variable to hold texture
    
    //Added initialization function
    //Initializes 3D rendering
    void initRendering() { //in this function I may need to add in the 
    	glEnable(GL_DEPTH_TEST);
    	glEnable(GL_COLOR_MATERIAL);
    	//glEnable(GL_LIGHTING); //enable lighting
    	//glEnable(GL_LIGHT0); //enable light #0
    	//glEnable(GL_LIGHT1); //enable light #1
    	//glEnable(GL_NORMALIZE); //Automatically normalize normals
    	glShadeModel(GL_SMOOTH); //enable smooth shading
    	glEnable(GL_TEXTURE_2D);  //Enables the use of 2D textures
    	tex = loadTexture("Bricks.bmp");  //load up file
    
    	//Adds ambient light
    	//GLfloat ambientColor[] = {0.2, 0.2, 0.2, 1.0};
    	//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
    
    //Adds positioned light
    	/*GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};
    	GLfloat lightPos0[] = {4.0, 0.0, 8.0, 1.0};
    	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    	glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);*/
    
    //Adds directed light
    	/*GLfloat lightColor1[] = {0.5, 0.2, 0.2, 1.0};
    	GLfloat lightPos1[] = {-1.0, 0.5, 0.5, 0.0};
    	glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
          	glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);*/
    }
    
    //***********************************Texture Function for Loading*************************************************************
    /*Here I need to load a bitmap image, generate textures, bind texture, set glTextImage2D, glTextParameteri
      Some ppl use programs such as SDL to read in bitmap images (just not to reinvent the wheel). Depending on the requirements of the Viscube
      I can aslo us SDL.  In this case I am trying to establish reading using SDL*/
    
    //Reading the texture from an image file: OpenGL has no function to load an image
    //Here I am going to use the SDL as my image uploader  //SDL uploader test
    
    unsigned int loadTexture(const char* filename)
    {
      SDL_Surface* img =SDL_LoadBMP(filename);
      unsigned int id; //assigning an id for glGenTextures
      glGenTextures(1, &id);  //idea assigned
      glBindTexture(GL_TEXTURE_2D, id);  //binding texture to the id
    
      glTexImage2D(GL_TEXTURE_2D,
    	  0, 
    	  GL_RGB,
    	  img ->w,
    	  img->h,
    	  0,
    	  GL_RGB, 
    	  GL_UNSIGNED_SHORT_5_6_5,  //SDL is having trouble processing this in windows
    	  img->pixels); //For some reason this is causing a problem
    	  
      //Setting Parameters
      //Warping Parameters (how texture will be applied to the object)
      	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);  //Many different parameters can be set her to manipulate the textures
         	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    
      //Filtering Parameters
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //2d texture with a minimum filter, and linear interpolation (averaging pixlels)
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      SDL_FreeSurface(img);  //deletes the texture image out of the texture memory
      return id;
    }
    
    void polygon(int a, int b, int c, int d)
    {
    /*draw a polygon via list of vertices */
      
      glBindTexture(GL_TEXTURE_2D, tex); //This binds the textures to the polygon  
    	glBegin(GL_POLYGON);   //applying the textures are based on a square that is lower left(0,0), Upper left (0,1), lower right (1,0), upper right (1,1)
    	// glColor3fv(colors[a]);  //These texture points are placed witht he corresponding vertices, to make sure that the texture is applied correctly
    	        glTexCoord2f(0.0,0.0);
    		glVertex3fv(vertices[a]);
    		//glColor3fv(colors[b]);
    		glTexCoord2f(0.0,1.0);
    		glVertex3fv(vertices[b]);
    		//glColor3fv(colors[c]);
    		glTexCoord2f(1.0,1.0);
    		glVertex3fv(vertices[c]);
    		//glColor3fv(colors[d]);
    		glTexCoord2f(1.0,0.0);
    		glVertex3fv(vertices[d]);
    	glEnd();
    	/*Here are the vertices that he used
    	glBegin(GL_QUADS);
    	 glVertex3f(-2.0,2.0,0.0);
    	 glVertex3f(-2.0,-2.0,0.0);
    	 glVertex3f(2.0,-2.0,0.0);
    	 glVertex3f(2.0,2.0,0.0);
    	glEnd();*/
    }
    
    void colorcube(void)
    { //function is used to state which sections of the array for colors and vertext to use
    /*map vertices to faces */
    
    	polygon(0, 3, 2, 1);
    	polygon(2, 3, 7, 6);
    	polygon(3, 0, 4, 7);
    	polygon(1, 2, 6, 5);
    	polygon(4, 5, 6, 7);
    	polygon(5, 4, 0, 1);
    }
    
    static GLfloat theta[] = {0.0,0.0,0.0};  //declaring two static variables theta and axis
    static GLint axis = 2;
    
    //display function
    void display(void)
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    		glLoadIdentity();
    		glRotatef(theta[0], 1.0,0.0,0.0);
    		glRotatef(theta[1], 0.0,1.0,0.0);
    		glRotatef(theta[2], 0.0,0.0,1.0);
    
    		colorcube();  //refers to the function that mapped the vertices to faces
    
    		glutSwapBuffers();
    
    }
    
    //The below section contains the interaction component of the program
    void spinCube()
    {
    	theta[axis] += 0.05;
    	if(theta[axis] > 360.0)
    		theta[axis] -= 360.0;
    	glutPostRedisplay();
    
    }
    
    void mouse(int btn, int state, int x, int y)
    {
    //Mouse button interaction
    
    	if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    		axis = 0;
    	if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
    		axis = 1;
    	if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    		axis = 2;
    }
    
    void myReshape(int w, int h)
    {
    	glViewport(0,0,w,h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	if(w <= h)
    		glOrtho(-2.0, 2.0, -2.0* (GLfloat) h/ (GLfloat) w, 2.0 *(GLfloat) h/ (GLfloat) w, -10.0, 10.0);
    	else
    		glOrtho(-2.0* (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h/ (GLfloat) w, -2.0,2.0,-10.0,10.0);
    	glMatrixMode(GL_MODELVIEW);
    }
    
    int main(int argc, char ** argv)
    {
    	GLubyte image[64][64][3];  //seems like 64 by 64 pixels taking a vertice made up of 3 points
    	int i, j, r, c;
    	for(i =0; i <64; i++)
    	{
    		for(j=0; j<64; j++)
    		{
    			c = ((((i & 0x8)== 0)^((j & 0x8)) == 0))*255;  //I think 0x8 has to always be written like this, not 0 x 8 or it will cause problems.
    			image[i][j][0] = (GLubyte) c;
    			image[i][j][1] = (GLubyte) c;
    			image[i][j][2] = (GLubyte) c;
    		}
    	}
    
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowSize(500,500);
    	glutCreateWindow("colorcube");
    
    	//Place initialization function here
    	initRendering();
    
    	glutReshapeFunc(myReshape);
    	glutDisplayFunc(display);
    	glutIdleFunc(spinCube);
    	glutMouseFunc(mouse);
    	glEnable(GL_DEPTH_TEST);  //glEnables can be moved to the initialization function
    	glEnable(GL_TEXTURE_2D);  //This is where texturing starts
    
    	//Texture section, All of this can be defined in a load texture function
    	//	glTexImage2D(GL_TEXTURE, 0,3,64,64,0, GL_RGB, GL_UNSIGNED_BYTE, image);
    	//	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);  //Many different parameters can be set her to manipulate the textures
    	//	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    	//	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    	//	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //part of the original program
    
    	glutMainLoop();
    
    	return 0;
    
    }
    Attached Images Attached Images SDL and OpenGL problems-bricks-bmp 
    Attached Files Attached Files

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    At first it was saying that it didnot recognized glTexImage2D, so I included glext.h still have that same issue though.
    Sounds like it's a linker error, especially since your code compiles just fine for me. You need to link with the SDL, with OpenGL, and with glut individually. That probably means linking with SDL.lib, GL.lib, and glut.lib (for Windows) or possibly libSDL.a, libGL.a, and libglut.a (for Linux or Macs, and also for certain Windows compilers). Here's the Linux command line that works for me.
    Code:
    gcc sdl1.c -o sdl1 -lGL -lglut -lSDL
    When I leave out one of those libraries, for example the SDL, I get linker error messages:
    Code:
    $ gcc sdl1.c -o sdl1 -lGL -lglut
    /tmp/ccuaHAil.o: In function `loadTexture':
    sdl1.c:(.text+0x57): undefined reference to `SDL_RWFromFile'
    sdl1.c:(.text+0x64): undefined reference to `SDL_LoadBMP_RW'
    sdl1.c:(.text+0x133): undefined reference to `SDL_FreeSurface'
    collect2: ld returned 1 exit status
    It's important to be able to distinguish between compiler errors and linker errors; the former you fix by including more header files, and the latter you fix by linking with the libraries that actually contain the code. ld is the Linux linker in this case.

    When I run your program I see a texture mapped cube of sorts, although it looks like your colours are not mapped properly; i.e. maybe you need to use RBG somewhere instead of RGB. I remember having this issue myself, if you can't figure it out let me know and I can try to dig up the solution again. Good luck!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Problems(again)
    By Xterria in forum Game Programming
    Replies: 8
    Last Post: 06-23-2004, 01:21 PM
  2. OpenGL Problems(again)
    By Xterria in forum Game Programming
    Replies: 5
    Last Post: 06-03-2004, 03:41 AM
  3. OpenGL Problems(again)
    By Xterria in forum Game Programming
    Replies: 2
    Last Post: 03-28-2004, 06:46 PM
  4. OpenGL problems
    By Mecha in forum Game Programming
    Replies: 5
    Last Post: 01-07-2004, 08:21 PM
  5. OPENGL noo--bee, fullscreen problems
    By Shadow12345 in forum Game Programming
    Replies: 2
    Last Post: 08-13-2002, 01:53 PM

Tags for this Thread