Jump to content
  • Advertisement
Sign in to follow this  
brodi816

OpenGL Opengl Initialization problem

This topic is 3059 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

The code below does not want to draw anything at all. I had it drawing 2d polygons, but I want it to draw 3d polygons.
int Window::Create(GraphicsManager::Graphics*graphics,std::string title,int width,int height,int bpp=32,bool fullscreen=false)
        {
            if(SDL_Init(SDL_INIT_EVERYTHING)!=0)
            {
                return 1;
            }
            SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
            //Set up window
            if(fullscreen)
            graphics->_Buffer=SDL_SetVideoMode(width,height,bpp,SDL_OPENGL|SDL_FULLSCREEN);
            else
                graphics->_Buffer=SDL_SetVideoMode(width,height,bpp,SDL_OPENGL);
            if(graphics->_Buffer==NULL)//make sure no problems creating window
                return 2;
            //enable stuff
            glEnableStuff(width,height);

            glLoadIdentity();
            SDL_WM_SetCaption(title.c_str(),NULL);//set the window title

            //set stuff
            _W=width;
            _H=height;
            _BPP=bpp;
            _Fullscreen=fullscreen;
            _Title=title;
            _Graphics=graphics;
            graphics->_W=width;
            graphics->_H=height;
            return 0;
        }

        void Window::glEnableStuff(int width,int height)
        {
            glEnable(GL_TEXTURE_2D);
            glEnable(GL_NORMALIZE);
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

            glShadeModel(GL_SMOOTH);
            glClearDepth(1.0f);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LEQUAL);
            glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
            glLoadIdentity();


            glMatrixMode(GL_PROJECTION);
            glViewport(0,0,width,height);
            gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 0.1f ,150.0f);
            glOrtho(0.0f,width,height,0.0f,-1.0f,150.0f);
            glLoadIdentity();


            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
        }

Share this post


Link to post
Share on other sites
Advertisement
gluPerspective and glOrtho are 2 different ways of creating a perspective matrix. You are using both. If you want 3D with perspective use gluPerspective only. glOrtho creates a matrix that projects objects with no vanishing point perspective.


gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 0.1f ,150.0f);
glOrtho(0.0f,width,height,0.0f,-1.0f,150.0f);



-me

Share this post


Link to post
Share on other sites
Getting rid of glOrtho still does not work for me

my drawing functions
void Graphics::DrawQuad(int x,int y,int w,int h,Color color)
{
glColor3f((float)color._r/255,(float)color._g/255,(float)color._b/255);
//top left
glVertex3f((float)x,(float)y,0.0);
//top right
glVertex3f((float)x+w,(float)y,0.0);
//bottom right
glVertex3f((float)x+w,(float)y+h,0.0);
//bottom left
glVertex3f((float)x,(float)y+h,0.0);
glColor3f(1.0,1.0,1.0);
}

the drawcube is not complete yet
void Graphics::DrawCube(int x,int y,int z,int w,int h,int d,Color color)
{
glColor3f((float)color._r/255,(float)color._g/255,(float)color._b/255);

//front
glVertex3f(x,y,z);
glVertex3f(x+w,y,z);
glVertex3f(x+w,y+h,z);
glVertex3f(x,y+h,z);

glColor3f(1.0,1.0,1.0);
}

Share this post


Link to post
Share on other sites
Rather than the specific drawing functions, I was more curious about the actual coordinates you were trying to draw your object at.

3D is a lot different than 2d where you just specify the pixel coordinates. In 3D your camera is at coordinates (0,0,0) looking in the negative z direction.




gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 0.1f ,150.0f);
...
glVertex3f((float)x,(float)y,0.0);
glVertex3f((float)x+w,(float)y,0.0);
glVertex3f((float)x+w,(float)y+h,0.0);
glVertex3f((float)x,(float)y+h,0.0);


This won't show up because a Z coordinate of 0.0 is outside your view frustum (its closer than the near clipping plane of 0.1f). You need to read up more about 3D graphics and the functions you're using. I don't think you're understanding the arguments you're passing to gluPerspective.

Try a quad with these coordinates and see if you can see it:

(-1,-1,-5)
(1,-1,-5)
(1,1,-5)
(-1,1,-5)



Share this post


Link to post
Share on other sites
I tried manually drawing quad at the positions and it did not show up.

code:
int main(int argc,char*args[])
{
freopen( "CON", "wt", stdout );
freopen( "CON", "wt", stderr );
game.Create(&gfx,"test",800,600,32,false);
gfx.SetClearColor(Shadow::GraphicsManager::Colors::CornFlowerBlue);
gfx.BeginDraw();
glLoadIdentity();
gfx.BeginQuad();
gfx.DrawCube(10,10,1,10,10,10,Shadow::GraphicsManager::Colors::Black);
gfx.DrawCube(-1,-1,-5,2,2,2,Shadow::GraphicsManager::Colors::Black);
gfx.EndQuad();
gfx.EndDraw();
SDL_Delay(5000);
game.Quit();
return 0;
});

Share this post


Link to post
Share on other sites
Quote:
Original post by brodi816

glMatrixMode(GL_PROJECTION);
glViewport(0,0,width,height);
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 0.1f ,150.0f);
glOrtho(0.0f,width,height,0.0f,-1.0f,150.0f);
glLoadIdentity();


Palidine already pointed out not to use glOrtho, but are you really setting the projection matrix back to identity after you set it up? I'm sure that is not what you want. That line should also be removed.

Share this post


Link to post
Share on other sites
I fixed it, my init code.

glEnable(GL_TEXTURE_2D);
glEnable(GL_NORMALIZE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

glShadeModel(GL_SMOOTH);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,150.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!