OpenGL:Hello World

来自WHY42

使用Freeglut搭建OpenGL开发环境。开发环境如下:

  • Win8.1
  • TDM-GCC64

下载Freeglut,新建一个c文件:

#include <stdlib.h>
#include <GL/glut.h>

//refer:http://www.transmissionzero.co.uk/computing/using-glut-with-mingw/

void keyboard(unsigned char key, int x, int y);
void display();

int main(int argc, char** argv){
	glutInit(&argc, argv);
	glutCreateWindow("Hello GLUT!");
	glutKeyboardFunc(&keyboard);
	glutDisplayFunc(&display);
	glutMainLoop();

	return EXIT_SUCCESS;
}

void keyboard(unsigned char key, int x, int y){
	switch(key){
		case '\x1B':
			exit(EXIT_SUCCESS);
			break;
	}
}
void display(){
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0f, 0.0f, 0.0f);
	
	glBegin(GL_POLYGON);
	glVertex2f(-0.5f, 0.5f);
	glVertex2f(0.5f, -0.5f);
	glVertex2f(0.5f, 0.5f);
	glVertex2f(-0.5f, 0.5f);
	glEnd();

	glFlush();
}

新建一个Makefile文件:

OBJS = hello.c

CC = gcc
COMPILER_FLAGS = -Wl,--subsystem,windows
LINKER_FLAGS = -lfreeglut -lopengl32

HEADERS = -I../freeglut/include
LIBS = -L../freeglut/lib/x64

OBJ_NAME = hello.exe


all : $(OBJS)
	$(CC) $(OBJS) $(COMPILER_FLAGS) $(HEADERS) $(LIBS) $(LINKER_FLAGS) -o $(OBJ_NAME)

clean:
	rm -rf *.o *.exe

执行make进行编译。

make