Steward
分享是一種喜悅、更是一種幸福
程式語言 - The OpenGL Utility Toolkit (GLUT) - OpenGL 1.0 - Interpolate Color
參考資訊:
https://github.com/gamedev-net/nehe-opengl
https://cs.lmu.edu/~ray/notes/openglexamples/
https://www.khronos.org/opengl/wiki/Code_Resources
https://www.opengl.org/resources/libraries/glut/glut_downloads.php
main.c
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.0f, 0.0f, -5.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();
glLoadIdentity();
glTranslatef(1.0f, 0.0f, -5.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
glFlush();
}
int main(int argc, char * argv[])
{
const int w = 320;
const int h = 240;
GLdouble nearPlane = 0.1;
GLdouble farPlane = 100.0;
GLdouble top = nearPlane * tan(45.0 * 3.14159 / 360.0);
GLdouble right = top * ((GLfloat)w / (GLfloat)h);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(w, h);
glutCreateWindow("main");
glutDisplayFunc(display);
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glFrustum(-right, right, -top, top, nearPlane, farPlane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutMainLoop();
return 0;
}
編譯、執行
$ gcc main.c -o main -lglut -lGL $ ./main