当前位置:网站首页>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
边栏推荐
- 快手、抖音、视频号交战内容付费
- Ctfshow 2022 Spring Festival welcome (detailed commentary)
- Scheduling system of kubernetes cluster
- 【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
- 【thingsboard】替换首页logo的方法
- [brush questions] BFS topic selection
- Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
- As soon as I write the code, President Wang talks with me about the pattern all day
- MacBook installation postgresql+postgis
- Alibaba cloud ECS uses cloudfs4oss to mount OSS
猜你喜欢

【FineBI】使用FineBI制作自定义地图过程

如何实现实时音视频聊天功能

Learning notes 8

Three level linkage demo of uniapp uview u-picker components

A real day for Beijing programmers!!!!!

基于TCP的移动端IM即时通讯开发仍然需要心跳保活

在线SQL转Excel(xls/xlsx)工具

【虚幻引擎UE】实现测绘三脚架展开动画制作

mysql的七种join连接查询
![[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording](/img/11/b55f85ef9e1f22254218cb9083b823.png)
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
随机推荐
学习MVVM笔记(一)
Ffmepg usage guide
Why do big companies such as Baidu and Alibaba prefer to spend 25K to recruit fresh students rather than raise wages by 5K to retain old employees?
[finebi] the process of making custom maps using finebi
Pyqt5 displays file names and pictures
【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
Clickpaas low code platform
Ctfshow web entry code audit
provide/inject
Threejs factory model 3DMAX model obj+mtl format, source file download
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
About the recent experience of writing questions
Machine learning -- neural network
[charging station]_ Secular wisdom_ Philosophical wisdom _
A application wakes up B should be a fast method
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information