開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Visual studio 2012 C++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
OpenGL
問題(Question):
最近在練習openGL,想把opengl tutorial (http://www.opengl-tutorial.org/) 中的
code跟code-project中把openGL顯示在windows form(http://ppt.cc/ZvmL)中的範例結
合在一起,在程式中用呼叫COpenGL這個class設定完繪圖的環境後,用timer不斷呼叫
Render()來產生動畫。
但原本的tutorial中用來存model、貼圖等資訊的vector到了windows form中要放在class
裡面,class本身繼承了nativewindow是個managed的class,因此沒辦法直接宣告成成員
變數,
如:
std::vector<glm::vec3> vertices;
而必須改成pointer:
std::vector<glm::vec3> *vertices;
然後在建構式中new 中來:
vertices = new std::vector<glm::vec3>;
改成這樣後原本能順利畫出的圖變成只有色塊的扭曲圖形,應該是vector裡的內容或位置
跑掉了,請教改成pointer的話要怎麼做才能不會跑掉?
餵入的資料(Input):
純文字文件中紀錄的vertices、uvs和normals值。
預期的正確結果(Expected Output):
原本vector的空間經由loadOBJ讀取純文字文件的model檔賦值後能畫出完整的3D圖形顯示
在windows form上
錯誤結果(Wrong Output):
把vector改成指標後只剩下一堆奇怪的色塊
程式碼(Code):(請善用置底文網頁, 記得排版)
public ref class COpenGL:
public System::Windows::Forms::NativeWindow
{
public:
GLuint vertexbuffer;
GLuint uvbuffer;
GLuint VertexArrayID;
GLuint Texture;
GLuint MatrixID;
GLuint TextureID;
GLuint programID;
bool res;
HDC m_hDC;
HGLRC m_hglrc;
System::Void Render(System::Void)
{
std::vector<glm::vec3> vertices; //原本可以畫出圖形時的的vector
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> normals;
////////////////////////////////////以下是一些範例中抓的繪圖的過程
res = loadOBJ("plat.obj", vertices, uvs, normals);
// Load it into a VBO
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3),
&vertices, GL_STATIC_DRAW);
GLuint uvbuffer;
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs,
GL_STATIC_DRAW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use our shader
glUseProgram(programID);
// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix= getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture);
// Set our "myTextureSampler" sampler to user Texture Unit 0
glUniform1i(TextureID, 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
1, // attribute
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, vertices.size() );
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &uvbuffer);
}
現在把vector改成指標並放在成員變數中後,在建構式中加入了new std::Vector,並把
繪圖過程中Render()的vertices.size()改成vertices->size()、&vertices改成vertices
等。
public ref class COpenGL:
public System::Windows::Forms::NativeWindow
{
public:
GLuint vertexbuffer;
GLuint uvbuffer;
GLuint VertexArrayID;
GLuint Texture;
GLuint MatrixID;
GLuint TextureID;
GLuint programID;
bool res;
std::vector<glm::vec3> *vertices;//這三個vector
std::vector<glm::vec2> *uvs;
std::vector<glm::vec3> *normals;
COpenGL(System::Windows::Forms::Panel ^ parentForm, GLsizei iWidth, GLsizei
iHeight) //建構式
{
vertices = new std::vector<glm::vec3>; //這三個new
uvs = new std::vector<glm::vec2>; //移到建構式裡去
normals = new std::vector<glm::vec3>;
.
.
.
}
我現在查到的是說在managed下應改用List<>,但整個讀取模型和貼圖的範例太大了要整
個改managed很頭痛啊,而openGL在windows form中顯示的範例現在只找到這個,只好上
來求救了,感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.94.123
※ 編輯: gino0717 來自: 140.112.94.123 (02/12 19:54)
※ 編輯: gino0717 來自: 140.112.94.123 (02/12 19:56)