0013 OpenGL Day2(shader, texture)

这节简单实现对三角形的彩色着色,顺便通过vertex shader实现图形的移动,然后是简单的贴图

  

从上一节可以看到,Shader是流程线中必不可少的部分,单独分离出来方便后面使用

 

别的也没什么,彩色写在了顶点数据中(可以相像为点着色)

 

实现移动主要利用uniform变量(可以看作是cpu与gpu之间共享的全局变量)

 

贴图主要在fragment shader中对纹理按坐标采样就好

 

https://github.com/WendyAndAndy/OpenGL

 

#include <glad/glad.h>

#include <GLFW/glfw3.h>

#include <iostream>

#include "shader.h"

#include <filesystem.h>

 

using namespace std;

 

const unsigned int WIDTH = 800;

const unsigned int HEIGHT = 600;

void framebuffer_size_callback(GLFWwindow*, int, int);

void processInput(GLFWwindow*);

 

const float vertices[] = {

.5f, -.5f, 0.0f, 1.0f, 0.0f, 0.0f, //bottom right, red

-.5f,-.5f, 0.0f, 0.0f, 1.0f, 0.0f,  //bottom left, green

0.0f, .5f, 0.0f, 0.0f, 0.0f, 1.0f //top middle, blue

};

 

Shader shader;

float xPos = 0.0f; //for vertex-shader move the triangle

float yPos = 0.0f; //for vertex-shader move the triangle

 

 

int main()

{

/*cout << "003 hello shader" << endl;

getchar();*/

glfwInit();

glfwWindowHint(GLFW_SAMPLES, 8);

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, u8"你好, Shader(WSAD move..)", NULL, NULL);

if (window == NULL)

{

cout << "Failed to create GLFW window\n";

glfwTerminate();

return -1;

}

glfwMakeContextCurrent(window);

glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))

{

cout << "Failed to initialize GLAD\n";

}

 

shader = Shader(FileSystem::getPath("shaders/002_shader.vs").c_str(), FileSystem::getPath("shaders/002_shader.fs").c_str());

 

unsigned int VAO, VBO;

glGenVertexArrays(1, &VAO);

glGenBuffers(1, &VBO);

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);

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

//position attribute

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);

glEnableVertexAttribArray(0);

//color attribute

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));

glEnableVertexAttribArray(1);

 

while (!glfwWindowShouldClose(window))

{

processInput(window);

 

//glClearColor(.2, .3, .3, 1);

glClear(GL_COLOR_BUFFER_BIT);

 

shader.use();

glBindVertexArray(VAO);

glDrawArrays(GL_TRIANGLES, 0, 3);

 

glfwSwapBuffers(window);

glfwPollEvents();

}

 

glfwTerminate();

return 0;

}

 

void framebuffer_size_callback(GLFWwindow* window, int w, int h)

{

glViewport(0, 0, w, h);

}

 

void processInput(GLFWwindow* window)

{

if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)

{

glfwSetWindowShouldClose(window, true);

}

 

if (glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS)

{

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

cout << "F3 -> wireframe mode" << endl;

}

 

if (glfwGetKey(window, GLFW_KEY_F4) == GLFW_PRESS)

{

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

cout << "F4 -> polygon mode" << endl;

}

 

const float SPEED = 0.001f;

//Move Left

if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)

{

xPos -= SPEED;

shader.setFloat("xPos", xPos);

cout << "pressed A key, xPos = " << xPos << endl;

}

//Move Right

if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)

{

xPos += SPEED;

shader.setFloat("xPos", xPos);

cout << "pressed D key, xPos = " << xPos << endl;

}

//Move Up

if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)

{

yPos += SPEED;

shader.setFloat("yPos", yPos);

cout << "pressed W key, yPos = " << yPos << endl;

}

//Move Down

if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)

{

yPos -= SPEED;

shader.setFloat("yPos", yPos);

cout << "pressed S key, yPos = " << yPos << endl;

}

 

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值