新建ios 及win32 opengl工程
ios上新建opengl工程很简单,打开xcode->选择opengl模板,然后运行。就可以看到三个立方体了。
win32上比较复杂。不过我们可以通过使用glfw来简化流程。打开vs2012->新建win32 application->在main 中调用 glfw。代码如下:
#include "stdafx.h"
#include <stdio.h>
#include "vector"
#include "string"
#include <fstream>
#include "glew.h"
#include <glfw3.h>
GLuint LoadShaders(const char* vertex_file_path,const char* fragment_file_path)
{
//Create the shaders
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
//Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path,std::ios::in);
if (VertexShaderStream.is_open())
{
std::string Line = "";
while (getline(VertexShaderStream,Line))
{
VertexShaderCode += "\n" + Line;
}
VertexShaderStream.close();
}
//Read the Fragment Shader code from the file
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragment_file_path,std::ios::in);
if (FragmentShaderStream.is_open())
{
std::string Line = "";
while (getline(FragmentShaderStream,Line))
{
FragmentShaderCode += "\n" + Line;
}
FragmentShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;
//compile Vertex Shader
printf("Compiling shader : %s\n",vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID,1,&VertexSourcePointer,NULL);
glCompileShader(VertexShaderID);
// Check vertex Shader
glGetShaderiv(VertexShaderID,GL_COMPILE_STATUS,&Result);
glGetShaderiv(VertexShaderID,GL_INFO_LOG_LENGTH,&InfoLogLength);
std::vector<char> VerTexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID,InfoLogLength,NULL,&VerTexShaderErrorMessage[0]);
fprintf(stdout,"%s\n",&VerTexShaderErrorMessage[0]);
//compile Fragment Shader
printf("Compiling shader : %s\n",vertex_file_path);
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID,1,&FragmentSourcePointer,NULL);
glCompileShader(FragmentShaderID);
// Check vertex Shader
glGetShaderiv(FragmentShaderID,GL_COMPILE_STATUS,&Result);
glGetShaderiv(FragmentShaderID,GL_INFO_LOG_LENGTH,&InfoLogLength);
std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(FragmentShaderID,InfoLogLength,NULL,&FragmentShaderErrorMessage[0]);
fprintf(stdout,"%s\n",&FragmentShaderErrorMessage[0]);
//Link the program
fprintf(stdout,"Linking programn");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID,VertexShaderID);
glAttachShader(ProgramID,FragmentShaderID);
glLinkProgram(ProgramID);
//Check the program
glGetProgramiv(ProgramID,GL_LINK_STATUS,&Result);
glGetProgramiv(ProgramID,GL_INFO_LOG_LENGTH,&InfoLogLength);
std::vector<char> ProgramErrorMessage(InfoLogLength);
glGetProgramInfoLog(ProgramID,InfoLogLength,NULL,&ProgramErrorMessage[0]);
fprintf(stdout,"%s\n",&ProgramErrorMessage[0]);
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
return ProgramID;
}
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
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)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
// Needed in core profile
if( glewInit() != GLEW_OK)
{
glfwTerminate();
return -1;
}
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f,-1.0f,0.0f,
1.0f,-1.0f,0.0f,
0.0f,1.0f,0.0f,
};
//This will identify our vertex buffer
GLuint vertexbuffer;
//Generate 1 buffer,put the resulting identifier in vertexbuffer
glGenBuffers(1,&vertexbuffer);
//The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);
//Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER,sizeof(g_vertex_buffer_data),g_vertex_buffer_data,GL_STATIC_DRAW);
GLuint programID = LoadShaders("vertex.shader","fragment.shader");
glUseProgram(programID);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableVertexAttribArray(0);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES,0,3);// Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return true;
}
这样我们就在win32上绘制了一个三角形。注意要导入相应的.h,lib和dll文件。glew,glfw。