首页 \ 问答 \ 使用openGL渲染场景(Render a scene with openGL)

使用openGL渲染场景(Render a scene with openGL)

我将使用openGL渲染包含各种网格的场景。 网格定义如下:

struct Mesh {
frame3f         frame;      // frame
vector<vec3f>   pos;        // vertex position
vector<vec3f>   norm;       // vertex normal
vector<vec2f>   texcoord;   // vertex texcture coordinates
vector<vec3i>   triangle;   // triangle
vector<vec4i>   quad;       // quad
Material*       mat;        // material}

网格可以由三角形和四边形组成,我尝试使用以下代码渲染顶点:

for (auto mesh : scene->meshes)
{
    // bind material kd, ks, n
    glVertexAttrib3f(material_kd_loc, mesh->mat->kd.x, mesh->mat->kd.y, mesh->mat->kd.z);
    glVertexAttrib3f(material_ks_loc, mesh->mat->ks.x, mesh->mat->ks.y, mesh->mat->ks.z);
    glVertexAttrib1f(material_n_loc, mesh->mat->n);

    // bind mesh frame - use frame_to_matrix
    mat4f mesh_mat = frame_to_matrix(mesh->frame);
    glUniformMatrix4fv(mesh_frame_loc, 1, GL_TRUE, &mesh_mat[0][0]);

    // enable vertex attributes arrays
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    //and set up pointers to the mesh data
    glVertexAttribPointer(vertex_pos_loc, mesh->pos.size(), GL_FLOAT, GL_FALSE, 0, mesh->pos.data());
    glVertexAttribPointer(vertex_norm_loc, mesh->norm.size(), GL_FLOAT, GL_TRUE, 0, mesh->norm.data());

    // draw triangles and quads
    if (mesh->triangle.size() != 0){

        glDrawElements(GL_TRIANGLES, mesh->pos.size(), GL_UNSIGNED_INT, mesh->triangle.data());
    }
    if (mesh->quad.size() != 0){
        glDrawElements(GL_QUADS, mesh->pos.size(), GL_UNSIGNED_INT, mesh->quad.data());

    }
    // disable vertex attribute arrays
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
}

这是我画的(不关心颜色,因为我还没有计算它们) 这是我画的(不关心颜色,因为我还没有计算它们)

如果我的代码是正确的,这就是我应该看到的 如果我的代码是正确的,这就是我应该看到的

任何人都知道错误在哪里?


I've to render a scene that include various mesh with openGL. the meshes are defined like this:

struct Mesh {
frame3f         frame;      // frame
vector<vec3f>   pos;        // vertex position
vector<vec3f>   norm;       // vertex normal
vector<vec2f>   texcoord;   // vertex texcture coordinates
vector<vec3i>   triangle;   // triangle
vector<vec4i>   quad;       // quad
Material*       mat;        // material}

the mesh can be made of triangles and quads, I try to render the vertex with this code:

for (auto mesh : scene->meshes)
{
    // bind material kd, ks, n
    glVertexAttrib3f(material_kd_loc, mesh->mat->kd.x, mesh->mat->kd.y, mesh->mat->kd.z);
    glVertexAttrib3f(material_ks_loc, mesh->mat->ks.x, mesh->mat->ks.y, mesh->mat->ks.z);
    glVertexAttrib1f(material_n_loc, mesh->mat->n);

    // bind mesh frame - use frame_to_matrix
    mat4f mesh_mat = frame_to_matrix(mesh->frame);
    glUniformMatrix4fv(mesh_frame_loc, 1, GL_TRUE, &mesh_mat[0][0]);

    // enable vertex attributes arrays
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    //and set up pointers to the mesh data
    glVertexAttribPointer(vertex_pos_loc, mesh->pos.size(), GL_FLOAT, GL_FALSE, 0, mesh->pos.data());
    glVertexAttribPointer(vertex_norm_loc, mesh->norm.size(), GL_FLOAT, GL_TRUE, 0, mesh->norm.data());

    // draw triangles and quads
    if (mesh->triangle.size() != 0){

        glDrawElements(GL_TRIANGLES, mesh->pos.size(), GL_UNSIGNED_INT, mesh->triangle.data());
    }
    if (mesh->quad.size() != 0){
        glDrawElements(GL_QUADS, mesh->pos.size(), GL_UNSIGNED_INT, mesh->quad.data());

    }
    // disable vertex attribute arrays
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
}

} this is what i draw (don't care about colors as i don't calculate them yet) this is what i draw (don't care about colors as i don't calculate them yet)

this is what i should see if my code is correct this is what i should see if my code is correct

anyone as an idea of where is the error?


原文:https://stackoverflow.com/questions/19587569
更新时间:2023-05-06 14:05

最满意答案

试试这个,它对我有用!

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_lista, container, false);

    ListView lst = (ListView) rootView.findViewById(R.id.lst);
    Adapter adapter = //youradapter;
    lst.setAdapter(adapter);

    return rootView;
}

Try this, it works for me!

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_lista, container, false);

    ListView lst = (ListView) rootView.findViewById(R.id.lst);
    Adapter adapter = //youradapter;
    lst.setAdapter(adapter);

    return rootView;
}

相关问答

更多
  • 对我来说,使用我自己的ID最简单,所以我会将ListView ID更改为另一个。 对于我的例子,我选择android:id="@+id/myList" 。 在您的片段中,在onCreateView方法中,尝试通过扩充布局并按ID获取视图。 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView ...
  • 尝试这个; 在将新的数据列表添加到现有列表之前,请使用list.clear(); 将新数据添加到列表中,您应该只在列表中添加新数据。 将列表传递给适配器类。 通知新数据的适配器,即adapter.notifyDataSetChanged() 你在getRows()中返回两次列表 在案例结构之前删除“返回列表” public void onActivityCreated(Bundle savedInstanceState){super.onActivityCreated(savedInstanceState) ...
  • 我看了一下,看起来答案是今天不可能,因为有两个错误。 出于某种原因,如果ListView是布局的根元素,而不是嵌入到布局中,则ListView预览不起作用。 解决方法很简单; 只是将ListView与其他一些布局(如LinearLayout)一起环绕,将ListView的layout_width和layout_height设置为match_parent。 出于某种原因,当具有ListView和自定义视图的布局包含在另一个布局中时,自定义列表项布局不起作用。 我知道这是以前的工作方式,所以它在某种程度上肯定已 ...
  • 你的android:id="@+id/list"与错误信息中要求的不一样,即android:id="@android:id/list" 。 前者是你的对象; Android需要它的OWN对象。 除此之外,请参阅此内容 。 Your android:id="@+id/list" is not the same as what is being asked for in the error message, which is android:id="@android:id/list". The former i ...
  • 我会这样做: private void displayView(int position) { // update the main content by replacing fragments Fragment fragment = null; ListFragment listfragment = null; switch (position) { case 0: fragment = new HomeFragment(); bre ...
  • ListFragment不是Context的子类,构造函数需要它。 尝试更换 SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, ... 同 SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist, ... Finally found solution by using a custom adapter: http://codehenge.net/blog/2011 ...
  • 非常感谢你的朋友们。 通过将onCreateView方法更改为onActivityCreated可以成功完成任务。 现在,该应用程序就像一个魅力。 谢谢。 Thank you so much friends. Task is successfully achieved by changing onCreateView method to onActivityCreated. Now the app works like a charm. Thanks.
  • 问题是进口报表之间存在冲突。 在MainActivity我有 import android.support.v7.app.ActionBarActivity; 在XXXFragment中,我使用 import android.app.Fragment (or ListFragment) 哪个失败了。 如果我改成这个 import android.support.v4.app.ListFragment; (or Fragment) 有用。 The problem was a conflict bet ...
  • 试试这个,它对我有用! @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_lista, container, false); ListView lst = (ListView) rootView.findViewById ...
  • 我终于找到了解决这个问题的办法... FiveFragment.java应该是这样的: public class FiveFragment extends Fragment { private ListView listView; private String items[]; public FiveFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedIn ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。