当前位置:网站首页>C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
2022-07-05 04:17:00 【Miaowei】
openGL List of articles
List of articles
Preface
vs2019 Grammatical errors :
c26451: Arithmetic overflow : Use 4 Operator on byte value *, Then convert the result to 8 Byte value . When calling an operator * You can avoid overflow by previously casting the value to a wide type (io.2)
One 、 The reason for the error
stay c++ Calculation in grammar uses glsl( Shader programming syntax ) Will report a mistake ,
float toRadians(float degrees)
{
return (degrees * 2.f * 3.14 * pai / 360.0f);
}
Two 、 solve
Put the above 2.f Modified into 2.0f
float toRadians(float degrees)
{
return (degrees * 2.0f * 3.14 * pai / 360.0f);
}
Example
#include "glew/glew.h"
#include "glfw/glfw3.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "camera.h"
#include "Utils.h"
#include "Torus.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
static const float pai = 3.1415926;
float toRadians(float degrees)
{
return (degrees * 2.0f * 3.14 * pai / 360.0f);
}
static const int screen_width = 1920;
static const int screen_height = 1080;
int width = 0;
int height = 0;
float aspect = 0.f;
GLuint renderingProgram = 0;
static const int numVAOs = 1;
static const int numVBOs = 4;
GLuint vao[numVAOs] = {
0 };
GLuint vbo[numVBOs] = {
0 };
Torus myTorus(0.5f, 0.2f, 48);
int numTorusVertices = myTorus.getNumVertices();
int numTorusIndices = myTorus.getNumIndices();
float torusLocX = 0.f, torusLocY = 0.f, torusLocZ = 0.f;
Camera camera(glm::vec3(0.f, 0.f, 1.f));
float cameraX = 0.f, cameraY = 0.f, cameraZ = 0.f;
glm::mat4 mMat(1.f), vMat(1.f), pMat(1.f), mvMat(1.f), invTrMat(1.f), rMat(1.f);
glm::vec3 currentLightPos(0.f), transformed(0.f);
float lightPos[3] = {
0.f };
glm::vec3 lightLoc = glm::vec3(5.f, 2.f, 2.f);
float amt = 0.f; //y Axis rotation component
// variable allocation for display
GLuint mvLoc = 0, projLoc = 0, nLoc = 0;
GLuint globalAmbLoc = 0, ambLoc = 0, diffLoc = 0, specLoc = 0, posLoc = 0, mAmbLoc = 0, mDiffLoc = 0, mSpecLoc = 0, mShinLoc = 0;
float lastFrame = 0.f;
float deltaTime = 0.f;
//white light
float globalAmbient[4] = {
0.7f, 0.7f, 0.7f, 1.f };
float lightAmbient[4] = {
0.f, 0.f, 0.f, 1.f };
float lightDiffuse[4] = {
1.f, 1.f, 1.f, 1.f };
float lightSpecular[4] = {
1.f, 1.f, 1.f, 1.f };
// gold material
float* matAmb = Utils::goldAmbient();
float* matDiff = Utils::goldDiffuse();
float* matSpec = Utils::goldSpecular();
float matShin = Utils::goldShininess();
void installLights(glm::mat4 vMatrix)
{
transformed = glm::vec3(vMatrix * glm::vec4(currentLightPos, 1.f));
lightPos[0] = transformed.x;
lightPos[1] = transformed.y;
lightPos[2] = transformed.z;
// get the locations of the light and material fields in the shader
globalAmbLoc = glGetUniformLocation(renderingProgram, "globalAmbient");
ambLoc = glGetUniformLocation(renderingProgram, "light.ambient");
diffLoc = glGetUniformLocation(renderingProgram, "light.diffuse");
specLoc = glGetUniformLocation(renderingProgram, "light.specular");
posLoc = glGetUniformLocation(renderingProgram, "light.position");
mAmbLoc = glGetUniformLocation(renderingProgram, "material.ambient");
mDiffLoc = glGetUniformLocation(renderingProgram, "material.diffuse");
mSpecLoc = glGetUniformLocation(renderingProgram, "material.specular");
mShinLoc = glGetUniformLocation(renderingProgram, "material.shininess");
// set the uniform light and material values in the shader
glProgramUniform4fv(renderingProgram, globalAmbLoc, 1, globalAmbient);
glProgramUniform4fv(renderingProgram, ambLoc, 1, lightAmbient);
glProgramUniform4fv(renderingProgram, diffLoc, 1, lightDiffuse);
glProgramUniform4fv(renderingProgram, specLoc, 1, lightSpecular);
glProgramUniform3fv(renderingProgram, posLoc, 1, lightPos);
glProgramUniform4fv(renderingProgram, mAmbLoc, 1, matAmb);
glProgramUniform4fv(renderingProgram, mDiffLoc, 1, matDiff);
glProgramUniform4fv(renderingProgram, mSpecLoc, 1, matSpec);
glProgramUniform1f(renderingProgram, mShinLoc, matShin);
}
void setupVertices(void)
{
vector<int> ind = myTorus.getIndices();
vector<glm::vec3> vert = myTorus.getVertices();
vector<glm::vec2> tex = myTorus.getTexCoords();
vector<glm::vec3> norm = myTorus.getNormals();
vector<float> pValues;
vector<float> tValues;
vector<float> nValues;
for (int i=0; i<myTorus.getNumVertices(); i++)
{
pValues.push_back(vert[i].x);
pValues.push_back(vert[i].y);
pValues.push_back(vert[i].z);
tValues.push_back(tex[i].s);
tValues.push_back(tex[i].t);
nValues.push_back(norm[i].x);
nValues.push_back(norm[i].y);
nValues.push_back(norm[i].z);
}
glGenVertexArrays(numVAOs, vao);
glBindVertexArray(vao[0]);
glGenBuffers(numVBOs, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, pValues.size() * sizeof(float), &(pValues[0]), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, tValues.size() * sizeof(float), &(tValues[0]), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
glBufferData(GL_ARRAY_BUFFER, nValues.size() * sizeof(float), &(nValues[0]), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ind.size() * sizeof(int), &ind[0], GL_STATIC_DRAW);
}
void init(GLFWwindow* window)
{
renderingProgram = Utils::createShaderProgram("GouraudShaders/vertShader.glsl", "GouraudShaders/fragShader.glsl");
glfwGetFramebufferSize(window, &width, &height);
aspect = (float)width / (float)height;
pMat = glm::perspective(glm::radians(45.f), aspect, 0.01f, 1000.f);
cameraX = 0.f, cameraY = 0.f, cameraZ = 1.f;
torusLocX = 0.f; torusLocY = 0.f; torusLocZ = -1.f;
setupVertices();
}
void display(GLFWwindow* window, double currentTime)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.f, 1.f, 1.f, 1.f);
glUseProgram(renderingProgram);
mvLoc = glGetUniformLocation(renderingProgram, "mv_matrix");
projLoc = glGetUniformLocation(renderingProgram, "proj_matrix");
nLoc = glGetUniformLocation(renderingProgram, "norm_matrix");
vMat = glm::translate(glm::mat4(1.f), glm::vec3(cameraX, cameraY, -cameraZ));
mMat = glm::translate(glm::mat4(1.f), glm::vec3(torusLocX, torusLocY, torusLocZ));
mMat *= glm::rotate(mMat, glm::radians(45.f), glm::vec3(1.f, 0.f, 0.f));
currentLightPos = glm::vec3(lightLoc.x, lightLoc.y, lightLoc.z);
amt += 0.5f;
rMat = glm::rotate(glm::mat4(1.f), glm::radians(amt), glm::vec3(0.f, 0.f, 1.f));
currentLightPos = glm::vec3(rMat * glm::vec4(currentLightPos, 1.f));
installLights(vMat);
mvMat = vMat * mMat;
/** Why use transpose matrix here ? Because model vertices need to be transformed into visual space , And normals also need to be transformed into visual space . Apply directly to the normal vector MV The matrix cannot guarantee that the normal vector is still perpendicular to the object surface . The correct transformation is MV Inverse transpose matrix of **/
invTrMat = glm::transpose(glm::inverse(mvMat));
// Change one uniform The value of a matrix variable or array . To change uniform The position of the variable is determined by location Appoint ,location The value of should be determined by glGetUniformLocation The function returns
// The perspective matrix and MV The matrix is copied to the corresponding unified variable
/* Through consistent variables (uniform Decorated variable ) Reference passes consistent variable values into the rendering pipeline . location : uniform The location of . count : The number of array elements to load data or the number of matrices to modify . transpose : Indicates that the matrix is column first (column major) matrix (GL_FALSE) OK first (row major) matrix (GL_TRUE). value : Point to by count A pointer to an array of elements . */
glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mvMat));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(pMat));
glUniformMatrix4fv(nLoc, 1, GL_FALSE, glm::value_ptr(invTrMat));
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
// Specifies that the index value at render time is index The data format and position of the vertex attribute array
/*Parameters index Specifies the index value of the vertex attribute to modify size Specify the number of components per vertex attribute . It has to be for 1、2、3 perhaps 4. The initial value is 4.( Mengwei : Such as position By 3 individual (x, y, z) form , And the color is 4 individual (r, g, b, a)) type Specify the data type of each component in the array . The available symbolic constants are GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, and GL_FLOAT, The initial value is GL_FLOAT. normalized Specify when accessed , Should fixed point data values be normalized (GL_TRUE) Or directly convert to fixed point value (GL_FALSE). stride Specifies the offset between consecutive vertex attributes . If 0, Then the vertex attribute will be interpreted as : They are closely aligned . The initial value is 0. pointer Specify a pointer , Point to the first component of the first vertex attribute in the array . The initial value is 0. */
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(2);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]);
glDrawElements(GL_TRIANGLES, numTorusIndices, GL_UNSIGNED_INT, 0);
}
void window_size_callback(GLFWwindow* window, int newWidth, int newHeight)
{
aspect = (float)newWidth / (float)newHeight;
glViewport(0, 0, newWidth, newHeight);
pMat = glm::perspective(glm::radians(45.f), aspect, 0.01f, 1000.f);
}
int main(int argc, char** argv)
{
int glfwState = glfwInit();
if (glfwState == GLFW_FALSE)
{
cout << "GLFW initialize failed,invoke glfwInit()......Error file:" << __FILE__ << "......Error line:" << __FILE__ << endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(screen_width, screen_height, "Light ads gouraud", nullptr, nullptr);
if (!window)
{
cout << "GLFW create window failed,invoke glfwCreateWindow()......Error file:" << __FILE__ << "......Error line:" << __LINE__ << endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
int glewState = glewInit();
if (glewState != GLEW_OK)
{
cout << "GLEW initialize failed,invoke glewInit()......Error file:" << __FILE__ << "......Error line:" << __LINE__ << endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSwapInterval(1);
glfwSetWindowSizeCallback(window, window_size_callback);
init(window);
while (!glfwWindowShouldClose(window))
{
display(window, glfwGetTime());
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
return 0;
}
Running effect

Complete code
边栏推荐
- Uni app change the default component style
- How to realize real-time audio and video chat function
- Uni app common functions /api
- 机器学习 --- 决策树
- Behavior perception system
- Realize the attention function of the article in the applet
- File upload bypass summary (upload labs 21 customs clearance tutorial attached)
- MySQL: view with subquery in the from clause limit
- Threejs implements labels and displays labels with custom styles
- [phantom engine UE] package error appears! Solutions to findpin errors
猜你喜欢

What is the reason why the webrtc protocol video cannot be played on the easycvr platform?

mysql的七种join连接查询

As soon as I write the code, President Wang talks with me about the pattern all day

Threejs Internet of things, 3D visualization of farms (II)

Containerd series - detailed explanation of plugins
![[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)](/img/eb/a93630aff7545c6c3b71dcc9f5aa61.png)
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)

【UNIAPP】系统热更新实现思路
![[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)](/img/eb/a93630aff7545c6c3b71dcc9f5aa61.png)
[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)

Network security - record web vulnerability fixes

【虚幻引擎UE】实现测绘三脚架展开动画制作
随机推荐
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
WGS84 coordinate system, web Mercator, gcj02 coordinate system, bd09 coordinate system - brief introduction to common coordinate systems
Containerd series - what is containerd?
FFmepg使用指南
这是一个不确定的时代
A real day for Beijing programmers!!!!!
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
Pyqt pyside custom telescopic menu bar sharing (including tutorial)
[charging station]_ Secular wisdom_ Philosophical wisdom _
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
在线SQL转Excel(xls/xlsx)工具
长度为n的入栈顺序的可能出栈顺序种数
Mixed compilation of C and CC
防护电路中的元器件
Use object composition in preference to class inheritance
【虚幻引擎UE】运行和启动的区别,常见问题分析
Threejs rendering obj+mtl model source code, 3D factory model
Why can't all browsers on my computer open web pages
web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)