2011-04-07 124 views
1

我一直在學習的OpenGL過去幾周和閱讀訥河的TUTS的Swiftless TUTS和零件,以及關於法線,場景圖,喇嘛喇嘛其他文章偏後,我決定創造一些一些「框架」,使我能夠更容易地插入新學習的代碼。所以我決定創建一個場景圖/渲染器的組合,而不是漂亮,不是那麼快(prolly),但與選項添加只是一個新的派生節點類來呈現一個新的功能,無論我可能只是讀過。所以我期待的趣聞看到如何實現一個定居在這個基類各種例子:OpenGL的VBO訪問衝突

#pragma once 

#ifndef NODE_H 
#define NODE_H 

#include <stdlib.h> 
#include <stdio.h>  
#include <iostream> 
#include <GL/glew.h> 
#include <GL/freeglut.h> 
#include <GL/glext.h> 
#include <math.h> 
#include <windows.h> 
#include <list> 
using namespace std; 

class Node 
{ 
public: 
     Node(void); 
     ~Node(void); 

     //Node* parent; 

     void addChild(Node* node); 
     void removeChilds(Node* node); 

     virtual void traverse(); 

     void kill(); 

protected: 
     std::list<Node*> childs; 
}; 

#endif 

....從我做了這個類:

#pragma once 

#ifndef NODESPHERE_H 
#define NODESPHERE_H 

#include <stdlib.h> 
#include <stdio.h>  
#include <iostream> 
#include <GL/glew.h> 
#include <GL/freeglut.h> 
#include <GL/glext.h> 
#include <math.h> 
#include <windows.h> 
#include "Various.h" 
#include "Node.h" 
class NodeSphere : public Node 
{ 
public: 
     NodeSphere(double R, double H, double K, double Z, const int space); 
     ~NodeSphere(){delete this;} 

     void traverse(); 

private: 
     float out[3]; 
     int Radius; 
     int vhVertexCount; 
    Vert *vhVertices; 
     Normal *vhNormals,*vhVNormals; 
    TexCoord *vhTexCoords; 
     unsigned int vhVBOVertices; 
    unsigned int vhVBOTexCoords; 
     unsigned int vhVBONormals; 
    bool Init(void); 
     bool Normals(const int nIndex); 
     bool ReduceToUnit(void); 
}; 

#endif 

現在,我使用freeglut處理窗口創建和循環:

#include <windows.h> 
#include <stdlib.h> 
#include <stdio.h>  
#include <iostream> 
#include <GL/glew.h> 
#include <GL/freeglut.h> 
#include <GL/glext.h> 
#include <math.h> 
#include "Scenegraph.h" 
#include "View.h" 
#include "Control.h" 

#define WIDTH 800 
#define HEIGHT 600 


Scenegraph *bob; 
View *myView; 
Control *controler; 

................. 
................. 

void main(int argc, char **argv) 
{ 
     glewInit(); 
     glutInit(&argc, argv); 
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
     glutInitWindowSize(WIDTH,HEIGHT); 
     glutInitWindowPosition(0,0); 
     glutCreateWindow("Mediensoftware - Finale Übung"); 
     bob = new Scenegraph(); 
     myView = new View(WIDTH, HEIGHT, bob); 
     controler = new Control(myView); 
     glutDisplayFunc(display); 
     glutIdleFunc(idle); 
     glutReshapeFunc(resize); 
     glutKeyboardFunc(keyboard); 
     glutSpecialFunc(keyboard_s); 

     glutMainLoop(); 
} 

場景圖「鮑勃」處理這樣的創作節點:

Scenegraph::Scenegraph() 
{ 
     this->root = new Node(); 

     this->sun = new NodeSphere(3,0,0,0,10); 
     this->sun_transform = new NodeTransform(); 
     this->sun_tex = new NodeTexture("sunmap.bmp"); 
     this->sun_mat = new NodeMaterial(1.0,0,1.0,0.7,0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0); 
     this->planet_earth = new NodeSphere(1,0,0,0,10); 
     this->planet_earth_transform = new NodeTransform(6,0,0); 
     this->planet_earth_tex = new NodeTexture("earthmap.bmp"); 
     this->planet_earth_mat = new NodeMaterial(1.0,0,1.0,0.7,0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0); 
     this->moon = new NodeSphere(0.3,0,0,0,10); 
     this->moon_tex = new NodeTexture("moonmap.bmp"); 
     this->moon_transform = new NodeTransform(1.5,0,0); 
     this->moon_mat = new NodeMaterial(1.0,0,1.0,0.7,0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0); 
     this->planet_venus = new NodeSphere(0.8,0,0,0,10); 
     this->planet_venus_transform = new NodeTransform(3,0,0); 
     this->planet_venus_tex = new NodeTexture("venusmap.bmp"); 
     this->planet_venus_mat = new NodeMaterial(1.0,0,1.0,0.7,0,1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0); 
     this->planet_mars = new NodeSphere(0.6,0,0,0,10); 
................................................................................................ 
     this->root->addChild(this->sun_transform); 
     this->sun_transform->addChild(this->sun_mat); 
     this->sun_mat->addChild(this->sun_tex); 
     this->sun_tex->addChild(this->sun); 
     this->sun->addChild(this->planet_venus_transform); 
     this->planet_venus_transform->addChild(this->planet_venus_mat); 
     this->planet_venus_mat->addChild(this->planet_venus_tex); 
................................................................................................... 
     } 

編譯器不會看到任何錯誤並執行的代碼,但在這裏接受訪問衝突:

bool NodeSphere::Init(void){ 
     glGenBuffersARB(1, &vhVBOVertices); // DERP here 
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, this->vhVBOVertices); 
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, this->vhVertexCount * 3 * sizeof(float), this->vhVertices, GL_STATIC_DRAW_ARB); 
     glGenBuffersARB(1, &vhVBONormals); 
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, this->vhVBONormals); 
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, this->vhVertexCount * 3 * sizeof(float), this->vhVNormals, GL_STATIC_DRAW_ARB); 
    glGenBuffersARB(1, &vhVBOTexCoords); 
     glBindBufferARB(GL_ARRAY_BUFFER_ARB, this->vhVBOTexCoords); 
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, this->vhVertexCount * 2 * sizeof(float), this->vhTexCoords, GL_STATIC_DRAW_ARB); 

     delete [] vhVertices; 
    vhVertices = NULL; 
    delete [] vhTexCoords; 
    vhTexCoords = NULL; 
     delete [] vhNormals; 
     vhNormals = NULL; 
     delete [] vhVNormals; 
     vhVNormals = NULL; 
     return true; 
} 

的擴展被稱作在View :: initExtensions()的初始化場景圖的窗口創建後完成的,所以....我敢肯定它是與指針...

以前的節目我用VBO創建完全相同的代碼和它的工作。唯一的區別是這裏使用的代碼在Node *中使用,而之前的代碼僅在Node中使用(無指針)。但無論如何,我會檢查glew是否啓動良好。 至於我用什麼顯卡,在筆記本電腦上,我有一個糟糕的Intel集成卡,應該支持OpenGL 1.5,但即使沒有GLSL,並在PC上,一個6850的事情是,我在筆記本電腦上工作很多,因爲還有教師和其他人員。

但無論如何,創造VBO作品中的代碼,我的意思是,它已經爲以下幾點:

#include <stdlib.h> 
#include <windows.h> 
#include <iostream> 
#include <stdio.h> 
#include <GL/glew.h> 
#include <GL/gl.h>                // Header File For The OpenGL32 Library 
#include <GL/glu.h>                // Header File For The GLu32 Library 
#include <GL/glext.h> 
#include <math.h> 

#define PI 3.14159265f 


class Vert{ 
public: 
     float x; 
     float y; 
    float z; 
}; 

class Normal{ 
public: 
     float x; 
     float y; 
    float z; 
}; 

class TexCoord{ 
    public: 
       float u; 
       float v; 
}; 

class Scenegraph { 
private: 
     float out[3]; 
     int Radius; 
     GLuint tID[2]; 
     int vhVertexCount; 
    Vert *vhVertices; 
     Normal *vhNormals,*vhVNormals; 
    TexCoord *vhTexCoords; 
     unsigned int vhVBOVertices; 
    unsigned int vhVBOTexCoords; 
     unsigned int vhVBONormals; 
    bool Init(void); 
     bool Normals(const int nIndex); 
     bool ReduceToUnit(void); 


public: 
     bool Create(double R, double H, double K, double Z, const int space); 

     void Render(); 

}; 

它等同於NodeSphere,除了它是靜態聲明。提前致謝。

+0

郵政抽獎代碼。 – 2011-04-07 01:47:36

回答

0

最有可能的錯誤發生的原因是當您嘗試撥打init()NodeSphere不存在(或不再有效)。

你或許應該移動資源的初始化代碼到構造函數(這是一般他們是什麼),除非你需要它出現以後。 VBO對物體具有類似的使用壽命是有意義的,因此在ctor/dtor中創建和銷燬它們應該可以工作,並且可以爲您節省顯式初始化的複雜性。

+1

NodeSphere :: init()在構造函數中調用,但無論如何,我已經將NodeGraph :: NodeSphere中的glGenBuffer移動了,它也是一樣的,它給了我glGenBuffer的訪問衝突。 – Cosmo 2011-04-07 01:23:16

+0

定義了「glGenBuffersARB」的位置?你是否在使用GLEW而不是初始化它,或者沿着這些方向? – ssube 2011-04-07 01:27:52