OpenGL(1)

1. creating a window

http://learnopengl.com

1.glfw
download 32-BIT WINDOWS lib-Visio2015
2.glew lib release
window sysytem32 opengl32.lib
glew3.lib
projection

dll 动态连接 可以不用和代码一起给打包,提交
lib 静态连接
所有组件
所有平台

c/c++ 一般 include
链接器 一般 其他程序式目录

2. hello windows

#include <isotream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
float vertices[] ={
-0.5f,-0.5f,0.0f;
 0.5f,-0.5f,0.0f;
 0.0f,-0.5f,0.0f
};
void processInput(GLFWwindow* window){
  if (glfwGetKey(window,GLFW_KEY_ESCAPE) == GLEW_PRESS){
     glfwSetWindowShouldClose(window,true);
}    
}

int main(){
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);

//Open GLFW Window
GLFWwindow* window = glfwCreatWindow(800,600,"My OpenGL",NULL,NULL);
if(window == NULL){
  printf("Open window failed!");
  glfwTerminate();
  return -1;
}

glfwMakeContextCurrent(window);

glewExperimental = true;
if(flewInit() != GLEW_OK){
  printf("Init GLEW failed!");
  glfwTerminate();
  return -1;
}

glViewport(0,0,800,600);

unsigned int VAO;
glGenVertexArrays(1,&VAO);
glBindVertexArrsy(VAO);

unsigned int VBO;
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);

glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

//GL_STATIC_DRAW  GL_DYNAMIC_DRAW GL_STREAM_DRAW
//ready your engines 一直运行 直到我们想停止
while(!glfwWindowsShouldClose(windows)){
   //input
   ProcessInput(window);
  
  //rendering commands  here
 
  //check and call event and swap the buffers
   glfwSwapBuffers(window);
   glfwPollEvents();//按键 操作 获取实现
}

glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glfwTerminate();
return 0;
}
glClear类型
GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT深度
GL_STENCIL_BUFFER_BIT遮罩
double bufferWINDOW APPLICATIONS swap
the front buffershow at the screen
the back bufferthe rendering commands

3. hello triangle

1.transforms 3d coordinates into 2d coordinates precise
2.transforms 2d coordinates into actual colored pixels aproximation

the small programs can easily be executed in parallel: shaders

graphics pipelineabstract representation
vertex data[]CPU 来的数据 顶点
vertex shader移动顶点 操作顶点 (膨胀)
shade assembly连接点 线 面
geometry shader插点 补点 (增强纹理)
rasterization取点 变成一个一个点
fragment shader补色 材质 uv
tests and blending测深度 测遮挡 如何融合

vertex data ——> fully rendered pixel

primitives
GL_POINTS
GL_TRIANGLES
GL_LINE_STRIP
primitives assembly

blender*.obj
v点坐标
vtuv
vn法向量 normal

vbo
vao
任何时候只会执行一个VAO,将VBO绑上VAO;也可以使用EBO对VAO进行说明

三角形绘制的缺省是逆时针方向

#include <isotream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
float vertices[] ={
-0.5f,-0.5f,0.0f;
 0.5f,-0.5f,0.0f;
 0.0f,-0.5f,0.0f;
 0.8f, 0.8f,0.0f
};

unsigned int indices[] ={
0,1,2,
2,1,3
};

const char* vertexShaderSource = 
"#version 330 core                                   \n"
"layout(location = 6) in vec3 aPos;                  \n"
"void main(){                                        \n"
"    gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0);}  \n";


const char* fragmentShaderSource = 
"#version 330 core                                    \n"
"out vec4 FragColor;                                  \n"
"void main(){                                         \n"
"    FragColor = vec4(1.0f,0.5f,0.2f,1.0f);}          \n";

void processInput(GLFWwindow* window){
  if (glfwGetKey(window,GLFW_KEY_ESCAPE) == GLEW_PRESS){
     glfwSetWindowShouldClose(window,true);
}    
}

int main(){
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);

//Open GLFW Window
GLFWwindow* window = glfwCreatWindow(800,600,"My OpenGL",NULL,NULL);
if(window == NULL){
  printf("Open window failed!");
  glfwTerminate();
  return -1;
}

glfwMakeContextCurrent(window);

glewExperimental = true;
if(glewInit() != GLEW_OK){
  printf("Init GLEW failed!");
  glfwTerminate();
  return -1;
}

glViewport(0,0,800,600);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

//1.bind Vertex Array Object
unsigned int VAO;
glGenVertexArrays(1,&VAO);
glBindVertexArray(VAO);

//2.copy our vertices array in a buffer for OpenGL to use
unsigned int VBO;
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

unsigned int EBO;
glGenBuffers(1,&EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);


unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader,1,&vertexShaderSource,NULL):
glCompileShader(vertexShader);

unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL):
glCompileShader(fragmentShader);


unsigned int shaderProgram;
shaderProgram = glCreatProgram();
glAttachShader(shaderProgram,vertexShader);
glAttachShader(shaderProgram,fragmentShader);
glLinkProgram():

//3.then set our vertex attributes pointers
glVertexAttribPointer(6,3,GL_FLOAT,GL_FALSE,3*sizeof(float),void*(0));
//attribPointer:location,number,类型,是否标准化到-1:1,几个数一组,偏移量offset
glEnableVertexAttribArray(6);

//GL_STATIC_DRAW  GL_DYNAMIC_DRAW GL_STREAM_DRAW
//ready your engines 一直运行 直到我们想停止
while(!glfwWindowsShouldClose(windows)){
   //input
   ProcessInput(window);
  
  //rendering commands  here
  glClearColor(0.2f,0.3f,0.3f,1.0f);
  glClear(GL_COLOR_BUFFER_BIT);

  //4.draw the object
  glBindVertexArray(VAO);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
  glUseProgram(shaderProgram);
  //glDrawArrays(GL_TRIANGLES,0,6);
  glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
 
  //check and call event and swap the buffers
   glfwSwapBuffers(window);
   glfwPollEvents();//按键 操作 获取实现
}

glfwTerminate();
return 0;
}

vertex buffer object(VBO)

element buffer object (EBO)
索引缓存对象

OpenGL是状态机,有上下文(context)

VAO
一个时间只能处理一个VBO和一个VAO,但存在很多VBO的接口(Array Buffer)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值