使用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 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.
-
ListFragment与片段(ListFragment vs Fragment)[2021-09-23]
问题是进口报表之间存在冲突。 在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 ...