Learning OpenGL from scratch
I have decided to start learning OpenGL on the Mac, and turned to the tutorial at http://nehe.gamedev.net/lesson.asp?index=01 . I created myself a project in Xcode, and ran the program, but I am just getting a blank window. I don't have enough experience in OpenGL to know what I am doing wrong, so I would appreciate someone pointing out my error. The following is based on lesson 2:
Code:
#include <GLUT/glut.h>
#include "tk.h"
#define kWindowWidth 400
#define kWindowHeight 300
#define BOOL short
#define TRUE 1
#define FALSE 0
GLvoid ReSizeGLScene(int width, int height) // Resize And Initialize The GL Window
{
}
void InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return;// TRUE; // Initialization Went OK
}
void DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES); // Drawing Using 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
glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad
return; // Keep Going
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize (kWindowWidth, kWindowHeight);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow (argv[0]);
glutInitWindowPosition (100, 100);
InitGL();
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
//glutVisibilityFunc(vis);
glutMainLoop();
return 0;
}
glutSwapBuffer(); before the return in DrawGLScene() should do it.
edit:buffers, i mean
edit:buffers, i mean
Sir, e^iπ + 1 = 0, hence God exists; reply!
I added
glutSwapBuffers();
just before the return in DrawGLScene(), but now I just get a window filled with black
glutSwapBuffers();
just before the return in DrawGLScene(), but now I just get a window filled with black
Black being your clear color.
Increase clear depth.
Code:
glClearDepth(1.0);Code:
glTranslatef(-1.5f,0.0f,-6.0f);Increase clear depth.
Sir, e^iπ + 1 = 0, hence God exists; reply!
also for your triangle/square not to show up as black you need to specify a color before you start drawing them e.g.
glColor3f(1.0, 1.0, 1.0); //White (glColor3f( float red, float green, float blue)
glBegin(GL_QUADS);
glVertex3f(1.0, 1.0, 1.0);
.
.
.
glEnd();
glColor3f(1.0, 1.0, 1.0); //White (glColor3f( float red, float green, float blue)

glBegin(GL_QUADS);
glVertex3f(1.0, 1.0, 1.0);
.
.
.
glEnd();
-CarbonX
Another problem is you never set up a viewport or coordinate system (either with gluOrtho or gluPerspective). Those should be done in ReSizeGLScene.
BTW, just as a random programming tip, it's redundant to put a return at the end of a function that returns void.
BTW, just as a random programming tip, it's redundant to put a return at the end of a function that returns void.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Learning OpenGL | Justin Brimm | 5 | 2,632 |
May 1, 2003 10:21 AM Last Post: GoodDoug |
|
| Learning | Quicksilver | 1 | 2,274 |
Dec 13, 2002 11:18 AM Last Post: Feanor |
|
| Learning OpenGL | Justin Brimm | 19 | 7,220 |
Dec 13, 2002 03:41 AM Last Post: OneSadCookie |
|

