首页 > 解决方案 > 寻找洞察为什么我在这里遇到访问冲突

问题描述

我正在尝试创建一个点列表,其 z 值将被灰度图像的 alpha 值修改。由于点被分配到一个列表,我不断收到访问冲突。在调试过程中,我注意到 alpha 数组的大小在 for 循环的中间突然发生变化,以及我的宽度和高度值。我是 C++ 新手,所以我想这是一个明显的错误。以下是相关代码:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

// image processing libs
#include "vendors/stb_image/stb_image.h"
#include "vendors/stb_image/stb_image_write.h"

#include "Image.h"
#include "Renderer.h"
#include "VertexBufferLayout.h"

// signed normalization function for scaling input value with a known input range to an output range
float snorm(float value, float in_Min, float in_Max) 
{
    float out_Value = ( ((1.0f - 1.0f) * ((value - in_Min) / (in_Max - in_Min))) + -1.0f );
    return out_Value;
}

int main(void) 
{   
    // CONSTANTS
    const int SCREEN_WIDTH = 2000;
    const int SCREEN_HEIGHT = 2000;
    // glsl version
    const char* glsl_version = "#version 330";

    Image image("res/images/drama_mask_white.jpg");
    // loads an image to greyscale (normal) and returns the path to that normal
    std::string normal_Path = image.ImgToGrayScale("gray_mask");
    image.GetAlphas(normal_Path);
    image.setMinMaxAlphas();
    
    const std::vector<float> * lcl_Alphas = &image.alpha_Map;
    const int lcl_Width = image.img_Width;
    const int lcl_Height = image.img_Height;

    const float x_Increment = 2.0f / lcl_Width; 
    const float y_Increment = 2.0f / lcl_Height;


    float positions[] = { 0 };
    //unsigned int indices[] = { 0 };
    unsigned int row = 0;
    unsigned int col = 0;
    unsigned int num_Rows = 0;
    unsigned int num_Cols = 0;
    unsigned int num_Verteces = 0; 
    unsigned int pos_Count = 0; 

    for (int i = 0; (unsigned)i < image.alpha_Map.size(); i++)  
    {
        // checks for the end of the row
        if (i > 0 && (i % (image.img_Width - 1)) == 0)
        {
            row++; // if we've reached the end of a row, increment row index
            num_Cols = col;
            col = 0; // reset column index at end of each row
        }


        // assign position values starting from bottom left
        // X
        positions[pos_Count] = -1.0f + (col * x_Increment);
        // Y
        positions[pos_Count + 1] = -1.0f + (row * y_Increment);
        // Z
        // ERROR OCCURS HERE
        positions[pos_Count + 2] = snorm(image.alpha_Map[i], image.min_Alpha, image.max_Alpha);
        pos_Count += 3;
        // indices
        //indices[i] = i;

        col++; // increment column index 
        num_Verteces++;
    }

    std::cout << "Num Verteces: " << num_Verteces << std::endl;
    std::cout << "Num Positions: " << pos_Count << std::endl;

    num_Rows = row; 

    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    // create window and context with core profile
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    glfwSwapInterval(1);

    // checks if GLEW intializes correctly
    if (glewInit() != GLEW_OK)
        std::cout << "ERROR!" << std::endl;

    std::cout << glGetString(GL_VERSION) << std::endl;

    // enable blending
    GLCall(glEnable(GL_BLEND));
    // get source alpha, subtract from one to blend alpha at destination  
    GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

    // init renderer object
    Renderer renderer;

    GLfloat test_Vertex[] = { SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0.0f };

    while (!glfwWindowShouldClose(window)) {

        GLCall(glClearColor(0.0, 0.0, 0.0, 1.0));
        renderer.Clear();

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, test_Vertex);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDrawArrays(GL_POINTS, 0, 1);

        // Swap front and back buffers
        glfwSwapBuffers(window);

        // Poll for, and process events
        glfwPollEvents();
    }
    glfwTerminate();
 }

标签: c++

解决方案


@Rabbid76 和 @Peter 在这里有正确的想法......我需要使用 std::vector 因为我只用元素初始化数组,但在 for 循环期间填充它。一旦我转换为 std::vector 它工作正常。现在要让这件事真正画出要点......


推荐阅读