Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

glDrawElements Patches Crashing

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Rhakiras   Members   


Posted 04 February 2014 - 04:26 PM

Hi, firstly I would like to say I'm a beginner with OpenGL so sorry if this is a simple fix. I'm trying to draw an Icosahedron following the code here: http://prideout.net/blog/?p=48


When I use 

glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);  

everything works OK and I can see the shape on screen. However I need to use GL_PATCHES for tessellation but when I try this:

glDrawElements(GL_PATCHES, indexCount, GL_UNSIGNED_INT, 0); 

it causes the program to crash with "Unhandled exception at 0x697DD6E9 (atioglxx.dll) in TerrainOpenGL.exe: 0xC0000005: Access violation reading location 0x00000054.".


The .h and .cpp files are below. Thanks in advance!

#ifndef ICO_H
#define ICO_H

#include <GL\glew.h>
#include "glm\glm.hpp"
#include "glm\gtc\matrix_transform.hpp"

class Icosahedron {

	void	createIcosahedron();
	void	render(GLuint program);
	void	update(float msec);
	void	updateUniforms(GLuint program);

	GLuint vao;
	GLsizei indexCount;
	glm::mat3	normalMatrix;
	GLuint		LightPosition;
        GLuint		AmbientMaterial;
        GLuint		DiffuseMaterial;
	float		tessLevelInner;
	float		tessLevelOuter;

#endif // !--ICO_H

#include "Icosahedron.h"
#include "Mesh.h"
#include <iostream>

Icosahedron::Icosahedron(void) {
	this->tessLevelInner = 3;
	this->tessLevelOuter = 2;

	glGenVertexArrays(1, &vao);

Icosahedron::~Icosahedron(void) {
	glDeleteVertexArrays(1, &vao);

void Icosahedron::createIcosahedron() {
    const int faces[] = {
        2, 1, 0,
        3, 2, 0,
        4, 3, 0,
        5, 4, 0,
        1, 5, 0,

        11, 6,  7,
        11, 7,  8,
        11, 8,  9,
        11, 9,  10,
        11, 10, 6,

        1, 2, 6,
        2, 3, 7,
        3, 4, 8,
        4, 5, 9,
        5, 1, 10,

        2,  7, 6,
        3,  8, 7,
        4,  9, 8,
        5, 10, 9,
        1, 6, 10 

	const float vertices[] = {
         0.000f,  0.000f,  1.000f,
         0.894f,  0.000f,  0.447f,
         0.276f,  0.851f,  0.447f,
        -0.724f,  0.526f,  0.447f,
        -0.724f, -0.526f,  0.447f,
         0.276f, -0.851f,  0.447f,
         0.724f,  0.526f, -0.447f,
        -0.276f,  0.851f, -0.447f,
        -0.894f,  0.000f, -0.447f,
        -0.276f, -0.851f, -0.447f,
         0.724f, -0.526f, -0.447f,
         0.000f,  0.000f, -1.000f 

    indexCount = sizeof(faces) / sizeof(faces[0]);

    // Bind the VAO:

    // Create the VBO for positions:
    GLuint positions;
    GLsizei stride = 3 * sizeof(float);
    glGenBuffers(1, &positions);
    glBindBuffer(GL_ARRAY_BUFFER, positions);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(VERTEX_BUFFER, 3, GL_FLOAT, GL_FALSE, stride, 0);

    // Create the VBO for indices:
    GLuint indices;
    glGenBuffers(1, &indices);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(faces), faces, GL_STATIC_DRAW); 


void Icosahedron::render(GLuint program) {

	//glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
	glDrawElements(GL_PATCHES, indexCount, GL_UNSIGNED_INT, 0); // CRASH HERE !

void Icosahedron::update(float msec) {

void Icosahedron::updateUniforms(GLuint program) {
    glUniform1f(glGetUniformLocation(program, "tessLevelInner"), tessLevelInner);
    glUniform1f(glGetUniformLocation(program, "tessLevelOuter"), tessLevelOuter);
    glm::vec4 lightPosition = glm::vec4(0.25f, 0.25f, 1.0f, 0.0f);
    glUniform3fv(glGetUniformLocation(program, "lightPosition"), 1, &lightPosition.x);

    // Render the scene:
    glPatchParameteri(GL_PATCH_VERTICES, 3);
    glUniform3f(glGetUniformLocation(program, "ambientMaterial"), 0.04f, 0.04f, 0.04f);
    glUniform3f(glGetUniformLocation(program, "diffuseMaterial"), 0, 0.75, 0.75);

#2 richardurich   Members   


Posted 04 February 2014 - 07:14 PM

You need to call glUseProgram(). If you do somewhere you didn't include, have you checked glGetError() before the call that crashes?

#3 empirical2   Members   


Posted 05 February 2014 - 03:04 AM

Are you sure you have attached the tesselation control and evaluation shaders to the program object?

#4 Rhakiras   Members   


Posted 05 February 2014 - 07:11 AM

Hi guys, sorry feel a bit embarrassed. Empirical2 I double checked my shaders and it turns out that the evaluation shader had a compilation error that I missed, I fixed that and it now works fine. Thanks for your both your help! :)

#5 Irlan Robson   Members   


Posted 08 February 2014 - 08:21 PM

A little bit off-topic but If you are starting to OpenGL, you can follow the right path and try to not put OpenGL code inside the shape class itself. Create some wrapper class that encapsulates the OpenGL Objects and some Renderer that owns them (manages its life/creates/destroy/consult).

Other advice: load the model from a file. Hardcode nothing that can be loaded from stream.



Irlan Robson

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.