added rotate cube example

This commit is contained in:
Alexander Popov 2015-09-20 23:47:22 +03:00
parent 221e716458
commit 06b1e197e3
5 changed files with 134 additions and 3 deletions

View File

@ -1,6 +1,9 @@
CC=gcc
all: fps_counter
all: fps_counter rotate_cube
fps_counter:
$(CC) fps_counter.c -lglfw -lGL -o fps_counter
rotate_cube:
$(CC) rotate_cube.c -lglfw -lGL -o rotate_cube.exe

View File

@ -1,16 +1,19 @@
CC=tcc
CFLAGS= -I./win32/include -L./win32/lib
all: fps_counter windows_icon
all: fps_counter rotate_cube windows_icon
fps_counter:
$(CC) $(CFLAGS) fps_counter.c -lglfw3 -lopengl32 -o fps_counter.exe
rotate_cube:
$(CC) $(CFLAGS) rotate_cube.c -lglfw3 -lopengl32 -o rotate_cube.exe
windows_icon: icon.o
$(CC) $(CFLAGS) windows_icon.c icon.o -lglfw3 -lopengl32 -o windows_icon.exe
icon.o:
windres -O coff win32/icon/resource.rc -o icon.o
windres -O coff win32/icon/resource.rc -o icon.o
clean:
del *.o

BIN
images/Thumbs.db Normal file

Binary file not shown.

BIN
images/rotate_cube.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

125
rotate_cube.c Normal file
View File

@ -0,0 +1,125 @@
/*
GLFW3 rotate cube
Source: https://github.com/IIIypuk/glfw-examples
*/
#include <stdio.h>
#ifndef __WIN32
#include <windows.h>
#endif
#include <GLFW/glfw3.h>
void keyboard_callback(GLFWwindow *window, int key, int scancode,
int action, int mods);
int rotate_y = 0;
int rotate_x = 0;
#ifndef __WIN32
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
LPSTR lpszCmdLine, int nShowCmd)
#else
int main(int argc, char const *argv[])
#endif
{
GLFWwindow *window;
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(320, 320, "Rorate Cube", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glEnable(GL_DEPTH_TEST);
while (!glfwWindowShouldClose(window))
{
glfwSetKeyCallback(window, keyboard_callback);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glBegin(GL_POLYGON); // Yellow side - FRONT
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, -0.5);
glVertex3f( 0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
glBegin(GL_POLYGON); // White side - BACK
glColor3f(1.0, 1.0, 1.0);
glVertex3f( 0.5, -0.5, 0.5);
glVertex3f( 0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
glBegin(GL_POLYGON); // Purple side - RIGHT
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glEnd();
glBegin(GL_POLYGON); // Green side - LEFT
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
glBegin(GL_POLYGON); // Blue side - TOP
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.5, 0.5, 0.5);
glVertex3f( 0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glEnd();
glBegin(GL_POLYGON); // Red side - BOTTOM
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 0.5, -0.5, -0.5);
glVertex3f( 0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void keyboard_callback(GLFWwindow* window, int key, int scancode,
int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_UP && action == GLFW_PRESS)
rotate_x += 5;
if (key == GLFW_KEY_DOWN && action == GLFW_PRESS)
rotate_x -= 5;
if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
rotate_y -= 5;
if (key == GLFW_KEY_LEFT && action == GLFW_PRESS)
rotate_y += 5;
}