Jump to content

  • Log In with Google      Sign In   
  • Create Account

Need advice on my code and help with Skybox implementation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 ThatHippieFromBethlehem   Members   -  Reputation: 112

Like
0Likes
Like

Posted 26 May 2014 - 03:18 AM

Hi All,

 

I've been learning SDL2 and Opengl for about a month now and need advice on my code alot of which from various resources online.

I've also been adding it all up ie. background music and texture so now I would like to add background Image to the rotating textured cube which is essentially a skybox. Any advice on the skybox and on my code is welcomed. Feel free to use this code because I'm pretty sure you'll find similar ones out there.

 

My Code here:

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <GL/glu.h>

void Reshape();
void Render();

double rotquad;
int WIDTH = 640;
int HEIGHT = 480;
SDL_Window *window;
SDL_Surface *surface;
Mix_Music *music = NULL;
GLuint texture;

int main(int argc, char* argv[])
{
	Uint32 last_time = SDL_GetTicks();
	Uint32 current_time,ellapsed_time;

	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
	Mix_Init(MIX_INIT_MP3);
	IMG_Init(IMG_INIT_JPG);
	SDL_ShowCursor(0);
	
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

    	window = SDL_CreateWindow("3D",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_OPENGL);
	
	SDL_GLContext context = SDL_GL_CreateContext(window);
	
	surface = IMG_Load("texture.jpg");

	Reshape();

	Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
	Mix_AllocateChannels(0);
        music = Mix_LoadMUS("sound.wav");
        Mix_PlayMusic(music, -1);

	while (1) 
	{
		SDL_Event event;

    		while (SDL_PollEvent(&event))
			{
			switch(event.type)
				{
				case SDL_QUIT:
					SDL_GL_DeleteContext(context);
        				SDL_DestroyWindow(window);
					Mix_FreeMusic(music);
		                        Mix_CloseAudio();
					Mix_Quit();
					IMG_Quit();
    					SDL_Quit();
					return 0;
				break;
				}
    			}
		Mix_PlayingMusic();

		current_time = SDL_GetTicks();
		ellapsed_time = current_time - last_time;
		last_time = current_time;
 
		rotquad += 0.05 * ellapsed_time;

		Render();
	
		ellapsed_time = SDL_GetTicks();
		if (ellapsed_time < 10)
		{
			SDL_Delay(10 - ellapsed_time);
		}
	}
	return 0;
}

void Reshape()
{
	glClearColor(0.3, 0.3, 0.3, 1.0);
	glViewport(0, 0, WIDTH, HEIGHT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45, WIDTH / HEIGHT, 1, 1000);
	glMatrixMode(GL_MODELVIEW);
	glClearDepth(1);
	glEnable(GL_DEPTH_TEST);

	glGenTextures(1,&texture);
	glBindTexture(GL_TEXTURE_2D,texture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w,surface->h, 0, GL_RGB,GL_UNSIGNED_BYTE,surface->pixels);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}

void Render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

	glBindTexture(GL_TEXTURE_2D,texture);

	glRotated(rotquad, 0, 1, 0);
	glRotated(rotquad, 1, 1, 1);

	glEnable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
	glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
  	glTexCoord2i(0, 1); glVertex3f(-1, -1,  1);
  	glTexCoord2i(1, 1); glVertex3f(-1,  1,  1);
  	glTexCoord2i(1, 0); glVertex3f(-1,  1, -1);

  	glTexCoord2i(0, 0); glVertex3f( 1, -1, -1);
  	glTexCoord2i(0, 1); glVertex3f( 1, -1,  1);
  	glTexCoord2i(1, 1); glVertex3f( 1,  1,  1);
  	glTexCoord2i(1, 0); glVertex3f( 1,  1, -1);

  	glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
  	glTexCoord2i(0, 1); glVertex3f(-1, -1,  1);
  	glTexCoord2i(1, 1); glVertex3f( 1, -1,  1);
  	glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);

  	glTexCoord2i(0, 0); glVertex3f(-1,  1, -1);
  	glTexCoord2i(0, 1); glVertex3f(-1,  1,  1);
  	glTexCoord2i(1, 1); glVertex3f( 1,  1,  1);
  	glTexCoord2i(1, 0); glVertex3f( 1,  1, -1);

  	glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
  	glTexCoord2i(0, 1); glVertex3f(-1,  1, -1);
  	glTexCoord2i(1, 1); glVertex3f( 1,  1, -1);
  	glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);

  	glTexCoord2i(0, 0); glVertex3f(-1, -1,  1);
  	glTexCoord2i(0, 1); glVertex3f(-1,  1,  1);
  	glTexCoord2i(1, 1); glVertex3f( 1,  1,  1);
  	glTexCoord2i(1, 0); glVertex3f( 1, -1,  1);
	glEnd();
	glDisable(GL_TEXTURE_2D);

	glFlush();

	SDL_GL_SwapWindow(window);
}

See attached file for screenshot. smile.png

 

Attached Thumbnails

  • 3D.png


Sponsor:

#2 krinosx   Members   -  Reputation: 565

Like
2Likes
Like

Posted 26 May 2014 - 09:19 AM

Hummm may I suggest you to migrate to a 'modern' OpenGL approach?

 

I think using 'glBegin' and setting point to point coordinates are not recommended anymore...

 

I think you may take a look in this site: http://arcsynthesis.org/gltut/ and try to figure out how to implement your code using this approach!

 

[]´s



#3 ThatHippieFromBethlehem   Members   -  Reputation: 112

Like
0Likes
Like

Posted 26 May 2014 - 06:52 PM

Hi krinosx,

 

Thank you for the advice and the link it will be useful. :)

I will migrate to modern OpenGL eventually but my hardware isn't that great.

I think I can only go upto OpenGL 3.1. I'll get new hardware soon.



#4 krinosx   Members   -  Reputation: 565

Like
0Likes
Like

Posted 28 May 2014 - 08:57 AM

I think 3.0 will suffice to use VOs and the new techniques....
 
I was trying to learn the OGL 3.0+ but I had to stop due to my 'end course monograph'... I am doing some research in OpenCL... so I have no time to study OpenGL right now...
 
Obout my OpenGL studies... I had achieved some results with VOs in a  OpenGL + SDL2 setup... I did just a little progress... but I have some textured objects, an OBJ file importer and 'primitive camera control'... I was starting to work with 'normals' but I got stuck with some vector math...
 
If you want to take a look in what I got so far, just tell me and I share my BitBucket repository with you...
 
[]´s


#5 ThatHippieFromBethlehem   Members   -  Reputation: 112

Like
0Likes
Like

Posted 29 May 2014 - 03:41 AM

 

I think 3.0 will suffice to use VOs and the new techniques....
 
I was trying to learn the OGL 3.0+ but I had to stop due to my 'end course monograph'... I am doing some research in OpenCL... so I have no time to study OpenGL right now...
 
Obout my OpenGL studies... I had achieved some results with VOs in a  OpenGL + SDL2 setup... I did just a little progress... but I have some textured objects, an OBJ file importer and 'primitive camera control'... I was starting to work with 'normals' but I got stuck with some vector math...
 
If you want to take a look in what I got so far, just tell me and I share my BitBucket repository with you...
 
[]´s

 

Thanks that would boost my learning!!! :) PM?



#6 krinosx   Members   -  Reputation: 565

Like
0Likes
Like

Posted 29 May 2014 - 09:19 AM

Well.. I changed the repository to 'public' so feel free to access, clone and or just consult it at:

 

https://bitbucket.org/krinosx/studycorner

 

 

My commit comments are in portuguese, but, as far as I remember, the code and code comments are in english... 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS