环境
运行环境Mac,Xcode,OpenGL4.1,glew,glfw,glm,soil2
参考书籍:《计算机图形学编程c++版》
以下程序在我的本地都跑出结果了
展示出一个红色的窗口
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void init(GLFWwindow* window){
}
void display(GLFWwindow* window,double currentTime){
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
int main(void){
if(!glfwInit()){exit(EXIT_FAILURE);}
GLFWwindow* window = glfwCreateWindow(600,600,"Chapter2 - program1",NULL,NULL);
glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK){exit(EXIT_FAILURE);}
glfwSwapInterval(1);
init(window);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
while (!glfwWindowShouldClose(window)) {
display(window, glfwGetTime());
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
窗口中间显示一个蓝色的点
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#define numVAOs 1
GLuint renderingProgram;
GLuint vao[numVAOs];
GLuint createShaderProgram(){
const char *vshaderSource =
"#version 430 \n"
"void main(void) \n"
"{ gl_Position = vec4(0.0,0.0,0.0,1.0); }";
const char *fshaderSource =
"#version 430 \n"
"out vec4 color; \n"
"void main(void) \n"
"{color = vec4(0.0,0.0,1.0,1.0); }";
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader,1,&vshaderSource,NULL);
glShaderSource(fShader,1,&fshaderSource,NULL);
glCompileShader(vShader );
glCompileShader(fShader);
GLuint vfProgram = glCreateProgram();
glAttachShader(vfProgram,vShader);
glAttachShader(vfProgram,fShader);
glLinkProgram(vfProgram);
return vfProgram;
}
void init(GLFWwindow* window){
renderingProgram = createShaderProgram();
glGenVertexArrays(numVAOs,vao);
glBindVertexArray(vao[0]);
}
void display(GLFWwindow* window,double currentTime){
glUseProgram(renderingProgram);
glDrawArrays(GL_POINTS,0,1);
}
int main(void){
if(!glfwInit()){exit(EXIT_FAILURE);}
GLFWwindow* window = glfwCreateWindow(600,600,"Chapter2 - program1",NULL,NULL);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK){exit(EXIT_FAILURE);}
glfwSwapInterval(1);
init(window);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
while (!glfwWindowShouldClose(window)) {
display(window, glfwGetTime());
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
绘制点、线、面
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
void drawPoint()
{
/* Draw a point */
// 设置清除背景时用的颜色 RGBA 值
glClearColor (0.0, 0.0, 0.0, 0.0);
// 使用指定的颜色对颜色缓冲区进行填充
glClear(GL_COLOR_BUFFER_BIT);
// 指定光栅化点的直径
glPointSize(2.0f);
// 分隔基元或一组类似基元的顶点,包括GL_LINES、GL_TRIANGLES、GL QUADS、GL QUAD STRIP
glBegin(GL_POINTS);
// 设置当前颜色
glColor3f(1.0, 0.0, 0.0); // Red
// 指定顶点
glVertex2f(0.0f,0.0f);
glVertex2f(0.5f,0.8f);
// 分隔基元或一组类似基元的顶点
glEnd();
}
void drawLint()
{
// 设置清除背景时用的颜色
glClearColor (0.0, 0.0, 0.0, 0.0);
// 使用指定的颜色对颜色缓冲区进行填充
glClear (GL_COLOR_BUFFER_BIT);
//设置线段宽度
glLineWidth(2);
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);
// 定点坐标范围2D点只需要xy,3D要xyz
glVertex2f(-1.0,1.0);
glVertex2f(0.0,-0.5);
glEnd();
}
// 画三角形
void drawTriangle()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); // Red
glVertex3f(0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 0.0); // Green
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(0.0, 0.0, 1.0); // Blue
glVertex3f(1.0, -1.0, 0.0);
glEnd();
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
// 创建窗口
window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL);
if (!window)
{
// 关闭glfw库本身
glfwTerminate();
return -1;
}
/* Make the window's context current */
// 设置参数window中的窗口所关联的OpenGL环境为当前环境。这个环境在当前线程中会一直保持为当前环境,直到另一个环境被设置为当前环境,或者窗口被删除为止
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
// 如果用户准备关闭参数window所指定的窗口,那么此接口将会返回GL_TRUE,否则将会返回GL_FALSE
while (!glfwWindowShouldClose(window))
{
/*your draw*/
// drawPoint();
// drawLint();
drawTriangle();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
// 处理事件
glfwPollEvents();
}
// 关闭glfw库本身
glfwTerminate();
return 0;
}