Sign in to follow this  
TheAirPump

Getting started with OpenGL?

Recommended Posts

Alright, I've used several different languages before, such as DarkBasic Pro and BlitzMax, and I've finally learned C++. However, because I'd like to provide cross-platform capability to my games, I'd like to learn how to use OpenGL with C++. There doesn't seem to be a tutorial for it in the OpenGL section, and I've had trouble finding information on anything but extremely outdated versions on Google. So, can anyone help me find OpenGl, and some tutorials to help me use it properly?

Share this post


Link to post
Share on other sites
Someone needs to make a tutorial using only forward-compatible OpenGL 3 stuff (no deprecated items used). And preferably include some examples with the direct state access extension, which I'm sure will get into core by the next revision.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kwizatz
Try the tutorials at NeHe's.

You don't "find" OpenGL, its implementation is provided with your video card drivers, the headers and libraries are usually installed along with the compiler, but you can get the latest extension definitions from here.


Thank you. That's cool, but is there anything on making 2D in OpenGL? I'd like to start with 2D.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheAirPump
Thank you. That's cool, but is there anything on making 2D in OpenGL? I'd like to start with 2D.


I'd strongly suggest getting SFML and using OpenGL via it for starting out in 2D. You will be able to start directly with OpenGL and not worry about having to set it up. Here's a simple example:

#include <SFML/Graphics.hpp>
#include <iostream>

#pragma comment(lib, "sfml-graphics-s-d.lib")
#pragma comment(lib, "sfml-window-s-d.lib")
#pragma comment(lib, "sfml-system-s-d.lib")
#pragma comment(lib, "glu32.lib")

// http://www.gamedev.net/community/forums/topic.asp?topic_id=104791
void glEnable2D()
{
int vPort[4];
glGetIntegerv(GL_VIEWPORT, vPort);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}

// http://www.gamedev.net/community/forums/topic.asp?topic_id=104791
void glDisable2D()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

int main()
{
// Create main window
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML OpenGL");
App.PreserveOpenGLStates(true);

// Enable 2D mode
glEnable2D();

// Create a clock for measuring time elapsed
sf::Clock Clock;

// No fps limit
App.SetFramerateLimit(0);

// Start game loop
while(App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();

// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();

// Adjust the viewport when the window is resized
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}

glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslated(325, 200, 0);
glTranslated(sin(Clock.GetElapsedTime()) * 50, cos(Clock.GetElapsedTime()) * 50, 0);

// Draw a triangle
glBegin(GL_TRIANGLES);
glColor3ub(255, 0, 0); glVertex2d(0, 0);
glColor3ub(0, 255, 0); glVertex2d(100, 0);
glColor3ub(0, 0, 255); glVertex2d(50, 50);
glEnd();

// Finally, display the rendered frame on screen
App.Display();
}
return EXIT_SUCCESS;
}



With this framework setup, you can then read up on any OpenGL tutorials that explain the basics of texture loading. After that, you just use it in a 2D fashion (self plug) and you have the basics for representing objects graphically on the screen.

Once you can actually get a textured object on the screen, then you can just add in the other aspects of a game, such as input and audio (both doable via SFML, OpenGL is for graphics only). You might need to implement some OpenGL picking if you want to be able to click on "objects" (link 1, link 2). If you want to get into collisions, you can do it yourself or use a free ismple library such as coldet (remember 2D is 3D with all Z values being the same [wink]). When you want to get into using OpenGL examples you can use something like GLEE or GLEW.

It's a lot of just "cherry picking" from resources you find in terms of using OpenGL to make games with. As mentioned using thered book will be the single most concentrated source of OpenGL techniques that you will need to study. There is also the main OpenGL site that has a collection of other important information as well.

Oh and don't forget to check out GameDev's OpenGL Forum FAQ which provides a lot of detailed information as well. Good luck learning!

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
Quote:
Original post by TheAirPump
Thank you. That's cool, but is there anything on making 2D in OpenGL? I'd like to start with 2D.


I'd strongly suggest getting SFML and using OpenGL via it for starting out in 2D. You will be able to start directly with OpenGL and not worry about having to set it up. Here's a simple example:
*** Source Snippet Removed ***
With this framework setup, you can then read up on any OpenGL tutorials that explain the basics of texture loading. After that, you just use it in a 2D fashion (self plug) and you have the basics for representing objects graphically on the screen.

Once you can actually get a textured object on the screen, then you can just add in the other aspects of a game, such as input and audio (both doable via SFML, OpenGL is for graphics only). You might need to implement some OpenGL picking if you want to be able to click on "objects" (link 1, link 2). If you want to get into collisions, you can do it yourself or use a free ismple library such as coldet (remember 2D is 3D with all Z values being the same [wink]). When you want to get into using OpenGL examples you can use something like GLEE or GLEW.

It's a lot of just "cherry picking" from resources you find in terms of using OpenGL to make games with. As mentioned using thered book will be the single most concentrated source of OpenGL techniques that you will need to study. There is also the main OpenGL site that has a collection of other important information as well.

Oh and don't forget to check out GameDev's OpenGL Forum FAQ which provides a lot of detailed information as well. Good luck learning!


Thank you! That's incredibly helpful; I'll be sure to check that out.

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