Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,456
4,164
Isla Nublar
Hi guys,

I'm brushing up (aka relearning OpenGL) and for some reason my line will not draw, but my triangle does (I added the triangle to make sure I could render things, the line is not hidden because of the triangle).

Here is my code:

Code:
void RenderScene()
{
    
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    
    //glTranslatef(0.0f,0.0f,-6.0f);	
    
    glColor3f(1.0f, 0.0f, 0.0f);
    
    //Load up line (THIS DOESN"T WORK)
    glBegin(GL_LINES);
        glVertex3f(180, 15, 0);
        glVertex3f(10, 45, 0);
        cout << "TEST" << endl;
    glEnd();
    
//(THIS DOES)
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 1.0f, 0.0f);				// Top
        glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
        glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
	glEnd();							// Finished Drawing The Triangle

   
    //glFlush();
    glutSwapBuffers();  
}

My code reaches the "test" line that writes to the console, but the line will not render. Any ideas?
 

SidBala

macrumors 6502a
Jun 27, 2010
533
0
That line looks to be outside the view volume.

The default view volume is a cube with sides of 2 with the camera at the center pointing towards the negative z direction. The default projection is ortho.

Unless if you changed the defaults, 180,15,0 and 10,45,0 are completely outside of the view volume.

Try something inside, for example:

Code:
glBegin(GL_LINES);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();

You should also put in the translation by -6 if you are using a perspective camera.
 

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,456
4,164
Isla Nublar
That line looks to be outside the view volume.

The default view volume is a cube with sides of 2 with the camera at the center pointing towards the negative z direction. The default projection is ortho.

Unless if you changed the defaults, 180,15,0 and 10,45,0 are completely outside of the view volume.

Try something inside, for example:

Code:
glBegin(GL_LINES);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();

You should also put in the translation by -6 if you are using a perspective camera.

Oh I am an idiot. Thanks so much Sidbala that was the problem :) (I'm pretty rusty with OpenGL, I haven't used it since December :rolleyes:) I'm getting back into it for good now though :)
 

holmesf

macrumors 6502a
Sep 30, 2001
528
25
Hi guys,

I'm brushing up (aka relearning OpenGL) and for some reason my line will not draw, but my triangle does (I added the triangle to make sure I could render things, the line is not hidden because of the triangle).

Here is my code:

Code:
void RenderScene()
{
    
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    
    //glTranslatef(0.0f,0.0f,-6.0f);	
    
    glColor3f(1.0f, 0.0f, 0.0f);
    
    //Load up line (THIS DOESN"T WORK)
    glBegin(GL_LINES);
        glVertex3f(180, 15, 0);
        glVertex3f(10, 45, 0);
        cout << "TEST" << endl;
    glEnd();
    
//(THIS DOES)
    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 1.0f, 0.0f);				// Top
        glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
        glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
	glEnd();							// Finished Drawing The Triangle

   
    //glFlush();
    glutSwapBuffers();  
}

My code reaches the "test" line that writes to the console, but the line will not render. Any ideas?

If you're in the process of relearning OpenGL, I heavily advise learning OpenGL 3. Every single gl function call in that code sample is deprecated with the exception of glClear and glClearColor. For desktop OpenGL that means you aren't following best practices (for example immediate mode drawing using glBegin() / glEnd() is the slowest possible way to put anything on the screen) and you may face compatibility problems later on.
It also means that the program won't work with OpenGL ES or WebGL. You should be careful not to use the deprecated functionality because, frankly, it's already obsolete. You'd be doing yourself a disservice to learn it.

Word on the street is that in Mac OS X 10.7 the OpenGL 3 compatibility context is not available ... meaning to use any new OpenGL features you'll have to strip off all deprecated functionality from your program.

Following a modern approach will also teach you a lot about how graphics really works behind the scenes. You'll gain a better understanding of the mathematics behind it and the modern programmable graphics pipeline. Employers will like it too, if that matters to you (I know it wouldn't have mattered to me when I started learning this stuff).

Here is a set of tutorials that uses a modern OpenGL 3 approach that doesn't rely on deprecated functionality:
http://ogldev.atspace.org/
 
Last edited:

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,456
4,164
Isla Nublar
If you're in the process of relearning OpenGL, I heavily advise learning OpenGL 3. Every single gl function call in that code sample is deprecated with the exception of glClear and glClearColor. For desktop OpenGL that means you aren't following best practices (for example immediate mode drawing using glBegin() / glEnd() is the slowest possible way to put anything on the screen) and you may face compatibility problems later on.
It also means that the program won't work with OpenGL ES or WebGL. You should be careful not to use the deprecated functionality because, frankly, it's already obsolete. You'd be doing yourself a disservice to learn it.

Word on the street is that in Mac OS X 10.7 the OpenGL 3 compatibility context is not available ... meaning to use any new OpenGL features you'll have to strip off all deprecated functionality from your program.

Following a modern approach will also teach you a lot about how graphics really works behind the scenes. You'll gain a better understanding of the mathematics behind it and the modern programmable graphics pipeline. Employers will like it too, if that matters to you (I know it wouldn't have mattered to me when I started learning this stuff).

Here is a set of tutorials that uses a modern OpenGL 3 approach that doesn't rely on deprecated functionality:
http://ogldev.atspace.org/

Thank you so much for this information! I can't wait to try these tutorials. I thought my stuff may be deprecated but I was used to XCode telling me when it was or not, and since it didn't (I guess theres some checks I don't have turned on) I thought I was in the clear. I will definitely be digging into these tutorials tonight!

Also the employer thing does matter a lot to me since I graduate in 5 months :) If I snag a programming job the last thing I want to be is the new guy who doesn't know anything : / (Thats actually my biggest fear at the moment).
 

holmesf

macrumors 6502a
Sep 30, 2001
528
25
Thank you so much for this information! I can't wait to try these tutorials. I thought my stuff may be deprecated but I was used to XCode telling me when it was or not, and since it didn't (I guess theres some checks I don't have turned on) I thought I was in the clear. I will definitely be digging into these tutorials tonight!

Also the employer thing does matter a lot to me since I graduate in 5 months :) If I snag a programming job the last thing I want to be is the new guy who doesn't know anything : / (Thats actually my biggest fear at the moment).

OS X 10.6 is still using OpenGL 2 so the features aren't officially deprecated yet. But this is mostly Apple's failing ... it's taken them too long to adopt OpenGL 3. It's pretty safe to program as if you were using OpenGL 3, however, because OS X is really only missing one or two features of OpenGL 3 these days. Sometimes you'll just have to do silly things to access the features like writing "glBindVertexArrayAPPLE" instead of "glBindVertexArray" to access OpenGL 3 features which exist in OpenGL 2 only as extensions. If you restrict yourself to the features available in the OpenGL 3 core profile you'll also be following best practices and be more easily able to port the code to OpenGL ES and WebGL (or even DirectX for that matter).

When OS X 10.7 arrives you'll get the choice to program in OpenGL 2 (and use the deprecated functionality) or throw up an OpenGL 3 context and get any new OpenGL 3 features (with no deprecated functionality allowed). Unlike in some operating systems you will not be able to put up a "compatibility profile" context which allows OpenGL 3 features while also using the deprecated functionality.

If you are interviewing at game companies, they will probably want to make sure you know how to program with shaders (instead of using the older fixed functionality model) and that you understand the various stages of the graphics pipeline. If you're not interviewing at game companies or graphics companies you'll probably have more experience with OpenGL than the person interviewing you, so you probably won't have to worry :)
 
Last edited:

MorisT

macrumors newbie
Aug 16, 2011
4
0
Check OpenGL for error states. Use glslDevil, glIntercept or gDebugger. Check the glGetError function. Can you test whether SDL actually acquired a device context? Does the Window reflect changes in the glClearColor call? Don't use 0.5 as an alpha value in glClearColor. Try these suggestions and report back with a minimal example as Simucal suggested.
:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.