前言
最近突然打算写OpenGL1.0,于是打开GLFW,配置上下文,创建失败… 看来GLFW只支持现代OpenGL 于是我又转向FreeGLUT,打开示例,发现FreeGLUT原来是支持现代OpenGL的 OpenGL3.1官方示例 FreeGLUT 如何在Windows 10(64位)的MinGW-w64上设置OpenGL
FreeGLUT函数说明
1.glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); '设置显示模式,这里是双缓冲(GLUT_DOUBLE),所以需要调用glutSwapBuffers(); 2.glutInitContextVersion(3, 3); 设置上下文版本,这里是OpenGL3.3 3.glutInitContextFlags(GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG); 设置上下文标志(???),向前兼容(GLUT_FORWARD_COMPATIBLE) 4.glutInitContextProfile(GLUT_CORE_PROFILE); 设置为核心模式(???)
代码
#include "glad.c"
#include <GL/freeglut.h>
#include <iostream>
const char* vSource = "#version 330 core\nlayout(location=0)in vec3 a_position;\nlayout(location=1)in vec3 a_color;\nout vec3 v_color;\nvoid main(){gl_Position = vec4(a_position,1.0);\nv_color = a_color;}";
const char* fSource = "#version 330 core\nin vec3 v_color;\nout vec4 o_color;\nvoid main(){o_color = vec4(v_color,1.0);}";
GLfloat ver[] =
{
0.0, 0.5, 0.0,
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0
};
GLfloat col[] =
{
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
};
GLuint program;
GLuint vao;
void display();
void keyboard(unsigned char key, int x, int y);
bool init();
bool loadProgram();
bool loadVAO();
void debug(char* title);
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitContextVersion(3, 3);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500, 500);
glutCreateWindow("GLUT - OpenGL 3.3");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
if (!init())
{
std::cout << "Failed to initialize." << std::endl;
return -1;
}
glutMainLoop();
return 0;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
}
}
bool init()
{
if (!gladLoadGL())
{
std::cout << "Failed to init GL." << std::endl;
return false;
}
if (!loadProgram())
{
std::cout << "Failed to init program." << std::endl;
return false;
}
if (!loadVAO())
{
std::cout << "Failed to init VAO." << std::endl;
return false;
}
return true;
}
bool loadProgram()
{
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vSource, nullptr);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fSource, nullptr);
glCompileShader(fs);
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (success == false)
{
std::cout << "Failed to link program." << std::endl;
return false;
}
debug("loadProgram");
return true;
}
bool loadVAO()
{
GLuint vbo1, vbo2;
glGenBuffers(1, &vbo1);
glGenBuffers(1, &vbo2);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
glBufferData(GL_ARRAY_BUFFER, sizeof(ver), ver, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
glBufferData(GL_ARRAY_BUFFER, sizeof(col), col, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
glBindVertexArray(0);
glDeleteBuffers(1, &vbo1);
glDeleteBuffers(1, &vbo2);
debug("loadVAO");
return true;
}
void debug(char* title)
{
GLenum error = glGetError();
if (error != GL_NO_ERROR)
{
std::cout << title << ":" << error << std::endl;
}
}
运行参数
我是用MinGW编译的,所以要加上参数
-lgdi32 -lfreeglut -lopengl32
运行结果
|