当前位置:网站首页>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
边栏推荐
- [phantom engine UE] realize the animation production of mapping tripod deployment
- Un réveil de l'application B devrait être rapide
- Ffmepg usage guide
- Phpmailer reported an error: SMTP error: failed to connect to server: (0)
- [phantom engine UE] the difference between running and starting, and the analysis of common problems
- Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
- Longyuan war "epidemic" 2021 network security competition web easyjaba
- Hexadecimal to decimal
- Pyqt5 displays file names and pictures
- Common features of ES6
猜你喜欢
我国算力规模排名全球第二:计算正向智算跨越
Rome chain analysis
Longyuan war "epidemic" 2021 network security competition web easyjaba
如何优雅的获取每个分组的前几条数据
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
MacBook安装postgreSQL+postgis
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
[phantom engine UE] the difference between running and starting, and the analysis of common problems
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
随机推荐
Wechat applet development process (with mind map)
TPG x AIDU|AI领军人才招募计划进行中!
[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)
电源管理总线 (PMBus)
JVM garbage collection
Is "golden nine and silver ten" the best time to find a job? Not necessarily
Interview related high-frequency algorithm test site 3
Kwai, Tiktok, video number, battle content payment
Un réveil de l'application B devrait être rapide
Clickpaas low code platform
web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)
[phantom engine UE] the difference between running and starting, and the analysis of common problems
Ctfshow web entry code audit
Use of vscode software
PR video clip (project packaging)
[brush questions] BFS topic selection
网络安全-记录web漏洞修复
Hexadecimal to octal
[phantom engine UE] realize the animation production of mapping tripod deployment
Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes