C++ 如何调试openGL代码?
我对openGL调试有问题。我发现很多时候,OpenGL会向你们展示它失败的原因是并没有画任何东西。每次代码看起来都很好,但它并没有在GL窗口上绘制任何东西 < >考虑下面的代码。我编写它来绘制立方体,但它没有画任何东西,我找不到原因。 ========================================================C++ 如何调试openGL代码?,c++,c,opengl,visual-c++,opengl-3,C++,C,Opengl,Visual C++,Opengl 3,我对openGL调试有问题。我发现很多时候,OpenGL会向你们展示它失败的原因是并没有画任何东西。每次代码看起来都很好,但它并没有在GL窗口上绘制任何东西 < >考虑下面的代码。我编写它来绘制立方体,但它没有画任何东西,我找不到原因。 ======================================================== // cube_vertex_array.cpp : Defines the entry point for the console applica
// cube_vertex_array.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <glut.h>
static GLfloat vertex[]=
{
100.0,100.0,0.0,
400.0,100.0,0.0,
400.0,400.0,0.0,
100.0,400.0,0.0,
100.0,100.0,-300.0,
400.0,100.0,-300.0,
400.0,400.0,-300.0,
100.0,400.0,-300.0
};
static GLfloat color[]=
{
1.0,0.0,0.0,
0.0,1.0,0.0,
0.0,0.0,1.0,
1.0,1.0,0.0,
1.0,0.0,1.0,
0.0,1.0,1.0
};
static GLubyte frontIndices[] = {0,1,2,3};
static GLubyte leftIndices[] = {1,5,6,2};
static GLubyte backIndices[] = {4,7,6,5};
static GLubyte rightIndices[] = {0,3,7,4};
static GLubyte topIndices[] = {3,2,6,7};
static GLubyte bottomIndices[] = {0,4,5,1};
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //Set default background color to black.
glClearDepth(2.0); //Set the depth level for clearing depth buffer.
glShadeModel(GL_FLAT); //Set the shading model to FLAT
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the color and depth buffer.
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the color and depth buffer.
glColor3f(1.0,0.0,0.0);
//glBegin(GL_LINE_STRIP);
// glVertex3f(0.0,0.0,0.0);
// glVertex3f(200.0,100.0,0.0);
//glEnd();
glEnableClientState(GL_VERTEX_ARRAY); //Enable vertex array.
glEnableClientState(GL_COLOR_ARRAY); //Enable vertex array color.
glColorPointer(3,GL_FLOAT,0,color); //Specify the array for colors.
glVertexPointer(3,GL_FLOAT,0,vertex); //Specify the array for vertex.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,frontIndices); //Draw front face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,leftIndices); //Draw left face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,backIndices); //Draw back face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,rightIndices); //Draw right face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,topIndices); //Draw top face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,bottomIndices); //Draw bottom face.
glutSwapBuffers(); //Swap the buffers.
}
void Reshape(int w,int h)
{
glViewport(0.0,(GLsizei)w,0.0,(GLsizei)h); //Set the viewport according to new window size.
glMatrixMode(GL_PROJECTION); //Set matrix mode to projection.
glLoadIdentity(); //Replace the top matrix in the stack to the identity matrix.
gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h); //Set the orthographic projection.
glMatrixMode(GL_MODELVIEW); //Set matrix mode to modelview.
}
int main(int argc, char **argv)
{
glutInit(&argc,argv); //Initialize the glut.
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //Set display mode and also enable double buffering.
glutInitWindowSize(500,500); //Set the initial window size.
glutCreateWindow("Cube"); //Create the window and also assign name to it.
init(); //Initialize the app.
glutDisplayFunc(Display); //Register the Display function.
glutReshapeFunc(Reshape); //Register the Reshape function.
glutMainLoop(); //Start the main loop.
return 0;
}
//cube\u vertex\u array.cpp:定义控制台应用程序的入口点。
#包括“stdafx.h”
#包括
静态GLfloat顶点[]=
{
100.0,100.0,0.0,
400.0,100.0,0.0,
400.0,400.0,0.0,
100.0,400.0,0.0,
100.0,100.0,-300.0,
400.0,100.0,-300.0,
400.0,400.0,-300.0,
100.0,400.0,-300.0
};
静态GLfloat颜色[]=
{
1.0,0.0,0.0,
0.0,1.0,0.0,
0.0,0.0,1.0,
1.0,1.0,0.0,
1.0,0.0,1.0,
0.0,1.0,1.0
};
静态索引[]={0,1,2,3};
静态索引[]={1,5,6,2};
静态GLubyte反向索引[]={4,7,6,5};
静态GLubyte RightIndex[]={0,3,7,4};
静态索引[]={3,2,6,7};
静态索引[]={0,4,5,1};
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);//将默认背景色设置为黑色。
glClearDepth(2.0);//设置清除深度缓冲区的深度级别。
glShadeModel(GL_平面);//将着色模型设置为平面
glClear(GL_颜色_缓冲区| GL_深度_缓冲区|位);//清除颜色和深度缓冲区。
}
作废显示(作废)
{
glClear(GL_颜色_缓冲区| GL_深度_缓冲区|位);//清除颜色和深度缓冲区。
GL3F(1.0,0.0,0.0);
//glBegin(GL_线_带);
//glVertex3f(0.0,0.0,0.0);
//glVertex3f(200.0100.0,0.0);
//格伦德();
glEnableClientState(GL_顶点数组);//启用顶点数组。
glEnableClientState(GL_颜色_数组);//启用顶点数组颜色。
glColorPointer(3,GL_FLOAT,0,color);//指定颜色的数组。
glVertexPointer(3,GL_FLOAT,0,顶点);//指定顶点的数组。
gldrawerelements(GL_四元组,4,GL_无符号字节,frontindex);//绘制正面。
GLD元素(GL_四元组,4,GL_无符号字节,左索引);//绘制左面。
GLD元素(GL_四元组,4,GL_无符号字节,后向索引);//后向绘制面。
GLD元素(GL_四元组,4,GL_无符号字节,右索引);//绘制右面。
gldrawerelements(GL_四边形,4,GL_无符号字节,topIndex);//绘制顶面。
GLD元素(GL_四元组,4,GL_无符号字节,底部索引);//绘制底面。
glutSwapBuffers();//交换缓冲区。
}
空洞重塑(整数w,整数h)
{
glViewport(0.0,(GLsizei)w,0.0,(GLsizei)h);//根据新窗口大小设置视口。
glMatrixMode(GL_投影);//将矩阵模式设置为投影。
glLoadIdentity();//将堆栈中的顶部矩阵替换为标识矩阵。
gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);//设置正交投影。
glMatrixMode(GL_MODELVIEW);//将矩阵模式设置为MODELVIEW。
}
int main(int argc,字符**argv)
{
glutInit(&argc,argv);//初始化glut。
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//设置显示模式并启用双缓冲。
GLUTINITWindowsSize(500500);//设置初始窗口大小。
glutCreateWindow(“多维数据集”);//创建窗口并为其指定名称。
init();//初始化应用程序。
glutDisplayFunc(显示);//注册显示函数。
GLUTREFORMATEFUNC(REFORMATE);//注册REFORMATE函数。
glutMainLoop();//启动主循环。
返回0;
}
使用glTrace/glIntercept(查看OpenGL调用跟踪)、gDebugger(可视化纹理、着色器、OGL状态等)使用glTrace/glIntercept(查看OpenGL调用跟踪)、gDebugger(可视化纹理、着色器、OGL状态等)等工具。您已将GL_UNSIGNED_字节作为gldrawerelements中的类型参数()。这将导致openGL将插入的索引数组解释为每个索引一个字节。您应该在此处使用GL_UNSIGNED_INT
以下是基于您提供的代码的工作代码(尽管我已将其移植到java):
导入java.nio.ByteBuffer;
导入org.lwjgl.BufferUtils;
导入org.lwjgl.LWJGLException;
导入org.lwjgl.opengl.Display;
导入org.lwjgl.opengl.DisplayMode;
导入静态org.lwjgl.opengl.GL11.*;
公开课考试{
公共静态void main(字符串[]args){
试一试{
Display.create();
Display.setDisplayMode(新的显示模式(500500));
Display.setresizeable(true);
//与指定的数组相同。
float[]顶点=新的float[]{100.0f,100.0f,0.0f,
400.0华氏度,100.0华氏度,0.0华氏度,
400.0f,400.0f,0.0f,
100.0f、400.0f、0.0f、,
100.0f,100.0f,-300.0f,
400.0华氏度,100.0华氏度,-300.0华氏度,
400.0f,400.0f,-300.0f,
100.0f,400.0f,-300.0f};
float[]color=新的float[]{1,0,0,
0,1,0,
0,0,1,
1,1,0,
1,0,1,
0,1,1};
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class GLTest {
public static void main(String[] args) {
try {
Display.create();
Display.setDisplayMode(new DisplayMode(500, 500));
Display.setResizable(true);
//the same arrays as the ones you specified.
float[] vertices = new float[]{100.0f,100.0f,0.0f,
400.0f,100.0f,0.0f,
400.0f,400.0f,0.0f,
100.0f,400.0f,0.0f,
100.0f,100.0f,-300.0f,
400.0f,100.0f,-300.0f,
400.0f,400.0f,-300.0f,
100.0f,400.0f,-300.0f};
float[] color = new float[]{1,0,0,
0,1,0,
0,0,1,
1,1,0,
1,0,1,
0,1,1};
int[] frontIndices = new int[]{0, 1, 2, 3};
//JWJGL bookkeeping..
ByteBuffer vertexBuffer = BufferUtils.createByteBuffer(vertices.length * 4);
ByteBuffer colourBuffer = BufferUtils.createByteBuffer(color.length * 4);
for(int i = 0; i < vertices.length; i++) {
vertexBuffer.putFloat(vertices[i]);
}
vertexBuffer.rewind();
for(int i = 0; i < color.length; i++) {
colourBuffer.putFloat(color[i]);
}
colourBuffer.rewind();
ByteBuffer indexBuffer = BufferUtils.createByteBuffer(4 * frontIndices.length);
for(int i = 0; i < frontIndices.length; i++) {
indexBuffer.putInt(frontIndices[i]);
}
indexBuffer.rewind();
//back you your code
glClearColor(1,1,1,1);
glShadeModel(GL_SMOOTH);
while(!Display.isCloseRequested()) {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Display.getWidth(), 0, Display.getHeight(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colourBuffer);
glVertexPointer(3, GL_FLOAT, 0, vertexBuffer);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, indexBuffer);
Display.update();
Display.sync(60);
}
} catch (LWJGLException e) {
e.printStackTrace();
}
}
}