C Glut OpenGL & Xcode Project

Two people asked me about OpenGL C and Xcode this last week. So I figured I’d make a quick video to show an easy way to get up and running.

Little Mistake
Note that you should change the Active Architecture of your project to i386. I forgot to do that in the video. It will still work if you don’t do this, but you’ll get some loading errors in the console… so, make this change after you’ve watched the video:

The first 3 minutes of this video show everything you need to know, the next 7 minutes is just me sort of rambling about a few things that you may or may not need to know, so feel free to just watch the first 3 minutes and then go and code some OpenGL.


Source for template is at bottom of post.

Links:
Red Book
stdlib.h
vbl sync

Here is the source for the 2D template, once you’ve got your project set up, just copy it into main.c and compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
 
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
 
int mouseX = 0;
int mouseY = 0;
int width;
int height;
 
void init(){
	glPointSize(60.0);
}
 
void render() {
	//glClear(GL_COLOR_BUFFER_BIT);	
 
	glColor4f(1, 1, 1, 0.5);
	glBegin(GL_POINTS);
	glVertex2i(mouseX, mouseY);
	glEnd();
 
	// you need this (see double buffering)
	glutSwapBuffers();
}
 
void resize(int w, int h) {
	glClear(GL_COLOR_BUFFER_BIT);
	width = w;
	height = h;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);  
	glLoadIdentity();            
	glOrtho(0, w, 0, h, -1, 1);   
	glScalef(1, -1, 1);         
	glTranslatef(0, -h, 0);
}
 
void idle() {
	glutPostRedisplay();
}
 
void mouse(int x, int y){
	mouseX = x;
	mouseY = y;
}
 
int main(int argc, char** argv){
 
	glutInit(&argc, argv);
 
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
 
	width = 640;
	height = 480;
	glutInitWindowSize(width, height);
	glutCreateWindow("Template");
	//glutFullScreen();
	//glutSetCursor(GLUT_CURSOR_NONE); 
 
	glutPassiveMotionFunc(mouse);
	glutDisplayFunc(render);
	glutReshapeFunc(resize);
	glutIdleFunc(idle);
 
	glEnable(GL_BLEND);	
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDisable(GL_DEPTH_TEST);
 
	glEnable(GL_POINT_SMOOTH);
	glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
 
	// VBL synching prevent tearing
	GLint sync = 1;
	CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &sync);
 
	init();
 
	glutMainLoop();
 
	return EXIT_SUCCESS;
}


 
 
 

Leave a Reply

Spam protection by WP Captcha-Free