palmer789 wrote on Saturday, January 23, 2016:
I got it!
I had traveled a long circle for finding the answer.Because of I could not understand it fully, so I will first use a easy way.
Just use the default lib and dll of GLFW, no need to rebuild then use CMake.And use GLEW to get all the GL method through the extension mechanism provied by GLEM.So then if your driver support one method, you can call it through the extension of it.
I will explain more detail after I will have been understanded it.The following is my code, I got it 5 minutes ago.
// Aix3DEdtion.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <GL/glew.h>
#define GLFW_DLL
#define GLFW_INCLUDE_NONE // 使用GLEW里的头文件
#include <glfw3.h>
#include <stdlib.h>
#include <stdio.h>
GLuint g_programObject ;
GLuint LoadShader ( GLenum type, const char *shaderSrc )
{
GLuint shader;
GLint compiled;
// Create the shader object
shader = glCreateShader ( type );
if ( shader == 0 )
{
return 0;
}
// Load the shader source
glShaderSource ( shader, 1, &shaderSrc, NULL );
// Compile the shader
glCompileShader ( shader );
// Check the compile status
glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled )
{
GLint infoLen = 0;
glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );
if ( infoLen > 1 )
{
char *infoLog = (char *)malloc ( sizeof ( char ) * infoLen );
glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );
free ( infoLog );
}
glDeleteShader ( shader );
return 0;
}
return shader;
}
bool Init ()
{
char vShaderStr[] =
"#version 300 es \n"
"layout(location = 0) in vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"} \n";
char fShaderStr[] =
"#version 300 es \n"
"precision mediump float; \n"
"out vec4 fragColor; \n"
"void main() \n"
"{ \n"
" fragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 ); \n"
"} \n";
GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;
// Load the vertex/fragment shaders
vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );
fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );
// Create the program object
programObject = glCreateProgram ( );
if ( programObject == 0 )
{
return 0;
}
glAttachShader ( programObject, vertexShader );
glAttachShader ( programObject, fragmentShader );
// Link the program
glLinkProgram ( programObject );
// Check the link status
glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
if ( !linked )
{
GLint infoLen = 0;
glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
if ( infoLen > 1 )
{
char *infoLog = (char *)malloc ( sizeof ( char ) * infoLen );
glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
free ( infoLog );
}
glDeleteProgram ( programObject );
return false;
}
// Store the program object
g_programObject = programObject;
glClearColor ( 1.0f, 1.0f, 1.0f, 0.0f );
return true;
}
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int _tmain(int argc, _TCHAR* argv[])
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3) ;
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0) ;
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API) ;
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
int result = glfwGetWindowAttrib(window, GLFW_CLIENT_API) ;
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
// start GLEW extension handler
glewExperimental = GL_TRUE;
glewInit ();
printf("%s", glGetString(GL_VERSION)) ;
if (!Init())
return 0 ;
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
glViewport ( 0, 0, width, height );
glClear ( GL_COLOR_BUFFER_BIT );
// Use the program object
glUseProgram ( g_programObject );
// Load the vertex data
glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
glEnableVertexAttribArray ( 0 );
glDrawArrays ( GL_TRIANGLES, 0, 3 );
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}