Sign in to follow this  

Need advice on my code and help with Skybox implementation

This topic is 1328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

 

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?

Share this post


Link to post
Share on other sites

This topic is 1328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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